Improve ExtensionRegistry test coverage
[lhc/web/wiklou.git] / tests / phpunit / includes / registration / ExtensionRegistryTest.php
1 <?php
2
3 /**
4 * @covers ExtensionRegistry
5 */
6 class ExtensionRegistryTest extends MediaWikiTestCase {
7
8 private $dataDir;
9
10 public function setUp() {
11 parent::setUp();
12 $this->dataDir = __DIR__ . '/../../data/registration';
13 }
14
15 public function testQueue_invalid() {
16 $registry = new ExtensionRegistry();
17 $path = __DIR__ . '/doesnotexist.json';
18 $this->setExpectedException(
19 Exception::class,
20 "$path does not exist!"
21 );
22 $registry->queue( $path );
23 }
24
25 public function testQueue() {
26 $registry = new ExtensionRegistry();
27 $path = "{$this->dataDir}/good.json";
28 $registry->queue( $path );
29 $this->assertArrayHasKey(
30 $path,
31 $registry->getQueue()
32 );
33 $registry->clearQueue();
34 $this->assertEmpty( $registry->getQueue() );
35 }
36
37 public function testLoadFromQueue_empty() {
38 $registry = new ExtensionRegistry();
39 $registry->loadFromQueue();
40 $this->assertEmpty( $registry->getAllThings() );
41 }
42
43 public function testLoadFromQueue_late() {
44 $registry = new ExtensionRegistry();
45 $registry->finish();
46 $registry->queue( "{$this->dataDir}/good.json" );
47 $this->setExpectedException(
48 MWException::class,
49 "The following paths tried to load late: {$this->dataDir}/good.json"
50 );
51 $registry->loadFromQueue();
52 }
53
54 public function testLoadFromQueue() {
55 $registry = new ExtensionRegistry();
56 $registry->queue( "{$this->dataDir}/good.json" );
57 $registry->loadFromQueue();
58 $this->assertArrayHasKey( 'FooBar', $registry->getAllThings() );
59 $this->assertTrue( $registry->isLoaded( 'FooBar' ) );
60 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
61 $this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) );
62 }
63
64 /**
65 * @expectedException PHPUnit_Framework_Error
66 */
67 public function testReadFromQueue_nonexistent() {
68 $registry = new ExtensionRegistry();
69 $registry->readFromQueue( [
70 __DIR__ . '/doesnotexist.json' => 1
71 ] );
72 }
73
74 /**
75 * @dataProvider provideExportExtractedDataGlobals
76 */
77 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
78 // Set globals for test
79 if ( $before ) {
80 foreach ( $before as $key => $value ) {
81 // mw prefixed globals does not exist normally
82 if ( substr( $key, 0, 2 ) == 'mw' ) {
83 $GLOBALS[$key] = $value;
84 } else {
85 $this->setMwGlobals( $key, $value );
86 }
87 }
88 }
89
90 $info = [
91 'globals' => $globals,
92 'callbacks' => [],
93 'defines' => [],
94 'credits' => [],
95 'attributes' => [],
96 'autoloaderPaths' => []
97 ];
98 $registry = new ExtensionRegistry();
99 $class = new ReflectionClass( ExtensionRegistry::class );
100 $method = $class->getMethod( 'exportExtractedData' );
101 $method->setAccessible( true );
102 $method->invokeArgs( $registry, [ $info ] );
103 foreach ( $expected as $name => $value ) {
104 $this->assertArrayHasKey( $name, $GLOBALS, $desc );
105 $this->assertEquals( $value, $GLOBALS[$name], $desc );
106 }
107
108 // Remove mw prefixed globals
109 if ( $before ) {
110 foreach ( $before as $key => $value ) {
111 if ( substr( $key, 0, 2 ) == 'mw' ) {
112 unset( $GLOBALS[$key] );
113 }
114 }
115 }
116 }
117
118 public static function provideExportExtractedDataGlobals() {
119 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
120 return [
121 [
122 'Simple non-array values',
123 [
124 'mwtestFooBarConfig' => true,
125 'mwtestFooBarConfig2' => 'string',
126 ],
127 [
128 'mwtestFooBarDefault' => 1234,
129 'mwtestFooBarConfig' => false,
130 ],
131 [
132 'mwtestFooBarConfig' => true,
133 'mwtestFooBarConfig2' => 'string',
134 'mwtestFooBarDefault' => 1234,
135 ],
136 ],
137 [
138 'No global already set, simple array',
139 null,
140 [
141 'mwtestDefaultOptions' => [
142 'foobar' => true,
143 ]
144 ],
145 [
146 'mwtestDefaultOptions' => [
147 'foobar' => true,
148 ]
149 ],
150 ],
151 [
152 'Global already set, simple array',
153 [
154 'mwtestDefaultOptions' => [
155 'foobar' => true,
156 'foo' => 'string'
157 ],
158 ],
159 [
160 'mwtestDefaultOptions' => [
161 'barbaz' => 12345,
162 'foobar' => false,
163 ],
164 ],
165 [
166 'mwtestDefaultOptions' => [
167 'barbaz' => 12345,
168 'foo' => 'string',
169 'foobar' => true,
170 ],
171 ]
172 ],
173 [
174 'Global already set, 1d array that appends',
175 [
176 'mwAvailableRights' => [
177 'foobar',
178 'foo'
179 ],
180 ],
181 [
182 'mwAvailableRights' => [
183 'barbaz',
184 ],
185 ],
186 [
187 'mwAvailableRights' => [
188 'barbaz',
189 'foobar',
190 'foo',
191 ],
192 ]
193 ],
194 [
195 'Global already set, array with integer keys',
196 [
197 'mwNamespacesFoo' => [
198 100 => true,
199 102 => false
200 ],
201 ],
202 [
203 'mwNamespacesFoo' => [
204 100 => false,
205 500 => true,
206 ExtensionRegistry::MERGE_STRATEGY => 'array_plus',
207 ],
208 ],
209 [
210 'mwNamespacesFoo' => [
211 100 => true,
212 102 => false,
213 500 => true,
214 ],
215 ]
216 ],
217 [
218 'No global already set, $wgHooks',
219 [
220 'wgHooks' => [],
221 ],
222 [
223 'wgHooks' => [
224 'FooBarEvent' => [
225 'FooBarClass::onFooBarEvent'
226 ],
227 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive'
228 ],
229 ],
230 [
231 'wgHooks' => [
232 'FooBarEvent' => [
233 'FooBarClass::onFooBarEvent'
234 ],
235 ],
236 ],
237 ],
238 [
239 'Global already set, $wgHooks',
240 [
241 'wgHooks' => [
242 'FooBarEvent' => [
243 'FooBarClass::onFooBarEvent'
244 ],
245 'BazBarEvent' => [
246 'FooBarClass::onBazBarEvent',
247 ],
248 ],
249 ],
250 [
251 'wgHooks' => [
252 'FooBarEvent' => [
253 'BazBarClass::onFooBarEvent',
254 ],
255 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive',
256 ],
257 ],
258 [
259 'wgHooks' => [
260 'FooBarEvent' => [
261 'FooBarClass::onFooBarEvent',
262 'BazBarClass::onFooBarEvent',
263 ],
264 'BazBarEvent' => [
265 'FooBarClass::onBazBarEvent',
266 ],
267 ],
268 ],
269 ],
270 [
271 'Global already set, $wgGroupPermissions',
272 [
273 'wgGroupPermissions' => [
274 'sysop' => [
275 'something' => true,
276 ],
277 'user' => [
278 'somethingtwo' => true,
279 ]
280 ],
281 ],
282 [
283 'wgGroupPermissions' => [
284 'customgroup' => [
285 'right' => true,
286 ],
287 'user' => [
288 'right' => true,
289 'somethingtwo' => false,
290 'nonduplicated' => true,
291 ],
292 ExtensionRegistry::MERGE_STRATEGY => 'array_plus_2d',
293 ],
294 ],
295 [
296 'wgGroupPermissions' => [
297 'customgroup' => [
298 'right' => true,
299 ],
300 'sysop' => [
301 'something' => true,
302 ],
303 'user' => [
304 'somethingtwo' => true,
305 'right' => true,
306 'nonduplicated' => true,
307 ]
308 ],
309 ],
310 ],
311 [
312 'False local setting should not be overridden (T100767)',
313 [
314 'mwtestT100767' => false,
315 ],
316 [
317 'mwtestT100767' => true,
318 ],
319 [
320 'mwtestT100767' => false,
321 ],
322 ],
323 [
324 'test array_replace_recursive',
325 [
326 'mwtestJsonConfigs' => [
327 'JsonZeroConfig' => [
328 'namespace' => 480,
329 'nsName' => 'Zero',
330 'isLocal' => true,
331 ],
332 ],
333 ],
334 [
335 'mwtestJsonConfigs' => [
336 'JsonZeroConfig' => [
337 'isLocal' => false,
338 'remote' => [
339 'username' => 'foo',
340 ],
341 ],
342 ExtensionRegistry::MERGE_STRATEGY => 'array_replace_recursive',
343 ],
344 ],
345 [
346 'mwtestJsonConfigs' => [
347 'JsonZeroConfig' => [
348 'namespace' => 480,
349 'nsName' => 'Zero',
350 'isLocal' => false,
351 'remote' => [
352 'username' => 'foo',
353 ],
354 ],
355 ],
356 ],
357 ],
358 [
359 'global is null before',
360 [
361 'NullGlobal' => null,
362 ],
363 [
364 'NullGlobal' => 'not-null'
365 ],
366 [
367 'NullGlobal' => null
368 ],
369 ],
370 ];
371 }
372 }