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 ],
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{}',
76 'test.scripts.user' => [ 'group' => 'user' ],
77 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
78 'test.scripts.raw' => [ 'isRaw' => true ],
79 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
81 'test.ordering.a' => [ 'shouldEmbed' => false ],
82 'test.ordering.b' => [ 'shouldEmbed' => false ],
83 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
84 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
85 'test.ordering.e' => [ 'shouldEmbed' => false ],
87 return array_map( function ( $options ) {
88 return self
::makeModule( $options );
93 * @covers ResourceLoaderClientHtml::getDocumentAttributes
95 public function testGetDocumentAttributes() {
96 $client = new ResourceLoaderClientHtml( self
::makeContext() );
97 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
101 * @covers ResourceLoaderClientHtml::__construct
102 * @covers ResourceLoaderClientHtml::setModules
103 * @covers ResourceLoaderClientHtml::setModuleStyles
104 * @covers ResourceLoaderClientHtml::setModuleScripts
105 * @covers ResourceLoaderClientHtml::getData
106 * @covers ResourceLoaderClientHtml::getContext
108 public function testGetData() {
109 $context = self
::makeContext();
110 $context->getResourceLoader()->register( self
::makeSampleModules() );
112 $client = new ResourceLoaderClientHtml( $context );
113 $client->setModules( [
116 'test.shouldembed.empty',
120 $client->setModuleStyles( [
122 'test.styles.user.empty',
123 'test.styles.private',
125 'test.styles.shouldembed',
126 'test.unregistered.styles',
128 $client->setModuleScripts( [
131 'test.scripts.user.empty',
132 'test.scripts.shouldembed',
133 'test.unregistered.scripts',
138 'test.private' => 'loading',
139 'test.shouldembed.empty' => 'ready',
140 'test.shouldembed' => 'loading',
141 'test.styles.pure' => 'ready',
142 'test.styles.user.empty' => 'ready',
143 'test.styles.private' => 'ready',
144 'test.styles.shouldembed' => 'ready',
145 'test.scripts' => 'loading',
146 'test.scripts.user' => 'loading',
147 'test.scripts.user.empty' => 'ready',
148 'test.scripts.shouldembed' => 'loading',
159 'test.scripts.shouldembed',
162 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
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 );
186 $client->setConfig( [ 'key' => 'value' ] );
187 $client->setModules( [
191 $client->setModuleStyles( [
193 'test.styles.private',
195 $client->setModuleScripts( [
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.scripts":"loading"});'
207 . 'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
208 . 'mw.loader.load(["test"]);'
209 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
210 . '});</script>' . "\n"
211 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&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 * @covers ResourceLoaderClientHtml::getBodyHtml
222 * @covers ResourceLoaderClientHtml::getLoad
224 public function testGetBodyHtml() {
225 $context = self
::makeContext();
226 $context->getResourceLoader()->register( self
::makeSampleModules() );
228 $client = new ResourceLoaderClientHtml( $context );
229 $client->setConfig( [ 'key' => 'value' ] );
230 $client->setModules( [
232 'test.private.bottom',
234 $client->setModuleScripts( [
239 $expected = self
::expandVariables( $expected );
241 $this->assertEquals( $expected, $client->getBodyHtml() );
244 public static function provideMakeLoad() {
245 // phpcs:disable Generic.Files.LineLength
249 'modules' => [ 'test.unknown' ],
250 'only' => ResourceLoaderModule
::TYPE_STYLES
,
255 'modules' => [ 'test.styles.private' ],
256 'only' => ResourceLoaderModule
::TYPE_STYLES
,
257 'output' => '<style>.private{}</style>',
261 'modules' => [ 'test.private' ],
262 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
263 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
267 // Eg. startup module
268 'modules' => [ 'test.scripts.raw' ],
269 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
270 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
273 'context' => [ 'sync' => true ],
274 'modules' => [ 'test.scripts.raw' ],
275 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
276 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
280 'modules' => [ 'test.scripts.user' ],
281 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
282 '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>',
285 'context' => [ 'debug' => true ],
286 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
287 'only' => ResourceLoaderModule
::TYPE_STYLES
,
288 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
289 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
292 'context' => [ 'debug' => false ],
293 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
294 'only' => ResourceLoaderModule
::TYPE_STYLES
,
295 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
299 'modules' => [ 'test.styles.noscript' ],
300 'only' => ResourceLoaderModule
::TYPE_STYLES
,
301 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
305 'modules' => [ 'test.shouldembed' ],
306 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
307 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
311 'modules' => [ 'test.styles.shouldembed' ],
312 'only' => ResourceLoaderModule
::TYPE_STYLES
,
313 'output' => '<style>.shouldembed{}</style>',
317 'modules' => [ 'test.scripts.shouldembed' ],
318 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
319 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
323 'modules' => [ 'test', 'test.shouldembed' ],
324 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
325 '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>',
329 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
330 'only' => ResourceLoaderModule
::TYPE_STYLES
,
332 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
333 . '<style>.shouldembed{}</style>'
337 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
338 'only' => ResourceLoaderModule
::TYPE_STYLES
,
340 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
341 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
342 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
349 * @dataProvider provideMakeLoad
350 * @covers ResourceLoaderClientHtml::makeLoad
351 * @covers ResourceLoaderClientHtml::makeContext
352 * @covers ResourceLoader::makeModuleResponse
353 * @covers ResourceLoaderModule::getModuleContent
354 * @covers ResourceLoader::getCombinedVersion
355 * @covers ResourceLoader::createLoaderURL
356 * @covers ResourceLoader::createLoaderQuery
357 * @covers ResourceLoader::makeLoaderQuery
358 * @covers ResourceLoader::makeInlineScript
360 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
361 $context = self
::makeContext( $extraQuery );
362 $context->getResourceLoader()->register( self
::makeSampleModules() );
363 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery );
364 $expected = self
::expandVariables( $expected );
365 $this->assertEquals( $expected, (string)$actual );