3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
16 protected static function makeContext( $extraQuery = [] ) {
17 $conf = new HashConfig( [
18 'ResourceLoaderSources' => [],
19 'ResourceModuleSkinStyles' => [],
20 'ResourceModules' => [],
21 'EnableJavaScriptTest' => false,
22 'ResourceLoaderDebug' => false,
23 'LoadScript' => '/w/load.php',
25 return new ResourceLoaderContext(
26 new ResourceLoader( $conf ),
27 new FauxRequest( array_merge( [
31 'target' => 'phpunit',
36 protected static function makeModule( array $options = [] ) {
37 return new ResourceLoaderTestModule( $options );
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' ],
47 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
48 'test.styles.mixed' => [],
49 'test.styles.noscript' => [
50 'type' => ResourceLoaderModule
::LOAD_STYLES
,
51 'group' => 'noscript',
53 'test.styles.user' => [
54 'type' => ResourceLoaderModule
::LOAD_STYLES
,
57 'test.styles.user.empty' => [
58 'type' => ResourceLoaderModule
::LOAD_STYLES
,
60 'isKnownEmpty' => true,
62 'test.styles.private' => [
63 'type' => ResourceLoaderModule
::LOAD_STYLES
,
65 'styles' => '.private{}',
69 'test.scripts.top' => [ 'position' => 'top' ],
70 'test.scripts.user' => [ 'group' => 'user' ],
71 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
72 'test.scripts.raw' => [ 'isRaw' => true ],
74 return array_map( function ( $options ) {
75 return self
::makeModule( $options );
80 * @covers ResourceLoaderClientHtml::getDocumentAttributes
82 public function testGetDocumentAttributes() {
83 $client = new ResourceLoaderClientHtml( self
::makeContext() );
84 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
88 * @covers ResourceLoaderClientHtml::__construct
89 * @covers ResourceLoaderClientHtml::setModules
90 * @covers ResourceLoaderClientHtml::setModuleStyles
91 * @covers ResourceLoaderClientHtml::setModuleScripts
92 * @covers ResourceLoaderClientHtml::getData
93 * @covers ResourceLoaderClientHtml::getContext
95 public function testGetData() {
96 $context = self
::makeContext();
97 $context->getResourceLoader()->register( self
::makeSampleModules() );
99 $client = new ResourceLoaderClientHtml( $context );
100 $client->setModules( [
102 'test.private.bottom',
107 $client->setModuleStyles( [
109 'test.styles.user.empty',
110 'test.styles.private',
112 'test.unregistered.styles',
114 $client->setModuleScripts( [
116 'test.scripts.user.empty',
118 'test.unregistered.scripts',
123 'test.private.top' => 'loading',
124 'test.private.bottom' => 'loading',
125 'test.styles.pure' => 'ready',
126 'test.styles.user.empty' => 'ready',
127 'test.styles.private' => 'ready',
128 'test.scripts' => 'loading',
129 'test.scripts.top' => 'loading',
130 'test.scripts.user.empty' => 'ready',
144 'styles' => [ 'test.styles.private' ],
146 'test.private.bottom',
152 $access = TestingAccessWrapper
::newFromObject( $client );
153 $this->assertEquals( $expected, $access->getData() );
157 * @covers ResourceLoaderClientHtml::setConfig
158 * @covers ResourceLoaderClientHtml::setExemptStates
159 * @covers ResourceLoaderClientHtml::getHeadHtml
160 * @covers ResourceLoaderClientHtml::getLoad
161 * @covers ResourceLoader::makeLoaderStateScript
163 public function testGetHeadHtml() {
164 $context = self
::makeContext();
165 $context->getResourceLoader()->register( self
::makeSampleModules() );
167 $client = new ResourceLoaderClientHtml( $context );
168 $client->setConfig( [ 'key' => 'value' ] );
169 $client->setModules( [
173 $client->setModuleStyles( [
175 'test.styles.private',
177 $client->setModuleScripts( [
180 $client->setExemptStates( [
181 'test.exempt' => 'ready',
184 // @codingStandardsIgnoreStart Generic.Files.LineLength
185 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
186 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
187 . 'mw.config.set({"key":"value"});'
188 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
189 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
190 . 'mw.loader.load(["test.top"]);'
191 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
192 . '});</script>' . "\n"
193 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
194 . '<style>.private{}</style>' . "\n"
195 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
196 // @codingStandardsIgnoreEnd
197 $expected = self
::expandVariables( $expected );
199 $this->assertEquals( $expected, $client->getHeadHtml() );
203 * @covers ResourceLoaderClientHtml::getBodyHtml
204 * @covers ResourceLoaderClientHtml::getLoad
206 public function testGetBodyHtml() {
207 $context = self
::makeContext();
208 $context->getResourceLoader()->register( self
::makeSampleModules() );
210 $client = new ResourceLoaderClientHtml( $context );
211 $client->setConfig( [ 'key' => 'value' ] );
212 $client->setModules( [
214 'test.private.bottom',
216 $client->setModuleScripts( [
221 $expected = self
::expandVariables( $expected );
223 $this->assertEquals( $expected, $client->getBodyHtml() );
226 public static function provideMakeLoad() {
228 // @codingStandardsIgnoreStart Generic.Files.LineLength
231 'modules' => [ 'test.unknown' ],
232 'only' => ResourceLoaderModule
::TYPE_STYLES
,
237 'modules' => [ 'test.styles.private' ],
238 'only' => ResourceLoaderModule
::TYPE_STYLES
,
239 'output' => '<style>.private{}</style>',
243 'modules' => [ 'test.private.top' ],
244 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
245 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
249 // Eg. startup module
250 'modules' => [ 'test.scripts.raw' ],
251 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
252 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
256 'modules' => [ 'test.scripts.user' ],
257 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
258 '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>',
261 'context' => [ 'debug' => true ],
262 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
263 'only' => ResourceLoaderModule
::TYPE_STYLES
,
264 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
265 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
268 'context' => [ 'debug' => false ],
269 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
270 'only' => ResourceLoaderModule
::TYPE_STYLES
,
271 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
275 'modules' => [ 'test.styles.noscript' ],
276 'only' => ResourceLoaderModule
::TYPE_STYLES
,
277 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
279 // @codingStandardsIgnoreEnd
284 * @dataProvider provideMakeLoad
285 * @covers ResourceLoaderClientHtml::makeLoad
286 * @covers ResourceLoaderClientHtml::makeContext
287 * @covers ResourceLoader::makeModuleResponse
288 * @covers ResourceLoaderModule::getModuleContent
289 * @covers ResourceLoader::getCombinedVersion
290 * @covers ResourceLoader::createLoaderURL
291 * @covers ResourceLoader::createLoaderQuery
292 * @covers ResourceLoader::makeLoaderQuery
293 * @covers ResourceLoader::makeInlineScript
295 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
296 $context = self
::makeContext( $extraQuery );
297 $context->getResourceLoader()->register( self
::makeSampleModules() );
298 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type );
299 $expected = self
::expandVariables( $expected );
300 $this->assertEquals( $expected, (string)$actual );