Merge "installer: Do not wrongly hide namespace input field"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderClientHtmlTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group ResourceLoader
7 */
8 class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
9
10 use MediaWikiCoversValidator;
11
12 protected static function expandVariables( $text ) {
13 return strtr( $text, [
14 '{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION
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 = [] ) {
39 return new ResourceLoaderTestModule( $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 'test.user' => [ 'group' => 'user' ],
49
50 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
51 'test.styles.mixed' => [],
52 'test.styles.noscript' => [
53 'type' => ResourceLoaderModule::LOAD_STYLES,
54 'group' => 'noscript',
55 ],
56 'test.styles.user' => [
57 'type' => ResourceLoaderModule::LOAD_STYLES,
58 'group' => 'user',
59 ],
60 'test.styles.user.empty' => [
61 'type' => ResourceLoaderModule::LOAD_STYLES,
62 'group' => 'user',
63 'isKnownEmpty' => true,
64 ],
65 'test.styles.private' => [
66 'type' => ResourceLoaderModule::LOAD_STYLES,
67 'group' => 'private',
68 'styles' => '.private{}',
69 ],
70 'test.styles.shouldembed' => [
71 'type' => ResourceLoaderModule::LOAD_STYLES,
72 'shouldEmbed' => true,
73 'styles' => '.shouldembed{}',
74 ],
75 'test.styles.deprecated' => [
76 'type' => ResourceLoaderModule::LOAD_STYLES,
77 'deprecated' => 'Deprecation message.',
78 ],
79
80 'test.scripts' => [],
81 'test.scripts.user' => [ 'group' => 'user' ],
82 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
83 'test.scripts.raw' => [ 'isRaw' => true ],
84 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
85
86 'test.ordering.a' => [ 'shouldEmbed' => false ],
87 'test.ordering.b' => [ 'shouldEmbed' => false ],
88 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
89 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
90 'test.ordering.e' => [ 'shouldEmbed' => false ],
91 ];
92 return array_map( function ( $options ) {
93 return self::makeModule( $options );
94 }, $modules );
95 }
96
97 /**
98 * @covers ResourceLoaderClientHtml::getDocumentAttributes
99 */
100 public function testGetDocumentAttributes() {
101 $client = new ResourceLoaderClientHtml( self::makeContext() );
102 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
103 }
104
105 /**
106 * @covers ResourceLoaderClientHtml::__construct
107 * @covers ResourceLoaderClientHtml::setModules
108 * @covers ResourceLoaderClientHtml::setModuleStyles
109 * @covers ResourceLoaderClientHtml::setModuleScripts
110 * @covers ResourceLoaderClientHtml::getData
111 * @covers ResourceLoaderClientHtml::getContext
112 */
113 public function testGetData() {
114 $context = self::makeContext();
115 $context->getResourceLoader()->register( self::makeSampleModules() );
116
117 $client = new ResourceLoaderClientHtml( $context );
118 $client->setModules( [
119 'test',
120 'test.private',
121 'test.shouldembed.empty',
122 'test.shouldembed',
123 'test.user',
124 'test.unregistered',
125 ] );
126 $client->setModuleStyles( [
127 'test.styles.mixed',
128 'test.styles.user.empty',
129 'test.styles.private',
130 'test.styles.pure',
131 'test.styles.shouldembed',
132 'test.styles.deprecated',
133 'test.unregistered.styles',
134 ] );
135 $client->setModuleScripts( [
136 'test.scripts',
137 'test.scripts.user',
138 'test.scripts.user.empty',
139 'test.scripts.shouldembed',
140 'test.unregistered.scripts',
141 ] );
142
143 $expected = [
144 'states' => [
145 'test.private' => 'loading',
146 'test.shouldembed.empty' => 'ready',
147 'test.shouldembed' => 'loading',
148 'test.user' => 'loading',
149 'test.styles.pure' => 'ready',
150 'test.styles.user.empty' => 'ready',
151 'test.styles.private' => 'ready',
152 'test.styles.shouldembed' => 'ready',
153 'test.styles.deprecated' => 'ready',
154 'test.scripts' => 'loading',
155 'test.scripts.user' => 'loading',
156 'test.scripts.user.empty' => 'ready',
157 'test.scripts.shouldembed' => 'loading',
158 ],
159 'general' => [
160 'test',
161 ],
162 'styles' => [
163 'test.styles.pure',
164 'test.styles.deprecated',
165 ],
166 'scripts' => [
167 'test.scripts',
168 'test.scripts.user',
169 'test.scripts.shouldembed',
170 ],
171 'embed' => [
172 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
173 'general' => [
174 'test.private',
175 'test.shouldembed',
176 'test.user',
177 ],
178 ],
179 'styledeprecations' => [
180 Xml::encodeJsCall(
181 'mw.log.warn',
182 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
183 Deprecation message.' ]
184 )
185 ],
186 ];
187
188 $access = TestingAccessWrapper::newFromObject( $client );
189 $this->assertEquals( $expected, $access->getData() );
190 }
191
192 /**
193 * @covers ResourceLoaderClientHtml::setConfig
194 * @covers ResourceLoaderClientHtml::setExemptStates
195 * @covers ResourceLoaderClientHtml::getHeadHtml
196 * @covers ResourceLoaderClientHtml::getLoad
197 * @covers ResourceLoader::makeLoaderStateScript
198 */
199 public function testGetHeadHtml() {
200 $context = self::makeContext();
201 $context->getResourceLoader()->register( self::makeSampleModules() );
202
203 $client = new ResourceLoaderClientHtml( $context, [
204 'nonce' => false,
205 ] );
206 $client->setConfig( [ 'key' => 'value' ] );
207 $client->setModules( [
208 'test',
209 'test.private',
210 ] );
211 $client->setModuleStyles( [
212 'test.styles.pure',
213 'test.styles.private',
214 'test.styles.deprecated',
215 ] );
216 $client->setModuleScripts( [
217 'test.scripts',
218 ] );
219 $client->setExemptStates( [
220 'test.exempt' => 'ready',
221 ] );
222
223 // phpcs:disable Generic.Files.LineLength
224 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
225 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
226 . 'mw.config.set({"key":"value"});'
227 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
228 . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
229 . 'mw.loader.load(["test"]);'
230 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
231 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
232 . '});</script>' . "\n"
233 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.deprecated%2Cpure&amp;only=styles&amp;skin=fallback"/>' . "\n"
234 . '<style>.private{}</style>' . "\n"
235 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
236 // phpcs:enable
237 $expected = self::expandVariables( $expected );
238
239 $this->assertEquals( $expected, $client->getHeadHtml() );
240 }
241
242 /**
243 * Confirm that 'target' is passed down to the startup module's load url.
244 *
245 * @covers ResourceLoaderClientHtml::getHeadHtml
246 */
247 public function testGetHeadHtmlWithTarget() {
248 $client = new ResourceLoaderClientHtml(
249 self::makeContext(),
250 [ 'target' => 'example' ]
251 );
252
253 // phpcs:disable Generic.Files.LineLength
254 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
255 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback&amp;target=example"></script>';
256 // phpcs:enable
257
258 $this->assertEquals( $expected, $client->getHeadHtml() );
259 }
260
261 /**
262 * Confirm that 'safemode' is passed down to startup.
263 *
264 * @covers ResourceLoaderClientHtml::getHeadHtml
265 */
266 public function testGetHeadHtmlWithSafemode() {
267 $client = new ResourceLoaderClientHtml(
268 self::makeContext(),
269 [ 'safemode' => '1' ]
270 );
271
272 // phpcs:disable Generic.Files.LineLength
273 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
274 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;safemode=1&amp;skin=fallback"></script>';
275 // phpcs:enable
276
277 $this->assertEquals( $expected, $client->getHeadHtml() );
278 }
279
280 /**
281 * Confirm that a null 'target' is the same as no target.
282 *
283 * @covers ResourceLoaderClientHtml::getHeadHtml
284 */
285 public function testGetHeadHtmlWithNullTarget() {
286 $client = new ResourceLoaderClientHtml(
287 self::makeContext(),
288 [ 'target' => null ]
289 );
290
291 // phpcs:disable Generic.Files.LineLength
292 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
293 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
294 // phpcs:enable
295
296 $this->assertEquals( $expected, $client->getHeadHtml() );
297 }
298
299 /**
300 * @covers ResourceLoaderClientHtml::getBodyHtml
301 * @covers ResourceLoaderClientHtml::getLoad
302 */
303 public function testGetBodyHtml() {
304 $context = self::makeContext();
305 $context->getResourceLoader()->register( self::makeSampleModules() );
306
307 $client = new ResourceLoaderClientHtml( $context );
308 $client->setConfig( [ 'key' => 'value' ] );
309 $client->setModules( [
310 'test',
311 'test.private.bottom',
312 ] );
313 $client->setModuleScripts( [
314 'test.scripts',
315 ] );
316
317 $expected = '';
318 $expected = self::expandVariables( $expected );
319
320 $this->assertEquals( $expected, $client->getBodyHtml() );
321 }
322
323 public static function provideMakeLoad() {
324 // phpcs:disable Generic.Files.LineLength
325 return [
326 [
327 'context' => [],
328 'modules' => [ 'test.unknown' ],
329 'only' => ResourceLoaderModule::TYPE_STYLES,
330 'output' => '',
331 ],
332 [
333 'context' => [],
334 'modules' => [ 'test.styles.private' ],
335 'only' => ResourceLoaderModule::TYPE_STYLES,
336 'output' => '<style>.private{}</style>',
337 ],
338 [
339 'context' => [],
340 'modules' => [ 'test.private' ],
341 'only' => ResourceLoaderModule::TYPE_COMBINED,
342 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
343 ],
344 [
345 'context' => [],
346 // Eg. startup module
347 'modules' => [ 'test.scripts.raw' ],
348 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
349 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
350 ],
351 [
352 'context' => [ 'sync' => true ],
353 'modules' => [ 'test.scripts.raw' ],
354 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
355 '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>',
356 ],
357 [
358 'context' => [],
359 'modules' => [ 'test.scripts.user' ],
360 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
361 '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>',
362 ],
363 [
364 'context' => [],
365 'modules' => [ 'test.user' ],
366 'only' => ResourceLoaderModule::TYPE_COMBINED,
367 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.user\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
368 ],
369 [
370 'context' => [ 'debug' => true ],
371 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
372 'only' => ResourceLoaderModule::TYPE_STYLES,
373 '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"
374 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
375 ],
376 [
377 'context' => [ 'debug' => false ],
378 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
379 'only' => ResourceLoaderModule::TYPE_STYLES,
380 '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"/>',
381 ],
382 [
383 'context' => [],
384 'modules' => [ 'test.styles.noscript' ],
385 'only' => ResourceLoaderModule::TYPE_STYLES,
386 '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>',
387 ],
388 [
389 'context' => [],
390 'modules' => [ 'test.shouldembed' ],
391 'only' => ResourceLoaderModule::TYPE_COMBINED,
392 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
393 ],
394 [
395 'context' => [],
396 'modules' => [ 'test.styles.shouldembed' ],
397 'only' => ResourceLoaderModule::TYPE_STYLES,
398 'output' => '<style>.shouldembed{}</style>',
399 ],
400 [
401 'context' => [],
402 'modules' => [ 'test.scripts.shouldembed' ],
403 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
404 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
405 ],
406 [
407 'context' => [],
408 'modules' => [ 'test', 'test.shouldembed' ],
409 'only' => ResourceLoaderModule::TYPE_COMBINED,
410 '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>',
411 ],
412 [
413 'context' => [],
414 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
415 'only' => ResourceLoaderModule::TYPE_STYLES,
416 'output' =>
417 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
418 . '<style>.shouldembed{}</style>'
419 ],
420 [
421 'context' => [],
422 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
423 'only' => ResourceLoaderModule::TYPE_STYLES,
424 'output' =>
425 '<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"
426 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
427 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
428 ],
429 ];
430 // phpcs:enable
431 }
432
433 /**
434 * @dataProvider provideMakeLoad
435 * @covers ResourceLoaderClientHtml::makeLoad
436 * @covers ResourceLoaderClientHtml::makeContext
437 * @covers ResourceLoader::makeModuleResponse
438 * @covers ResourceLoaderModule::getModuleContent
439 * @covers ResourceLoader::getCombinedVersion
440 * @covers ResourceLoader::createLoaderURL
441 * @covers ResourceLoader::createLoaderQuery
442 * @covers ResourceLoader::makeLoaderQuery
443 * @covers ResourceLoader::makeInlineScript
444 */
445 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
446 $context = self::makeContext( $extraQuery );
447 $context->getResourceLoader()->register( self::makeSampleModules() );
448 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery, false );
449 $expected = self::expandVariables( $expected );
450 $this->assertEquals( $expected, (string)$actual );
451 }
452 }