Debian Codegraph @

tests/phpunit/unit/includes/registration/ExtensionRegistryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Registration;
4
5 use Exception;
6 use InvalidArgumentException;
7 use LogicException;
8 use MediaWiki\Registration\ExtensionRegistry;
9 use MediaWiki\Settings\SettingsBuilder;
10 use MediaWikiUnitTestCase;
11 use Wikimedia\ScopedCallback;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @covers \MediaWiki\Registration\ExtensionRegistry
16 */
17 class ExtensionRegistryTest extends MediaWikiUnitTestCase {
18 private const DATA_DIR = __DIR__ . '/../../../data/registration';
19
20 /** @var array */
21 private $restoreGlobals = [];
22
23 /** @var array */
24 private $unsetGlobals = [];
25
26 protected function tearDown(): void {
27 foreach ( $this->restoreGlobals as $key => $value ) {
28 $GLOBALS[$key] = $value;
29 }
30
31 foreach ( $this->unsetGlobals as $var ) {
32 unset( $GLOBALS[$var] );
33 }
34
35 parent::tearDown();
36 }
37
38 private function getRegistry(): ExtensionRegistry {
39 $registry = new ExtensionRegistry();
40 // Mock the global SettingsBuilder dependencies, as this is a unit test. And because SettingsBuilder
41 // has a reverse dependency on the global ExtensionRegistry instance, it would throw an exception
42 // because access to the global ExtensionRegistry instance is forbidden in unit tests.
43 $registry->setSettingsBuilder( $this->createMock( SettingsBuilder::class ) );
44 return $registry;
45 }
46
47 private function setGlobal( $key, $value ) {
48 if ( isset( $GLOBALS[$key] ) ) {
49 $this->restoreGlobals[$key] = $GLOBALS[$key];
50 } else {
51 $this->unsetGlobals[] = $key;
52 }
53
54 $GLOBALS[$key] = $value;
55 }
56
57 public function testQueue_invalid() {
58 $this->setGlobal( 'wgExtensionInfoMTime', false );
59
60 $registry = $this->getRegistry();
61 $path = __DIR__ . '/doesnotexist.json';
62 $this->expectException( Exception::class );
63 $this->expectExceptionMessage( "file $path" );
64 $registry->queue( $path );
65 }
66
67 public function testQueue() {
68 $registry = $this->getRegistry();
69 $path = self::DATA_DIR . "/good.json";
70 $registry->queue( $path );
71 $this->assertArrayHasKey(
72 $path,
73 $registry->getQueue()
74 );
75 $registry->clearQueue();
76 $this->assertSame( [], $registry->getQueue() );
77 }
78
79 public function testLoadFromQueue_empty() {
80 $registry = $this->getRegistry();
81 $registry->loadFromQueue();
82 $this->assertSame( [], $registry->getAllThings() );
83 }
84
85 public function testLoadFromQueue_late() {
86 $registry = $this->getRegistry();
87 $registry->finish();
88 $registry->queue( self::DATA_DIR . "/good.json" );
89 $this->expectException( LogicException::class );
90 $this->expectExceptionMessage(
91 "The following paths tried to load late: " . self::DATA_DIR . "/good.json" );
92 $registry->loadFromQueue();
93 }
94
95 public function testLoadFromQueue() {
96 $registry = $this->getRegistry();
97 $registry->queue( self::DATA_DIR . "/good.json" );
98 $registry->loadFromQueue();
99 $this->assertArrayHasKey( 'FooBar', $registry->getAllThings() );
100 $this->assertTrue( $registry->isLoaded( 'FooBar' ) );
101 $this->assertTrue( $registry->isLoaded( 'FooBar', '*' ) );
102 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
103 $this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) );
104 }
105
106 public function testLoadFromQueueWithConstraintWithVersion() {
107 $registry = $this->getRegistry();
108 $registry->queue( self::DATA_DIR . "/good_with_version.json" );
109 $registry->loadFromQueue();
110 $this->assertTrue( $registry->isLoaded( 'FooBar', '>= 1.2.0' ) );
111 $this->assertFalse( $registry->isLoaded( 'FooBar', '^1.3.0' ) );
112 }
113
114 public function testLoadFromQueueWithConstraintWithoutVersion() {
115 $registry = $this->getRegistry();
116 $registry->queue( self::DATA_DIR . "/good.json" );
117 $registry->loadFromQueue();
118 $this->expectException( LogicException::class );
119 $registry->isLoaded( 'FooBar', '>= 1.2.0' );
120 }
121
122 public function testReadFromQueue_nonexistent() {
123 $registry = $this->getRegistry();
124 $this->expectException( InvalidArgumentException::class );
125 $this->expectExceptionMessage( 'Unable to read' );
126 $this->expectPHPError(
127 E_WARNING,
128 static function () use ( $registry ) {
129 $registry->readFromQueue( [
130 __DIR__ . '/doesnotexist.json' => 1
131 ] );
132 }
133 );
134 }
135
136 public function testExportExtractedDataNamespaceAlreadyDefined() {
137 define( 'FOO_VALUE', 123 ); // Emulates overriding a namespace set in LocalSettings.php
138 $registry = $this->getRegistry();
139 $info = [ 'defines' => [ 'FOO_VALUE' => 456 ], 'globals' => [] ];
140 $this->expectException( Exception::class );
141 $this->expectExceptionMessage(
142 "FOO_VALUE cannot be re-defined with 456 it has already been set with 123"
143 );
144 TestingAccessWrapper::newFromObject( $registry )->exportExtractedData( $info );
145 }
146
147 /**
148 * @dataProvider provideExportExtractedDataGlobals
149 */
150 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
151 // Set globals for test
152 if ( $before ) {
153 foreach ( $before as $key => $value ) {
154 // mw prefixed globals does not exist normally
155 if ( str_starts_with( $key, 'mw' ) ) {
156 $GLOBALS[$key] = $value;
157 } else {
158 $this->setGlobal( $key, $value );
159 }
160 }
161 }
162
163 $info = [
164 'globals' => $globals,
165 'callbacks' => [],
166 'defines' => [],
167 'credits' => [],
168 'attributes' => [],
169 'autoloaderPaths' => []
170 ];
171 $registry = $this->getRegistry();
172 TestingAccessWrapper::newFromObject( $registry )->exportExtractedData( $info );
173 $result = array_intersect_key( $GLOBALS, $expected );
174 $this->assertEquals( $expected, $result, $desc );
175
176 // Remove mw prefixed globals
177 if ( $before ) {
178 foreach ( $before as $key => $value ) {
179 if ( str_starts_with( $key, 'mw' ) ) {
180 unset( $GLOBALS[$key] );
181 }
182 }
183 }
184 }
185
186 public static function provideExportExtractedDataGlobals() {
187 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
188 return [
189 [
190 'Simple non-array values',
191 [
192 'mwtestFooBarConfig' => true,
193 'mwtestFooBarConfig2' => 'string',
194 ],
195 [
196 'mwtestFooBarDefault' => 1234,
197 'mwtestFooBarConfig' => false,
198 ],
199 [
200 'mwtestFooBarConfig' => true,
201 'mwtestFooBarConfig2' => 'string',
202 'mwtestFooBarDefault' => 1234,
203 ],
204 ],
205 [
206 'No global already set, simple array',
207 null,
208 [
209 'mwtestDefaultOptions' => [
210 'foobar' => true,
211 ]
212 ],
213 [
214 'mwtestDefaultOptions' => [
215 'foobar' => true,
216 ]
217 ],
218 ],
219 [
220 'Global already set, simple array',
221 [
222 'mwtestDefaultOptions' => [
223 'foobar' => true,
224 'foo' => 'string'
225 ],
226 ],
227 [
228 'mwtestDefaultOptions' => [
229 'barbaz' => 12345,
230 'foobar' => false,
231 ],
232 ],
233 [
234 'mwtestDefaultOptions' => [
235 'barbaz' => 12345,
236 'foo' => 'string',
237 'foobar' => true,
238 ],
239 ]
240 ],
241 [
242 'Global already set, 1d array that appends',
243 [
244 'mwAvailableRights' => [
245 'foobar',
246 'foo'
247 ],
248 ],
249 [
250 'mwAvailableRights' => [
251 'barbaz',
252 ],
253 ],
254 [
255 'mwAvailableRights' => [
256 'barbaz',
257 'foobar',
258 'foo',
259 ],
260 ]
261 ],
262 [
263 'Global already set, array with integer keys',
264 [
265 'mwNamespacesFoo' => [
266 100 => true,
267 102 => false
268 ],
269 ],
270 [
271 'mwNamespacesFoo' => [
272 100 => false,
273 500 => true,
274 ExtensionRegistry::MERGE_STRATEGY => 'array_plus',
275 ],
276 ],
277 [
278 'mwNamespacesFoo' => [
279 100 => true,
280 102 => false,
281 500 => true,
282 ],
283 ]
284 ],
285 [
286 'No global already set, $wgHooks',
287 [
288 'wgHooks' => [],
289 ],
290 [
291 'wgHooks' => [
292 'FooBarEvent' => [
293 'FooBarClass::onFooBarEvent'
294 ],
295 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive'
296 ],
297 ],
298 [
299 'wgHooks' => [
300 'FooBarEvent' => [
301 'FooBarClass::onFooBarEvent'
302 ],
303 ],
304 ],
305 ],
306 [
307 'Global already set, $wgHooks',
308 [
309 'wgHooks' => [
310 'FooBarEvent' => [
311 'FooBarClass::onFooBarEvent'
312 ],
313 'BazBarEvent' => [
314 'FooBarClass::onBazBarEvent',
315 ],
316 ],
317 ],
318 [
319 'wgHooks' => [
320 'FooBarEvent' => [
321 'BazBarClass::onFooBarEvent',
322 ],
323 ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive',
324 ],
325 ],
326 [
327 'wgHooks' => [
328 'FooBarEvent' => [
329 'FooBarClass::onFooBarEvent',
330 'BazBarClass::onFooBarEvent',
331 ],
332 'BazBarEvent' => [
333 'FooBarClass::onBazBarEvent',
334 ],
335 ],
336 ],
337 ],
338 [
339 'Global already set, $wgGroupPermissions',
340 [
341 'wgGroupPermissions' => [
342 'sysop' => [
343 'something' => true,
344 ],
345 'user' => [
346 'somethingtwo' => true,
347 ]
348 ],
349 ],
350 [
351 'wgGroupPermissions' => [
352 'customgroup' => [
353 'right' => true,
354 ],
355 'user' => [
356 'right' => true,
357 'somethingtwo' => false,
358 'nonduplicated' => true,
359 ],
360 ExtensionRegistry::MERGE_STRATEGY => 'array_plus_2d',
361 ],
362 ],
363 [
364 'wgGroupPermissions' => [
365 'customgroup' => [
366 'right' => true,
367 ],
368 'sysop' => [
369 'something' => true,
370 ],
371 'user' => [
372 'somethingtwo' => true,
373 'right' => true,
374 'nonduplicated' => true,
375 ]
376 ],
377 ],
378 ],
379 [
380 'False local setting should not be overridden (T100767)',
381 [
382 'mwtestT100767' => false,
383 ],
384 [
385 'mwtestT100767' => true,
386 ],
387 [
388 'mwtestT100767' => false,
389 ],
390 ],
391 [
392 'test array_replace_recursive',
393 [
394 'mwtestJsonConfigs' => [
395 'JsonZeroConfig' => [
396 'namespace' => 480,
397 'nsName' => 'Zero',
398 'isLocal' => false,
399 'remote' => [
400 'username' => 'foo',
401 ],
402 ],
403 ],
404 ],
405 [
406 'mwtestJsonConfigs' => [
407 'JsonZeroConfig' => [
408 'isLocal' => true,
409 ],
410 ExtensionRegistry::MERGE_STRATEGY => 'array_replace_recursive',
411 ],
412 ],
413 [
414 'mwtestJsonConfigs' => [
415 'JsonZeroConfig' => [
416 'namespace' => 480,
417 'nsName' => 'Zero',
418 'isLocal' => false,
419 'remote' => [
420 'username' => 'foo',
421 ],
422 ],
423 ],
424 ],
425 ],
426 [
427 'global is null before',
428 [
429 'NullGlobal' => null,
430 ],
431 [
432 'NullGlobal' => 'not-null'
433 ],
434 [
435 'NullGlobal' => null
436 ],
437 ],
438 [
439 'provide_default passive case',
440 [
441 'wgFlatArray' => [],
442 ],
443 [
444 'wgFlatArray' => [
445 1,
446 ExtensionRegistry::MERGE_STRATEGY => 'provide_default'
447 ],
448 ],
449 [
450 'wgFlatArray' => []
451 ],
452 ],
453 [
454 'provide_default active case',
455 [
456 ],
457 [
458 'wgFlatArray' => [
459 1,
460 ExtensionRegistry::MERGE_STRATEGY => 'provide_default'
461 ],
462 ],
463 [
464 'wgFlatArray' => [ 1 ]
465 ],
466 ],
467 [
468 'a configured value should not turn into a default null value',
469 [
470 'ArrayValue' => [],
471 'BooleanValue' => false,
472 'IntegerValue' => 0,
473 'StringValue' => '',
474 ],
475 [
476 'ArrayValue' => null,
477 'BooleanValue' => null,
478 'IntegerValue' => null,
479 'StringValue' => null,
480 ],
481 [
482 'ArrayValue' => [],
483 'BooleanValue' => false,
484 'IntegerValue' => 0,
485 'StringValue' => '',
486 ],
487 ],
488 [
489 'a configured value should not turn into a default empty array value',
490 [
491 'BooleanValue' => false,
492 'IntegerValue' => 0,
493 'NullValue' => null,
494 'StringValue' => '',
495 ],
496 [
497 'BooleanValue' => [],
498 'IntegerValue' => [],
499 'NullValue' => [],
500 'StringValue' => [],
501 ],
502 [
503 'BooleanValue' => false,
504 'IntegerValue' => 0,
505 'NullValue' => null,
506 'StringValue' => '',
507 ],
508 ],
509 ];
510 }
511
512 public function testSetAttributeForTest() {
513 $registry = $this->getRegistry();
514 $registry->queue( self::DATA_DIR . "/good.json" );
515 $registry->loadFromQueue();
516 // Check that it worked
517 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
518 $reset = $registry->setAttributeForTest( 'FooBarAttr', [ 'override' ] );
519 // overridden properly
520 $this->assertSame( [ 'override' ], $registry->getAttribute( 'FooBarAttr' ) );
521 ScopedCallback::consume( $reset );
522 // reset properly
523 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
524 }
525
526 public function testSetAttributeForTestDuplicate() {
527 $registry = $this->getRegistry();
528 $reset1 = $registry->setAttributeForTest( 'foo', [ 'val1' ] );
529 $this->expectException( Exception::class );
530 $this->expectExceptionMessage( "The attribute 'foo' has already been overridden" );
531 $reset2 = $registry->setAttributeForTest( 'foo', [ 'val2' ] );
532 }
533
534 public function testGetLazyLoadedAttribute() {
535 $registry = TestingAccessWrapper::newFromObject(
536 $this->getRegistry()
537 );
538 // Verify the registry is absolutely empty
539 $this->assertSame( [], $registry->getLazyLoadedAttribute( 'FooBarBaz' ) );
540 // Indicate what paths should be checked for the lazy attributes
541 $registry->queue( self::DATA_DIR . "/attribute.json" );
542 $registry->loadFromQueue();
543 // Set in attribute.json
544 $this->assertEquals(
545 [ 'buzz' => true ],
546 $registry->getLazyLoadedAttribute( 'FooBarBaz' )
547 );
548 // Still return an array if nothing was set
549 $this->assertSame(
550 [],
551 $registry->getLazyLoadedAttribute( 'NotSetAtAll' )
552 );
553
554 // Test test overrides
555 $reset = $registry->setAttributeForTest( 'FooBarBaz',
556 [ 'lightyear' => true ] );
557 $this->assertEquals(
558 [ 'lightyear' => true ],
559 $registry->getLazyLoadedAttribute( 'FooBarBaz' )
560 );
561 }
562
563 }