Merge "SpecialMovepage: Convert form to use OOUI controls"
[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 = array(
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 + array(
28 '@metadata' => array( 'foobarbaz' ),
29 'AnAttribute' => array( 'omg' ),
30 'AutoloadClasses' => array( '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 = array( ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' );
42 // Format:
43 // Current $wgHooks
44 // Content in extension.json
45 // Expected value of $wgHooks
46 return array(
47 // No hooks
48 array(
49 array(),
50 self::$default,
51 $merge,
52 ),
53 // No current hooks, adding one for "FooBaz"
54 array(
55 array(),
56 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
57 array( 'FooBaz' => array( 'FooBazCallback' ) ) + $merge,
58 ),
59 // Hook for "FooBaz", adding another one
60 array(
61 array( 'FooBaz' => array( 'PriorCallback' ) ),
62 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
63 array( 'FooBaz' => array( 'PriorCallback', 'FooBazCallback' ) ) + $merge,
64 ),
65 // Hook for "BarBaz", adding one for "FooBaz"
66 array(
67 array( 'BarBaz' => array( 'BarBazCallback' ) ),
68 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
69 array(
70 'BarBaz' => array( 'BarBazCallback' ),
71 'FooBaz' => array( 'FooBazCallback' ),
72 ) + $merge,
73 ),
74 // Callbacks for FooBaz wrapped in an array
75 array(
76 array(),
77 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1' ) ) ) + self::$default,
78 array(
79 'FooBaz' => array( 'Callback1' ),
80 ) + $merge,
81 ),
82 // Multiple callbacks for FooBaz hook
83 array(
84 array(),
85 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1', 'Callback2' ) ) ) + self::$default,
86 array(
87 'FooBaz' => array( '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( array( '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 = array(
110 'config' => array(
111 'Bar' => 'somevalue',
112 'Foo' => 10,
113 '@IGNORED' => 'yes',
114 ),
115 ) + self::$default;
116 $info2 = array(
117 'config' => array(
118 '_prefix' => 'eg',
119 'Bar' => 'somevalue'
120 ),
121 ) + self::$default;
122 $processor->extractInfo( $this->dir, $info, 1 );
123 $processor->extractInfo( $this->dir, $info2, 1 );
124 $extracted = $processor->getExtractedInfo();
125 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
126 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
127 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
128 // Custom prefix:
129 $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
130 }
131
132 public static function provideExtracttExtensionMessagesFiles() {
133 $dir = __DIR__ . '/FooBar/';
134 return array(
135 array(
136 array( 'ExtensionMessagesFiles' => array( 'FooBarAlias' => 'FooBar.alias.php' ) ),
137 array( 'wgExtensionMessagesFiles' => array( 'FooBarAlias' => $dir . 'FooBar.alias.php' ) )
138 ),
139 array(
140 array(
141 'ExtensionMessagesFiles' => array(
142 'FooBarAlias' => 'FooBar.alias.php',
143 'FooBarMagic' => 'FooBar.magic.i18n.php',
144 ),
145 ),
146 array(
147 'wgExtensionMessagesFiles' => array(
148 'FooBarAlias' => $dir . 'FooBar.alias.php',
149 'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
150 ),
151 ),
152 ),
153 );
154 }
155
156 /**
157 * @covers ExtensionProcessor::extracttExtensionMessagesFiles
158 * @dataProvider provideExtracttExtensionMessagesFiles
159 */
160 public function testExtracttExtensionMessagesFiles( $input, $expected ) {
161 $processor = new ExtensionProcessor();
162 $processor->extractInfo( $this->dir, $input + self::$default, 1 );
163 $out = $processor->getExtractedInfo();
164 foreach ( $expected as $key => $value ) {
165 $this->assertEquals( $value, $out['globals'][$key] );
166 }
167 }
168
169
170 public static function provideExtractMessagesDirs() {
171 $dir = __DIR__ . '/FooBar/';
172 return array(
173 array(
174 array( 'MessagesDirs' => array( 'VisualEditor' => 'i18n' ) ),
175 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n' ) ) )
176 ),
177 array(
178 array( 'MessagesDirs' => array( 'VisualEditor' => array( 'i18n', 'foobar' ) ) ),
179 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $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::extractResourceLoaderModules
199 * @dataProvider provideExtractResourceLoaderModules
200 */
201 public function testExtractResourceLoaderModules( $input, $expected ) {
202 $processor = new ExtensionProcessor();
203 $processor->extractInfo( $this->dir, $input + self::$default, 1 );
204 $out = $processor->getExtractedInfo();
205 foreach ( $expected as $key => $value ) {
206 $this->assertEquals( $value, $out['globals'][$key] );
207 }
208 }
209
210 public static function provideExtractResourceLoaderModules() {
211 $dir = __DIR__ . '/FooBar/';
212 return array(
213 // Generic module with localBasePath/remoteExtPath specified
214 array(
215 // Input
216 array(
217 'ResourceModules' => array(
218 'test.foo' => array(
219 'styles' => 'foobar.js',
220 'localBasePath' => '',
221 'remoteExtPath' => 'FooBar',
222 ),
223 ),
224 ),
225 // Expected
226 array(
227 'wgResourceModules' => array(
228 'test.foo' => array(
229 'styles' => 'foobar.js',
230 'localBasePath' => $dir,
231 'remoteExtPath' => 'FooBar',
232 ),
233 ),
234 ),
235 ),
236 // ResourceFileModulePaths specified:
237 array(
238 // Input
239 array(
240 'ResourceFileModulePaths' => array(
241 'localBasePath' => '',
242 'remoteExtPath' => 'FooBar',
243 ),
244 'ResourceModules' => array(
245 // No paths
246 'test.foo' => array(
247 'styles' => 'foo.js',
248 ),
249 // Different paths set
250 'test.bar' => array(
251 'styles' => 'bar.js',
252 'localBasePath' => 'subdir',
253 'remoteExtPath' => 'FooBar/subdir',
254 ),
255 // Custom class with no paths set
256 'test.class' => array(
257 'class' => 'FooBarModule',
258 'extra' => 'argument',
259 ),
260 // Custom class with a localBasePath
261 'test.class.with.path' => array(
262 'class' => 'FooBarPathModule',
263 'extra' => 'argument',
264 'localBasePath' => '',
265 )
266 ),
267 ),
268 // Expected
269 array(
270 'wgResourceModules' => array(
271 'test.foo' => array(
272 'styles' => 'foo.js',
273 'localBasePath' => $dir,
274 'remoteExtPath' => 'FooBar',
275 ),
276 'test.bar' => array(
277 'styles' => 'bar.js',
278 'localBasePath' => $dir . 'subdir',
279 'remoteExtPath' => 'FooBar/subdir',
280 ),
281 'test.class' => array(
282 'class' => 'FooBarModule',
283 'extra' => 'argument',
284 'localBasePath' => $dir,
285 'remoteExtPath' => 'FooBar',
286 ),
287 'test.class.with.path' => array(
288 'class' => 'FooBarPathModule',
289 'extra' => 'argument',
290 'localBasePath' => $dir,
291 'remoteExtPath' => 'FooBar',
292 )
293 ),
294 ),
295 ),
296 // ResourceModuleSkinStyles with file module paths
297 array(
298 // Input
299 array(
300 'ResourceFileModulePaths' => array(
301 'localBasePath' => '',
302 'remoteSkinPath' => 'FooBar',
303 ),
304 'ResourceModuleSkinStyles' => array(
305 'foobar' => array(
306 'test.foo' => 'foo.css',
307 )
308 ),
309 ),
310 // Expected
311 array(
312 'wgResourceModuleSkinStyles' => array(
313 'foobar' => array(
314 'test.foo' => 'foo.css',
315 'localBasePath' => $dir,
316 'remoteSkinPath' => 'FooBar',
317 ),
318 ),
319 ),
320 ),
321 // ResourceModuleSkinStyles with file module paths and an override
322 array(
323 // Input
324 array(
325 'ResourceFileModulePaths' => array(
326 'localBasePath' => '',
327 'remoteSkinPath' => 'FooBar',
328 ),
329 'ResourceModuleSkinStyles' => array(
330 'foobar' => array(
331 'test.foo' => 'foo.css',
332 'remoteSkinPath' => 'BarFoo'
333 ),
334 ),
335 ),
336 // Expected
337 array(
338 'wgResourceModuleSkinStyles' => array(
339 'foobar' => array(
340 'test.foo' => 'foo.css',
341 'localBasePath' => $dir,
342 'remoteSkinPath' => 'BarFoo',
343 ),
344 ),
345 ),
346 ),
347 );
348 }
349
350 public static function provideSetToGlobal() {
351 return array(
352 array(
353 array( 'wgAPIModules', 'wgAvailableRights' ),
354 array(),
355 array(
356 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
357 'AvailableRights' => array( 'foobar', 'unfoobar' ),
358 ),
359 array(
360 'wgAPIModules' => array( 'foobar' => 'ApiFooBar' ),
361 'wgAvailableRights' => array( 'foobar', 'unfoobar' ),
362 ),
363 ),
364 array(
365 array( 'wgAPIModules', 'wgAvailableRights' ),
366 array(
367 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz' ),
368 'wgAvailableRights' => array( 'barbaz' )
369 ),
370 array(
371 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
372 'AvailableRights' => array( 'foobar', 'unfoobar' ),
373 ),
374 array(
375 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ),
376 'wgAvailableRights' => array( 'barbaz', 'foobar', 'unfoobar' ),
377 ),
378 ),
379 array(
380 array( 'wgGroupPermissions' ),
381 array(
382 'wgGroupPermissions' => array( 'sysop' => array( 'delete' ) ),
383 ),
384 array(
385 'GroupPermissions' => array( 'sysop' => array( 'undelete' ), 'user' => array( 'edit' ) ),
386 ),
387 array(
388 'wgGroupPermissions' => array( 'sysop' => array( 'delete', 'undelete' ), 'user' => array( 'edit' ) ),
389 )
390 )
391 );
392 }
393 }
394
395
396 /**
397 * Allow overriding the default value of $this->globals
398 * so we can test merging
399 */
400 class MockExtensionProcessor extends ExtensionProcessor {
401 public function __construct( $globals = array() ) {
402 $this->globals = $globals + $this->globals;
403 }
404 }