3 use Wikimedia\TestingAccessWrapper;
8 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase {
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
16 protected static function makeContext( $extraQuery = [] ) {
18 'ResourceLoaderSources' => [],
19 'ResourceModuleSkinStyles' => [],
20 'ResourceModules' => [],
21 'EnableJavaScriptTest' =>
false,
22 'ResourceLoaderDebug' =>
false,
23 'LoadScript' =>
'/w/load.php',
26 new ResourceLoader( $conf ),
31 'target' =>
'phpunit',
40 protected static function makeSampleModules() {
43 'test.top' => [
'position' =>
'top' ],
44 'test.private.top' => [
'group' =>
'private',
'position' =>
'top' ],
45 'test.private.bottom' => [
'group' =>
'private',
'position' =>
'bottom' ],
46 'test.shouldembed' => [
'shouldEmbed' =>
true ],
49 'test.styles.mixed' => [],
50 'test.styles.noscript' => [
52 'group' =>
'noscript',
54 'test.styles.user' => [
58 'test.styles.user.empty' => [
61 'isKnownEmpty' =>
true,
63 'test.styles.private' => [
66 'styles' =>
'.private{}',
68 'test.styles.shouldembed' => [
70 'shouldEmbed' =>
true,
71 'styles' =>
'.shouldembed{}',
75 'test.scripts.top' => [
'position' =>
'top' ],
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 ) {
95 public function testGetDocumentAttributes() {
97 $this->assertInternalType(
'array', $client->getDocumentAttributes() );
108 public function testGetData() {
110 $context->getResourceLoader()->register( self::makeSampleModules() );
113 $client->setModules( [
115 'test.private.bottom',
121 $client->setModuleStyles( [
123 'test.styles.user.empty',
124 'test.styles.private',
126 'test.styles.shouldembed',
127 'test.unregistered.styles',
129 $client->setModuleScripts( [
131 'test.scripts.user.empty',
133 'test.scripts.shouldembed',
134 'test.unregistered.scripts',
139 'test.private.top' =>
'loading',
140 'test.private.bottom' =>
'loading',
141 'test.shouldembed' =>
'loading',
142 'test.styles.pure' =>
'ready',
143 'test.styles.user.empty' =>
'ready',
144 'test.styles.private' =>
'ready',
145 'test.styles.shouldembed' =>
'ready',
146 'test.scripts' =>
'loading',
147 'test.scripts.top' =>
'loading',
148 'test.scripts.user.empty' =>
'ready',
149 'test.scripts.shouldembed' =>
'loading',
161 'test.scripts.shouldembed',
164 'styles' => [
'test.styles.private',
'test.styles.shouldembed' ],
166 'test.private.bottom',
173 $access = TestingAccessWrapper::newFromObject( $client );
174 $this->assertEquals( $expected, $access->getData() );
184 public function testGetHeadHtml() {
186 $context->getResourceLoader()->register( self::makeSampleModules() );
189 $client->setConfig( [
'key' =>
'value' ] );
190 $client->setModules( [
194 $client->setModuleStyles( [
196 'test.styles.private',
198 $client->setModuleScripts( [
201 $client->setExemptStates( [
202 'test.exempt' =>
'ready',
206 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
207 .
'<script>(window.RLQ=window.RLQ||[]).push(function(){'
208 .
'mw.config.set({"key":"value"});'
209 .
'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
210 .
'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
211 .
'mw.loader.load(["test.top"]);'
212 .
'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
213 .
'});</script>' .
"\n"
214 .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' .
"\n"
215 .
'<style>.private{}</style>' .
"\n"
216 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
218 $expected = self::expandVariables( $expected );
220 $this->assertEquals( $expected, $client->getHeadHtml() );
227 public function testGetBodyHtml() {
229 $context->getResourceLoader()->register( self::makeSampleModules() );
232 $client->setConfig( [
'key' =>
'value' ] );
233 $client->setModules( [
235 'test.private.bottom',
237 $client->setModuleScripts( [
242 $expected = self::expandVariables( $expected );
244 $this->assertEquals( $expected, $client->getBodyHtml() );
247 public static function provideMakeLoad() {
252 'modules' => [
'test.unknown' ],
258 'modules' => [
'test.styles.private' ],
260 'output' =>
'<style>.private{}</style>',
264 'modules' => [
'test.private.top' ],
266 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
271 'modules' => [
'test.scripts.raw' ],
273 'output' =>
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
277 'modules' => [
'test.scripts.user' ],
279 '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>',
282 'context' => [
'debug' =>
true ],
283 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
285 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' .
"\n"
286 .
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
289 'context' => [
'debug' =>
false ],
290 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
292 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
296 'modules' => [
'test.styles.noscript' ],
298 'output' =>
'<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
302 'modules' => [
'test.shouldembed' ],
304 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
308 'modules' => [
'test.styles.shouldembed' ],
310 'output' =>
'<style>.shouldembed{}</style>',
314 'modules' => [
'test.scripts.shouldembed' ],
316 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
320 'modules' => [
'test',
'test.shouldembed' ],
322 '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>',
326 'modules' => [
'test.styles.pure',
'test.styles.shouldembed' ],
329 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' .
"\n"
330 .
'<style>.shouldembed{}</style>'
334 'modules' => [
'test.ordering.a',
'test.ordering.e',
'test.ordering.b',
'test.ordering.d',
'test.ordering.c' ],
337 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' .
"\n"
338 .
'<style>.orderingC{}.orderingD{}</style>' .
"\n"
339 .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
358 $context = self::makeContext( $extraQuery );
359 $context->getResourceLoader()->register( self::makeSampleModules() );
361 $expected = self::expandVariables( $expected );
362 $this->assertEquals( $expected, (
string)$actual );