Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderStartUpModuleTest.php
blob9a36d1848fa5dc7014a1f8a34c54ee6ee56545b3
1 <?php
3 class ResourceLoaderStartUpModuleTest extends ResourceLoaderTestCase {
5 // Version hash for a blank file module.
6 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
7 // and ResourceLoaderFileModule::getDefinitionSummary().
8 protected static $blankVersion = 'GqV9IPpY';
10 protected static function expandPlaceholders( $text ) {
11 return strtr( $text, array(
12 '{blankVer}' => self::$blankVersion
13 ) );
16 public static function provideGetModuleRegistrations() {
17 return array(
18 array( array(
19 'msg' => 'Empty registry',
20 'modules' => array(),
21 'out' => '
22 mw.loader.addSource( {
23 "local": "/w/load.php"
24 } );
25 mw.loader.register( [] );'
26 ) ),
27 array( array(
28 'msg' => 'Basic registry',
29 'modules' => array(
30 'test.blank' => new ResourceLoaderTestModule(),
32 'out' => '
33 mw.loader.addSource( {
34 "local": "/w/load.php"
35 } );
36 mw.loader.register( [
38 "test.blank",
39 "{blankVer}"
41 ] );',
42 ) ),
43 array( array(
44 'msg' => 'Group signature',
45 'modules' => array(
46 'test.blank' => new ResourceLoaderTestModule(),
47 'test.group.foo' => new ResourceLoaderTestModule( array( 'group' => 'x-foo' ) ),
48 'test.group.bar' => new ResourceLoaderTestModule( array( 'group' => 'x-bar' ) ),
50 'out' => '
51 mw.loader.addSource( {
52 "local": "/w/load.php"
53 } );
54 mw.loader.register( [
56 "test.blank",
57 "{blankVer}"
60 "test.group.foo",
61 "{blankVer}",
62 [],
63 "x-foo"
66 "test.group.bar",
67 "{blankVer}",
68 [],
69 "x-bar"
71 ] );'
72 ) ),
73 array( array(
74 'msg' => 'Different target (non-test should not be registered)',
75 'modules' => array(
76 'test.blank' => new ResourceLoaderTestModule(),
77 'test.target.foo' => new ResourceLoaderTestModule( array( 'targets' => array( 'x-foo' ) ) ),
79 'out' => '
80 mw.loader.addSource( {
81 "local": "/w/load.php"
82 } );
83 mw.loader.register( [
85 "test.blank",
86 "{blankVer}"
88 ] );'
89 ) ),
90 array( array(
91 'msg' => 'Foreign source',
92 'sources' => array(
93 'example' => array(
94 'loadScript' => 'http://example.org/w/load.php',
95 'apiScript' => 'http://example.org/w/api.php',
98 'modules' => array(
99 'test.blank' => new ResourceLoaderTestModule( array( 'source' => 'example' ) ),
101 'out' => '
102 mw.loader.addSource( {
103 "local": "/w/load.php",
104 "example": "http://example.org/w/load.php"
105 } );
106 mw.loader.register( [
108 "test.blank",
109 "{blankVer}",
111 null,
112 "example"
114 ] );'
115 ) ),
116 array( array(
117 'msg' => 'Conditional dependency function',
118 'modules' => array(
119 'test.x.core' => new ResourceLoaderTestModule(),
120 'test.x.polyfill' => new ResourceLoaderTestModule( array(
121 'skipFunction' => 'return true;'
122 ) ),
123 'test.y.polyfill' => new ResourceLoaderTestModule( array(
124 'skipFunction' =>
125 'return !!(' .
126 ' window.JSON &&' .
127 ' JSON.parse &&' .
128 ' JSON.stringify' .
129 ');'
130 ) ),
131 'test.z.foo' => new ResourceLoaderTestModule( array(
132 'dependencies' => array(
133 'test.x.core',
134 'test.x.polyfill',
135 'test.y.polyfill',
137 ) ),
139 'out' => '
140 mw.loader.addSource( {
141 "local": "/w/load.php"
142 } );
143 mw.loader.register( [
145 "test.x.core",
146 "{blankVer}"
149 "test.x.polyfill",
150 "{blankVer}",
152 null,
153 null,
154 "return true;"
157 "test.y.polyfill",
158 "{blankVer}",
160 null,
161 null,
162 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
165 "test.z.foo",
166 "{blankVer}",
173 ] );',
174 ) ),
175 array( array(
176 // This may seem like an edge case, but a plain MediaWiki core install
177 // with a few extensions installed is likely far more complex than this
178 // even, not to mention an install like Wikipedia.
179 // TODO: Make this even more realistic.
180 'msg' => 'Advanced (everything combined)',
181 'sources' => array(
182 'example' => array(
183 'loadScript' => 'http://example.org/w/load.php',
184 'apiScript' => 'http://example.org/w/api.php',
187 'modules' => array(
188 'test.blank' => new ResourceLoaderTestModule(),
189 'test.x.core' => new ResourceLoaderTestModule(),
190 'test.x.util' => new ResourceLoaderTestModule( array(
191 'dependencies' => array(
192 'test.x.core',
194 ) ),
195 'test.x.foo' => new ResourceLoaderTestModule( array(
196 'dependencies' => array(
197 'test.x.core',
199 ) ),
200 'test.x.bar' => new ResourceLoaderTestModule( array(
201 'dependencies' => array(
202 'test.x.core',
203 'test.x.util',
205 ) ),
206 'test.x.quux' => new ResourceLoaderTestModule( array(
207 'dependencies' => array(
208 'test.x.foo',
209 'test.x.bar',
210 'test.x.util',
211 'test.x.unknown',
213 ) ),
214 'test.group.foo.1' => new ResourceLoaderTestModule( array(
215 'group' => 'x-foo',
216 ) ),
217 'test.group.foo.2' => new ResourceLoaderTestModule( array(
218 'group' => 'x-foo',
219 ) ),
220 'test.group.bar.1' => new ResourceLoaderTestModule( array(
221 'group' => 'x-bar',
222 ) ),
223 'test.group.bar.2' => new ResourceLoaderTestModule( array(
224 'group' => 'x-bar',
225 'source' => 'example',
226 ) ),
227 'test.target.foo' => new ResourceLoaderTestModule( array(
228 'targets' => array( 'x-foo' ),
229 ) ),
230 'test.target.bar' => new ResourceLoaderTestModule( array(
231 'source' => 'example',
232 'targets' => array( 'x-foo' ),
233 ) ),
235 'out' => '
236 mw.loader.addSource( {
237 "local": "/w/load.php",
238 "example": "http://example.org/w/load.php"
239 } );
240 mw.loader.register( [
242 "test.blank",
243 "{blankVer}"
246 "test.x.core",
247 "{blankVer}"
250 "test.x.util",
251 "{blankVer}",
257 "test.x.foo",
258 "{blankVer}",
264 "test.x.bar",
265 "{blankVer}",
271 "test.x.quux",
272 "{blankVer}",
276 "test.x.unknown"
280 "test.group.foo.1",
281 "{blankVer}",
283 "x-foo"
286 "test.group.foo.2",
287 "{blankVer}",
289 "x-foo"
292 "test.group.bar.1",
293 "{blankVer}",
295 "x-bar"
298 "test.group.bar.2",
299 "{blankVer}",
301 "x-bar",
302 "example"
304 ] );'
305 ) ),
310 * @dataProvider provideGetModuleRegistrations
311 * @covers ResourceLoaderStartUpModule::compileUnresolvedDependencies
312 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
313 * @covers ResourceLoader::makeLoaderSourcesScript
314 * @covers ResourceLoader::makeLoaderRegisterScript
316 public function testGetModuleRegistrations( $case ) {
317 if ( isset( $case['sources'] ) ) {
318 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
321 $context = $this->getResourceLoaderContext();
322 $rl = $context->getResourceLoader();
323 $rl->register( $case['modules'] );
324 $module = new ResourceLoaderStartUpModule();
325 $out = ltrim( $case['out'], "\n" );
327 $this->assertEquals(
328 self::expandPlaceholders( $out ),
329 $module->getModuleRegistrations( $context ),
330 $case['msg']
334 public static function provideRegistrations() {
335 return array(
336 array( array(
337 'test.blank' => new ResourceLoaderTestModule(),
338 'test.min' => new ResourceLoaderTestModule( array(
339 'skipFunction' =>
340 'return !!(' .
341 ' window.JSON &&' .
342 ' JSON.parse &&' .
343 ' JSON.stringify' .
344 ');',
345 'dependencies' => array(
346 'test.blank',
348 ) ),
353 * @dataProvider provideRegistrations
355 public function testRegistrationsMinified( $modules ) {
356 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
358 $context = $this->getResourceLoaderContext();
359 $rl = $context->getResourceLoader();
360 $rl->register( $modules );
361 $module = new ResourceLoaderStartUpModule();
362 $out = 'mw.loader.addSource({"local":"/w/load.php"});' . "\n"
363 . 'mw.loader.register(['
364 . '["test.blank","{blankVer}"],'
365 . '["test.min","{blankVer}",[0],null,null,'
366 . '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"'
367 . ']]);';
369 $this->assertEquals(
370 self::expandPlaceholders( $out ),
371 $module->getModuleRegistrations( $context ),
372 'Minified output'
377 * @dataProvider provideRegistrations
379 public function testRegistrationsUnminified( $modules ) {
380 $context = $this->getResourceLoaderContext();
381 $rl = $context->getResourceLoader();
382 $rl->register( $modules );
383 $module = new ResourceLoaderStartUpModule();
384 $out =
385 'mw.loader.addSource( {
386 "local": "/w/load.php"
387 } );
388 mw.loader.register( [
390 "test.blank",
391 "{blankVer}"
394 "test.min",
395 "{blankVer}",
399 null,
400 null,
401 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
403 ] );';
405 $this->assertEquals(
406 self::expandPlaceholders( $out ),
407 $module->getModuleRegistrations( $context ),
408 'Unminified output'