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{}',
77 'test.scripts.user' => [ 'group' => 'user' ],
78 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
79 'test.scripts.raw' => [ 'isRaw' => true ],
80 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
82 'test.ordering.a' => [ 'shouldEmbed' => false ],
83 'test.ordering.b' => [ 'shouldEmbed' => false ],
84 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
85 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
86 'test.ordering.e' => [ 'shouldEmbed' => false ],
88 return array_map( function ( $options ) {
89 return self
::makeModule( $options );
94 * @covers ResourceLoaderClientHtml::getDocumentAttributes
96 public function testGetDocumentAttributes() {
97 $client = new ResourceLoaderClientHtml( self
::makeContext() );
98 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
102 * @covers ResourceLoaderClientHtml::__construct
103 * @covers ResourceLoaderClientHtml::setModules
104 * @covers ResourceLoaderClientHtml::setModuleStyles
105 * @covers ResourceLoaderClientHtml::setModuleScripts
106 * @covers ResourceLoaderClientHtml::getData
107 * @covers ResourceLoaderClientHtml::getContext
109 public function testGetData() {
110 $context = self
::makeContext();
111 $context->getResourceLoader()->register( self
::makeSampleModules() );
113 $client = new ResourceLoaderClientHtml( $context );
114 $client->setModules( [
117 'test.shouldembed.empty',
122 $client->setModuleStyles( [
124 'test.styles.user.empty',
125 'test.styles.private',
127 'test.styles.shouldembed',
128 'test.unregistered.styles',
130 $client->setModuleScripts( [
133 'test.scripts.user.empty',
134 'test.scripts.shouldembed',
135 'test.unregistered.scripts',
140 'test.private' => 'loading',
141 'test.shouldembed.empty' => 'ready',
142 'test.shouldembed' => 'loading',
143 'test.user' => 'loading',
144 'test.styles.pure' => 'ready',
145 'test.styles.user.empty' => 'ready',
146 'test.styles.private' => 'ready',
147 'test.styles.shouldembed' => 'ready',
148 'test.scripts' => 'loading',
149 'test.scripts.user' => 'loading',
150 'test.scripts.user.empty' => 'ready',
151 'test.scripts.shouldembed' => 'loading',
162 'test.scripts.shouldembed',
165 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
174 $access = TestingAccessWrapper
::newFromObject( $client );
175 $this->assertEquals( $expected, $access->getData() );
179 * @covers ResourceLoaderClientHtml::setConfig
180 * @covers ResourceLoaderClientHtml::setExemptStates
181 * @covers ResourceLoaderClientHtml::getHeadHtml
182 * @covers ResourceLoaderClientHtml::getLoad
183 * @covers ResourceLoader::makeLoaderStateScript
185 public function testGetHeadHtml() {
186 $context = self
::makeContext();
187 $context->getResourceLoader()->register( self
::makeSampleModules() );
189 $client = new ResourceLoaderClientHtml( $context );
190 $client->setConfig( [ 'key' => 'value' ] );
191 $client->setModules( [
195 $client->setModuleStyles( [
197 'test.styles.private',
199 $client->setModuleScripts( [
202 $client->setExemptStates( [
203 'test.exempt' => 'ready',
206 // phpcs:disable Generic.Files.LineLength
207 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
208 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
209 . 'mw.config.set({"key":"value"});'
210 . 'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts":"loading"});'
211 . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
212 . 'mw.loader.load(["test"]);'
213 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
214 . '});</script>' . "\n"
215 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
216 . '<style>.private{}</style>' . "\n"
217 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
219 $expected = self
::expandVariables( $expected );
221 $this->assertEquals( $expected, $client->getHeadHtml( false ) );
225 * Confirm that 'target' is passed down to the startup module's load url.
227 * @covers ResourceLoaderClientHtml::getHeadHtml
229 public function testGetHeadHtmlWithTarget() {
230 $client = new ResourceLoaderClientHtml(
232 [ 'target' => 'example' ]
235 // phpcs:disable Generic.Files.LineLength
236 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
237 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
240 $this->assertEquals( $expected, $client->getHeadHtml( false ) );
244 * Confirm that a null 'target' is the same as no target.
246 * @covers ResourceLoaderClientHtml::getHeadHtml
248 public function testGetHeadHtmlWithNullTarget() {
249 $client = new ResourceLoaderClientHtml(
254 // phpcs:disable Generic.Files.LineLength
255 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
256 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
259 $this->assertEquals( $expected, $client->getHeadHtml( false ) );
263 * @covers ResourceLoaderClientHtml::getBodyHtml
264 * @covers ResourceLoaderClientHtml::getLoad
266 public function testGetBodyHtml() {
267 $context = self
::makeContext();
268 $context->getResourceLoader()->register( self
::makeSampleModules() );
270 $client = new ResourceLoaderClientHtml( $context );
271 $client->setConfig( [ 'key' => 'value' ] );
272 $client->setModules( [
274 'test.private.bottom',
276 $client->setModuleScripts( [
281 $expected = self
::expandVariables( $expected );
283 $this->assertEquals( $expected, $client->getBodyHtml() );
286 public static function provideMakeLoad() {
287 // phpcs:disable Generic.Files.LineLength
291 'modules' => [ 'test.unknown' ],
292 'only' => ResourceLoaderModule
::TYPE_STYLES
,
297 'modules' => [ 'test.styles.private' ],
298 'only' => ResourceLoaderModule
::TYPE_STYLES
,
299 'output' => '<style>.private{}</style>',
303 'modules' => [ 'test.private' ],
304 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
305 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
309 // Eg. startup module
310 'modules' => [ 'test.scripts.raw' ],
311 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
312 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
315 'context' => [ 'sync' => true ],
316 'modules' => [ 'test.scripts.raw' ],
317 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
318 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
322 'modules' => [ 'test.scripts.user' ],
323 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
324 '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>',
328 'modules' => [ 'test.user' ],
329 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
330 '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>',
333 'context' => [ 'debug' => true ],
334 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
335 'only' => ResourceLoaderModule
::TYPE_STYLES
,
336 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
337 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
340 'context' => [ 'debug' => false ],
341 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
342 'only' => ResourceLoaderModule
::TYPE_STYLES
,
343 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
347 'modules' => [ 'test.styles.noscript' ],
348 'only' => ResourceLoaderModule
::TYPE_STYLES
,
349 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
353 'modules' => [ 'test.shouldembed' ],
354 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
355 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
359 'modules' => [ 'test.styles.shouldembed' ],
360 'only' => ResourceLoaderModule
::TYPE_STYLES
,
361 'output' => '<style>.shouldembed{}</style>',
365 'modules' => [ 'test.scripts.shouldembed' ],
366 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
367 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
371 'modules' => [ 'test', 'test.shouldembed' ],
372 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
373 '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>',
377 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
378 'only' => ResourceLoaderModule
::TYPE_STYLES
,
380 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
381 . '<style>.shouldembed{}</style>'
385 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
386 'only' => ResourceLoaderModule
::TYPE_STYLES
,
388 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
389 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
390 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
397 * @dataProvider provideMakeLoad
398 * @covers ResourceLoaderClientHtml::makeLoad
399 * @covers ResourceLoaderClientHtml::makeContext
400 * @covers ResourceLoader::makeModuleResponse
401 * @covers ResourceLoaderModule::getModuleContent
402 * @covers ResourceLoader::getCombinedVersion
403 * @covers ResourceLoader::createLoaderURL
404 * @covers ResourceLoader::createLoaderQuery
405 * @covers ResourceLoader::makeLoaderQuery
406 * @covers ResourceLoader::makeInlineScript
408 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
409 $context = self
::makeContext( $extraQuery );
410 $context->getResourceLoader()->register( self
::makeSampleModules() );
411 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery, false );
412 $expected = self
::expandVariables( $expected );
413 $this->assertEquals( $expected, (string)$actual );