4 * @group ResourceLoader
6 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
8 protected static function makeContext( $extraQuery = [] ) {
9 $conf = new HashConfig( [
10 'ResourceLoaderSources' => [],
11 'ResourceModuleSkinStyles' => [],
12 'ResourceModules' => [],
13 'EnableJavaScriptTest' => false,
14 'ResourceLoaderDebug' => false,
15 'LoadScript' => '/w/load.php',
17 return new ResourceLoaderContext(
18 new ResourceLoader( $conf ),
19 new FauxRequest( array_merge( [
23 'target' => 'phpunit',
28 protected static function makeModule( array $options = [] ) {
29 return new ResourceLoaderTestModule( $options );
32 protected static function makeSampleModules() {
35 'test.top' => [ 'position' => 'top' ],
36 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
37 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
39 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
40 'test.styles.mixed' => [],
41 'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule
::LOAD_STYLES
],
42 'test.styles.mixed.user' => [ 'group' => 'user' ],
43 'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
44 'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
47 'test.scripts.top' => [ 'position' => 'top' ],
48 'test.scripts.mixed.user' => [ 'group' => 'user' ],
49 'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
50 'test.scripts.raw' => [ 'isRaw' => true ],
52 return array_map( function ( $options ) {
53 return self
::makeModule( $options );
58 * @covers ResourceLoaderClientHtml::getDocumentAttributes
60 public function testGetDocumentAttributes() {
61 $client = new ResourceLoaderClientHtml( self
::makeContext() );
62 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
66 * @covers ResourceLoaderClientHtml::__construct
67 * @covers ResourceLoaderClientHtml::setModules
68 * @covers ResourceLoaderClientHtml::setModuleStyles
69 * @covers ResourceLoaderClientHtml::setModuleScripts
70 * @covers ResourceLoaderClientHtml::getData
71 * @covers ResourceLoaderClientHtml::getContext
73 public function testGetData() {
74 $context = self
::makeContext();
75 $context->getResourceLoader()->register( self
::makeSampleModules() );
77 $client = new ResourceLoaderClientHtml( $context );
78 $client->setModules( [
80 'test.private.bottom',
85 $client->setModuleStyles( [
87 'test.styles.mixed.user.empty',
88 'test.styles.private',
90 'test.unregistered.styles',
92 $client->setModuleScripts( [
94 'test.scripts.mixed.user.empty',
96 'test.unregistered.scripts',
101 'test.private.top' => 'loading',
102 'test.private.bottom' => 'loading',
103 'test.styles.pure' => 'ready',
104 'test.styles.mixed.user.empty' => 'ready',
105 'test.styles.private' => 'ready',
106 'test.scripts' => 'loading',
107 'test.scripts.top' => 'loading',
108 'test.scripts.mixed.user.empty' => 'ready',
111 'top' => [ 'test.top' ],
112 'bottom' => [ 'test' ],
119 'top' => [ 'test.scripts.top' ],
120 'bottom' => [ 'test.scripts' ],
123 'styles' => [ 'test.styles.private' ],
125 'top' => [ 'test.private.top' ],
126 'bottom' => [ 'test.private.bottom' ],
131 $access = TestingAccessWrapper
::newFromObject( $client );
132 $this->assertEquals( $expected, $access->getData() );
136 * @covers ResourceLoaderClientHtml::setConfig
137 * @covers ResourceLoaderClientHtml::setExemptStates
138 * @covers ResourceLoaderClientHtml::getHeadHtml
139 * @covers ResourceLoaderClientHtml::getLoad
140 * @covers ResourceLoader::makeLoaderStateScript
142 public function testGetHeadHtml() {
143 $context = self
::makeContext();
144 $context->getResourceLoader()->register( self
::makeSampleModules() );
146 $client = new ResourceLoaderClientHtml( $context );
147 $client->setConfig( [ 'key' => 'value' ] );
148 $client->setModules( [
152 $client->setModuleStyles( [
154 'test.styles.private',
156 $client->setModuleScripts( [
159 $client->setExemptStates( [
160 'test.exempt' => 'ready',
163 // @codingStandardsIgnoreStart Generic.Files.LineLength
164 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
165 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
166 . 'mw.config.set({"key":"value"});'
167 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
168 . 'mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});'
169 . 'mw.loader.load(["test.top"]);'
170 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
171 . '});</script>' . "\n"
172 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
173 . '<style>.private{}</style>' . "\n"
174 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
175 // @codingStandardsIgnoreEnd
177 $this->assertEquals( $expected, $client->getHeadHtml() );
181 * @covers ResourceLoaderClientHtml::getBodyHtml
182 * @covers ResourceLoaderClientHtml::getLoad
184 public function testGetBodyHtml() {
185 $context = self
::makeContext();
186 $context->getResourceLoader()->register( self
::makeSampleModules() );
188 $client = new ResourceLoaderClientHtml( $context );
189 $client->setConfig( [ 'key' => 'value' ] );
190 $client->setModules( [
192 'test.private.bottom',
194 $client->setModuleScripts( [
198 // @codingStandardsIgnoreStart Generic.Files.LineLength
199 $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){'
200 . 'mw.loader.implement("test.private.bottom",function($,jQuery,require,module){},{"css":[]});'
201 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");'
202 . 'mw.loader.load(["test"]);'
204 // @codingStandardsIgnoreEnd
206 $this->assertEquals( $expected, $client->getBodyHtml() );
209 public static function provideMakeLoad() {
211 // @codingStandardsIgnoreStart Generic.Files.LineLength
214 'modules' => [ 'test.unknown' ],
215 'only' => ResourceLoaderModule
::TYPE_STYLES
,
220 'modules' => [ 'test.styles.private' ],
221 'only' => ResourceLoaderModule
::TYPE_STYLES
,
222 'output' => '<style>.private{}</style>',
226 'modules' => [ 'test.private.top' ],
227 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
228 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});});</script>',
232 // Eg. startup module
233 'modules' => [ 'test.scripts.raw' ],
234 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
235 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
239 'modules' => [ 'test.scripts.mixed.user' ],
240 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
241 '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>',
244 'context' => [ 'debug' => true ],
245 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
246 'only' => ResourceLoaderModule
::TYPE_STYLES
,
247 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
248 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>',
252 'modules' => [ 'test.styles.noscript' ],
253 'only' => ResourceLoaderModule
::TYPE_STYLES
,
254 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
256 // @codingStandardsIgnoreEnd
261 * @dataProvider provideMakeLoad
262 * @covers ResourceLoaderClientHtml::makeLoad
263 * @covers ResourceLoaderClientHtml::makeContext
264 * @covers ResourceLoader::makeModuleResponse
265 * @covers ResourceLoaderModule::getModuleContent
266 * @covers ResourceLoader::getCombinedVersion
267 * @covers ResourceLoader::createLoaderURL
268 * @covers ResourceLoader::createLoaderQuery
269 * @covers ResourceLoader::makeLoaderQuery
270 * @covers ResourceLoader::makeInlineScript
272 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
273 $context = self
::makeContext( $extraQuery );
274 $context->getResourceLoader()->register( self
::makeSampleModules() );
275 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type );
276 $this->assertEquals( $expected, (string)$actual );