3 use Wikimedia\TestingAccessWrapper;
8 class ResourceLoaderClientHtmlTest
extends PHPUnit\Framework\TestCase {
10 use MediaWikiCoversValidator;
12 protected static function expandVariables( $text ) {
13 return strtr( $text, [
18 protected static function makeContext( $extraQuery = [] ) {
20 'ResourceLoaderSources' => [],
21 'ResourceModuleSkinStyles' => [],
22 'ResourceModules' => [],
23 'EnableJavaScriptTest' =>
false,
24 'ResourceLoaderDebug' =>
false,
25 'LoadScript' =>
'/w/load.php',
28 new ResourceLoader( $conf ),
33 'target' =>
'phpunit',
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' ],
51 'test.styles.mixed' => [],
52 'test.styles.noscript' => [
54 'group' =>
'noscript',
56 'test.styles.user' => [
60 'test.styles.user.empty' => [
63 'isKnownEmpty' =>
true,
65 'test.styles.private' => [
68 'styles' =>
'.private{}',
70 'test.styles.shouldembed' => [
72 'shouldEmbed' =>
true,
73 'styles' =>
'.shouldembed{}',
75 'test.styles.deprecated' => [
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 ) {
100 public function testGetDocumentAttributes() {
102 $this->assertInternalType(
'array', $client->getDocumentAttributes() );
113 public function testGetData() {
115 $context->getResourceLoader()->register( self::makeSampleModules() );
118 $client->setModules( [
121 'test.shouldembed.empty',
126 $client->setModuleStyles( [
128 'test.styles.user.empty',
129 'test.styles.private',
131 'test.styles.shouldembed',
132 'test.styles.deprecated',
133 'test.unregistered.styles',
135 $client->setModuleScripts( [
138 'test.scripts.user.empty',
139 'test.scripts.shouldembed',
140 'test.unregistered.scripts',
145 'test.private' =>
'loading',
146 'test.shouldembed.empty' =>
'ready',
147 'test.shouldembed' =>
'loading',
148 'test.user' =>
'loading',
149 'test.styles.pure' =>
'ready',
150 'test.styles.user.empty' =>
'ready',
151 'test.styles.private' =>
'ready',
152 'test.styles.shouldembed' =>
'ready',
153 'test.styles.deprecated' =>
'ready',
154 'test.scripts' =>
'loading',
155 'test.scripts.user' =>
'loading',
156 'test.scripts.user.empty' =>
'ready',
157 'test.scripts.shouldembed' =>
'loading',
164 'test.styles.deprecated',
169 'test.scripts.shouldembed',
172 'styles' => [
'test.styles.private',
'test.styles.shouldembed' ],
179 'styleDeprecations' => [
182 [
'This page is using the deprecated ResourceLoader module "test.styles.deprecated".
183 Deprecation message.' ]
188 $access = TestingAccessWrapper::newFromObject( $client );
189 $this->assertEquals( $expected, $access->getData() );
199 public function testGetHeadHtml() {
201 $context->getResourceLoader()->register( self::makeSampleModules() );
206 $client->setConfig( [
'key' =>
'value' ] );
207 $client->setModules( [
211 $client->setModuleStyles( [
213 'test.styles.private',
214 'test.styles.deprecated',
216 $client->setModuleScripts( [
219 $client->setExemptStates( [
220 'test.exempt' =>
'ready',
224 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
225 .
'<script>(window.RLQ=window.RLQ||[]).push(function(){'
226 .
'mw.config.set({"key":"value"});'
227 .
'mw.loader.state({"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready","test.scripts":"loading"});'
228 .
'mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
229 .
'RLPAGEMODULES=["test"];mw.loader.load(RLPAGEMODULES);'
230 .
'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
231 .
'});</script>' .
"\n"
232 .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.deprecated%2Cpure&only=styles&skin=fallback"/>' .
"\n"
233 .
'<style>.private{}</style>' .
"\n"
234 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
236 $expected = self::expandVariables( $expected );
238 $this->assertEquals( $expected, $client->getHeadHtml() );
246 public function testGetHeadHtmlWithTarget() {
249 [
'target' =>
'example' ]
253 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
254 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback&target=example"></script>';
257 $this->assertEquals( $expected, $client->getHeadHtml() );
265 public function testGetHeadHtmlWithSafemode() {
268 [
'safemode' =>
'1' ]
272 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
273 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&safemode=1&skin=fallback"></script>';
276 $this->assertEquals( $expected, $client->getHeadHtml() );
284 public function testGetHeadHtmlWithNullTarget() {
291 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
292 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
295 $this->assertEquals( $expected, $client->getHeadHtml() );
302 public function testGetBodyHtml() {
304 $context->getResourceLoader()->register( self::makeSampleModules() );
307 $client->setConfig( [
'key' =>
'value' ] );
308 $client->setModules( [
310 'test.private.bottom',
312 $client->setModuleStyles( [
313 'test.styles.deprecated',
315 $client->setModuleScripts( [
319 $expected =
'<script>(window.RLQ=window.RLQ||[]).push(function(){'
320 .
'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");'
324 $this->assertEquals( $expected, $client->getBodyHtml() );
327 public static function provideMakeLoad() {
332 'modules' => [
'test.unknown' ],
338 'modules' => [
'test.styles.private' ],
340 'output' =>
'<style>.private{}</style>',
344 'modules' => [
'test.private' ],
346 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
351 'modules' => [
'test.scripts.raw' ],
353 'output' =>
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
356 'context' => [
'sync' =>
true ],
357 'modules' => [
'test.scripts.raw' ],
359 'output' =>
'<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
363 'modules' => [
'test.scripts.user' ],
365 '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>',
369 'modules' => [
'test.user' ],
371 '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>',
374 'context' => [
'debug' =>
true ],
375 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
377 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' .
"\n"
378 .
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
381 'context' => [
'debug' =>
false ],
382 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
384 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
388 'modules' => [
'test.styles.noscript' ],
390 'output' =>
'<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
394 'modules' => [
'test.shouldembed' ],
396 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
400 'modules' => [
'test.styles.shouldembed' ],
402 'output' =>
'<style>.shouldembed{}</style>',
406 'modules' => [
'test.scripts.shouldembed' ],
408 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
412 'modules' => [
'test',
'test.shouldembed' ],
414 '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>',
418 'modules' => [
'test.styles.pure',
'test.styles.shouldembed' ],
421 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' .
"\n"
422 .
'<style>.shouldembed{}</style>'
426 'modules' => [
'test.ordering.a',
'test.ordering.e',
'test.ordering.b',
'test.ordering.d',
'test.ordering.c' ],
429 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' .
"\n"
430 .
'<style>.orderingC{}.orderingD{}</style>' .
"\n"
431 .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
450 $context = self::makeContext( $extraQuery );
451 $context->getResourceLoader()->register( self::makeSampleModules() );
453 $expected = self::expandVariables( $expected );
454 $this->assertEquals( $expected, (
string)$actual );