Merge "objectcache: add metrics for WAN cache deletes and check key touches/resets"
[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::getData
110 * @covers ResourceLoaderClientHtml::getContext
111 */
112 public function testGetData() {
113 $context = self::makeContext();
114 $context->getResourceLoader()->register( self::makeSampleModules() );
115
116 $client = new ResourceLoaderClientHtml( $context );
117 $client->setModules( [
118 'test',
119 'test.private',
120 'test.shouldembed.empty',
121 'test.shouldembed',
122 'test.user',
123 'test.unregistered',
124 ] );
125 $client->setModuleStyles( [
126 'test.styles.mixed',
127 'test.styles.user.empty',
128 'test.styles.private',
129 'test.styles.pure',
130 'test.styles.shouldembed',
131 'test.styles.deprecated',
132 'test.unregistered.styles',
133 ] );
134
135 $expected = [
136 'states' => [
137 'test.private' => 'loading',
138 'test.shouldembed.empty' => 'ready',
139 'test.shouldembed' => 'loading',
140 'test.user' => 'loading',
141 'test.styles.pure' => 'ready',
142 'test.styles.user.empty' => 'ready',
143 'test.styles.private' => 'ready',
144 'test.styles.shouldembed' => 'ready',
145 'test.styles.deprecated' => 'ready',
146 ],
147 'general' => [
148 'test',
149 ],
150 'styles' => [
151 'test.styles.pure',
152 'test.styles.deprecated',
153 ],
154 'embed' => [
155 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
156 'general' => [
157 'test.private',
158 'test.shouldembed',
159 'test.user',
160 ],
161 ],
162 'styleDeprecations' => [
163 Xml::encodeJsCall(
164 'mw.log.warn',
165 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
166 Deprecation message.' ]
167 )
168 ],
169 ];
170
171 $access = TestingAccessWrapper::newFromObject( $client );
172 $this->assertEquals( $expected, $access->getData() );
173 }
174
175 /**
176 * @covers ResourceLoaderClientHtml::setConfig
177 * @covers ResourceLoaderClientHtml::setExemptStates
178 * @covers ResourceLoaderClientHtml::getHeadHtml
179 * @covers ResourceLoaderClientHtml::getLoad
180 * @covers ResourceLoader::makeLoaderStateScript
181 */
182 public function testGetHeadHtml() {
183 $context = self::makeContext();
184 $context->getResourceLoader()->register( self::makeSampleModules() );
185
186 $client = new ResourceLoaderClientHtml( $context, [
187 'nonce' => false,
188 ] );
189 $client->setConfig( [ 'key' => 'value' ] );
190 $client->setModules( [
191 'test',
192 'test.private',
193 ] );
194 $client->setModuleStyles( [
195 'test.styles.pure',
196 'test.styles.private',
197 'test.styles.deprecated',
198 ] );
199 $client->setExemptStates( [
200 'test.exempt' => 'ready',
201 ] );
202
203 // phpcs:disable Generic.Files.LineLength
204 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
205 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
206 . 'mw.config.set({"key":"value"});'
207 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"});'
208 . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
209 . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
210 . '});</script>' . "\n"
211 . '<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"
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
220 /**
221 * Confirm that 'target' is passed down to the startup module's load url.
222 *
223 * @covers ResourceLoaderClientHtml::getHeadHtml
224 */
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
239 /**
240 * Confirm that 'safemode' is passed down to startup.
241 *
242 * @covers ResourceLoaderClientHtml::getHeadHtml
243 */
244 public function testGetHeadHtmlWithSafemode() {
245 $client = new ResourceLoaderClientHtml(
246 self::makeContext(),
247 [ 'safemode' => '1' ]
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;safemode=1&amp;skin=fallback"></script>';
253 // phpcs:enable
254
255 $this->assertEquals( $expected, $client->getHeadHtml() );
256 }
257
258 /**
259 * Confirm that a null 'target' is the same as no target.
260 *
261 * @covers ResourceLoaderClientHtml::getHeadHtml
262 */
263 public function testGetHeadHtmlWithNullTarget() {
264 $client = new ResourceLoaderClientHtml(
265 self::makeContext(),
266 [ 'target' => null ]
267 );
268
269 // phpcs:disable Generic.Files.LineLength
270 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
271 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
272 // phpcs:enable
273
274 $this->assertEquals( $expected, $client->getHeadHtml() );
275 }
276
277 /**
278 * @covers ResourceLoaderClientHtml::getBodyHtml
279 * @covers ResourceLoaderClientHtml::getLoad
280 */
281 public function testGetBodyHtml() {
282 $context = self::makeContext();
283 $context->getResourceLoader()->register( self::makeSampleModules() );
284
285 $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
286 $client->setConfig( [ 'key' => 'value' ] );
287 $client->setModules( [
288 'test',
289 'test.private.bottom',
290 ] );
291 $client->setModuleStyles( [
292 'test.styles.deprecated',
293 ] );
294 // phpcs:disable Generic.Files.LineLength
295 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
296 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
297 . '});</script>';
298 // phpcs:enable
299
300 $this->assertEquals( $expected, $client->getBodyHtml() );
301 }
302
303 public static function provideMakeLoad() {
304 // phpcs:disable Generic.Files.LineLength
305 return [
306 [
307 'context' => [],
308 'modules' => [ 'test.unknown' ],
309 'only' => ResourceLoaderModule::TYPE_STYLES,
310 'output' => '',
311 ],
312 [
313 'context' => [],
314 'modules' => [ 'test.styles.private' ],
315 'only' => ResourceLoaderModule::TYPE_STYLES,
316 'output' => '<style>.private{}</style>',
317 ],
318 [
319 'context' => [],
320 'modules' => [ 'test.private' ],
321 'only' => ResourceLoaderModule::TYPE_COMBINED,
322 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
323 ],
324 [
325 'context' => [],
326 // Eg. startup module
327 'modules' => [ 'test.scripts.raw' ],
328 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
329 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
330 ],
331 [
332 'context' => [ 'sync' => true ],
333 'modules' => [ 'test.scripts.raw' ],
334 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
335 '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>',
336 ],
337 [
338 'context' => [],
339 'modules' => [ 'test.scripts.user' ],
340 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
341 '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>',
342 ],
343 [
344 'context' => [],
345 'modules' => [ 'test.user' ],
346 'only' => ResourceLoaderModule::TYPE_COMBINED,
347 '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>',
348 ],
349 [
350 'context' => [ 'debug' => true ],
351 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
352 'only' => ResourceLoaderModule::TYPE_STYLES,
353 '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"
354 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
355 ],
356 [
357 'context' => [ 'debug' => false ],
358 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
359 'only' => ResourceLoaderModule::TYPE_STYLES,
360 '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"/>',
361 ],
362 [
363 'context' => [],
364 'modules' => [ 'test.styles.noscript' ],
365 'only' => ResourceLoaderModule::TYPE_STYLES,
366 '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>',
367 ],
368 [
369 'context' => [],
370 'modules' => [ 'test.shouldembed' ],
371 'only' => ResourceLoaderModule::TYPE_COMBINED,
372 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
373 ],
374 [
375 'context' => [],
376 'modules' => [ 'test.styles.shouldembed' ],
377 'only' => ResourceLoaderModule::TYPE_STYLES,
378 'output' => '<style>.shouldembed{}</style>',
379 ],
380 [
381 'context' => [],
382 'modules' => [ 'test.scripts.shouldembed' ],
383 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
384 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
385 ],
386 [
387 'context' => [],
388 'modules' => [ 'test', 'test.shouldembed' ],
389 'only' => ResourceLoaderModule::TYPE_COMBINED,
390 '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",null,{"css":[]});});</script>',
391 ],
392 [
393 'context' => [],
394 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
395 'only' => ResourceLoaderModule::TYPE_STYLES,
396 'output' =>
397 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
398 . '<style>.shouldembed{}</style>'
399 ],
400 [
401 'context' => [],
402 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
403 'only' => ResourceLoaderModule::TYPE_STYLES,
404 'output' =>
405 '<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"
406 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
407 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
408 ],
409 ];
410 // phpcs:enable
411 }
412
413 /**
414 * @dataProvider provideMakeLoad
415 * @covers ResourceLoaderClientHtml::makeLoad
416 * @covers ResourceLoaderClientHtml::makeContext
417 * @covers ResourceLoader::makeModuleResponse
418 * @covers ResourceLoaderModule::getModuleContent
419 * @covers ResourceLoader::getCombinedVersion
420 * @covers ResourceLoader::createLoaderURL
421 * @covers ResourceLoader::createLoaderQuery
422 * @covers ResourceLoader::makeLoaderQuery
423 * @covers ResourceLoader::makeInlineScript
424 */
425 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
426 $context = self::makeContext( $extraQuery );
427 $context->getResourceLoader()->register( self::makeSampleModules() );
428 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery, false );
429 $expected = self::expandVariables( $expected );
430 $this->assertEquals( $expected, (string)$actual );
431 }
432 }