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