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