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::getData
110 * @covers ResourceLoaderClientHtml::getContext
112 public function testGetData() {
113 $context = self
::makeContext();
114 $context->getResourceLoader()->register( self
::makeSampleModules() );
116 $client = new ResourceLoaderClientHtml( $context );
117 $client->setModules( [
120 'test.shouldembed.empty',
125 $client->setModuleStyles( [
127 'test.styles.user.empty',
128 'test.styles.private',
130 'test.styles.shouldembed',
131 'test.styles.deprecated',
132 'test.unregistered.styles',
137 'test.private' => 'loading',
138 'test.shouldembed.empty' => 'ready',
139 'test.shouldembed' => 'loading',
140 'test.user' => 'loading',
141 'test.styles.pure' => 'ready',
142 'test.styles.user.empty' => 'ready',
143 'test.styles.private' => 'ready',
144 'test.styles.shouldembed' => 'ready',
145 'test.styles.deprecated' => 'ready',
152 'test.styles.deprecated',
155 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
162 'styleDeprecations' => [
165 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
166 Deprecation message.' ]
171 $access = TestingAccessWrapper
::newFromObject( $client );
172 $this->assertEquals( $expected, $access->getData() );
176 * @covers ResourceLoaderClientHtml::setConfig
177 * @covers ResourceLoaderClientHtml::setExemptStates
178 * @covers ResourceLoaderClientHtml::getHeadHtml
179 * @covers ResourceLoaderClientHtml::getLoad
180 * @covers ResourceLoader::makeLoaderStateScript
182 public function testGetHeadHtml() {
183 $context = self
::makeContext();
184 $context->getResourceLoader()->register( self
::makeSampleModules() );
186 $client = new ResourceLoaderClientHtml( $context, [
189 $client->setConfig( [ 'key' => 'value' ] );
190 $client->setModules( [
194 $client->setModuleStyles( [
196 'test.styles.private',
197 'test.styles.deprecated',
199 $client->setExemptStates( [
200 'test.exempt' => 'ready',
203 // phpcs:disable Generic.Files.LineLength
204 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
205 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
206 . 'mw.config.set({"key":"value"});'
207 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"});'
208 . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
209 . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
210 . '});</script>' . "\n"
211 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.deprecated%2Cpure&only=styles&skin=fallback"/>' . "\n"
212 . '<style>.private{}</style>' . "\n"
213 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
215 $expected = self
::expandVariables( $expected );
217 $this->assertEquals( $expected, $client->getHeadHtml() );
221 * Confirm that 'target' is passed down to the startup module's load url.
223 * @covers ResourceLoaderClientHtml::getHeadHtml
225 public function testGetHeadHtmlWithTarget() {
226 $client = new ResourceLoaderClientHtml(
228 [ 'target' => 'example' ]
231 // phpcs:disable Generic.Files.LineLength
232 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
233 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
236 $this->assertEquals( $expected, $client->getHeadHtml() );
240 * Confirm that 'safemode' is passed down to startup.
242 * @covers ResourceLoaderClientHtml::getHeadHtml
244 public function testGetHeadHtmlWithSafemode() {
245 $client = new ResourceLoaderClientHtml(
247 [ 'safemode' => '1' ]
250 // phpcs:disable Generic.Files.LineLength
251 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
252 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&safemode=1&skin=fallback"></script>';
255 $this->assertEquals( $expected, $client->getHeadHtml() );
259 * Confirm that a null 'target' is the same as no target.
261 * @covers ResourceLoaderClientHtml::getHeadHtml
263 public function testGetHeadHtmlWithNullTarget() {
264 $client = new ResourceLoaderClientHtml(
269 // phpcs:disable Generic.Files.LineLength
270 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
271 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
274 $this->assertEquals( $expected, $client->getHeadHtml() );
278 * @covers ResourceLoaderClientHtml::getBodyHtml
279 * @covers ResourceLoaderClientHtml::getLoad
281 public function testGetBodyHtml() {
282 $context = self
::makeContext();
283 $context->getResourceLoader()->register( self
::makeSampleModules() );
285 $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
286 $client->setConfig( [ 'key' => 'value' ] );
287 $client->setModules( [
289 'test.private.bottom',
291 $client->setModuleStyles( [
292 'test.styles.deprecated',
294 // phpcs:disable Generic.Files.LineLength
295 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
296 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
300 $this->assertEquals( $expected, $client->getBodyHtml() );
303 public static function provideMakeLoad() {
304 // phpcs:disable Generic.Files.LineLength
308 'modules' => [ 'test.unknown' ],
309 'only' => ResourceLoaderModule
::TYPE_STYLES
,
314 'modules' => [ 'test.styles.private' ],
315 'only' => ResourceLoaderModule
::TYPE_STYLES
,
316 'output' => '<style>.private{}</style>',
320 'modules' => [ 'test.private' ],
321 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
322 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
326 // Eg. startup module
327 'modules' => [ 'test.scripts.raw' ],
328 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
329 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
332 'context' => [ 'sync' => true ],
333 'modules' => [ 'test.scripts.raw' ],
334 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
335 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
339 'modules' => [ 'test.scripts.user' ],
340 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
341 '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>',
345 'modules' => [ 'test.user' ],
346 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
347 '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>',
350 'context' => [ 'debug' => true ],
351 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
352 'only' => ResourceLoaderModule
::TYPE_STYLES
,
353 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
354 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
357 'context' => [ 'debug' => false ],
358 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
359 'only' => ResourceLoaderModule
::TYPE_STYLES
,
360 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
364 'modules' => [ 'test.styles.noscript' ],
365 'only' => ResourceLoaderModule
::TYPE_STYLES
,
366 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
370 'modules' => [ 'test.shouldembed' ],
371 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
372 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
376 'modules' => [ 'test.styles.shouldembed' ],
377 'only' => ResourceLoaderModule
::TYPE_STYLES
,
378 'output' => '<style>.shouldembed{}</style>',
382 'modules' => [ 'test.scripts.shouldembed' ],
383 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
384 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
388 'modules' => [ 'test', 'test.shouldembed' ],
389 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
390 '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>',
394 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
395 'only' => ResourceLoaderModule
::TYPE_STYLES
,
397 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
398 . '<style>.shouldembed{}</style>'
402 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
403 'only' => ResourceLoaderModule
::TYPE_STYLES
,
405 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
406 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
407 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
414 * @dataProvider provideMakeLoad
415 * @covers ResourceLoaderClientHtml::makeLoad
416 * @covers ResourceLoaderClientHtml::makeContext
417 * @covers ResourceLoader::makeModuleResponse
418 * @covers ResourceLoaderModule::getModuleContent
419 * @covers ResourceLoader::getCombinedVersion
420 * @covers ResourceLoader::createLoaderURL
421 * @covers ResourceLoader::createLoaderQuery
422 * @covers ResourceLoader::makeLoaderQuery
423 * @covers ResourceLoader::makeInlineScript
425 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
426 $context = self
::makeContext( $extraQuery );
427 $context->getResourceLoader()->register( self
::makeSampleModules() );
428 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery, false );
429 $expected = self
::expandVariables( $expected );
430 $this->assertEquals( $expected, (string)$actual );