Merge "Ignore noop DB transactions errors on connection loss"
[lhc/web/wiklou.git] / tests / phpunit / includes / registration / ExtensionProcessorTest.php
1 <?php
2
3 class ExtensionProcessorTest extends MediaWikiTestCase {
4
5 private $dir;
6
7 public function setUp() {
8 parent::setUp();
9 $this->dir = __DIR__ . '/FooBar/extension.json';
10 }
11
12 /**
13 * 'name' is absolutely required
14 *
15 * @var array
16 */
17 public static $default = [
18 'name' => 'FooBar',
19 ];
20
21 /**
22 * @covers ExtensionProcessor::extractInfo
23 */
24 public function testExtractInfo() {
25 // Test that attributes that begin with @ are ignored
26 $processor = new ExtensionProcessor();
27 $processor->extractInfo( $this->dir, self::$default + [
28 '@metadata' => [ 'foobarbaz' ],
29 'AnAttribute' => [ 'omg' ],
30 'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
31 ], 1 );
32
33 $extracted = $processor->getExtractedInfo();
34 $attributes = $extracted['attributes'];
35 $this->assertArrayHasKey( 'AnAttribute', $attributes );
36 $this->assertArrayNotHasKey( '@metadata', $attributes );
37 $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
38 }
39
40 public static function provideRegisterHooks() {
41 $merge = [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ];
42 // Format:
43 // Current $wgHooks
44 // Content in extension.json
45 // Expected value of $wgHooks
46 return [
47 // No hooks
48 [
49 [],
50 self::$default,
51 $merge,
52 ],
53 // No current hooks, adding one for "FooBaz"
54 [
55 [],
56 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
57 [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
58 ],
59 // Hook for "FooBaz", adding another one
60 [
61 [ 'FooBaz' => [ 'PriorCallback' ] ],
62 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
63 [ 'FooBaz' => [ 'PriorCallback', 'FooBazCallback' ] ] + $merge,
64 ],
65 // Hook for "BarBaz", adding one for "FooBaz"
66 [
67 [ 'BarBaz' => [ 'BarBazCallback' ] ],
68 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
69 [
70 'BarBaz' => [ 'BarBazCallback' ],
71 'FooBaz' => [ 'FooBazCallback' ],
72 ] + $merge,
73 ],
74 // Callbacks for FooBaz wrapped in an array
75 [
76 [],
77 [ 'Hooks' => [ 'FooBaz' => [ 'Callback1' ] ] ] + self::$default,
78 [
79 'FooBaz' => [ 'Callback1' ],
80 ] + $merge,
81 ],
82 // Multiple callbacks for FooBaz hook
83 [
84 [],
85 [ 'Hooks' => [ 'FooBaz' => [ 'Callback1', 'Callback2' ] ] ] + self::$default,
86 [
87 'FooBaz' => [ 'Callback1', 'Callback2' ],
88 ] + $merge,
89 ],
90 ];
91 }
92
93 /**
94 * @covers ExtensionProcessor::extractHooks
95 * @dataProvider provideRegisterHooks
96 */
97 public function testRegisterHooks( $pre, $info, $expected ) {
98 $processor = new MockExtensionProcessor( [ 'wgHooks' => $pre ] );
99 $processor->extractInfo( $this->dir, $info, 1 );
100 $extracted = $processor->getExtractedInfo();
101 $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
102 }
103
104 /**
105 * @covers ExtensionProcessor::extractConfig
106 */
107 public function testExtractConfig() {
108 $processor = new ExtensionProcessor;
109 $info = [
110 'config' => [
111 'Bar' => 'somevalue',
112 'Foo' => 10,
113 '@IGNORED' => 'yes',
114 ],
115 ] + self::$default;
116 $info2 = [
117 'config' => [
118 '_prefix' => 'eg',
119 'Bar' => 'somevalue'
120 ],
121 'name' => 'FooBar2',
122 ];
123 $processor->extractInfo( $this->dir, $info, 1 );
124 $processor->extractInfo( $this->dir, $info2, 1 );
125 $extracted = $processor->getExtractedInfo();
126 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
127 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
128 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
129 // Custom prefix:
130 $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
131 }
132
133 public static function provideExtractExtensionMessagesFiles() {
134 $dir = __DIR__ . '/FooBar/';
135 return [
136 [
137 [ 'ExtensionMessagesFiles' => [ 'FooBarAlias' => 'FooBar.alias.php' ] ],
138 [ 'wgExtensionMessagesFiles' => [ 'FooBarAlias' => $dir . 'FooBar.alias.php' ] ]
139 ],
140 [
141 [
142 'ExtensionMessagesFiles' => [
143 'FooBarAlias' => 'FooBar.alias.php',
144 'FooBarMagic' => 'FooBar.magic.i18n.php',
145 ],
146 ],
147 [
148 'wgExtensionMessagesFiles' => [
149 'FooBarAlias' => $dir . 'FooBar.alias.php',
150 'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
151 ],
152 ],
153 ],
154 ];
155 }
156
157 /**
158 * @covers ExtensionProcessor::extractExtensionMessagesFiles
159 * @dataProvider provideExtractExtensionMessagesFiles
160 */
161 public function testExtractExtensionMessagesFiles( $input, $expected ) {
162 $processor = new ExtensionProcessor();
163 $processor->extractInfo( $this->dir, $input + self::$default, 1 );
164 $out = $processor->getExtractedInfo();
165 foreach ( $expected as $key => $value ) {
166 $this->assertEquals( $value, $out['globals'][$key] );
167 }
168 }
169
170 public static function provideExtractMessagesDirs() {
171 $dir = __DIR__ . '/FooBar/';
172 return [
173 [
174 [ 'MessagesDirs' => [ 'VisualEditor' => 'i18n' ] ],
175 [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n' ] ] ]
176 ],
177 [
178 [ 'MessagesDirs' => [ 'VisualEditor' => [ 'i18n', 'foobar' ] ] ],
179 [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n', $dir . 'foobar' ] ] ]
180 ],
181 ];
182 }
183
184 /**
185 * @covers ExtensionProcessor::extractMessagesDirs
186 * @dataProvider provideExtractMessagesDirs
187 */
188 public function testExtractMessagesDirs( $input, $expected ) {
189 $processor = new ExtensionProcessor();
190 $processor->extractInfo( $this->dir, $input + self::$default, 1 );
191 $out = $processor->getExtractedInfo();
192 foreach ( $expected as $key => $value ) {
193 $this->assertEquals( $value, $out['globals'][$key] );
194 }
195 }
196
197 /**
198 * @covers ExtensionProcessor::extractCredits
199 */
200 public function testExtractCredits() {
201 $processor = new ExtensionProcessor();
202 $processor->extractInfo( $this->dir, self::$default, 1 );
203 $this->setExpectedException( 'Exception' );
204 $processor->extractInfo( $this->dir, self::$default, 1 );
205 }
206
207 /**
208 * @covers ExtensionProcessor::extractResourceLoaderModules
209 * @dataProvider provideExtractResourceLoaderModules
210 */
211 public function testExtractResourceLoaderModules( $input, $expected ) {
212 $processor = new ExtensionProcessor();
213 $processor->extractInfo( $this->dir, $input + self::$default, 1 );
214 $out = $processor->getExtractedInfo();
215 foreach ( $expected as $key => $value ) {
216 $this->assertEquals( $value, $out['globals'][$key] );
217 }
218 }
219
220 public static function provideExtractResourceLoaderModules() {
221 $dir = __DIR__ . '/FooBar';
222 return [
223 // Generic module with localBasePath/remoteExtPath specified
224 [
225 // Input
226 [
227 'ResourceModules' => [
228 'test.foo' => [
229 'styles' => 'foobar.js',
230 'localBasePath' => '',
231 'remoteExtPath' => 'FooBar',
232 ],
233 ],
234 ],
235 // Expected
236 [
237 'wgResourceModules' => [
238 'test.foo' => [
239 'styles' => 'foobar.js',
240 'localBasePath' => $dir,
241 'remoteExtPath' => 'FooBar',
242 ],
243 ],
244 ],
245 ],
246 // ResourceFileModulePaths specified:
247 [
248 // Input
249 [
250 'ResourceFileModulePaths' => [
251 'localBasePath' => '',
252 'remoteExtPath' => 'FooBar',
253 ],
254 'ResourceModules' => [
255 // No paths
256 'test.foo' => [
257 'styles' => 'foo.js',
258 ],
259 // Different paths set
260 'test.bar' => [
261 'styles' => 'bar.js',
262 'localBasePath' => 'subdir',
263 'remoteExtPath' => 'FooBar/subdir',
264 ],
265 // Custom class with no paths set
266 'test.class' => [
267 'class' => 'FooBarModule',
268 'extra' => 'argument',
269 ],
270 // Custom class with a localBasePath
271 'test.class.with.path' => [
272 'class' => 'FooBarPathModule',
273 'extra' => 'argument',
274 'localBasePath' => '',
275 ]
276 ],
277 ],
278 // Expected
279 [
280 'wgResourceModules' => [
281 'test.foo' => [
282 'styles' => 'foo.js',
283 'localBasePath' => $dir,
284 'remoteExtPath' => 'FooBar',
285 ],
286 'test.bar' => [
287 'styles' => 'bar.js',
288 'localBasePath' => "$dir/subdir",
289 'remoteExtPath' => 'FooBar/subdir',
290 ],
291 'test.class' => [
292 'class' => 'FooBarModule',
293 'extra' => 'argument',
294 'localBasePath' => $dir,
295 'remoteExtPath' => 'FooBar',
296 ],
297 'test.class.with.path' => [
298 'class' => 'FooBarPathModule',
299 'extra' => 'argument',
300 'localBasePath' => $dir,
301 'remoteExtPath' => 'FooBar',
302 ]
303 ],
304 ],
305 ],
306 // ResourceModuleSkinStyles with file module paths
307 [
308 // Input
309 [
310 'ResourceFileModulePaths' => [
311 'localBasePath' => '',
312 'remoteSkinPath' => 'FooBar',
313 ],
314 'ResourceModuleSkinStyles' => [
315 'foobar' => [
316 'test.foo' => 'foo.css',
317 ]
318 ],
319 ],
320 // Expected
321 [
322 'wgResourceModuleSkinStyles' => [
323 'foobar' => [
324 'test.foo' => 'foo.css',
325 'localBasePath' => $dir,
326 'remoteSkinPath' => 'FooBar',
327 ],
328 ],
329 ],
330 ],
331 // ResourceModuleSkinStyles with file module paths and an override
332 [
333 // Input
334 [
335 'ResourceFileModulePaths' => [
336 'localBasePath' => '',
337 'remoteSkinPath' => 'FooBar',
338 ],
339 'ResourceModuleSkinStyles' => [
340 'foobar' => [
341 'test.foo' => 'foo.css',
342 'remoteSkinPath' => 'BarFoo'
343 ],
344 ],
345 ],
346 // Expected
347 [
348 'wgResourceModuleSkinStyles' => [
349 'foobar' => [
350 'test.foo' => 'foo.css',
351 'localBasePath' => $dir,
352 'remoteSkinPath' => 'BarFoo',
353 ],
354 ],
355 ],
356 ],
357 ];
358 }
359
360 public static function provideSetToGlobal() {
361 return [
362 [
363 [ 'wgAPIModules', 'wgAvailableRights' ],
364 [],
365 [
366 'APIModules' => [ 'foobar' => 'ApiFooBar' ],
367 'AvailableRights' => [ 'foobar', 'unfoobar' ],
368 ],
369 [
370 'wgAPIModules' => [ 'foobar' => 'ApiFooBar' ],
371 'wgAvailableRights' => [ 'foobar', 'unfoobar' ],
372 ],
373 ],
374 [
375 [ 'wgAPIModules', 'wgAvailableRights' ],
376 [
377 'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz' ],
378 'wgAvailableRights' => [ 'barbaz' ]
379 ],
380 [
381 'APIModules' => [ 'foobar' => 'ApiFooBar' ],
382 'AvailableRights' => [ 'foobar', 'unfoobar' ],
383 ],
384 [
385 'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ],
386 'wgAvailableRights' => [ 'barbaz', 'foobar', 'unfoobar' ],
387 ],
388 ],
389 [
390 [ 'wgGroupPermissions' ],
391 [
392 'wgGroupPermissions' => [
393 'sysop' => [ 'delete' ]
394 ],
395 ],
396 [
397 'GroupPermissions' => [
398 'sysop' => [ 'undelete' ],
399 'user' => [ 'edit' ]
400 ],
401 ],
402 [
403 'wgGroupPermissions' => [
404 'sysop' => [ 'delete', 'undelete' ],
405 'user' => [ 'edit' ]
406 ],
407 ]
408 ]
409 ];
410 }
411 }
412
413 /**
414 * Allow overriding the default value of $this->globals
415 * so we can test merging
416 */
417 class MockExtensionProcessor extends ExtensionProcessor {
418 public function __construct( $globals = [] ) {
419 $this->globals = $globals + $this->globals;
420 }
421 }