MediaWiki REL1_31
ResourceLoaderClientHtmlTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
8class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
12 protected static function expandVariables( $text ) {
13 return strtr( $text, [
15 ] );
16 }
17
18 protected static function makeContext( $extraQuery = [] ) {
19 $conf = new HashConfig( [
20 'ResourceLoaderSources' => [],
21 'ResourceModuleSkinStyles' => [],
22 'ResourceModules' => [],
23 'EnableJavaScriptTest' => false,
24 'ResourceLoaderDebug' => false,
25 'LoadScript' => '/w/load.php',
26 ] );
27 return new ResourceLoaderContext(
28 new ResourceLoader( $conf ),
29 new FauxRequest( array_merge( [
30 'lang' => 'nl',
31 'skin' => 'fallback',
32 'user' => 'Example',
33 'target' => 'phpunit',
34 ], $extraQuery ) )
35 );
36 }
37
38 protected static function makeModule( array $options = [] ) {
40 }
41
42 protected static function makeSampleModules() {
43 $modules = [
44 'test' => [],
45 'test.private' => [ 'group' => 'private' ],
46 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
47 'test.shouldembed' => [ 'shouldEmbed' => true ],
48
49 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
50 'test.styles.mixed' => [],
51 'test.styles.noscript' => [
53 'group' => 'noscript',
54 ],
55 'test.styles.user' => [
57 'group' => 'user',
58 ],
59 'test.styles.user.empty' => [
61 'group' => 'user',
62 'isKnownEmpty' => true,
63 ],
64 'test.styles.private' => [
66 'group' => 'private',
67 'styles' => '.private{}',
68 ],
69 'test.styles.shouldembed' => [
71 'shouldEmbed' => true,
72 'styles' => '.shouldembed{}',
73 ],
74
75 'test.scripts' => [],
76 'test.scripts.user' => [ 'group' => 'user' ],
77 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
78 'test.scripts.raw' => [ 'isRaw' => true ],
79 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
80
81 'test.ordering.a' => [ 'shouldEmbed' => false ],
82 'test.ordering.b' => [ 'shouldEmbed' => false ],
83 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
84 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
85 'test.ordering.e' => [ 'shouldEmbed' => false ],
86 ];
87 return array_map( function ( $options ) {
88 return self::makeModule( $options );
89 }, $modules );
90 }
91
95 public function testGetDocumentAttributes() {
96 $client = new ResourceLoaderClientHtml( self::makeContext() );
97 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
98 }
99
108 public function testGetData() {
110 $context->getResourceLoader()->register( self::makeSampleModules() );
111
112 $client = new ResourceLoaderClientHtml( $context );
113 $client->setModules( [
114 'test',
115 'test.private',
116 'test.shouldembed.empty',
117 'test.shouldembed',
118 'test.unregistered',
119 ] );
120 $client->setModuleStyles( [
121 'test.styles.mixed',
122 'test.styles.user.empty',
123 'test.styles.private',
124 'test.styles.pure',
125 'test.styles.shouldembed',
126 'test.unregistered.styles',
127 ] );
128 $client->setModuleScripts( [
129 'test.scripts',
130 'test.scripts.user',
131 'test.scripts.user.empty',
132 'test.scripts.shouldembed',
133 'test.unregistered.scripts',
134 ] );
135
136 $expected = [
137 'states' => [
138 'test.private' => 'loading',
139 'test.shouldembed.empty' => 'ready',
140 'test.shouldembed' => 'loading',
141 'test.styles.pure' => 'ready',
142 'test.styles.user.empty' => 'ready',
143 'test.styles.private' => 'ready',
144 'test.styles.shouldembed' => 'ready',
145 'test.scripts' => 'loading',
146 'test.scripts.user' => 'loading',
147 'test.scripts.user.empty' => 'ready',
148 'test.scripts.shouldembed' => 'loading',
149 ],
150 'general' => [
151 'test',
152 ],
153 'styles' => [
154 'test.styles.pure',
155 ],
156 'scripts' => [
157 'test.scripts',
158 'test.scripts.user',
159 'test.scripts.shouldembed',
160 ],
161 'embed' => [
162 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
163 'general' => [
164 'test.private',
165 'test.shouldembed',
166 ],
167 ],
168 ];
169
170 $access = TestingAccessWrapper::newFromObject( $client );
171 $this->assertEquals( $expected, $access->getData() );
172 }
173
181 public function testGetHeadHtml() {
183 $context->getResourceLoader()->register( self::makeSampleModules() );
184
185 $client = new ResourceLoaderClientHtml( $context );
186 $client->setConfig( [ 'key' => 'value' ] );
187 $client->setModules( [
188 'test',
189 'test.private',
190 ] );
191 $client->setModuleStyles( [
192 'test.styles.pure',
193 'test.styles.private',
194 ] );
195 $client->setModuleScripts( [
196 'test.scripts',
197 ] );
198 $client->setExemptStates( [
199 'test.exempt' => 'ready',
200 ] );
201
202 // phpcs:disable Generic.Files.LineLength
203 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
204 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
205 . 'mw.config.set({"key":"value"});'
206 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts":"loading"});'
207 . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
208 . 'mw.loader.load(["test"]);'
209 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
210 . '});</script>' . "\n"
211 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
212 . '<style>.private{}</style>' . "\n"
213 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
214 // phpcs:enable
215 $expected = self::expandVariables( $expected );
216
217 $this->assertEquals( $expected, $client->getHeadHtml() );
218 }
219
225 public function testGetHeadHtmlWithTarget() {
226 $client = new ResourceLoaderClientHtml(
227 self::makeContext(),
228 [ 'target' => 'example' ]
229 );
230
231 // phpcs:disable Generic.Files.LineLength
232 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
233 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback&amp;target=example"></script>';
234 // phpcs:enable
235
236 $this->assertEquals( $expected, $client->getHeadHtml() );
237 }
238
245 $client = new ResourceLoaderClientHtml(
246 self::makeContext(),
247 [ 'target' => null ]
248 );
249
250 // phpcs:disable Generic.Files.LineLength
251 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
252 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
253 // phpcs:enable
254
255 $this->assertEquals( $expected, $client->getHeadHtml() );
256 }
257
262 public function testGetBodyHtml() {
264 $context->getResourceLoader()->register( self::makeSampleModules() );
265
266 $client = new ResourceLoaderClientHtml( $context );
267 $client->setConfig( [ 'key' => 'value' ] );
268 $client->setModules( [
269 'test',
270 'test.private.bottom',
271 ] );
272 $client->setModuleScripts( [
273 'test.scripts',
274 ] );
275
276 $expected = '';
277 $expected = self::expandVariables( $expected );
278
279 $this->assertEquals( $expected, $client->getBodyHtml() );
280 }
281
282 public static function provideMakeLoad() {
283 // phpcs:disable Generic.Files.LineLength
284 return [
285 [
286 'context' => [],
287 'modules' => [ 'test.unknown' ],
289 'output' => '',
290 ],
291 [
292 'context' => [],
293 'modules' => [ 'test.styles.private' ],
295 'output' => '<style>.private{}</style>',
296 ],
297 [
298 'context' => [],
299 'modules' => [ 'test.private' ],
301 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
302 ],
303 [
304 'context' => [],
305 // Eg. startup module
306 'modules' => [ 'test.scripts.raw' ],
308 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
309 ],
310 [
311 'context' => [ 'sync' => true ],
312 'modules' => [ 'test.scripts.raw' ],
314 'output' => '<script src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback&amp;sync=1"></script>',
315 ],
316 [
317 'context' => [],
318 'modules' => [ 'test.scripts.user' ],
320 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
321 ],
322 [
323 'context' => [ 'debug' => true ],
324 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
326 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>' . "\n"
327 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
328 ],
329 [
330 'context' => [ 'debug' => false ],
331 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
333 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.mixed%2Cpure&amp;only=styles&amp;skin=fallback"/>',
334 ],
335 [
336 'context' => [],
337 'modules' => [ 'test.styles.noscript' ],
339 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.noscript&amp;only=styles&amp;skin=fallback"/></noscript>',
340 ],
341 [
342 'context' => [],
343 'modules' => [ 'test.shouldembed' ],
345 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
346 ],
347 [
348 'context' => [],
349 'modules' => [ 'test.styles.shouldembed' ],
351 'output' => '<style>.shouldembed{}</style>',
352 ],
353 [
354 'context' => [],
355 'modules' => [ 'test.scripts.shouldembed' ],
357 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
358 ],
359 [
360 'context' => [],
361 'modules' => [ 'test', 'test.shouldembed' ],
363 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
364 ],
365 [
366 'context' => [],
367 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
369 'output' =>
370 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
371 . '<style>.shouldembed{}</style>'
372 ],
373 [
374 'context' => [],
375 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
377 'output' =>
378 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.a%2Cb&amp;only=styles&amp;skin=fallback"/>' . "\n"
379 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
380 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
381 ],
382 ];
383 // phpcs:enable
384 }
385
398 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
399 $context = self::makeContext( $extraQuery );
400 $context->getResourceLoader()->register( self::makeSampleModules() );
401 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery );
402 $expected = self::expandVariables( $expected );
403 $this->assertEquals( $expected, (string)$actual );
404 }
405}
WebRequest clone which takes values from a provided array.
A Config instance which stores all settings as a member variable.
testGetData()
ResourceLoaderClientHtml::__construct ResourceLoaderClientHtml::setModules ResourceLoaderClientHtml::...
testGetHeadHtml()
ResourceLoaderClientHtml::setConfig ResourceLoaderClientHtml::setExemptStates ResourceLoaderClientHtm...
testGetHeadHtmlWithNullTarget()
Confirm that a null 'target' is the same as no target.
testGetDocumentAttributes()
ResourceLoaderClientHtml::getDocumentAttributes.
testGetBodyHtml()
ResourceLoaderClientHtml::getBodyHtml ResourceLoaderClientHtml::getLoad.
testGetHeadHtmlWithTarget()
Confirm that 'target' is passed down to the startup module's load url.
testMakeLoad(array $extraQuery, array $modules, $type, $expected)
provideMakeLoad ResourceLoaderClientHtml::makeLoad ResourceLoaderClientHtml::makeContext ResourceLoad...
Bootstrap a ResourceLoader client on an HTML page.
static makeLoad(ResourceLoaderContext $mainContext, array $modules, $only, array $extraQuery=[])
Explicily load or embed modules on a page.
Object passed around to modules which contains information about the state of a specific loader reque...
Dynamic JavaScript and CSS resource loading system.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187