3 class ExtensionProcessorTest
extends MediaWikiTestCase
{
7 public function setUp() {
9 $this->dir
= __DIR__
. '/FooBar/extension.json';
13 * 'name' is absolutely required
17 public 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() {
41 $merge = array( ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive' );
44 // Content in extension.json
45 // Expected value of $wgHooks
53 // No current hooks, adding one for "FooBaz"
56 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
57 array( 'FooBaz' => array( 'FooBazCallback' ) ) +
$merge,
59 // Hook for "FooBaz", adding another one
61 array( 'FooBaz' => array( 'PriorCallback' ) ),
62 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
63 array( 'FooBaz' => array( 'PriorCallback', 'FooBazCallback' ) ) +
$merge,
65 // Hook for "BarBaz", adding one for "FooBaz"
67 array( 'BarBaz' => array( 'BarBazCallback' ) ),
68 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self
::$default,
70 'BarBaz' => array( 'BarBazCallback' ),
71 'FooBaz' => array( 'FooBazCallback' ),
74 // Callbacks for FooBaz wrapped in an array
77 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1' ) ) ) + self
::$default,
79 'FooBaz' => array( 'Callback1' ),
82 // Multiple callbacks for FooBaz hook
85 array( 'Hooks' => array( 'FooBaz' => array( 'Callback1', 'Callback2' ) ) ) + self
::$default,
87 'FooBaz' => array( 'Callback1', 'Callback2' ),
94 * @covers ExtensionProcessor::extractHooks
95 * @dataProvider provideRegisterHooks
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'] );
105 * @covers ExtensionProcessor::extractConfig
107 public function testExtractConfig() {
108 $processor = new ExtensionProcessor
;
111 'Bar' => 'somevalue',
116 $processor->extractInfo( $this->dir
, $info, 1 );
117 $extracted = $processor->getExtractedInfo();
118 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
119 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
120 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
123 public static function provideExtracttExtensionMessagesFiles() {
124 $dir = __DIR__
. '/FooBar/';
127 array( 'ExtensionMessagesFiles' => array( 'FooBarAlias' => 'FooBar.alias.php' ) ),
128 array( 'wgExtensionMessagesFiles' => array( 'FooBarAlias' => $dir . 'FooBar.alias.php' ) )
132 'ExtensionMessagesFiles' => array(
133 'FooBarAlias' => 'FooBar.alias.php',
134 'FooBarMagic' => 'FooBar.magic.i18n.php',
138 'wgExtensionMessagesFiles' => array(
139 'FooBarAlias' => $dir . 'FooBar.alias.php',
140 'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
148 * @covers ExtensionProcessor::extracttExtensionMessagesFiles
149 * @dataProvider provideExtracttExtensionMessagesFiles
151 public function testExtracttExtensionMessagesFiles( $input, $expected ) {
152 $processor = new ExtensionProcessor();
153 $processor->extractInfo( $this->dir
, $input + self
::$default, 1 );
154 $out = $processor->getExtractedInfo();
155 foreach ( $expected as $key => $value ) {
156 $this->assertEquals( $value, $out['globals'][$key] );
161 public static function provideExtractMessagesDirs() {
162 $dir = __DIR__
. '/FooBar/';
165 array( 'MessagesDirs' => array( 'VisualEditor' => 'i18n' ) ),
166 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n' ) ) )
169 array( 'MessagesDirs' => array( 'VisualEditor' => array( 'i18n', 'foobar' ) ) ),
170 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n', $dir . 'foobar' ) ) )
176 * @covers ExtensionProcessor::extractMessagesDirs
177 * @dataProvider provideExtractMessagesDirs
179 public function testExtractMessagesDirs( $input, $expected ) {
180 $processor = new ExtensionProcessor();
181 $processor->extractInfo( $this->dir
, $input + self
::$default, 1 );
182 $out = $processor->getExtractedInfo();
183 foreach ( $expected as $key => $value ) {
184 $this->assertEquals( $value, $out['globals'][$key] );
189 * @covers ExtensionProcessor::extractResourceLoaderModules
190 * @dataProvider provideExtractResourceLoaderModules
192 public function testExtractResourceLoaderModules( $input, $expected ) {
193 $processor = new ExtensionProcessor();
194 $processor->extractInfo( $this->dir
, $input + self
::$default, 1 );
195 $out = $processor->getExtractedInfo();
196 foreach ( $expected as $key => $value ) {
197 $this->assertEquals( $value, $out['globals'][$key] );
201 public static function provideExtractResourceLoaderModules() {
202 $dir = __DIR__
. '/FooBar/';
204 // Generic module with localBasePath/remoteExtPath specified
208 'ResourceModules' => array(
210 'styles' => 'foobar.js',
211 'localBasePath' => '',
212 'remoteExtPath' => 'FooBar',
218 'wgResourceModules' => array(
220 'styles' => 'foobar.js',
221 'localBasePath' => $dir,
222 'remoteExtPath' => 'FooBar',
227 // ResourceFileModulePaths specified:
231 'ResourceFileModulePaths' => array(
232 'localBasePath' => '',
233 'remoteExtPath' => 'FooBar',
235 'ResourceModules' => array(
238 'styles' => 'foo.js',
240 // Different paths set
242 'styles' => 'bar.js',
243 'localBasePath' => 'subdir',
244 'remoteExtPath' => 'FooBar/subdir',
246 // Custom class with no paths set
247 'test.class' => array(
248 'class' => 'FooBarModule',
249 'extra' => 'argument',
251 // Custom class with a localBasePath
252 'test.class.with.path' => array(
253 'class' => 'FooBarPathModule',
254 'extra' => 'argument',
255 'localBasePath' => '',
261 'wgResourceModules' => array(
263 'styles' => 'foo.js',
264 'localBasePath' => $dir,
265 'remoteExtPath' => 'FooBar',
268 'styles' => 'bar.js',
269 'localBasePath' => $dir . 'subdir',
270 'remoteExtPath' => 'FooBar/subdir',
272 'test.class' => array(
273 'class' => 'FooBarModule',
274 'extra' => 'argument',
275 'localBasePath' => $dir,
276 'remoteExtPath' => 'FooBar',
278 'test.class.with.path' => array(
279 'class' => 'FooBarPathModule',
280 'extra' => 'argument',
281 'localBasePath' => $dir,
282 'remoteExtPath' => 'FooBar',
287 // ResourceModuleSkinStyles with file module paths
291 'ResourceFileModulePaths' => array(
292 'localBasePath' => '',
293 'remoteSkinPath' => 'FooBar',
295 'ResourceModuleSkinStyles' => array(
297 'test.foo' => 'foo.css',
303 'wgResourceModuleSkinStyles' => array(
305 'test.foo' => 'foo.css',
306 'localBasePath' => $dir,
307 'remoteSkinPath' => 'FooBar',
312 // ResourceModuleSkinStyles with file module paths and an override
316 'ResourceFileModulePaths' => array(
317 'localBasePath' => '',
318 'remoteSkinPath' => 'FooBar',
320 'ResourceModuleSkinStyles' => array(
322 'test.foo' => 'foo.css',
323 'remoteSkinPath' => 'BarFoo'
329 'wgResourceModuleSkinStyles' => array(
331 'test.foo' => 'foo.css',
332 'localBasePath' => $dir,
333 'remoteSkinPath' => 'BarFoo',
341 public static function provideSetToGlobal() {
344 array( 'wgAPIModules', 'wgAvailableRights' ),
347 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
348 'AvailableRights' => array( 'foobar', 'unfoobar' ),
351 'wgAPIModules' => array( 'foobar' => 'ApiFooBar' ),
352 'wgAvailableRights' => array( 'foobar', 'unfoobar' ),
356 array( 'wgAPIModules', 'wgAvailableRights' ),
358 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz' ),
359 'wgAvailableRights' => array( 'barbaz' )
362 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
363 'AvailableRights' => array( 'foobar', 'unfoobar' ),
366 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ),
367 'wgAvailableRights' => array( 'barbaz', 'foobar', 'unfoobar' ),
371 array( 'wgGroupPermissions' ),
373 'wgGroupPermissions' => array( 'sysop' => array( 'delete' ) ),
376 'GroupPermissions' => array( 'sysop' => array( 'undelete' ), 'user' => array( 'edit' ) ),
379 'wgGroupPermissions' => array( 'sysop' => array( 'delete', 'undelete' ), 'user' => array( 'edit' ) ),
388 * Allow overriding the default value of $this->globals
389 * so we can test merging
391 class MockExtensionProcessor
extends ExtensionProcessor
{
392 public function __construct( $globals = array() ) {
393 $this->globals
= $globals +
$this->globals
;