Support offsets in prefix searching
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderStartupModuleTest.php
blob1c25211b384626469c2dc8b615e966e11c9a5cac
1 <?php
3 class ResourceLoaderStartupModuleTest extends ResourceLoaderTestCase {
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 } );mw.loader.register( [] );'
14 ) ),
15 array( array(
16 'msg' => 'Basic registry',
17 'modules' => array(
18 'test.blank' => new ResourceLoaderTestModule(),
20 'out' => '
21 mw.loader.addSource( {
22 "local": "/w/load.php"
23 } );mw.loader.register( [
25 "test.blank",
26 "1388534400"
28 ] );',
29 ) ),
30 array( array(
31 'msg' => 'Group signature',
32 'modules' => array(
33 'test.blank' => new ResourceLoaderTestModule(),
34 'test.group.foo' => new ResourceLoaderTestModule( array( 'group' => 'x-foo' ) ),
35 'test.group.bar' => new ResourceLoaderTestModule( array( 'group' => 'x-bar' ) ),
37 'out' => '
38 mw.loader.addSource( {
39 "local": "/w/load.php"
40 } );mw.loader.register( [
42 "test.blank",
43 "1388534400"
46 "test.group.foo",
47 "1388534400",
48 [],
49 "x-foo"
52 "test.group.bar",
53 "1388534400",
54 [],
55 "x-bar"
57 ] );'
58 ) ),
59 array( array(
60 'msg' => 'Different target (non-test should not be registered)',
61 'modules' => array(
62 'test.blank' => new ResourceLoaderTestModule(),
63 'test.target.foo' => new ResourceLoaderTestModule( array( 'targets' => array( 'x-foo' ) ) ),
65 'out' => '
66 mw.loader.addSource( {
67 "local": "/w/load.php"
68 } );mw.loader.register( [
70 "test.blank",
71 "1388534400"
73 ] );'
74 ) ),
75 array( array(
76 'msg' => 'Foreign source',
77 'sources' => array(
78 'example' => array(
79 'loadScript' => 'http://example.org/w/load.php',
80 'apiScript' => 'http://example.org/w/api.php',
83 'modules' => array(
84 'test.blank' => new ResourceLoaderTestModule( array( 'source' => 'example' ) ),
86 'out' => '
87 mw.loader.addSource( {
88 "local": "/w/load.php",
89 "example": "http://example.org/w/load.php"
90 } );mw.loader.register( [
92 "test.blank",
93 "1388534400",
94 [],
95 null,
96 "example"
98 ] );'
99 ) ),
100 array( array(
101 'msg' => 'Conditional dependency function',
102 'modules' => array(
103 'test.x.core' => new ResourceLoaderTestModule(),
104 'test.x.polyfill' => new ResourceLoaderTestModule( array(
105 'skipFunction' => 'return true;'
106 ) ),
107 'test.y.polyfill' => new ResourceLoaderTestModule( array(
108 'skipFunction' =>
109 'return !!(' .
110 ' window.JSON &&' .
111 ' JSON.parse &&' .
112 ' JSON.stringify' .
113 ');'
114 ) ),
115 'test.z.foo' => new ResourceLoaderTestModule( array(
116 'dependencies' => array(
117 'test.x.core',
118 'test.x.polyfil',
119 'test.y.polyfil',
121 ) ),
123 'out' => '
124 mw.loader.addSource( {
125 "local": "/w/load.php"
126 } );mw.loader.register( [
128 "test.x.core",
129 "1388534400"
132 "test.x.polyfill",
133 "1388534400",
135 null,
136 "local",
137 "return true;"
140 "test.y.polyfill",
141 "1388534400",
143 null,
144 "local",
145 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
148 "test.z.foo",
149 "1388534400",
151 "test.x.core",
152 "test.x.polyfil",
153 "test.y.polyfil"
156 ] );',
157 ) ),
158 array( array(
159 // This may seem like an edge case, but a plain MediaWiki core install
160 // with a few extensions installed is likely far more complex than this
161 // even, not to mention an install like Wikipedia.
162 // TODO: Make this even more realistic.
163 'msg' => 'Advanced (everything combined)',
164 'sources' => array(
165 'example' => array(
166 'loadScript' => 'http://example.org/w/load.php',
167 'apiScript' => 'http://example.org/w/api.php',
170 'modules' => array(
171 'test.blank' => new ResourceLoaderTestModule(),
172 'test.x.core' => new ResourceLoaderTestModule(),
173 'test.x.util' => new ResourceLoaderTestModule( array(
174 'dependencies' => array(
175 'test.x.core',
177 ) ),
178 'test.x.foo' => new ResourceLoaderTestModule( array(
179 'dependencies' => array(
180 'test.x.core',
182 ) ),
183 'test.x.bar' => new ResourceLoaderTestModule( array(
184 'dependencies' => array(
185 'test.x.core',
186 'test.x.util',
188 ) ),
189 'test.x.quux' => new ResourceLoaderTestModule( array(
190 'dependencies' => array(
191 'test.x.foo',
192 'test.x.bar',
193 'test.x.util',
194 'test.x.unknown',
196 ) ),
197 'test.group.foo.1' => new ResourceLoaderTestModule( array(
198 'group' => 'x-foo',
199 ) ),
200 'test.group.foo.2' => new ResourceLoaderTestModule( array(
201 'group' => 'x-foo',
202 ) ),
203 'test.group.bar.1' => new ResourceLoaderTestModule( array(
204 'group' => 'x-bar',
205 ) ),
206 'test.group.bar.2' => new ResourceLoaderTestModule( array(
207 'group' => 'x-bar',
208 'source' => 'example',
209 ) ),
210 'test.target.foo' => new ResourceLoaderTestModule( array(
211 'targets' => array( 'x-foo' ),
212 ) ),
213 'test.target.bar' => new ResourceLoaderTestModule( array(
214 'source' => 'example',
215 'targets' => array( 'x-foo' ),
216 ) ),
218 'out' => '
219 mw.loader.addSource( {
220 "local": "/w/load.php",
221 "example": "http://example.org/w/load.php"
222 } );mw.loader.register( [
224 "test.blank",
225 "1388534400"
228 "test.x.core",
229 "1388534400"
232 "test.x.util",
233 "1388534400",
235 "test.x.core"
239 "test.x.foo",
240 "1388534400",
242 "test.x.core"
246 "test.x.bar",
247 "1388534400",
249 "test.x.util"
253 "test.x.quux",
254 "1388534400",
256 "test.x.foo",
257 "test.x.bar",
258 "test.x.unknown"
262 "test.group.foo.1",
263 "1388534400",
265 "x-foo"
268 "test.group.foo.2",
269 "1388534400",
271 "x-foo"
274 "test.group.bar.1",
275 "1388534400",
277 "x-bar"
280 "test.group.bar.2",
281 "1388534400",
283 "x-bar",
284 "example"
286 ] );'
287 ) ),
292 * @dataProvider provideGetModuleRegistrations
293 * @covers ResourceLoaderStartupModule::compileUnresolvedDependencies
294 * @covers ResourceLoaderStartUpModule::getModuleRegistrations
295 * @covers ResourceLoader::makeLoaderSourcesScript
296 * @covers ResourceLoader::makeLoaderRegisterScript
298 public function testGetModuleRegistrations( $case ) {
299 if ( isset( $case['sources'] ) ) {
300 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
303 $context = self::getResourceLoaderContext();
304 $rl = $context->getResourceLoader();
306 $rl->register( $case['modules'] );
308 $module = new ResourceLoaderStartUpModule();
309 $this->assertEquals(
310 ltrim( $case['out'], "\n" ),
311 $module->getModuleRegistrations( $context ),
312 $case['msg']
316 public static function provideRegistrations() {
317 return array(
318 array( array(
319 'test.blank' => new ResourceLoaderTestModule(),
320 'test.min' => new ResourceLoaderTestModule( array(
321 'skipFunction' =>
322 'return !!(' .
323 ' window.JSON &&' .
324 ' JSON.parse &&' .
325 ' JSON.stringify' .
326 ');',
327 'dependencies' => array(
328 'test.blank',
330 ) ),
335 * @dataProvider provideRegistrations
337 public function testRegistrationsMinified( $modules ) {
338 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
340 $context = self::getResourceLoaderContext();
341 $rl = $context->getResourceLoader();
342 $rl->register( $modules );
343 $module = new ResourceLoaderStartUpModule();
344 $this->assertEquals(
345 'mw.loader.addSource({"local":"/w/load.php"});'
346 . 'mw.loader.register(['
347 . '["test.blank","1388534400"],'
348 . '["test.min","1388534400",["test.blank"],null,"local",'
349 . '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"'
350 . ']]);',
351 $module->getModuleRegistrations( $context ),
352 'Minified output'
357 * @dataProvider provideRegistrations
359 public function testRegistrationsUnminified( $modules ) {
360 $context = self::getResourceLoaderContext();
361 $rl = $context->getResourceLoader();
362 $rl->register( $modules );
363 $module = new ResourceLoaderStartUpModule();
364 $this->assertEquals(
365 'mw.loader.addSource( {
366 "local": "/w/load.php"
367 } );mw.loader.register( [
369 "test.blank",
370 "1388534400"
373 "test.min",
374 "1388534400",
376 "test.blank"
378 null,
379 "local",
380 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
382 ] );',
383 $module->getModuleRegistrations( $context ),
384 'Unminified output'