Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderStartUpModuleTest.php
blob1b756be62904758f27e0834f6d32592b4ae36f46
1 <?php
3 class ResourceLoaderStartUpModuleTest extends ResourceLoaderTestCase {
5 protected static function expandPlaceholders( $text ) {
6 return strtr( $text, [
7 '{blankVer}' => self::BLANK_VERSION
8 ] );
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(),
27 'out' => '
28 mw.loader.addSource( {
29 "local": "/w/load.php"
30 } );
31 mw.loader.register( [
33 "test.blank",
34 "{blankVer}"
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' ] ),
45 'out' => '
46 mw.loader.addSource( {
47 "local": "/w/load.php"
48 } );
49 mw.loader.register( [
51 "test.blank",
52 "{blankVer}"
55 "test.group.foo",
56 "{blankVer}",
57 [],
58 "x-foo"
61 "test.group.bar",
62 "{blankVer}",
63 [],
64 "x-bar"
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' ] ] ),
74 'out' => '
75 mw.loader.addSource( {
76 "local": "/w/load.php"
77 } );
78 mw.loader.register( [
80 "test.blank",
81 "{blankVer}"
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',
93 'modules' => [
94 'test.blank' => new ResourceLoaderTestModule( [ 'source' => 'example' ] ),
96 'out' => '
97 mw.loader.addSource( {
98 "local": "/w/load.php",
99 "example": "http://example.org/w/load.php"
100 } );
101 mw.loader.register( [
103 "test.blank",
104 "{blankVer}",
106 null,
107 "example"
109 ] );'
110 ] ],
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',
132 ] ),
134 'out' => '
135 mw.loader.addSource( {
136 "local": "/w/load.php"
137 } );
138 mw.loader.register( [
140 "test.x.core",
141 "{blankVer}"
144 "test.x.polyfill",
145 "{blankVer}",
147 null,
148 null,
149 "return true;"
152 "test.y.polyfill",
153 "{blankVer}",
155 null,
156 null,
157 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
160 "test.z.foo",
161 "{blankVer}",
168 ] );',
169 ] ],
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',
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',
189 ] ),
190 'test.x.foo' => new ResourceLoaderTestModule( [
191 'dependencies' => [
192 'test.x.core',
194 ] ),
195 'test.x.bar' => new ResourceLoaderTestModule( [
196 'dependencies' => [
197 'test.x.core',
198 'test.x.util',
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',
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 ] ),
230 'out' => '
231 mw.loader.addSource( {
232 "local": "/w/load.php",
233 "example": "http://example.org/w/load.php"
234 } );
235 mw.loader.register( [
237 "test.blank",
238 "{blankVer}"
241 "test.x.core",
242 "{blankVer}"
245 "test.x.util",
246 "{blankVer}",
252 "test.x.foo",
253 "{blankVer}",
259 "test.x.bar",
260 "{blankVer}",
266 "test.x.quux",
267 "{blankVer}",
271 "test.x.unknown"
275 "test.group.foo.1",
276 "{blankVer}",
278 "x-foo"
281 "test.group.foo.2",
282 "{blankVer}",
284 "x-foo"
287 "test.group.bar.1",
288 "{blankVer}",
290 "x-bar"
293 "test.group.bar.2",
294 "{blankVer}",
296 "x-bar",
297 "example"
299 ] );'
300 ] ],
305 * @dataProvider provideGetModuleRegistrations
306 * @covers ResourceLoaderStartUpModule::compileUnresolvedDependencies
307 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
308 * @covers ResourceLoader::makeLoaderRegisterScript
310 public function testGetModuleRegistrations( $case ) {
311 if ( isset( $case['sources'] ) ) {
312 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
315 $context = $this->getResourceLoaderContext();
316 $rl = $context->getResourceLoader();
317 $rl->register( $case['modules'] );
318 $module = new ResourceLoaderStartUpModule();
319 $out = ltrim( $case['out'], "\n" );
321 $this->assertEquals(
322 self::expandPlaceholders( $out ),
323 $module->getModuleRegistrations( $context ),
324 $case['msg']
328 public static function provideRegistrations() {
329 return [
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',
342 ] ),
347 * @dataProvider provideRegistrations
349 public function testRegistrationsMinified( $modules ) {
350 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
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 . ']]);';
363 $this->assertEquals(
364 self::expandPlaceholders( $out ),
365 $module->getModuleRegistrations( $context ),
366 'Minified output'
371 * @dataProvider provideRegistrations
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( [
384 "test.blank",
385 "{blankVer}"
388 "test.min",
389 "{blankVer}",
393 null,
394 null,
395 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
397 ] );';
399 $this->assertEquals(
400 self::expandPlaceholders( $out ),
401 $module->getModuleRegistrations( $context ),
402 'Unminified output'