Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderClientHtmlTest.php
blob2db3c1692c29e070dfd90b1d6d83d55bfac4f13f
1 <?php
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
8 protected static function expandVariables( $text ) {
9 return strtr( $text, [
10 '{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION
11 ] );
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',
22 ] );
23 return new ResourceLoaderContext(
24 new ResourceLoader( $conf ),
25 new FauxRequest( array_merge( [
26 'lang' => 'nl',
27 'skin' => 'fallback',
28 'user' => 'Example',
29 'target' => 'phpunit',
30 ], $extraQuery ) )
34 protected static function makeModule( array $options = [] ) {
35 return new ResourceLoaderTestModule( $options );
38 protected static function makeSampleModules() {
39 $modules = [
40 'test' => [],
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{}' ],
52 'test.scripts' => [],
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 );
60 }, $modules );
63 /**
64 * @covers ResourceLoaderClientHtml::getDocumentAttributes
66 public function testGetDocumentAttributes() {
67 $client = new ResourceLoaderClientHtml( self::makeContext() );
68 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
71 /**
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( [
85 'test',
86 'test.private.bottom',
87 'test.private.top',
88 'test.top',
89 'test.unregistered',
90 ] );
91 $client->setModuleStyles( [
92 'test.styles.mixed',
93 'test.styles.mixed.user.empty',
94 'test.styles.private',
95 'test.styles.pure',
96 'test.unregistered.styles',
97 ] );
98 $client->setModuleScripts( [
99 'test.scripts',
100 'test.scripts.mixed.user.empty',
101 'test.scripts.top',
102 'test.unregistered.scripts',
103 ] );
105 $expected = [
106 'states' => [
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',
116 'general' => [
117 'test',
118 'test.top',
120 'styles' => [
121 'test.styles.mixed',
122 'test.styles.pure',
124 'scripts' => [
125 'test.scripts',
126 'test.scripts.top',
128 'embed' => [
129 'styles' => [ 'test.styles.private' ],
130 'general' => [
131 'test.private.bottom',
132 'test.private.top',
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( [
155 'test.top',
156 'test.private.top',
157 ] );
158 $client->setModuleStyles( [
159 'test.styles.pure',
160 'test.styles.private',
161 ] );
162 $client->setModuleScripts( [
163 'test.scripts.top',
164 ] );
165 $client->setExemptStates( [
166 'test.exempt' => 'ready',
167 ] );
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&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
179 . '<style>.private{}</style>' . "\n"
180 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;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( [
198 'test',
199 'test.private.bottom',
200 ] );
201 $client->setModuleScripts( [
202 'test.scripts',
203 ] );
205 $expected = '';
206 $expected = self::expandVariables( $expected );
208 $this->assertEquals( $expected, $client->getBodyHtml() );
211 public static function provideMakeLoad() {
212 return [
213 // @codingStandardsIgnoreStart Generic.Files.LineLength
215 'context' => [],
216 'modules' => [ 'test.unknown' ],
217 'only' => ResourceLoaderModule::TYPE_STYLES,
218 'output' => '',
221 'context' => [],
222 'modules' => [ 'test.styles.private' ],
223 'only' => ResourceLoaderModule::TYPE_STYLES,
224 'output' => '<style>.private{}</style>',
227 'context' => [],
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>',
233 'context' => [],
234 // Eg. startup module
235 'modules' => [ 'test.scripts.raw' ],
236 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
237 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
240 'context' => [],
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&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>' . "\n"
250 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;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&amp;lang=nl&amp;modules=test.styles.mixed%2Cpure&amp;only=styles&amp;skin=fallback"/>',
259 'context' => [],
260 'modules' => [ 'test.styles.noscript' ],
261 'only' => ResourceLoaderModule::TYPE_STYLES,
262 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.styles.noscript&amp;only=styles&amp;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 );