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