Merge "Fix \n handling for HTMLUsersMultiselectField"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderClientHtmlTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group ResourceLoader
7 */
8 class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase {
9
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION
13 ] );
14 }
15
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',
24 ] );
25 return new ResourceLoaderContext(
26 new ResourceLoader( $conf ),
27 new FauxRequest( array_merge( [
28 'lang' => 'nl',
29 'skin' => 'fallback',
30 'user' => 'Example',
31 'target' => 'phpunit',
32 ], $extraQuery ) )
33 );
34 }
35
36 protected static function makeModule( array $options = [] ) {
37 return new ResourceLoaderTestModule( $options );
38 }
39
40 protected static function makeSampleModules() {
41 $modules = [
42 'test' => [],
43 'test.top' => [ 'position' => 'top' ],
44 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
45 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
46
47 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ],
48 'test.styles.mixed' => [],
49 'test.styles.noscript' => [
50 'type' => ResourceLoaderModule::LOAD_STYLES,
51 'group' => 'noscript',
52 ],
53 'test.styles.user' => [
54 'type' => ResourceLoaderModule::LOAD_STYLES,
55 'group' => 'user',
56 ],
57 'test.styles.user.empty' => [
58 'type' => ResourceLoaderModule::LOAD_STYLES,
59 'group' => 'user',
60 'isKnownEmpty' => true,
61 ],
62 'test.styles.private' => [
63 'type' => ResourceLoaderModule::LOAD_STYLES,
64 'group' => 'private',
65 'styles' => '.private{}',
66 ],
67
68 'test.scripts' => [],
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 ],
73 ];
74 return array_map( function ( $options ) {
75 return self::makeModule( $options );
76 }, $modules );
77 }
78
79 /**
80 * @covers ResourceLoaderClientHtml::getDocumentAttributes
81 */
82 public function testGetDocumentAttributes() {
83 $client = new ResourceLoaderClientHtml( self::makeContext() );
84 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
85 }
86
87 /**
88 * @covers ResourceLoaderClientHtml::__construct
89 * @covers ResourceLoaderClientHtml::setModules
90 * @covers ResourceLoaderClientHtml::setModuleStyles
91 * @covers ResourceLoaderClientHtml::setModuleScripts
92 * @covers ResourceLoaderClientHtml::getData
93 * @covers ResourceLoaderClientHtml::getContext
94 */
95 public function testGetData() {
96 $context = self::makeContext();
97 $context->getResourceLoader()->register( self::makeSampleModules() );
98
99 $client = new ResourceLoaderClientHtml( $context );
100 $client->setModules( [
101 'test',
102 'test.private.bottom',
103 'test.private.top',
104 'test.top',
105 'test.unregistered',
106 ] );
107 $client->setModuleStyles( [
108 'test.styles.mixed',
109 'test.styles.user.empty',
110 'test.styles.private',
111 'test.styles.pure',
112 'test.unregistered.styles',
113 ] );
114 $client->setModuleScripts( [
115 'test.scripts',
116 'test.scripts.user.empty',
117 'test.scripts.top',
118 'test.unregistered.scripts',
119 ] );
120
121 $expected = [
122 'states' => [
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',
131 ],
132 'general' => [
133 'test',
134 'test.top',
135 ],
136 'styles' => [
137 'test.styles.pure',
138 ],
139 'scripts' => [
140 'test.scripts',
141 'test.scripts.top',
142 ],
143 'embed' => [
144 'styles' => [ 'test.styles.private' ],
145 'general' => [
146 'test.private.bottom',
147 'test.private.top',
148 ],
149 ],
150 ];
151
152 $access = TestingAccessWrapper::newFromObject( $client );
153 $this->assertEquals( $expected, $access->getData() );
154 }
155
156 /**
157 * @covers ResourceLoaderClientHtml::setConfig
158 * @covers ResourceLoaderClientHtml::setExemptStates
159 * @covers ResourceLoaderClientHtml::getHeadHtml
160 * @covers ResourceLoaderClientHtml::getLoad
161 * @covers ResourceLoader::makeLoaderStateScript
162 */
163 public function testGetHeadHtml() {
164 $context = self::makeContext();
165 $context->getResourceLoader()->register( self::makeSampleModules() );
166
167 $client = new ResourceLoaderClientHtml( $context );
168 $client->setConfig( [ 'key' => 'value' ] );
169 $client->setModules( [
170 'test.top',
171 'test.private.top',
172 ] );
173 $client->setModuleStyles( [
174 'test.styles.pure',
175 'test.styles.private',
176 ] );
177 $client->setModuleScripts( [
178 'test.scripts.top',
179 ] );
180 $client->setExemptStates( [
181 'test.exempt' => 'ready',
182 ] );
183
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&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>' . "\n"
194 . '<style>.private{}</style>' . "\n"
195 . '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=startup&amp;only=scripts&amp;skin=fallback"></script>';
196 // @codingStandardsIgnoreEnd
197 $expected = self::expandVariables( $expected );
198
199 $this->assertEquals( $expected, $client->getHeadHtml() );
200 }
201
202 /**
203 * @covers ResourceLoaderClientHtml::getBodyHtml
204 * @covers ResourceLoaderClientHtml::getLoad
205 */
206 public function testGetBodyHtml() {
207 $context = self::makeContext();
208 $context->getResourceLoader()->register( self::makeSampleModules() );
209
210 $client = new ResourceLoaderClientHtml( $context );
211 $client->setConfig( [ 'key' => 'value' ] );
212 $client->setModules( [
213 'test',
214 'test.private.bottom',
215 ] );
216 $client->setModuleScripts( [
217 'test.scripts',
218 ] );
219
220 $expected = '';
221 $expected = self::expandVariables( $expected );
222
223 $this->assertEquals( $expected, $client->getBodyHtml() );
224 }
225
226 public static function provideMakeLoad() {
227 return [
228 // @codingStandardsIgnoreStart Generic.Files.LineLength
229 [
230 'context' => [],
231 'modules' => [ 'test.unknown' ],
232 'only' => ResourceLoaderModule::TYPE_STYLES,
233 'output' => '',
234 ],
235 [
236 'context' => [],
237 'modules' => [ 'test.styles.private' ],
238 'only' => ResourceLoaderModule::TYPE_STYLES,
239 'output' => '<style>.private{}</style>',
240 ],
241 [
242 'context' => [],
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>',
246 ],
247 [
248 'context' => [],
249 // Eg. startup module
250 'modules' => [ 'test.scripts.raw' ],
251 'only' => ResourceLoaderModule::TYPE_SCRIPTS,
252 'output' => '<script async="" src="/w/load.php?debug=false&amp;lang=nl&amp;modules=test.scripts.raw&amp;only=scripts&amp;skin=fallback"></script>',
253 ],
254 [
255 'context' => [],
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>',
259 ],
260 [
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&amp;lang=nl&amp;modules=test.styles.mixed&amp;only=styles&amp;skin=fallback"/>' . "\n"
265 . '<link rel="stylesheet" href="/w/load.php?debug=true&amp;lang=nl&amp;modules=test.styles.pure&amp;only=styles&amp;skin=fallback"/>',
266 ],
267 [
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&amp;lang=nl&amp;modules=test.styles.mixed%2Cpure&amp;only=styles&amp;skin=fallback"/>',
272 ],
273 [
274 'context' => [],
275 'modules' => [ 'test.styles.noscript' ],
276 'only' => ResourceLoaderModule::TYPE_STYLES,
277 '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>',
278 ],
279 // @codingStandardsIgnoreEnd
280 ];
281 }
282
283 /**
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
294 */
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 );
301 }
302 }