Merge "Chinese Conversion Table Update 2017-6"
[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.top' => [ 'position' => 'top' ],
46 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
47 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
48 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
49 'test.shouldembed' => [ 'shouldEmbed' => true ],
50
51 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
52 'test.styles.mixed' => [],
53 'test.styles.noscript' => [
54 'type' => ResourceLoaderModule::LOAD_STYLES,
55 'group' => 'noscript',
56 ],
57 'test.styles.user' => [
58 'type' => ResourceLoaderModule::LOAD_STYLES,
59 'group' => 'user',
60 ],
61 'test.styles.user.empty' => [
62 'type' => ResourceLoaderModule::LOAD_STYLES,
63 'group' => 'user',
64 'isKnownEmpty' => true,
65 ],
66 'test.styles.private' => [
67 'type' => ResourceLoaderModule::LOAD_STYLES,
68 'group' => 'private',
69 'styles' => '.private{}',
70 ],
71 'test.styles.shouldembed' => [
72 'type' => ResourceLoaderModule::LOAD_STYLES,
73 'shouldEmbed' => true,
74 'styles' => '.shouldembed{}',
75 ],
76
77 'test.scripts' => [],
78 'test.scripts.top' => [ 'position' => 'top' ],
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::setModuleScripts
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.bottom',
119 'test.private.top',
120 'test.top',
121 'test.shouldembed.empty',
122 'test.shouldembed',
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.unregistered.styles',
132 ] );
133 $client->setModuleScripts( [
134 'test.scripts',
135 'test.scripts.user.empty',
136 'test.scripts.top',
137 'test.scripts.shouldembed',
138 'test.unregistered.scripts',
139 ] );
140
141 $expected = [
142 'states' => [
143 'test.private.top' => 'loading',
144 'test.private.bottom' => 'loading',
145 'test.shouldembed.empty' => 'ready',
146 'test.shouldembed' => 'loading',
147 'test.styles.pure' => 'ready',
148 'test.styles.user.empty' => 'ready',
149 'test.styles.private' => 'ready',
150 'test.styles.shouldembed' => 'ready',
151 'test.scripts' => 'loading',
152 'test.scripts.top' => 'loading',
153 'test.scripts.user.empty' => 'ready',
154 'test.scripts.shouldembed' => 'loading',
155 ],
156 'general' => [
157 'test',
158 'test.top',
159 ],
160 'styles' => [
161 'test.styles.pure',
162 ],
163 'scripts' => [
164 'test.scripts',
165 'test.scripts.top',
166 'test.scripts.shouldembed',
167 ],
168 'embed' => [
169 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
170 'general' => [
171 'test.private.bottom',
172 'test.private.top',
173 'test.shouldembed',
174 ],
175 ],
176 ];
177
178 $access = TestingAccessWrapper::newFromObject( $client );
179 $this->assertEquals( $expected, $access->getData() );
180 }
181
182 /**
183 * @covers ResourceLoaderClientHtml::setConfig
184 * @covers ResourceLoaderClientHtml::setExemptStates
185 * @covers ResourceLoaderClientHtml::getHeadHtml
186 * @covers ResourceLoaderClientHtml::getLoad
187 * @covers ResourceLoader::makeLoaderStateScript
188 */
189 public function testGetHeadHtml() {
190 $context = self::makeContext();
191 $context->getResourceLoader()->register( self::makeSampleModules() );
192
193 $client = new ResourceLoaderClientHtml( $context );
194 $client->setConfig( [ 'key' => 'value' ] );
195 $client->setModules( [
196 'test.top',
197 'test.private.top',
198 ] );
199 $client->setModuleStyles( [
200 'test.styles.pure',
201 'test.styles.private',
202 ] );
203 $client->setModuleScripts( [
204 'test.scripts.top',
205 ] );
206 $client->setExemptStates( [
207 'test.exempt' => 'ready',
208 ] );
209
210 // @codingStandardsIgnoreStart Generic.Files.LineLength
211 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
212 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
213 . 'mw.config.set({"key":"value"});'
214 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
215 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
216 . 'mw.loader.load(["test.top"]);'
217 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
218 . '});</script>' . "\n"
219 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
220 . '<style>.private{}</style>' . "\n"
221 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
222 // @codingStandardsIgnoreEnd
223 $expected = self::expandVariables( $expected );
224
225 $this->assertEquals( $expected, $client->getHeadHtml() );
226 }
227
228 /**
229 * @covers ResourceLoaderClientHtml::getBodyHtml
230 * @covers ResourceLoaderClientHtml::getLoad
231 */
232 public function testGetBodyHtml() {
233 $context = self::makeContext();
234 $context->getResourceLoader()->register( self::makeSampleModules() );
235
236 $client = new ResourceLoaderClientHtml( $context );
237 $client->setConfig( [ 'key' => 'value' ] );
238 $client->setModules( [
239 'test',
240 'test.private.bottom',
241 ] );
242 $client->setModuleScripts( [
243 'test.scripts',
244 ] );
245
246 $expected = '';
247 $expected = self::expandVariables( $expected );
248
249 $this->assertEquals( $expected, $client->getBodyHtml() );
250 }
251
252 public static function provideMakeLoad() {
253 return [
254 // @codingStandardsIgnoreStart Generic.Files.LineLength
255 [
256 'context' => [],
257 'modules' => [ 'test.unknown' ],
258 'only' => ResourceLoaderModule::TYPE_STYLES,
259 'output' => '',
260 ],
261 [
262 'context' => [],
263 'modules' => [ 'test.styles.private' ],
264 'only' => ResourceLoaderModule::TYPE_STYLES,
265 'output' => '<style>.private{}</style>',
266 ],
267 [
268 'context' => [],
269 'modules' => [ 'test.private.top' ],
270 'only' => ResourceLoaderModule::TYPE_COMBINED,
271 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
272 ],
273 [
274 'context' => [],
275 // Eg. startup module
276 'modules' => [ 'test.scripts.raw' ],
277 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
278 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
279 ],
280 [
281 'context' => [ 'sync' => true ],
282 'modules' => [ 'test.scripts.raw' ],
283 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
284 '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>',
285 ],
286 [
287 'context' => [],
288 'modules' => [ 'test.scripts.user' ],
289 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
290 '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>',
291 ],
292 [
293 'context' => [ 'debug' => true ],
294 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
295 'only' => ResourceLoaderModule::TYPE_STYLES,
296 '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"
297 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
298 ],
299 [
300 'context' => [ 'debug' => false ],
301 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
302 'only' => ResourceLoaderModule::TYPE_STYLES,
303 '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"/>',
304 ],
305 [
306 'context' => [],
307 'modules' => [ 'test.styles.noscript' ],
308 'only' => ResourceLoaderModule::TYPE_STYLES,
309 '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>',
310 ],
311 [
312 'context' => [],
313 'modules' => [ 'test.shouldembed' ],
314 'only' => ResourceLoaderModule::TYPE_COMBINED,
315 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
316 ],
317 [
318 'context' => [],
319 'modules' => [ 'test.styles.shouldembed' ],
320 'only' => ResourceLoaderModule::TYPE_STYLES,
321 'output' => '<style>.shouldembed{}</style>',
322 ],
323 [
324 'context' => [],
325 'modules' => [ 'test.scripts.shouldembed' ],
326 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
327 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
328 ],
329 [
330 'context' => [],
331 'modules' => [ 'test', 'test.shouldembed' ],
332 'only' => ResourceLoaderModule::TYPE_COMBINED,
333 '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>',
334 ],
335 [
336 'context' => [],
337 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
338 'only' => ResourceLoaderModule::TYPE_STYLES,
339 'output' =>
340 '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
341 . '<style>.shouldembed{}</style>'
342 ],
343 [
344 'context' => [],
345 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
346 'only' => ResourceLoaderModule::TYPE_STYLES,
347 'output' =>
348 '<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"
349 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
350 . '<link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.ordering.e&amp;only=styles&amp;skin=fallback"/>'
351 ],
352 // @codingStandardsIgnoreEnd
353 ];
354 }
355
356 /**
357 * @dataProvider provideMakeLoad
358 * @covers ResourceLoaderClientHtml::makeLoad
359 * @covers ResourceLoaderClientHtml::makeContext
360 * @covers ResourceLoader::makeModuleResponse
361 * @covers ResourceLoaderModule::getModuleContent
362 * @covers ResourceLoader::getCombinedVersion
363 * @covers ResourceLoader::createLoaderURL
364 * @covers ResourceLoader::createLoaderQuery
365 * @covers ResourceLoader::makeLoaderQuery
366 * @covers ResourceLoader::makeInlineScript
367 */
368 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
369 $context = self::makeContext( $extraQuery );
370 $context->getResourceLoader()->register( self::makeSampleModules() );
371 $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type, $extraQuery );
372 $expected = self::expandVariables( $expected );
373 $this->assertEquals( $expected, (string)$actual );
374 }
375 }