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