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 'ResourceModuleSkinStyles' => [],
21 'ResourceModules' => [],
22 'EnableJavaScriptTest' => false,
23 'LoadScript' => '/w/load.php',
25 return new ResourceLoaderContext(
26 new ResourceLoader( $conf ),
27 new FauxRequest( array_merge( [
31 'target' => 'phpunit',
36 protected static function makeModule( array $options = [] ) {
37 return new ResourceLoaderTestModule( $options );
40 protected static function makeSampleModules() {
43 'test.private' => [ 'group' => 'private' ],
44 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
45 'test.shouldembed' => [ 'shouldEmbed' => true ],
46 'test.user' => [ 'group' => 'user' ],
48 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
49 'test.styles.mixed' => [],
50 'test.styles.noscript' => [
51 'type' => ResourceLoaderModule
::LOAD_STYLES
,
52 'group' => 'noscript',
54 'test.styles.user' => [
55 'type' => ResourceLoaderModule
::LOAD_STYLES
,
58 'test.styles.user.empty' => [
59 'type' => ResourceLoaderModule
::LOAD_STYLES
,
61 'isKnownEmpty' => true,
63 'test.styles.private' => [
64 'type' => ResourceLoaderModule
::LOAD_STYLES
,
66 'styles' => '.private{}',
68 'test.styles.shouldembed' => [
69 'type' => ResourceLoaderModule
::LOAD_STYLES
,
70 'shouldEmbed' => true,
71 'styles' => '.shouldembed{}',
73 'test.styles.deprecated' => [
74 'type' => ResourceLoaderModule
::LOAD_STYLES
,
75 'deprecated' => 'Deprecation message.',
79 'test.scripts.user' => [ 'group' => 'user' ],
80 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
81 'test.scripts.raw' => [ 'isRaw' => true ],
82 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
84 'test.ordering.a' => [ 'shouldEmbed' => false ],
85 'test.ordering.b' => [ 'shouldEmbed' => false ],
86 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
87 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
88 'test.ordering.e' => [ 'shouldEmbed' => false ],
90 return array_map( function ( $options ) {
91 return self
::makeModule( $options );
96 * @covers ResourceLoaderClientHtml::getDocumentAttributes
98 public function testGetDocumentAttributes() {
99 $client = new ResourceLoaderClientHtml( self
::makeContext() );
100 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
104 * @covers ResourceLoaderClientHtml::__construct
105 * @covers ResourceLoaderClientHtml::setModules
106 * @covers ResourceLoaderClientHtml::setModuleStyles
107 * @covers ResourceLoaderClientHtml::getData
108 * @covers ResourceLoaderClientHtml::getContext
110 public function testGetData() {
111 $context = self
::makeContext();
112 $context->getResourceLoader()->register( self
::makeSampleModules() );
114 $client = new ResourceLoaderClientHtml( $context );
115 $client->setModules( [
118 'test.shouldembed.empty',
123 $client->setModuleStyles( [
125 'test.styles.user.empty',
126 'test.styles.private',
128 'test.styles.shouldembed',
129 'test.styles.deprecated',
130 'test.unregistered.styles',
135 'test.private' => 'loading',
136 'test.shouldembed.empty' => 'ready',
137 'test.shouldembed' => 'loading',
138 'test.user' => 'loading',
139 'test.styles.pure' => 'ready',
140 'test.styles.user.empty' => 'ready',
141 'test.styles.private' => 'ready',
142 'test.styles.shouldembed' => 'ready',
143 'test.styles.deprecated' => 'ready',
150 'test.styles.deprecated',
153 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
160 'styleDeprecations' => [
163 [ 'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
164 Deprecation message.' ]
169 $access = TestingAccessWrapper
::newFromObject( $client );
170 $this->assertEquals( $expected, $access->getData() );
174 * @covers ResourceLoaderClientHtml::setConfig
175 * @covers ResourceLoaderClientHtml::setExemptStates
176 * @covers ResourceLoaderClientHtml::getHeadHtml
177 * @covers ResourceLoaderClientHtml::getLoad
178 * @covers ResourceLoader::makeLoaderStateScript
180 public function testGetHeadHtml() {
181 $context = self
::makeContext();
182 $context->getResourceLoader()->register( self
::makeSampleModules() );
184 $client = new ResourceLoaderClientHtml( $context, [
187 $client->setConfig( [ 'key' => 'value' ] );
188 $client->setModules( [
192 $client->setModuleStyles( [
194 'test.styles.private',
195 'test.styles.deprecated',
197 $client->setExemptStates( [
198 'test.exempt' => 'ready',
201 // phpcs:disable Generic.Files.LineLength
202 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
203 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
204 . 'mw.config.set({"key":"value"});'
205 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"});'
206 . 'mw.loader.implement("test.private@{blankVer}",null,{"css":[]});'
207 . 'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
208 . '});</script>' . "\n"
209 . '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.deprecated%2Cpure&only=styles&skin=fallback"/>' . "\n"
210 . '<style>.private{}</style>' . "\n"
211 . '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
213 $expected = self
::expandVariables( $expected );
215 $this->assertEquals( $expected, $client->getHeadHtml() );
219 * Confirm that 'target' is passed down to the startup module's load url.
221 * @covers ResourceLoaderClientHtml::getHeadHtml
223 public function testGetHeadHtmlWithTarget() {
224 $client = new ResourceLoaderClientHtml(
226 [ 'target' => 'example' ]
229 // phpcs:disable Generic.Files.LineLength
230 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
231 . '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
234 $this->assertEquals( $expected, $client->getHeadHtml() );
238 * Confirm that 'safemode' is passed down to startup.
240 * @covers ResourceLoaderClientHtml::getHeadHtml
242 public function testGetHeadHtmlWithSafemode() {
243 $client = new ResourceLoaderClientHtml(
245 [ 'safemode' => '1' ]
248 // phpcs:disable Generic.Files.LineLength
249 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
250 . '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&safemode=1&skin=fallback"></script>';
253 $this->assertEquals( $expected, $client->getHeadHtml() );
257 * Confirm that a null 'target' is the same as no target.
259 * @covers ResourceLoaderClientHtml::getHeadHtml
261 public function testGetHeadHtmlWithNullTarget() {
262 $client = new ResourceLoaderClientHtml(
267 // phpcs:disable Generic.Files.LineLength
268 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
269 . '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
272 $this->assertEquals( $expected, $client->getHeadHtml() );
276 * @covers ResourceLoaderClientHtml::getBodyHtml
277 * @covers ResourceLoaderClientHtml::getLoad
279 public function testGetBodyHtml() {
280 $context = self
::makeContext();
281 $context->getResourceLoader()->register( self
::makeSampleModules() );
283 $client = new ResourceLoaderClientHtml( $context, [ 'nonce' => false ] );
284 $client->setConfig( [ 'key' => 'value' ] );
285 $client->setModules( [
287 'test.private.bottom',
289 $client->setModuleStyles( [
290 'test.styles.deprecated',
292 // phpcs:disable Generic.Files.LineLength
293 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
294 . 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
298 $this->assertEquals( $expected, $client->getBodyHtml() );
301 public static function provideMakeLoad() {
302 // phpcs:disable Generic.Files.LineLength
306 'modules' => [ 'test.unknown' ],
307 'only' => ResourceLoaderModule
::TYPE_STYLES
,
313 'modules' => [ 'test.styles.private' ],
314 'only' => ResourceLoaderModule
::TYPE_STYLES
,
316 'output' => '<style>.private{}</style>',
320 'modules' => [ 'test.private' ],
321 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
323 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",null,{"css":[]});});</script>',
327 // Eg. startup module
328 'modules' => [ 'test.scripts.raw' ],
329 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
331 'output' => '<script async="" src="/w/load.php?lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
335 'modules' => [ 'test.scripts.raw' ],
336 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
337 'extra' => [ 'sync' => '1' ],
338 'output' => '<script src="/w/load.php?lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
342 'modules' => [ 'test.scripts.user' ],
343 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
345 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
349 'modules' => [ 'test.user' ],
350 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
352 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.user\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
355 'context' => [ 'debug' => 'true' ],
356 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
357 'only' => ResourceLoaderModule
::TYPE_STYLES
,
359 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
360 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
363 'context' => [ 'debug' => 'false' ],
364 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
365 'only' => ResourceLoaderModule
::TYPE_STYLES
,
367 'output' => '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
371 'modules' => [ 'test.styles.noscript' ],
372 'only' => ResourceLoaderModule
::TYPE_STYLES
,
374 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
378 'modules' => [ 'test.shouldembed' ],
379 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
381 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
385 'modules' => [ 'test.styles.shouldembed' ],
386 'only' => ResourceLoaderModule
::TYPE_STYLES
,
388 'output' => '<style>.shouldembed{}</style>',
392 'modules' => [ 'test.scripts.shouldembed' ],
393 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
395 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
399 'modules' => [ 'test', 'test.shouldembed' ],
400 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
402 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",null,{"css":[]});});</script>',
406 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
407 'only' => ResourceLoaderModule
::TYPE_STYLES
,
410 '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
411 . '<style>.shouldembed{}</style>'
415 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
416 'only' => ResourceLoaderModule
::TYPE_STYLES
,
419 '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
420 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
421 . '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
428 * @dataProvider provideMakeLoad
429 * @covers ResourceLoaderClientHtml::makeLoad
430 * @covers ResourceLoaderClientHtml::makeContext
431 * @covers ResourceLoader::makeModuleResponse
432 * @covers ResourceLoaderModule::getModuleContent
433 * @covers ResourceLoader::getCombinedVersion
434 * @covers ResourceLoader::createLoaderURL
435 * @covers ResourceLoader::createLoaderQuery
436 * @covers ResourceLoader::makeLoaderQuery
437 * @covers ResourceLoader::makeInlineScript
439 public function testMakeLoad(
446 $context = self
::makeContext( $contextQuery );
447 $context->getResourceLoader()->register( self
::makeSampleModules() );
448 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery, false );
449 $expected = self
::expandVariables( $expected );
450 $this->assertEquals( $expected, (string)$actual );