MediaWiki REL1_27
ResourceLoaderStartUpModuleTest.php
Go to the documentation of this file.
1<?php
2
4
5 // Version hash for a blank file module.
6 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
7 // and ResourceLoaderFileModule::getDefinitionSummary().
8 protected static $blankVersion = 'GqV9IPpY';
9
10 protected static function expandPlaceholders( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => self::$blankVersion
13 ] );
14 }
15
16 public static function provideGetModuleRegistrations() {
17 return [
18 [ [
19 'msg' => 'Empty registry',
20 'modules' => [],
21 'out' => '
22mw.loader.addSource( {
23 "local": "/w/load.php"
24} );
25mw.loader.register( [] );'
26 ] ],
27 [ [
28 'msg' => 'Basic registry',
29 'modules' => [
30 'test.blank' => new ResourceLoaderTestModule(),
31 ],
32 'out' => '
33mw.loader.addSource( {
34 "local": "/w/load.php"
35} );
36mw.loader.register( [
37 [
38 "test.blank",
39 "{blankVer}"
40 ]
41] );',
42 ] ],
43 [ [
44 'msg' => 'Group signature',
45 'modules' => [
46 'test.blank' => new ResourceLoaderTestModule(),
47 'test.group.foo' => new ResourceLoaderTestModule( [ 'group' => 'x-foo' ] ),
48 'test.group.bar' => new ResourceLoaderTestModule( [ 'group' => 'x-bar' ] ),
49 ],
50 'out' => '
51mw.loader.addSource( {
52 "local": "/w/load.php"
53} );
54mw.loader.register( [
55 [
56 "test.blank",
57 "{blankVer}"
58 ],
59 [
60 "test.group.foo",
61 "{blankVer}",
62 [],
63 "x-foo"
64 ],
65 [
66 "test.group.bar",
67 "{blankVer}",
68 [],
69 "x-bar"
70 ]
71] );'
72 ] ],
73 [ [
74 'msg' => 'Different target (non-test should not be registered)',
75 'modules' => [
76 'test.blank' => new ResourceLoaderTestModule(),
77 'test.target.foo' => new ResourceLoaderTestModule( [ 'targets' => [ 'x-foo' ] ] ),
78 ],
79 'out' => '
80mw.loader.addSource( {
81 "local": "/w/load.php"
82} );
83mw.loader.register( [
84 [
85 "test.blank",
86 "{blankVer}"
87 ]
88] );'
89 ] ],
90 [ [
91 'msg' => 'Foreign source',
92 'sources' => [
93 'example' => [
94 'loadScript' => 'http://example.org/w/load.php',
95 'apiScript' => 'http://example.org/w/api.php',
96 ],
97 ],
98 'modules' => [
99 'test.blank' => new ResourceLoaderTestModule( [ 'source' => 'example' ] ),
100 ],
101 'out' => '
102mw.loader.addSource( {
103 "local": "/w/load.php",
104 "example": "http://example.org/w/load.php"
105} );
106mw.loader.register( [
107 [
108 "test.blank",
109 "{blankVer}",
110 [],
111 null,
112 "example"
113 ]
114] );'
115 ] ],
116 [ [
117 'msg' => 'Conditional dependency function',
118 'modules' => [
119 'test.x.core' => new ResourceLoaderTestModule(),
120 'test.x.polyfill' => new ResourceLoaderTestModule( [
121 'skipFunction' => 'return true;'
122 ] ),
123 'test.y.polyfill' => new ResourceLoaderTestModule( [
124 'skipFunction' =>
125 'return !!(' .
126 ' window.JSON &&' .
127 ' JSON.parse &&' .
128 ' JSON.stringify' .
129 ');'
130 ] ),
131 'test.z.foo' => new ResourceLoaderTestModule( [
132 'dependencies' => [
133 'test.x.core',
134 'test.x.polyfill',
135 'test.y.polyfill',
136 ],
137 ] ),
138 ],
139 'out' => '
140mw.loader.addSource( {
141 "local": "/w/load.php"
142} );
143mw.loader.register( [
144 [
145 "test.x.core",
146 "{blankVer}"
147 ],
148 [
149 "test.x.polyfill",
150 "{blankVer}",
151 [],
152 null,
153 null,
154 "return true;"
155 ],
156 [
157 "test.y.polyfill",
158 "{blankVer}",
159 [],
160 null,
161 null,
162 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
163 ],
164 [
165 "test.z.foo",
166 "{blankVer}",
167 [
168 0,
169 1,
170 2
171 ]
172 ]
173] );',
174 ] ],
175 [ [
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' => [
182 'example' => [
183 'loadScript' => 'http://example.org/w/load.php',
184 'apiScript' => 'http://example.org/w/api.php',
185 ],
186 ],
187 'modules' => [
188 'test.blank' => new ResourceLoaderTestModule(),
189 'test.x.core' => new ResourceLoaderTestModule(),
190 'test.x.util' => new ResourceLoaderTestModule( [
191 'dependencies' => [
192 'test.x.core',
193 ],
194 ] ),
195 'test.x.foo' => new ResourceLoaderTestModule( [
196 'dependencies' => [
197 'test.x.core',
198 ],
199 ] ),
200 'test.x.bar' => new ResourceLoaderTestModule( [
201 'dependencies' => [
202 'test.x.core',
203 'test.x.util',
204 ],
205 ] ),
206 'test.x.quux' => new ResourceLoaderTestModule( [
207 'dependencies' => [
208 'test.x.foo',
209 'test.x.bar',
210 'test.x.util',
211 'test.x.unknown',
212 ],
213 ] ),
214 'test.group.foo.1' => new ResourceLoaderTestModule( [
215 'group' => 'x-foo',
216 ] ),
217 'test.group.foo.2' => new ResourceLoaderTestModule( [
218 'group' => 'x-foo',
219 ] ),
220 'test.group.bar.1' => new ResourceLoaderTestModule( [
221 'group' => 'x-bar',
222 ] ),
223 'test.group.bar.2' => new ResourceLoaderTestModule( [
224 'group' => 'x-bar',
225 'source' => 'example',
226 ] ),
227 'test.target.foo' => new ResourceLoaderTestModule( [
228 'targets' => [ 'x-foo' ],
229 ] ),
230 'test.target.bar' => new ResourceLoaderTestModule( [
231 'source' => 'example',
232 'targets' => [ 'x-foo' ],
233 ] ),
234 ],
235 'out' => '
236mw.loader.addSource( {
237 "local": "/w/load.php",
238 "example": "http://example.org/w/load.php"
239} );
240mw.loader.register( [
241 [
242 "test.blank",
243 "{blankVer}"
244 ],
245 [
246 "test.x.core",
247 "{blankVer}"
248 ],
249 [
250 "test.x.util",
251 "{blankVer}",
252 [
253 1
254 ]
255 ],
256 [
257 "test.x.foo",
258 "{blankVer}",
259 [
260 1
261 ]
262 ],
263 [
264 "test.x.bar",
265 "{blankVer}",
266 [
267 2
268 ]
269 ],
270 [
271 "test.x.quux",
272 "{blankVer}",
273 [
274 3,
275 4,
276 "test.x.unknown"
277 ]
278 ],
279 [
280 "test.group.foo.1",
281 "{blankVer}",
282 [],
283 "x-foo"
284 ],
285 [
286 "test.group.foo.2",
287 "{blankVer}",
288 [],
289 "x-foo"
290 ],
291 [
292 "test.group.bar.1",
293 "{blankVer}",
294 [],
295 "x-bar"
296 ],
297 [
298 "test.group.bar.2",
299 "{blankVer}",
300 [],
301 "x-bar",
302 "example"
303 ]
304] );'
305 ] ],
306 ];
307 }
308
316 public function testGetModuleRegistrations( $case ) {
317 if ( isset( $case['sources'] ) ) {
318 $this->setMwGlobals( 'wgResourceLoaderSources', $case['sources'] );
319 }
320
322 $rl = $context->getResourceLoader();
323 $rl->register( $case['modules'] );
324 $module = new ResourceLoaderStartUpModule();
325 $out = ltrim( $case['out'], "\n" );
326
327 $this->assertEquals(
328 self::expandPlaceholders( $out ),
329 $module->getModuleRegistrations( $context ),
330 $case['msg']
331 );
332 }
333
334 public static function provideRegistrations() {
335 return [
336 [ [
337 'test.blank' => new ResourceLoaderTestModule(),
338 'test.min' => new ResourceLoaderTestModule( [
339 'skipFunction' =>
340 'return !!(' .
341 ' window.JSON &&' .
342 ' JSON.parse &&' .
343 ' JSON.stringify' .
344 ');',
345 'dependencies' => [
346 'test.blank',
347 ],
348 ] ),
349 ] ]
350 ];
351 }
355 public function testRegistrationsMinified( $modules ) {
356 $this->setMwGlobals( 'wgResourceLoaderDebug', false );
357
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 . ']]);';
368
369 $this->assertEquals(
370 self::expandPlaceholders( $out ),
371 $module->getModuleRegistrations( $context ),
372 'Minified output'
373 );
374 }
375
379 public function testRegistrationsUnminified( $modules ) {
381 $rl = $context->getResourceLoader();
382 $rl->register( $modules );
383 $module = new ResourceLoaderStartUpModule();
384 $out =
385'mw.loader.addSource( {
386 "local": "/w/load.php"
387} );
388mw.loader.register( [
389 [
390 "test.blank",
391 "{blankVer}"
392 ],
393 [
394 "test.min",
395 "{blankVer}",
396 [
397 0
398 ],
399 null,
400 null,
401 "return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);"
402 ]
403] );';
404
405 $this->assertEquals(
406 self::expandPlaceholders( $out ),
407 $module->getModuleRegistrations( $context ),
408 'Unminified output'
409 );
410 }
411
412}
setMwGlobals( $pairs, $value=null)
testRegistrationsUnminified( $modules)
provideRegistrations
testGetModuleRegistrations( $case)
provideGetModuleRegistrations ResourceLoaderStartUpModule::compileUnresolvedDependencies ResourceLoad...
testRegistrationsMinified( $modules)
provideRegistrations
getResourceLoaderContext( $lang='en', $dir='ltr')
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:846
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$context
Definition load.php:44