4 * @group ResourceLoader
6 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
8 protected static function expandVariables( $text ) {
10 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
14 protected static function makeContext( $extraQuery = [] ) {
15 $conf = new HashConfig( [
16 'ResourceLoaderSources' => [],
17 'ResourceModuleSkinStyles' => [],
18 'ResourceModules' => [],
19 'EnableJavaScriptTest' => false,
20 'ResourceLoaderDebug' => false,
21 'LoadScript' => '/w/load.php',
23 return new ResourceLoaderContext(
24 new ResourceLoader( $conf ),
25 new FauxRequest( array_merge( [
29 'target' => 'phpunit',
34 protected static function makeModule( array $options = [] ) {
35 return new ResourceLoaderTestModule( $options );
38 protected static function makeSampleModules() {
41 'test.top' => [ 'position' => 'top' ],
42 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
43 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
45 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
46 'test.styles.mixed' => [],
47 'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule
::LOAD_STYLES
],
48 'test.styles.mixed.user' => [ 'group' => 'user' ],
49 'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
50 'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
53 'test.scripts.top' => [ 'position' => 'top' ],
54 'test.scripts.mixed.user' => [ 'group' => 'user' ],
55 'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
56 'test.scripts.raw' => [ 'isRaw' => true ],
58 return array_map( function ( $options ) {
59 return self
::makeModule( $options );
64 * @covers ResourceLoaderClientHtml::getDocumentAttributes
66 public function testGetDocumentAttributes() {
67 $client = new ResourceLoaderClientHtml( self
::makeContext() );
68 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
72 * @covers ResourceLoaderClientHtml::__construct
73 * @covers ResourceLoaderClientHtml::setModules
74 * @covers ResourceLoaderClientHtml::setModuleStyles
75 * @covers ResourceLoaderClientHtml::setModuleScripts
76 * @covers ResourceLoaderClientHtml::getData
77 * @covers ResourceLoaderClientHtml::getContext
79 public function testGetData() {
80 $context = self
::makeContext();
81 $context->getResourceLoader()->register( self
::makeSampleModules() );
83 $client = new ResourceLoaderClientHtml( $context );
84 $client->setModules( [
86 'test.private.bottom',
91 $client->setModuleStyles( [
93 'test.styles.mixed.user.empty',
94 'test.styles.private',
96 'test.unregistered.styles',
98 $client->setModuleScripts( [
100 'test.scripts.mixed.user.empty',
102 'test.unregistered.scripts',
107 'test.private.top' => 'loading',
108 'test.private.bottom' => 'loading',
109 'test.styles.pure' => 'ready',
110 'test.styles.mixed.user.empty' => 'ready',
111 'test.styles.private' => 'ready',
112 'test.scripts' => 'loading',
113 'test.scripts.top' => 'loading',
114 'test.scripts.mixed.user.empty' => 'ready',
129 'styles' => [ 'test.styles.private' ],
131 'test.private.bottom',
137 $access = TestingAccessWrapper
::newFromObject( $client );
138 $this->assertEquals( $expected, $access->getData() );
142 * @covers ResourceLoaderClientHtml::setConfig
143 * @covers ResourceLoaderClientHtml::setExemptStates
144 * @covers ResourceLoaderClientHtml::getHeadHtml
145 * @covers ResourceLoaderClientHtml::getLoad
146 * @covers ResourceLoader::makeLoaderStateScript
148 public function testGetHeadHtml() {
149 $context = self
::makeContext();
150 $context->getResourceLoader()->register( self
::makeSampleModules() );
152 $client = new ResourceLoaderClientHtml( $context );
153 $client->setConfig( [ 'key' => 'value' ] );
154 $client->setModules( [
158 $client->setModuleStyles( [
160 'test.styles.private',
162 $client->setModuleScripts( [
165 $client->setExemptStates( [
166 'test.exempt' => 'ready',
169 // @codingStandardsIgnoreStart Generic.Files.LineLength
170 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
171 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
172 . 'mw.config.set({"key":"value"});'
173 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
174 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
175 . 'mw.loader.load(["test.top"]);'
176 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
177 . '});</script>' . "\n"
178 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
179 . '<style>.private{}</style>' . "\n"
180 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
181 // @codingStandardsIgnoreEnd
182 $expected = self
::expandVariables( $expected );
184 $this->assertEquals( $expected, $client->getHeadHtml() );
188 * @covers ResourceLoaderClientHtml::getBodyHtml
189 * @covers ResourceLoaderClientHtml::getLoad
191 public function testGetBodyHtml() {
192 $context = self
::makeContext();
193 $context->getResourceLoader()->register( self
::makeSampleModules() );
195 $client = new ResourceLoaderClientHtml( $context );
196 $client->setConfig( [ 'key' => 'value' ] );
197 $client->setModules( [
199 'test.private.bottom',
201 $client->setModuleScripts( [
206 $expected = self
::expandVariables( $expected );
208 $this->assertEquals( $expected, $client->getBodyHtml() );
211 public static function provideMakeLoad() {
213 // @codingStandardsIgnoreStart Generic.Files.LineLength
216 'modules' => [ 'test.unknown' ],
217 'only' => ResourceLoaderModule
::TYPE_STYLES
,
222 'modules' => [ 'test.styles.private' ],
223 'only' => ResourceLoaderModule
::TYPE_STYLES
,
224 'output' => '<style>.private{}</style>',
228 'modules' => [ 'test.private.top' ],
229 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
230 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
234 // Eg. startup module
235 'modules' => [ 'test.scripts.raw' ],
236 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
237 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
241 'modules' => [ 'test.scripts.mixed.user' ],
242 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
243 '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>',
246 'context' => [ 'debug' => true ],
247 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
248 'only' => ResourceLoaderModule
::TYPE_STYLES
,
249 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
250 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
253 'context' => [ 'debug' => false ],
254 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
255 'only' => ResourceLoaderModule
::TYPE_STYLES
,
256 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
260 'modules' => [ 'test.styles.noscript' ],
261 'only' => ResourceLoaderModule
::TYPE_STYLES
,
262 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
264 // @codingStandardsIgnoreEnd
269 * @dataProvider provideMakeLoad
270 * @covers ResourceLoaderClientHtml::makeLoad
271 * @covers ResourceLoaderClientHtml::makeContext
272 * @covers ResourceLoader::makeModuleResponse
273 * @covers ResourceLoaderModule::getModuleContent
274 * @covers ResourceLoader::getCombinedVersion
275 * @covers ResourceLoader::createLoaderURL
276 * @covers ResourceLoader::createLoaderQuery
277 * @covers ResourceLoader::makeLoaderQuery
278 * @covers ResourceLoader::makeInlineScript
280 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
281 $context = self
::makeContext( $extraQuery );
282 $context->getResourceLoader()->register( self
::makeSampleModules() );
283 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type );
284 $expected = self
::expandVariables( $expected );
285 $this->assertEquals( $expected, (string)$actual );