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