3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderClientHtmlTest
extends PHPUnit\Framework\TestCase
{
10 use MediaWikiCoversValidator
;
12 protected static function expandVariables( $text ) {
13 return strtr( $text, [
14 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
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',
27 return new ResourceLoaderContext(
28 new ResourceLoader( $conf ),
29 new FauxRequest( array_merge( [
33 'target' => 'phpunit',
38 protected static function makeModule( array $options = [] ) {
39 return new ResourceLoaderTestModule( $options );
42 protected static function makeSampleModules() {
45 'test.private' => [ 'group' => 'private' ],
46 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
47 'test.shouldembed' => [ 'shouldEmbed' => true ],
48 'test.user' => [ 'group' => 'user' ],
50 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
51 'test.styles.mixed' => [],
52 'test.styles.noscript' => [
53 'type' => ResourceLoaderModule
::LOAD_STYLES
,
54 'group' => 'noscript',
56 'test.styles.user' => [
57 'type' => ResourceLoaderModule
::LOAD_STYLES
,
60 'test.styles.user.empty' => [
61 'type' => ResourceLoaderModule
::LOAD_STYLES
,
63 'isKnownEmpty' => true,
65 'test.styles.private' => [
66 'type' => ResourceLoaderModule
::LOAD_STYLES
,
68 'styles' => '.private{}',
70 'test.styles.shouldembed' => [
71 'type' => ResourceLoaderModule
::LOAD_STYLES
,
72 'shouldEmbed' => true,
73 'styles' => '.shouldembed{}',
75 'test.styles.deprecated' => [
76 'type' => ResourceLoaderModule
::LOAD_STYLES
,
77 'deprecated' => 'Deprecation message.',
81 'test.scripts.user' => [ 'group' => 'user' ],
82 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
83 'test.scripts.raw' => [ 'isRaw' => true ],
84 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
86 'test.ordering.a' => [ 'shouldEmbed' => false ],
87 'test.ordering.b' => [ 'shouldEmbed' => false ],
88 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
89 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
90 'test.ordering.e' => [ 'shouldEmbed' => false ],
92 return array_map( function ( $options ) {
93 return self
::makeModule( $options );
98 * @covers ResourceLoaderClientHtml::getDocumentAttributes
100 public function testGetDocumentAttributes() {
101 $client = new ResourceLoaderClientHtml( self
::makeContext() );
102 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
106 * @covers ResourceLoaderClientHtml::__construct
107 * @covers ResourceLoaderClientHtml::setModules
108 * @covers ResourceLoaderClientHtml::setModuleStyles
109 * @covers ResourceLoaderClientHtml::setModuleScripts
110 * @covers ResourceLoaderClientHtml::getData
111 * @covers ResourceLoaderClientHtml::getContext
113 public function testGetData() {
114 $context = self
::makeContext();
115 $context->getResourceLoader()->register( self
::makeSampleModules() );
117 $client = new ResourceLoaderClientHtml( $context );
118 $client->setModules( [
121 'test.shouldembed.empty',
126 $client->setModuleStyles( [
128 'test.styles.user.empty',
129 'test.styles.private',
131 'test.styles.shouldembed',
132 'test.styles.deprecated',
133 'test.unregistered.styles',
135 $client->setModuleScripts( [
138 'test.scripts.user.empty',
139 'test.scripts.shouldembed',
140 'test.unregistered.scripts',
145 'test.private' => 'loading',
146 'test.shouldembed.empty' => 'ready',
147 'test.shouldembed' => 'loading',
148 'test.user' => 'loading',
149 'test.styles.pure' => 'ready',
150 'test.styles.user.empty' => 'ready',
151 'test.styles.private' => 'ready',
152 'test.styles.shouldembed' => 'ready',
153 'test.styles.deprecated' => 'ready',
154 'test.scripts' => 'loading',
155 'test.scripts.user' => 'loading',
156 'test.scripts.user.empty' => 'ready',
157 'test.scripts.shouldembed' => 'loading',
164 'test.styles.deprecated',
169 'test.scripts.shouldembed',
172 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
179 'styleDeprecations' => [
182 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
183 Deprecation message.' ]
188 $access = TestingAccessWrapper
::newFromObject( $client );
189 $this->assertEquals( $expected, $access->getData() );
193 * @covers ResourceLoaderClientHtml::setConfig
194 * @covers ResourceLoaderClientHtml::setExemptStates
195 * @covers ResourceLoaderClientHtml::getHeadHtml
196 * @covers ResourceLoaderClientHtml::getLoad
197 * @covers ResourceLoader::makeLoaderStateScript
199 public function testGetHeadHtml() {
200 $context = self
::makeContext();
201 $context->getResourceLoader()->register( self
::makeSampleModules() );
203 $client = new ResourceLoaderClientHtml( $context, [
206 $client->setConfig( [ 'key' => 'value' ] );
207 $client->setModules( [
211 $client->setModuleStyles( [
213 'test.styles.private',
214 'test.styles.deprecated',
216 $client->setModuleScripts( [
219 $client->setExemptStates( [
220 'test.exempt' => 'ready',
223 // phpcs:disable Generic.Files.LineLength
224 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
225 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
226 . 'mw.config.set({"key":"value"});'
227 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
228 . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
229 . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
230 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
231 . '});</script>' . "\n"
232 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.deprecated%2Cpure&only=styles&skin=fallback"/>' . "\n"
233 . '<style>.private{}</style>' . "\n"
234 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
236 $expected = self
::expandVariables( $expected );
238 $this->assertEquals( $expected, $client->getHeadHtml() );
242 * Confirm that 'target' is passed down to the startup module's load url.
244 * @covers ResourceLoaderClientHtml::getHeadHtml
246 public function testGetHeadHtmlWithTarget() {
247 $client = new ResourceLoaderClientHtml(
249 [ 'target' => 'example' ]
252 // phpcs:disable Generic.Files.LineLength
253 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
254 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
257 $this->assertEquals( $expected, $client->getHeadHtml() );
261 * Confirm that 'safemode' is passed down to startup.
263 * @covers ResourceLoaderClientHtml::getHeadHtml
265 public function testGetHeadHtmlWithSafemode() {
266 $client = new ResourceLoaderClientHtml(
268 [ 'safemode' => '1' ]
271 // phpcs:disable Generic.Files.LineLength
272 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
273 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&safemode=1&skin=fallback"></script>';
276 $this->assertEquals( $expected, $client->getHeadHtml() );
280 * Confirm that a null 'target' is the same as no target.
282 * @covers ResourceLoaderClientHtml::getHeadHtml
284 public function testGetHeadHtmlWithNullTarget() {
285 $client = new ResourceLoaderClientHtml(
290 // phpcs:disable Generic.Files.LineLength
291 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
292 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
295 $this->assertEquals( $expected, $client->getHeadHtml() );
299 * @covers ResourceLoaderClientHtml::getBodyHtml
300 * @covers ResourceLoaderClientHtml::getLoad
302 public function testGetBodyHtml() {
303 $context = self
::makeContext();
304 $context->getResourceLoader()->register( self
::makeSampleModules() );
306 $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
307 $client->setConfig( [ 'key' => 'value' ] );
308 $client->setModules( [
310 'test.private.bottom',
312 $client->setModuleStyles( [
313 'test.styles.deprecated',
315 $client->setModuleScripts( [
318 // phpcs:disable Generic.Files.LineLength
319 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
320 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
324 $this->assertEquals( $expected, $client->getBodyHtml() );
327 public static function provideMakeLoad() {
328 // phpcs:disable Generic.Files.LineLength
332 'modules' => [ 'test.unknown' ],
333 'only' => ResourceLoaderModule
::TYPE_STYLES
,
338 'modules' => [ 'test.styles.private' ],
339 'only' => ResourceLoaderModule
::TYPE_STYLES
,
340 'output' => '<style>.private{}</style>',
344 'modules' => [ 'test.private' ],
345 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
346 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
350 // Eg. startup module
351 'modules' => [ 'test.scripts.raw' ],
352 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
353 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
356 'context' => [ 'sync' => true ],
357 'modules' => [ 'test.scripts.raw' ],
358 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
359 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
363 'modules' => [ 'test.scripts.user' ],
364 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
365 '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>',
369 'modules' => [ 'test.user' ],
370 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
371 '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>',
374 'context' => [ 'debug' => true ],
375 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
376 'only' => ResourceLoaderModule
::TYPE_STYLES
,
377 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
378 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
381 'context' => [ 'debug' => false ],
382 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
383 'only' => ResourceLoaderModule
::TYPE_STYLES
,
384 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
388 'modules' => [ 'test.styles.noscript' ],
389 'only' => ResourceLoaderModule
::TYPE_STYLES
,
390 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
394 'modules' => [ 'test.shouldembed' ],
395 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
396 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
400 'modules' => [ 'test.styles.shouldembed' ],
401 'only' => ResourceLoaderModule
::TYPE_STYLES
,
402 'output' => '<style>.shouldembed{}</style>',
406 'modules' => [ 'test.scripts.shouldembed' ],
407 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
408 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
412 'modules' => [ 'test', 'test.shouldembed' ],
413 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
414 '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",null,{"css":[]});});</script>',
418 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
419 'only' => ResourceLoaderModule
::TYPE_STYLES
,
421 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
422 . '<style>.shouldembed{}</style>'
426 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
427 'only' => ResourceLoaderModule
::TYPE_STYLES
,
429 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
430 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
431 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
438 * @dataProvider provideMakeLoad
439 * @covers ResourceLoaderClientHtml::makeLoad
440 * @covers ResourceLoaderClientHtml::makeContext
441 * @covers ResourceLoader::makeModuleResponse
442 * @covers ResourceLoaderModule::getModuleContent
443 * @covers ResourceLoader::getCombinedVersion
444 * @covers ResourceLoader::createLoaderURL
445 * @covers ResourceLoader::createLoaderQuery
446 * @covers ResourceLoader::makeLoaderQuery
447 * @covers ResourceLoader::makeInlineScript
449 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
450 $context = self
::makeContext( $extraQuery );
451 $context->getResourceLoader()->register( self
::makeSampleModules() );
452 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery, false );
453 $expected = self
::expandVariables( $expected );
454 $this->assertEquals( $expected, (string)$actual );