Merge "mail: Always quote uncoded names in MailAddress"
[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 $client->setConfig( [ 'key' => 'value' ] );
191 $client->setModules( [
192 'test',
193 'test.private',
194 ] );
195 $client->setModuleStyles( [
196 'test.styles.pure',
197 'test.styles.private',
198 ] );
199 $client->setModuleScripts( [
200 'test.scripts',
201 ] );
202 $client->setExemptStates( [
203 'test.exempt' => 'ready',
204 ] );
205
206 // phpcs:disable Generic.Files.LineLength
207 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
208 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
209 . 'mw.config.set({"key":"value"});'
210 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts":"loading"});'
211 . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
212 . 'mw.loader.load(["test"]);'
213 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
214 . '});</script>' . "\n"
215 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
216 . '<style>.private{}</style>' . "\n"
217 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
218 // phpcs:enable
219 $expected = self::expandVariables( $expected );
220
221 $this->assertEquals( $expected, $client->getHeadHtml() );
222 }
223
224 /**
225 * Confirm that 'target' is passed down to the startup module's load url.
226 *
227 * @covers ResourceLoaderClientHtml::getHeadHtml
228 */
229 public function testGetHeadHtmlWithTarget() {
230 $client = new ResourceLoaderClientHtml(
231 self::makeContext(),
232 [ 'target' => 'example' ]
233 );
234
235 // phpcs:disable Generic.Files.LineLength
236 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
237 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback&amp;target=example"></script>';
238 // phpcs:enable
239
240 $this->assertEquals( $expected, $client->getHeadHtml() );
241 }
242
243 /**
244 * Confirm that a null 'target' is the same as no target.
245 *
246 * @covers ResourceLoaderClientHtml::getHeadHtml
247 */
248 public function testGetHeadHtmlWithNullTarget() {
249 $client = new ResourceLoaderClientHtml(
250 self::makeContext(),
251 [ 'target' => null ]
252 );
253
254 // phpcs:disable Generic.Files.LineLength
255 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
256 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
257 // phpcs:enable
258
259 $this->assertEquals( $expected, $client->getHeadHtml() );
260 }
261
262 /**
263 * @covers ResourceLoaderClientHtml::getBodyHtml
264 * @covers ResourceLoaderClientHtml::getLoad
265 */
266 public function testGetBodyHtml() {
267 $context = self::makeContext();
268 $context->getResourceLoader()->register( self::makeSampleModules() );
269
270 $client = new ResourceLoaderClientHtml( $context );
271 $client->setConfig( [ 'key' => 'value' ] );
272 $client->setModules( [
273 'test',
274 'test.private.bottom',
275 ] );
276 $client->setModuleScripts( [
277 'test.scripts',
278 ] );
279
280 $expected = '';
281 $expected = self::expandVariables( $expected );
282
283 $this->assertEquals( $expected, $client->getBodyHtml() );
284 }
285
286 public static function provideMakeLoad() {
287 // phpcs:disable Generic.Files.LineLength
288 return [
289 [
290 'context' => [],
291 'modules' => [ 'test.unknown' ],
292 'only' => ResourceLoaderModule::TYPE_STYLES,
293 'output' => '',
294 ],
295 [
296 'context' => [],
297 'modules' => [ 'test.styles.private' ],
298 'only' => ResourceLoaderModule::TYPE_STYLES,
299 'output' => '<style>.private{}</style>',
300 ],
301 [
302 'context' => [],
303 'modules' => [ 'test.private' ],
304 'only' => ResourceLoaderModule::TYPE_COMBINED,
305 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
306 ],
307 [
308 'context' => [],
309 // Eg. startup module
310 'modules' => [ 'test.scripts.raw' ],
311 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
312 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
313 ],
314 [
315 'context' => [ 'sync' => true ],
316 'modules' => [ 'test.scripts.raw' ],
317 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
318 '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>',
319 ],
320 [
321 'context' => [],
322 'modules' => [ 'test.scripts.user' ],
323 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
324 '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>',
325 ],
326 [
327 'context' => [],
328 'modules' => [ 'test.user' ],
329 'only' => ResourceLoaderModule::TYPE_COMBINED,
330 '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>',
331 ],
332 [
333 'context' => [ 'debug' => true ],
334 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
335 'only' => ResourceLoaderModule::TYPE_STYLES,
336 '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"
337 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
338 ],
339 [
340 'context' => [ 'debug' => false ],
341 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
342 'only' => ResourceLoaderModule::TYPE_STYLES,
343 '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"/>',
344 ],
345 [
346 'context' => [],
347 'modules' => [ 'test.styles.noscript' ],
348 'only' => ResourceLoaderModule::TYPE_STYLES,
349 '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>',
350 ],
351 [
352 'context' => [],
353 'modules' => [ 'test.shouldembed' ],
354 'only' => ResourceLoaderModule::TYPE_COMBINED,
355 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
356 ],
357 [
358 'context' => [],
359 'modules' => [ 'test.styles.shouldembed' ],
360 'only' => ResourceLoaderModule::TYPE_STYLES,
361 'output' => '<style>.shouldembed{}</style>',
362 ],
363 [
364 'context' => [],
365 'modules' => [ 'test.scripts.shouldembed' ],
366 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
367 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
368 ],
369 [
370 'context' => [],
371 'modules' => [ 'test', 'test.shouldembed' ],
372 'only' => ResourceLoaderModule::TYPE_COMBINED,
373 '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>',
374 ],
375 [
376 'context' => [],
377 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
378 'only' => ResourceLoaderModule::TYPE_STYLES,
379 'output' =>
380 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
381 . '<style>.shouldembed{}</style>'
382 ],
383 [
384 'context' => [],
385 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
386 'only' => ResourceLoaderModule::TYPE_STYLES,
387 'output' =>
388 '<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"
389 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
390 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
391 ],
392 ];
393 // phpcs:enable
394 }
395
396 /**
397 * @dataProvider provideMakeLoad
398 * @covers ResourceLoaderClientHtml::makeLoad
399 * @covers ResourceLoaderClientHtml::makeContext
400 * @covers ResourceLoader::makeModuleResponse
401 * @covers ResourceLoaderModule::getModuleContent
402 * @covers ResourceLoader::getCombinedVersion
403 * @covers ResourceLoader::createLoaderURL
404 * @covers ResourceLoader::createLoaderQuery
405 * @covers ResourceLoader::makeLoaderQuery
406 * @covers ResourceLoader::makeInlineScript
407 */
408 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
409 $context = self::makeContext( $extraQuery );
410 $context->getResourceLoader()->register( self::makeSampleModules() );
411 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery );
412 $expected = self::expandVariables( $expected );
413 $this->assertEquals( $expected, (string)$actual );
414 }
415 }