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