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