0d3187822762a89b31e87866a09b8e4cbd6b5ae2
[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 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 ) );
31
32 $extracted = $processor->getExtractedInfo();
33 $attributes = $extracted['attributes'];
34 $this->assertArrayHasKey( 'AnAttribute', $attributes );
35 $this->assertArrayNotHasKey( '@metadata', $attributes );
36 }
37
38 public static function provideRegisterHooks() {
39 return array(
40 // No hooks
41 array(
42 array(),
43 self::$default,
44 array(),
45 ),
46 // No current hooks, adding one for "FooBaz"
47 array(
48 array(),
49 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
50 array( 'FooBaz' => array( 'FooBazCallback' ) ),
51 ),
52 // Hook for "FooBaz", adding another one
53 array(
54 array( 'FooBaz' => array( 'PriorCallback' ) ),
55 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
56 array( 'FooBaz' => array( 'PriorCallback', 'FooBazCallback' ) ),
57 ),
58 // Hook for "BarBaz", adding one for "FooBaz"
59 array(
60 array( 'BarBaz' => array( 'BarBazCallback' ) ),
61 array( 'Hooks' => array( 'FooBaz' => 'FooBazCallback' ) ) + self::$default,
62 array(
63 'BarBaz' => array( 'BarBazCallback' ),
64 'FooBaz' => array( 'FooBazCallback' ),
65 ),
66 ),
67 );
68 }
69
70 /**
71 * @covers ExtensionProcessor::extractHooks
72 * @dataProvider provideRegisterHooks
73 */
74 public function testRegisterHooks( $pre, $info, $expected ) {
75 $processor = new MockExtensionProcessor( array( 'wgHooks' => $pre ) );
76 $processor->extractInfo( $this->dir, $info );
77 $extracted = $processor->getExtractedInfo();
78 $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
79 }
80
81 /**
82 * @covers ExtensionProcessor::extractConfig
83 */
84 public function testExtractConfig() {
85 $processor = new ExtensionProcessor;
86 $info = array(
87 'config' => array(
88 'Bar' => 'somevalue',
89 'Foo' => 10,
90 '@IGNORED' => 'yes',
91 ),
92 ) + self::$default;
93 $processor->extractInfo( $this->dir, $info );
94 $extracted = $processor->getExtractedInfo();
95 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
96 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
97 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
98 }
99
100 public static function provideExtractMessageSettings() {
101 $dir = __DIR__ . '/FooBar/';
102 return array(
103 array(
104 array( 'MessagesDirs' => array( 'VisualEditor' => 'i18n' ) ),
105 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n' ) ) )
106 ),
107 array(
108 array( 'MessagesDirs' => array( 'VisualEditor' => array( 'i18n', 'foobar' ) ) ),
109 array( 'wgMessagesDirs' => array( 'VisualEditor' => array( $dir . 'i18n', $dir . 'foobar' ) ) )
110 ),
111 );
112 }
113
114 /**
115 * @covers ExtensionProcessor::extractMessageSettings
116 * @dataProvider provideExtractMessageSettings
117 */
118 public function testExtractMessageSettings( $input, $expected ) {
119 $processor = new ExtensionProcessor();
120 $processor->extractInfo( $this->dir, $input + self::$default );
121 $out = $processor->getExtractedInfo();
122 foreach ( $expected as $key => $value ) {
123 $this->assertEquals( $value, $out['globals'][$key] );
124 }
125 }
126
127 public static function provideSetToGlobal() {
128 return array(
129 array(
130 array( 'wgAPIModules', 'wgAvailableRights' ),
131 array(),
132 array(
133 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
134 'AvailableRights' => array( 'foobar', 'unfoobar' ),
135 ),
136 array(
137 'wgAPIModules' => array( 'foobar' => 'ApiFooBar' ),
138 'wgAvailableRights' => array( 'foobar', 'unfoobar' ),
139 ),
140 ),
141 array(
142 array( 'wgAPIModules', 'wgAvailableRights' ),
143 array(
144 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz' ),
145 'wgAvailableRights' => array( 'barbaz' )
146 ),
147 array(
148 'APIModules' => array( 'foobar' => 'ApiFooBar' ),
149 'AvailableRights' => array( 'foobar', 'unfoobar' ),
150 ),
151 array(
152 'wgAPIModules' => array( 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ),
153 'wgAvailableRights' => array( 'barbaz', 'foobar', 'unfoobar' ),
154 ),
155 ),
156 array(
157 array( 'wgGroupPermissions' ),
158 array(
159 'wgGroupPermissions' => array( 'sysop' => array( 'delete' ) ),
160 ),
161 array(
162 'GroupPermissions' => array( 'sysop' => array( 'undelete' ), 'user' => array( 'edit' ) ),
163 ),
164 array(
165 'wgGroupPermissions' => array( 'sysop' => array( 'delete', 'undelete' ), 'user' => array( 'edit' ) ),
166 )
167 )
168 );
169 }
170 }
171
172
173 /**
174 * Allow overriding the default value of $this->globals
175 * so we can test merging
176 */
177 class MockExtensionProcessor extends ExtensionProcessor {
178 public function __construct( $globals = array() ) {
179 $this->globals = $globals + $this->globals;
180 }
181 }