3 class ExtensionProcessorTest
extends MediaWikiTestCase
{
7 public function setUp() {
9 $this->dir
= __DIR__
. '/FooBar/extension.json';
13 * 'name' is absolutely required
17 static $default = array(
22 * @covers ExtensionProcessor::extractInfo
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' ),
33 $extracted = $processor->getExtractedInfo();
34 $attributes = $extracted['attributes'];
35 $this->assertArrayHasKey( 'AnAttribute', $attributes );
36 $this->assertArrayNotHasKey( '@metadata', $attributes );
37 $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
40 public static function provideRegisterHooks() {
43 // Content in extension.json
44 // Expected value of $wgHooks
52 // No current hooks, adding one for "FooBaz"
55 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
56 array( 'FooBaz' => array( 'FooBazCallback' ) ),
58 // Hook for "FooBaz", adding another one
60 array( 'FooBaz' => array( 'PriorCallback' ) ),
61 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
62 array( 'FooBaz' => array( 'PriorCallback', 'FooBazCallback' ) ),
64 // Hook for "BarBaz", adding one for "FooBaz"
66 array( 'BarBaz' => array( 'BarBazCallback' ) ),
67 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
69 'BarBaz' => array( 'BarBazCallback' ),
70 'FooBaz' => array( 'FooBazCallback' ),
73 // Callbacks for FooBaz wrapped in an array
76 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1' ) ) ) + self
::$default,
78 'FooBaz' => array( 'Callback1' ),
81 // Multiple callbacks for FooBaz hook
84 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1', 'Callback2' ) ) ) + self
::$default,
86 'FooBaz' => array( 'Callback1', 'Callback2' ),
93 * @covers ExtensionProcessor::extractHooks
94 * @dataProvider provideRegisterHooks
96 public function testRegisterHooks( $pre, $info, $expected ) {
97 $processor = new MockExtensionProcessor( array( 'wgHooks' => $pre ) );
98 $processor->extractInfo( $this->dir
, $info );
99 $extracted = $processor->getExtractedInfo();
100 $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
104 * @covers ExtensionProcessor::extractConfig
106 public function testExtractConfig() {
107 $processor = new ExtensionProcessor
;
110 'Bar' => 'somevalue',
115 $processor->extractInfo( $this->dir
, $info );
116 $extracted = $processor->getExtractedInfo();
117 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
118 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
119 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
122 public static function provideExtracttExtensionMessagesFiles() {
123 $dir = __DIR__
. '/FooBar/';
126 array( 'ExtensionMessagesFiles' => array( 'FooBarAlias' => 'FooBar.alias.php' ) ),
127 array( 'wgExtensionMessagesFiles' => array( 'FooBarAlias' => $dir . 'FooBar.alias.php' ) )
131 'ExtensionMessagesFiles' => array(
132 'FooBarAlias' => 'FooBar.alias.php',
133 'FooBarMagic' => 'FooBar.magic.i18n.php',
137 'wgExtensionMessagesFiles' => array(
138 'FooBarAlias' => $dir . 'FooBar.alias.php',
139 'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
147 * @covers ExtensionProcessor::extracttExtensionMessagesFiles
148 * @dataProvider provideExtracttExtensionMessagesFiles
150 public function testExtracttExtensionMessagesFiles( $input, $expected ) {
151 $processor = new ExtensionProcessor();
152 $processor->extractInfo( $this->dir
, $input + self
::$default );
153 $out = $processor->getExtractedInfo();
154 foreach ( $expected as $key => $value ) {
155 $this->assertEquals( $value, $out['globals'][$key] );
160 public static function provideExtractMessagesDirs() {
161 $dir = __DIR__
. '/FooBar/';
164 array( 'MessagesDirs' => array( 'VisualEditor' => 'i18n' ) ),
165 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n' ) ) )
168 array( 'MessagesDirs' => array( 'VisualEditor' => array( 'i18n', 'foobar' ) ) ),
169 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n', $dir . 'foobar' ) ) )
175 * @covers ExtensionProcessor::extractMessagesDirs
176 * @dataProvider provideExtractMessagesDirs
178 public function testExtractMessagesDirs( $input, $expected ) {
179 $processor = new ExtensionProcessor();
180 $processor->extractInfo( $this->dir
, $input + self
::$default );
181 $out = $processor->getExtractedInfo();
182 foreach ( $expected as $key => $value ) {
183 $this->assertEquals( $value, $out['globals'][$key] );
188 * @covers ExtensionProcessor::extractResourceLoaderModules
189 * @dataProvider provideExtractResourceLoaderModules
191 public function testExtractResourceLoaderModules( $input, $expected ) {
192 $processor = new ExtensionProcessor();
193 $processor->extractInfo( $this->dir
, $input + self
::$default );
194 $out = $processor->getExtractedInfo();
195 foreach ( $expected as $key => $value ) {
196 $this->assertEquals( $value, $out['globals'][$key] );
200 public static function provideExtractResourceLoaderModules() {
201 $dir = __DIR__
. '/FooBar/';
203 // Generic module with localBasePath/remoteExtPath specified
207 'ResourceModules' => array(
209 'styles' => 'foobar.js',
210 'localBasePath' => '',
211 'remoteExtPath' => 'FooBar',
217 'wgResourceModules' => array(
219 'styles' => 'foobar.js',
220 'localBasePath' => $dir,
221 'remoteExtPath' => 'FooBar',
226 // ResourceFileModulePaths specified:
230 'ResourceFileModulePaths' => array(
231 'localBasePath' => '',
232 'remoteExtPath' => 'FooBar',
234 'ResourceModules' => array(
237 'styles' => 'foo.js',
239 // Different paths set
241 'styles' => 'bar.js',
242 'localBasePath' => 'subdir',
243 'remoteExtPath' => 'FooBar/subdir',
245 // Custom class with no paths set
246 'test.class' => array(
247 'class' => 'FooBarModule',
248 'extra' => 'argument',
250 // Custom class with a localBasePath
251 'test.class.with.path' => array(
252 'class' => 'FooBarPathModule',
253 'extra' => 'argument',
254 'localBasePath' => '',
260 'wgResourceModules' => array(
262 'styles' => 'foo.js',
263 'localBasePath' => $dir,
264 'remoteExtPath' => 'FooBar',
267 'styles' => 'bar.js',
268 'localBasePath' => $dir . 'subdir',
269 'remoteExtPath' => 'FooBar/subdir',
271 'test.class' => array(
272 'class' => 'FooBarModule',
273 'extra' => 'argument',
274 'localBasePath' => $dir,
275 'remoteExtPath' => 'FooBar',
277 'test.class.with.path' => array(
278 'class' => 'FooBarPathModule',
279 'extra' => 'argument',
280 'localBasePath' => $dir,
281 'remoteExtPath' => 'FooBar',
286 // ResourceModuleSkinStyles with file module paths
290 'ResourceFileModulePaths' => array(
291 'localBasePath' => '',
292 'remoteSkinPath' => 'FooBar',
294 'ResourceModuleSkinStyles' => array(
296 'test.foo' => 'foo.css',
302 'wgResourceModuleSkinStyles' => array(
304 'test.foo' => 'foo.css',
305 'localBasePath' => $dir,
306 'remoteSkinPath' => 'FooBar',
311 // ResourceModuleSkinStyles with file module paths and an override
315 'ResourceFileModulePaths' => array(
316 'localBasePath' => '',
317 'remoteSkinPath' => 'FooBar',
319 'ResourceModuleSkinStyles' => array(
321 'test.foo' => 'foo.css',
322 'remoteSkinPath' => 'BarFoo'
328 'wgResourceModuleSkinStyles' => array(
330 'test.foo' => 'foo.css',
331 'localBasePath' => $dir,
332 'remoteSkinPath' => 'BarFoo',
340 public static function provideSetToGlobal() {
343 array( 'wgAPIModules', 'wgAvailableRights' ),
346 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
347 'AvailableRights' => array( 'foobar', 'unfoobar' ),
350 'wgAPIModules' => array( 'foobar' => 'ApiFooBar' ),
351 'wgAvailableRights' => array( 'foobar', 'unfoobar' ),
355 array( 'wgAPIModules', 'wgAvailableRights' ),
357 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz' ),
358 'wgAvailableRights' => array( 'barbaz' )
361 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
362 'AvailableRights' => array( 'foobar', 'unfoobar' ),
365 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ),
366 'wgAvailableRights' => array( 'barbaz', 'foobar', 'unfoobar' ),
370 array( 'wgGroupPermissions' ),
372 'wgGroupPermissions' => array( 'sysop' => array( 'delete' ) ),
375 'GroupPermissions' => array( 'sysop' => array( 'undelete' ), 'user' => array( 'edit' ) ),
378 'wgGroupPermissions' => array( 'sysop' => array( 'delete', 'undelete' ), 'user' => array( 'edit' ) ),
387 * Allow overriding the default value of $this->globals
388 * so we can test merging
390 class MockExtensionProcessor
extends ExtensionProcessor
{
391 public function __construct( $globals = array() ) {
392 $this->globals
= $globals +
$this->globals
;