Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderStartUpModuleTest.php
1 <?php
2
3 class ResourceLoaderStartUpModuleTest extends ResourceLoaderTestCase {
4
5 public static function provideGetModuleRegistrations() {
6 return array(
7 array( array(
8 'msg' => 'Empty registry',
9 'modules' => array(),
10 'out' => '
11 mw.loader.addSource( {
12 "local": "/w/load.php"
13 } );
14 mw.loader.register( [] );'
15 ) ),
16 array( array(
17 'msg' => 'Basic registry',
18 'modules' => array(
19 'test.blank' => new ResourceLoaderTestModule(),
20 ),
21 'out' => '
22 mw.loader.addSource( {
23 "local": "/w/load.php"
24 } );
25 mw.loader.register( [
26 [
27 "test.blank",
28 "wvTifjse"
29 ]
30 ] );',
31 ) ),
32 array( array(
33 'msg' => 'Group signature',
34 'modules' => array(
35 'test.blank' => new ResourceLoaderTestModule(),
36 'test.group.foo' => new ResourceLoaderTestModule( array( 'group' => 'x-foo' ) ),
37 'test.group.bar' => new ResourceLoaderTestModule( array( 'group' => 'x-bar' ) ),
38 ),
39 'out' => '
40 mw.loader.addSource( {
41 "local": "/w/load.php"
42 } );
43 mw.loader.register( [
44 [
45 "test.blank",
46 "wvTifjse"
47 ],
48 [
49 "test.group.foo",
50 "wvTifjse",
51 [],
52 "x-foo"
53 ],
54 [
55 "test.group.bar",
56 "wvTifjse",
57 [],
58 "x-bar"
59 ]
60 ] );'
61 ) ),
62 array( array(
63 'msg' => 'Different target (non-test should not be registered)',
64 'modules' => array(
65 'test.blank' => new ResourceLoaderTestModule(),
66 'test.target.foo' => new ResourceLoaderTestModule( array( 'targets' => array( 'x-foo' ) ) ),
67 ),
68 'out' => '
69 mw.loader.addSource( {
70 "local": "/w/load.php"
71 } );
72 mw.loader.register( [
73 [
74 "test.blank",
75 "wvTifjse"
76 ]
77 ] );'
78 ) ),
79 array( array(
80 'msg' => 'Foreign source',
81 'sources' => array(
82 'example' => array(
83 'loadScript' => 'http://example.org/w/load.php',
84 'apiScript' => 'http://example.org/w/api.php',
85 ),
86 ),
87 'modules' => array(
88 'test.blank' => new ResourceLoaderTestModule( array( 'source' => 'example' ) ),
89 ),
90 'out' => '
91 mw.loader.addSource( {
92 "local": "/w/load.php",
93 "example": "http://example.org/w/load.php"
94 } );
95 mw.loader.register( [
96 [
97 "test.blank",
98 "wvTifjse",
99 [],
100 null,
101 "example"
102 ]
103 ] );'
104 ) ),
105 array( array(
106 'msg' => 'Conditional dependency function',
107 'modules' => array(
108 'test.x.core' => new ResourceLoaderTestModule(),
109 'test.x.polyfill' => new ResourceLoaderTestModule( array(
110 'skipFunction' => 'return true;'
111 ) ),
112 'test.y.polyfill' => new ResourceLoaderTestModule( array(
113 'skipFunction' =>
114 'return !!(' .
115 ' window.JSON &&' .
116 ' JSON.parse &&' .
117 ' JSON.stringify' .
118 ');'
119 ) ),
120 'test.z.foo' => new ResourceLoaderTestModule( array(
121 'dependencies' => array(
122 'test.x.core',
123 'test.x.polyfill',
124 'test.y.polyfill',
125 ),
126 ) ),
127 ),
128 'out' => '
129 mw.loader.addSource( {
130 "local": "/w/load.php"
131 } );
132 mw.loader.register( [
133 [
134 "test.x.core",
135 "wvTifjse"
136 ],
137 [
138 "test.x.polyfill",
139 "wvTifjse",
140 [],
141 null,
142 null,
143 "return true;"
144 ],
145 [
146 "test.y.polyfill",
147 "wvTifjse",
148 [],
149 null,
150 null,
151 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
152 ],
153 [
154 "test.z.foo",
155 "wvTifjse",
156 [
157 0,
158 1,
159 2
160 ]
161 ]
162 ] );',
163 ) ),
164 array( array(
165 // This may seem like an edge case, but a plain MediaWiki core install
166 // with a few extensions installed is likely far more complex than this
167 // even, not to mention an install like Wikipedia.
168 // TODO: Make this even more realistic.
169 'msg' => 'Advanced (everything combined)',
170 'sources' => array(
171 'example' => array(
172 'loadScript' => 'http://example.org/w/load.php',
173 'apiScript' => 'http://example.org/w/api.php',
174 ),
175 ),
176 'modules' => array(
177 'test.blank' => new ResourceLoaderTestModule(),
178 'test.x.core' => new ResourceLoaderTestModule(),
179 'test.x.util' => new ResourceLoaderTestModule( array(
180 'dependencies' => array(
181 'test.x.core',
182 ),
183 ) ),
184 'test.x.foo' => new ResourceLoaderTestModule( array(
185 'dependencies' => array(
186 'test.x.core',
187 ),
188 ) ),
189 'test.x.bar' => new ResourceLoaderTestModule( array(
190 'dependencies' => array(
191 'test.x.core',
192 'test.x.util',
193 ),
194 ) ),
195 'test.x.quux' => new ResourceLoaderTestModule( array(
196 'dependencies' => array(
197 'test.x.foo',
198 'test.x.bar',
199 'test.x.util',
200 'test.x.unknown',
201 ),
202 ) ),
203 'test.group.foo.1' => new ResourceLoaderTestModule( array(
204 'group' => 'x-foo',
205 ) ),
206 'test.group.foo.2' => new ResourceLoaderTestModule( array(
207 'group' => 'x-foo',
208 ) ),
209 'test.group.bar.1' => new ResourceLoaderTestModule( array(
210 'group' => 'x-bar',
211 ) ),
212 'test.group.bar.2' => new ResourceLoaderTestModule( array(
213 'group' => 'x-bar',
214 'source' => 'example',
215 ) ),
216 'test.target.foo' => new ResourceLoaderTestModule( array(
217 'targets' => array( 'x-foo' ),
218 ) ),
219 'test.target.bar' => new ResourceLoaderTestModule( array(
220 'source' => 'example',
221 'targets' => array( 'x-foo' ),
222 ) ),
223 ),
224 'out' => '
225 mw.loader.addSource( {
226 "local": "/w/load.php",
227 "example": "http://example.org/w/load.php"
228 } );
229 mw.loader.register( [
230 [
231 "test.blank",
232 "wvTifjse"
233 ],
234 [
235 "test.x.core",
236 "wvTifjse"
237 ],
238 [
239 "test.x.util",
240 "wvTifjse",
241 [
242 1
243 ]
244 ],
245 [
246 "test.x.foo",
247 "wvTifjse",
248 [
249 1
250 ]
251 ],
252 [
253 "test.x.bar",
254 "wvTifjse",
255 [
256 2
257 ]
258 ],
259 [
260 "test.x.quux",
261 "wvTifjse",
262 [
263 3,
264 4,
265 "test.x.unknown"
266 ]
267 ],
268 [
269 "test.group.foo.1",
270 "wvTifjse",
271 [],
272 "x-foo"
273 ],
274 [
275 "test.group.foo.2",
276 "wvTifjse",
277 [],
278 "x-foo"
279 ],
280 [
281 "test.group.bar.1",
282 "wvTifjse",
283 [],
284 "x-bar"
285 ],
286 [
287 "test.group.bar.2",
288 "wvTifjse",
289 [],
290 "x-bar",
291 "example"
292 ]
293 ] );'
294 ) ),
295 );
296 }
297
298 /**
299 * @dataProvider provideGetModuleRegistrations
300 * @covers ResourceLoaderStartUpModule::compileUnresolvedDependencies
301 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
302 * @covers ResourceLoader::makeLoaderSourcesScript
303 * @covers ResourceLoader::makeLoaderRegisterScript
304 */
305 public function testGetModuleRegistrations( $case ) {
306 if ( isset( $case['sources'] ) ) {
307 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
308 }
309
310 $context = $this->getResourceLoaderContext();
311 $rl = $context->getResourceLoader();
312
313 $rl->register( $case['modules'] );
314
315 $module = new ResourceLoaderStartUpModule();
316 $this->assertEquals(
317 ltrim( $case['out'], "\n" ),
318 $module->getModuleRegistrations( $context ),
319 $case['msg']
320 );
321 }
322
323 public static function provideRegistrations() {
324 return array(
325 array( array(
326 'test.blank' => new ResourceLoaderTestModule(),
327 'test.min' => new ResourceLoaderTestModule( array(
328 'skipFunction' =>
329 'return !!(' .
330 ' window.JSON &&' .
331 ' JSON.parse &&' .
332 ' JSON.stringify' .
333 ');',
334 'dependencies' => array(
335 'test.blank',
336 ),
337 ) ),
338 ) )
339 );
340 }
341 /**
342 * @dataProvider provideRegistrations
343 */
344 public function testRegistrationsMinified( $modules ) {
345 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
346
347 $context = $this->getResourceLoaderContext();
348 $rl = $context->getResourceLoader();
349 $rl->register( $modules );
350 $module = new ResourceLoaderStartUpModule();
351 $this->assertEquals(
352 'mw.loader.addSource({"local":"/w/load.php"});' . "\n"
353 . 'mw.loader.register(['
354 . '["test.blank","wvTifjse"],'
355 . '["test.min","wvTifjse",[0],null,null,'
356 . '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"'
357 . ']]);',
358 $module->getModuleRegistrations( $context ),
359 'Minified output'
360 );
361 }
362
363 /**
364 * @dataProvider provideRegistrations
365 */
366 public function testRegistrationsUnminified( $modules ) {
367 $context = $this->getResourceLoaderContext();
368 $rl = $context->getResourceLoader();
369 $rl->register( $modules );
370 $module = new ResourceLoaderStartUpModule();
371 $this->assertEquals(
372 'mw.loader.addSource( {
373 "local": "/w/load.php"
374 } );
375 mw.loader.register( [
376 [
377 "test.blank",
378 "wvTifjse"
379 ],
380 [
381 "test.min",
382 "wvTifjse",
383 [
384 0
385 ],
386 null,
387 null,
388 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
389 ]
390 ] );',
391 $module->getModuleRegistrations( $context ),
392 'Unminified output'
393 );
394 }
395
396 }