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 'LoadScript' => '/w/load.php',
26 return new ResourceLoaderContext(
27 new ResourceLoader( $conf ),
28 new FauxRequest( array_merge( [
32 'target' => 'phpunit',
37 protected static function makeModule( array $options = [] ) {
38 return new ResourceLoaderTestModule( $options );
41 protected static function makeSampleModules() {
44 'test.private' => [ 'group' => 'private' ],
45 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
46 'test.shouldembed' => [ 'shouldEmbed' => true ],
47 'test.user' => [ 'group' => 'user' ],
49 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
50 'test.styles.mixed' => [],
51 'test.styles.noscript' => [
52 'type' => ResourceLoaderModule
::LOAD_STYLES
,
53 'group' => 'noscript',
55 'test.styles.user' => [
56 'type' => ResourceLoaderModule
::LOAD_STYLES
,
59 'test.styles.user.empty' => [
60 'type' => ResourceLoaderModule
::LOAD_STYLES
,
62 'isKnownEmpty' => true,
64 'test.styles.private' => [
65 'type' => ResourceLoaderModule
::LOAD_STYLES
,
67 'styles' => '.private{}',
69 'test.styles.shouldembed' => [
70 'type' => ResourceLoaderModule
::LOAD_STYLES
,
71 'shouldEmbed' => true,
72 'styles' => '.shouldembed{}',
74 'test.styles.deprecated' => [
75 'type' => ResourceLoaderModule
::LOAD_STYLES
,
76 'deprecated' => 'Deprecation message.',
80 'test.scripts.user' => [ 'group' => 'user' ],
81 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
82 'test.scripts.raw' => [ 'isRaw' => true ],
83 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
85 'test.ordering.a' => [ 'shouldEmbed' => false ],
86 'test.ordering.b' => [ 'shouldEmbed' => false ],
87 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
88 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
89 'test.ordering.e' => [ 'shouldEmbed' => false ],
91 return array_map( function ( $options ) {
92 return self
::makeModule( $options );
97 * @covers ResourceLoaderClientHtml::getDocumentAttributes
99 public function testGetDocumentAttributes() {
100 $client = new ResourceLoaderClientHtml( self
::makeContext() );
101 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
105 * @covers ResourceLoaderClientHtml::__construct
106 * @covers ResourceLoaderClientHtml::setModules
107 * @covers ResourceLoaderClientHtml::setModuleStyles
108 * @covers ResourceLoaderClientHtml::getData
109 * @covers ResourceLoaderClientHtml::getContext
111 public function testGetData() {
112 $context = self
::makeContext();
113 $context->getResourceLoader()->register( self
::makeSampleModules() );
115 $client = new ResourceLoaderClientHtml( $context );
116 $client->setModules( [
119 'test.shouldembed.empty',
124 $client->setModuleStyles( [
126 'test.styles.user.empty',
127 'test.styles.private',
129 'test.styles.shouldembed',
130 'test.styles.deprecated',
131 'test.unregistered.styles',
136 'test.private' => 'loading',
137 'test.shouldembed.empty' => 'ready',
138 'test.shouldembed' => 'loading',
139 'test.user' => 'loading',
140 'test.styles.pure' => 'ready',
141 'test.styles.user.empty' => 'ready',
142 'test.styles.private' => 'ready',
143 'test.styles.shouldembed' => 'ready',
144 'test.styles.deprecated' => 'ready',
151 'test.styles.deprecated',
154 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
161 'styleDeprecations' => [
164 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
165 Deprecation message.' ]
170 $access = TestingAccessWrapper
::newFromObject( $client );
171 $this->assertEquals( $expected, $access->getData() );
175 * @covers ResourceLoaderClientHtml::setConfig
176 * @covers ResourceLoaderClientHtml::setExemptStates
177 * @covers ResourceLoaderClientHtml::getHeadHtml
178 * @covers ResourceLoaderClientHtml::getLoad
179 * @covers ResourceLoader::makeLoaderStateScript
181 public function testGetHeadHtml() {
182 $context = self
::makeContext();
183 $context->getResourceLoader()->register( self
::makeSampleModules() );
185 $client = new ResourceLoaderClientHtml( $context, [
188 $client->setConfig( [ 'key' => 'value' ] );
189 $client->setModules( [
193 $client->setModuleStyles( [
195 'test.styles.private',
196 'test.styles.deprecated',
198 $client->setExemptStates( [
199 'test.exempt' => 'ready',
202 // phpcs:disable Generic.Files.LineLength
203 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
204 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
205 . 'mw.config.set({"key":"value"});'
206 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"});'
207 . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
208 . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
209 . '});</script>' . "\n"
210 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.deprecated%2Cpure&only=styles&skin=fallback"/>' . "\n"
211 . '<style>.private{}</style>' . "\n"
212 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
214 $expected = self
::expandVariables( $expected );
216 $this->assertEquals( $expected, $client->getHeadHtml() );
220 * Confirm that 'target' is passed down to the startup module's load url.
222 * @covers ResourceLoaderClientHtml::getHeadHtml
224 public function testGetHeadHtmlWithTarget() {
225 $client = new ResourceLoaderClientHtml(
227 [ 'target' => 'example' ]
230 // phpcs:disable Generic.Files.LineLength
231 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
232 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
235 $this->assertEquals( $expected, $client->getHeadHtml() );
239 * Confirm that 'safemode' is passed down to startup.
241 * @covers ResourceLoaderClientHtml::getHeadHtml
243 public function testGetHeadHtmlWithSafemode() {
244 $client = new ResourceLoaderClientHtml(
246 [ 'safemode' => '1' ]
249 // phpcs:disable Generic.Files.LineLength
250 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
251 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&safemode=1&skin=fallback"></script>';
254 $this->assertEquals( $expected, $client->getHeadHtml() );
258 * Confirm that a null 'target' is the same as no target.
260 * @covers ResourceLoaderClientHtml::getHeadHtml
262 public function testGetHeadHtmlWithNullTarget() {
263 $client = new ResourceLoaderClientHtml(
268 // phpcs:disable Generic.Files.LineLength
269 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
270 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
273 $this->assertEquals( $expected, $client->getHeadHtml() );
277 * @covers ResourceLoaderClientHtml::getBodyHtml
278 * @covers ResourceLoaderClientHtml::getLoad
280 public function testGetBodyHtml() {
281 $context = self
::makeContext();
282 $context->getResourceLoader()->register( self
::makeSampleModules() );
284 $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
285 $client->setConfig( [ 'key' => 'value' ] );
286 $client->setModules( [
288 'test.private.bottom',
290 $client->setModuleStyles( [
291 'test.styles.deprecated',
293 // phpcs:disable Generic.Files.LineLength
294 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
295 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
299 $this->assertEquals( $expected, $client->getBodyHtml() );
302 public static function provideMakeLoad() {
303 // phpcs:disable Generic.Files.LineLength
307 'modules' => [ 'test.unknown' ],
308 'only' => ResourceLoaderModule
::TYPE_STYLES
,
314 'modules' => [ 'test.styles.private' ],
315 'only' => ResourceLoaderModule
::TYPE_STYLES
,
317 'output' => '<style>.private{}</style>',
321 'modules' => [ 'test.private' ],
322 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
324 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
328 // Eg. startup module
329 'modules' => [ 'test.scripts.raw' ],
330 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
332 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
336 'modules' => [ 'test.scripts.raw' ],
337 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
338 'extra' => [ 'sync' => '1' ],
339 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
343 'modules' => [ 'test.scripts.user' ],
344 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
346 '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>',
350 'modules' => [ 'test.user' ],
351 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
353 '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>',
356 'context' => [ 'debug' => 'true' ],
357 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
358 'only' => ResourceLoaderModule
::TYPE_STYLES
,
360 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
361 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
364 'context' => [ 'debug' => 'false' ],
365 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
366 'only' => ResourceLoaderModule
::TYPE_STYLES
,
368 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
372 'modules' => [ 'test.styles.noscript' ],
373 'only' => ResourceLoaderModule
::TYPE_STYLES
,
375 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
379 'modules' => [ 'test.shouldembed' ],
380 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
382 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
386 'modules' => [ 'test.styles.shouldembed' ],
387 'only' => ResourceLoaderModule
::TYPE_STYLES
,
389 'output' => '<style>.shouldembed{}</style>',
393 'modules' => [ 'test.scripts.shouldembed' ],
394 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
396 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
400 'modules' => [ 'test', 'test.shouldembed' ],
401 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
403 '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>',
407 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
408 'only' => ResourceLoaderModule
::TYPE_STYLES
,
411 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
412 . '<style>.shouldembed{}</style>'
416 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
417 'only' => ResourceLoaderModule
::TYPE_STYLES
,
420 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
421 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
422 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
429 * @dataProvider provideMakeLoad
430 * @covers ResourceLoaderClientHtml::makeLoad
431 * @covers ResourceLoaderClientHtml::makeContext
432 * @covers ResourceLoader::makeModuleResponse
433 * @covers ResourceLoaderModule::getModuleContent
434 * @covers ResourceLoader::getCombinedVersion
435 * @covers ResourceLoader::createLoaderURL
436 * @covers ResourceLoader::createLoaderQuery
437 * @covers ResourceLoader::makeLoaderQuery
438 * @covers ResourceLoader::makeInlineScript
440 public function testMakeLoad(
447 $context = self
::makeContext( $contextQuery );
448 $context->getResourceLoader()->register( self
::makeSampleModules() );
449 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery, false );
450 $expected = self
::expandVariables( $expected );
451 $this->assertEquals( $expected, (string)$actual );