Merge "MWExceptionRenderer: Fix db error outage page"
[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 public function testReadFromQueueInitializeAutoloaderWithPsr4Namespaces() {
75 $registry = new ExtensionRegistry();
76 $registry->readFromQueue( [
77 "{$this->dataDir}/autoload_namespaces.json" => 1
78 ] );
79 $this->assertTrue(
80 class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ),
81 "Registry initializes Autoloader from AutoloadNamespaces"
82 );
83 }
84
85 /**
86 * @dataProvider provideExportExtractedDataGlobals
87 */
88 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
89 // Set globals for test
90 if ( $before ) {
91 foreach ( $before as $key => $value ) {
92 // mw prefixed globals does not exist normally
93 if ( substr( $key, 0, 2 ) == 'mw' ) {
94 $GLOBALS[$key] = $value;
95 } else {
96 $this->setMwGlobals( $key, $value );
97 }
98 }
99 }
100
101 $info = [
102 'globals' => $globals,
103 'callbacks' => [],
104 'defines' => [],
105 'credits' => [],
106 'attributes' => [],
107 'autoloaderPaths' => []
108 ];
109 $registry = new ExtensionRegistry();
110 $class = new ReflectionClass( ExtensionRegistry::class );
111 $method = $class->getMethod( 'exportExtractedData' );
112 $method->setAccessible( true );
113 $method->invokeArgs( $registry, [ $info ] );
114 foreach ( $expected as $name => $value ) {
115 $this->assertArrayHasKey( $name, $GLOBALS, $desc );
116 $this->assertEquals( $value, $GLOBALS[$name], $desc );
117 }
118
119 // Remove mw prefixed globals
120 if ( $before ) {
121 foreach ( $before as $key => $value ) {
122 if ( substr( $key, 0, 2 ) == 'mw' ) {
123 unset( $GLOBALS[$key] );
124 }
125 }
126 }
127 }
128
129 public static function provideExportExtractedDataGlobals() {
130 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
131 return [
132 [
133 'Simple non-array values',
134 [
135 'mwtestFooBarConfig' => true,
136 'mwtestFooBarConfig2' => 'string',
137 ],
138 [
139 'mwtestFooBarDefault' => 1234,
140 'mwtestFooBarConfig' => false,
141 ],
142 [
143 'mwtestFooBarConfig' => true,
144 'mwtestFooBarConfig2' => 'string',
145 'mwtestFooBarDefault' => 1234,
146 ],
147 ],
148 [
149 'No global already set, simple array',
150 null,
151 [
152 'mwtestDefaultOptions' => [
153 'foobar' => true,
154 ]
155 ],
156 [
157 'mwtestDefaultOptions' => [
158 'foobar' => true,
159 ]
160 ],
161 ],
162 [
163 'Global already set, simple array',
164 [
165 'mwtestDefaultOptions' => [
166 'foobar' => true,
167 'foo' => 'string'
168 ],
169 ],
170 [
171 'mwtestDefaultOptions' => [
172 'barbaz' => 12345,
173 'foobar' => false,
174 ],
175 ],
176 [
177 'mwtestDefaultOptions' => [
178 'barbaz' => 12345,
179 'foo' => 'string',
180 'foobar' => true,
181 ],
182 ]
183 ],
184 [
185 'Global already set, 1d array that appends',
186 [
187 'mwAvailableRights' => [
188 'foobar',
189 'foo'
190 ],
191 ],
192 [
193 'mwAvailableRights' => [
194 'barbaz',
195 ],
196 ],
197 [
198 'mwAvailableRights' => [
199 'barbaz',
200 'foobar',
201 'foo',
202 ],
203 ]
204 ],
205 [
206 'Global already set, array with integer keys',
207 [
208 'mwNamespacesFoo' => [
209 100 => true,
210 102 => false
211 ],
212 ],
213 [
214 'mwNamespacesFoo' => [
215 100 => false,
216 500 => true,
217 ExtensionRegistry::MERGE_STRATEGY => 'array_plus',
218 ],
219 ],
220 [
221 'mwNamespacesFoo' => [
222 100 => true,
223 102 => false,
224 500 => true,
225 ],
226 ]
227 ],
228 [
229 'No global already set, $wgHooks',
230 [
231 'wgHooks' => [],
232 ],
233 [
234 'wgHooks' => [
235 'FooBarEvent' => [
236 'FooBarClass::onFooBarEvent'
237 ],
238 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive'
239 ],
240 ],
241 [
242 'wgHooks' => [
243 'FooBarEvent' => [
244 'FooBarClass::onFooBarEvent'
245 ],
246 ],
247 ],
248 ],
249 [
250 'Global already set, $wgHooks',
251 [
252 'wgHooks' => [
253 'FooBarEvent' => [
254 'FooBarClass::onFooBarEvent'
255 ],
256 'BazBarEvent' => [
257 'FooBarClass::onBazBarEvent',
258 ],
259 ],
260 ],
261 [
262 'wgHooks' => [
263 'FooBarEvent' => [
264 'BazBarClass::onFooBarEvent',
265 ],
266 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive',
267 ],
268 ],
269 [
270 'wgHooks' => [
271 'FooBarEvent' => [
272 'FooBarClass::onFooBarEvent',
273 'BazBarClass::onFooBarEvent',
274 ],
275 'BazBarEvent' => [
276 'FooBarClass::onBazBarEvent',
277 ],
278 ],
279 ],
280 ],
281 [
282 'Global already set, $wgGroupPermissions',
283 [
284 'wgGroupPermissions' => [
285 'sysop' => [
286 'something' => true,
287 ],
288 'user' => [
289 'somethingtwo' => true,
290 ]
291 ],
292 ],
293 [
294 'wgGroupPermissions' => [
295 'customgroup' => [
296 'right' => true,
297 ],
298 'user' => [
299 'right' => true,
300 'somethingtwo' => false,
301 'nonduplicated' => true,
302 ],
303 ExtensionRegistry::MERGE_STRATEGY => 'array_plus_2d',
304 ],
305 ],
306 [
307 'wgGroupPermissions' => [
308 'customgroup' => [
309 'right' => true,
310 ],
311 'sysop' => [
312 'something' => true,
313 ],
314 'user' => [
315 'somethingtwo' => true,
316 'right' => true,
317 'nonduplicated' => true,
318 ]
319 ],
320 ],
321 ],
322 [
323 'False local setting should not be overridden (T100767)',
324 [
325 'mwtestT100767' => false,
326 ],
327 [
328 'mwtestT100767' => true,
329 ],
330 [
331 'mwtestT100767' => false,
332 ],
333 ],
334 [
335 'test array_replace_recursive',
336 [
337 'mwtestJsonConfigs' => [
338 'JsonZeroConfig' => [
339 'namespace' => 480,
340 'nsName' => 'Zero',
341 'isLocal' => true,
342 ],
343 ],
344 ],
345 [
346 'mwtestJsonConfigs' => [
347 'JsonZeroConfig' => [
348 'isLocal' => false,
349 'remote' => [
350 'username' => 'foo',
351 ],
352 ],
353 ExtensionRegistry::MERGE_STRATEGY => 'array_replace_recursive',
354 ],
355 ],
356 [
357 'mwtestJsonConfigs' => [
358 'JsonZeroConfig' => [
359 'namespace' => 480,
360 'nsName' => 'Zero',
361 'isLocal' => false,
362 'remote' => [
363 'username' => 'foo',
364 ],
365 ],
366 ],
367 ],
368 ],
369 [
370 'global is null before',
371 [
372 'NullGlobal' => null,
373 ],
374 [
375 'NullGlobal' => 'not-null'
376 ],
377 [
378 'NullGlobal' => null
379 ],
380 ],
381 ];
382 }
383 }