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' ],
48 'test.styles.mixed' => [],
50 'test.styles.mixed.user' => [
'group' =>
'user' ],
51 'test.styles.mixed.user.empty' => [
'group' =>
'user',
'isKnownEmpty' =>
true ],
52 'test.styles.private' => [
'group' =>
'private',
'styles' =>
'.private{}' ],
55 'test.scripts.top' => [
'position' =>
'top' ],
56 'test.scripts.mixed.user' => [
'group' =>
'user' ],
57 'test.scripts.mixed.user.empty' => [
'group' =>
'user',
'isKnownEmpty' =>
true ],
58 'test.scripts.raw' => [
'isRaw' =>
true ],
60 return array_map(
function (
$options ) {
68 public function testGetDocumentAttributes() {
70 $this->assertInternalType(
'array', $client->getDocumentAttributes() );
81 public function testGetData() {
83 $context->getResourceLoader()->register( self::makeSampleModules() );
86 $client->setModules( [
88 'test.private.bottom',
93 $client->setModuleStyles( [
95 'test.styles.mixed.user.empty',
96 'test.styles.private',
98 'test.unregistered.styles',
100 $client->setModuleScripts( [
102 'test.scripts.mixed.user.empty',
104 'test.unregistered.scripts',
109 'test.private.top' =>
'loading',
110 'test.private.bottom' =>
'loading',
111 'test.styles.pure' =>
'ready',
112 'test.styles.mixed.user.empty' =>
'ready',
113 'test.styles.private' =>
'ready',
114 'test.scripts' =>
'loading',
115 'test.scripts.top' =>
'loading',
116 'test.scripts.mixed.user.empty' =>
'ready',
131 'styles' => [
'test.styles.private' ],
133 'test.private.bottom',
139 $access = TestingAccessWrapper::newFromObject( $client );
140 $this->assertEquals( $expected, $access->getData() );
150 public function testGetHeadHtml() {
152 $context->getResourceLoader()->register( self::makeSampleModules() );
155 $client->setConfig( [
'key' =>
'value' ] );
156 $client->setModules( [
160 $client->setModuleStyles( [
162 'test.styles.private',
164 $client->setModuleScripts( [
167 $client->setExemptStates( [
168 'test.exempt' =>
'ready',
172 $expected =
'<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' .
"\n"
173 .
'<script>(window.RLQ=window.RLQ||[]).push(function(){'
174 .
'mw.config.set({"key":"value"});'
175 .
'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
176 .
'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
177 .
'mw.loader.load(["test.top"]);'
178 .
'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
179 .
'});</script>' .
"\n"
180 .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' .
"\n"
181 .
'<style>.private{}</style>' .
"\n"
182 .
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
184 $expected = self::expandVariables( $expected );
186 $this->assertEquals( $expected, $client->getHeadHtml() );
193 public function testGetBodyHtml() {
195 $context->getResourceLoader()->register( self::makeSampleModules() );
198 $client->setConfig( [
'key' =>
'value' ] );
199 $client->setModules( [
201 'test.private.bottom',
203 $client->setModuleScripts( [
208 $expected = self::expandVariables( $expected );
210 $this->assertEquals( $expected, $client->getBodyHtml() );
213 public static function provideMakeLoad() {
218 'modules' => [
'test.unknown' ],
224 'modules' => [
'test.styles.private' ],
226 'output' =>
'<style>.private{}</style>',
230 'modules' => [
'test.private.top' ],
232 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
237 'modules' => [
'test.scripts.raw' ],
239 'output' =>
'<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
243 'modules' => [
'test.scripts.mixed.user' ],
245 'output' =>
'<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.mixed.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
248 'context' => [
'debug' =>
true ],
249 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
251 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' .
"\n"
252 .
'<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
255 'context' => [
'debug' =>
false ],
256 'modules' => [
'test.styles.pure',
'test.styles.mixed' ],
258 'output' =>
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
262 'modules' => [
'test.styles.noscript' ],
264 'output' =>
'<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
283 $context = self::makeContext( $extraQuery );
284 $context->getResourceLoader()->register( self::makeSampleModules() );
286 $expected = self::expandVariables( $expected );
287 $this->assertEquals( $expected, (
string)$actual );