3 use Wikimedia\ScopedCallback
;
6 * @covers ExtensionRegistry
8 class ExtensionRegistryTest
extends MediaWikiTestCase
{
12 public function setUp() {
14 $this->dataDir
= __DIR__
. '/../../data/registration';
17 public function testQueue_invalid() {
18 $registry = new ExtensionRegistry();
19 $path = __DIR__
. '/doesnotexist.json';
20 $this->setExpectedException(
24 $registry->queue( $path );
27 public function testQueue() {
28 $registry = new ExtensionRegistry();
29 $path = "{$this->dataDir}/good.json";
30 $registry->queue( $path );
31 $this->assertArrayHasKey(
35 $registry->clearQueue();
36 $this->assertEmpty( $registry->getQueue() );
39 public function testLoadFromQueue_empty() {
40 $registry = new ExtensionRegistry();
41 $registry->loadFromQueue();
42 $this->assertEmpty( $registry->getAllThings() );
45 public function testLoadFromQueue_late() {
46 $registry = new ExtensionRegistry();
48 $registry->queue( "{$this->dataDir}/good.json" );
49 $this->setExpectedException(
51 "The following paths tried to load late: {$this->dataDir}/good.json"
53 $registry->loadFromQueue();
56 public function testLoadFromQueue() {
57 $registry = new ExtensionRegistry();
58 $registry->queue( "{$this->dataDir}/good.json" );
59 $registry->loadFromQueue();
60 $this->assertArrayHasKey( 'FooBar', $registry->getAllThings() );
61 $this->assertTrue( $registry->isLoaded( 'FooBar' ) );
62 $this->assertTrue( $registry->isLoaded( 'FooBar', '*' ) );
63 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
64 $this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) );
67 public function testLoadFromQueueWithConstraintWithVersion() {
68 $registry = new ExtensionRegistry();
69 $registry->queue( "{$this->dataDir}/good_with_version.json" );
70 $registry->loadFromQueue();
71 $this->assertTrue( $registry->isLoaded( 'FooBar', '>= 1.2.0' ) );
72 $this->assertFalse( $registry->isLoaded( 'FooBar', '^1.3.0' ) );
76 * @expectedException LogicException
78 public function testLoadFromQueueWithConstraintWithoutVersion() {
79 $registry = new ExtensionRegistry();
80 $registry->queue( "{$this->dataDir}/good.json" );
81 $registry->loadFromQueue();
82 $registry->isLoaded( 'FooBar', '>= 1.2.0' );
86 * @expectedException PHPUnit_Framework_Error
88 public function testReadFromQueue_nonexistent() {
89 $registry = new ExtensionRegistry();
90 $registry->readFromQueue( [
91 __DIR__
. '/doesnotexist.json' => 1
95 public function testReadFromQueueInitializeAutoloaderWithPsr4Namespaces() {
96 $registry = new ExtensionRegistry();
97 $registry->readFromQueue( [
98 "{$this->dataDir}/autoload_namespaces.json" => 1
101 class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ),
102 "Registry initializes Autoloader from AutoloadNamespaces"
107 * @dataProvider provideExportExtractedDataGlobals
109 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
110 // Set globals for test
112 foreach ( $before as $key => $value ) {
113 // mw prefixed globals does not exist normally
114 if ( substr( $key, 0, 2 ) == 'mw' ) {
115 $GLOBALS[$key] = $value;
117 $this->setMwGlobals( $key, $value );
123 'globals' => $globals,
128 'autoloaderPaths' => []
130 $registry = new ExtensionRegistry();
131 $class = new ReflectionClass( ExtensionRegistry
::class );
132 $method = $class->getMethod( 'exportExtractedData' );
133 $method->setAccessible( true );
134 $method->invokeArgs( $registry, [ $info ] );
135 foreach ( $expected as $name => $value ) {
136 $this->assertArrayHasKey( $name, $GLOBALS, $desc );
137 $this->assertEquals( $value, $GLOBALS[$name], $desc );
140 // Remove mw prefixed globals
142 foreach ( $before as $key => $value ) {
143 if ( substr( $key, 0, 2 ) == 'mw' ) {
144 unset( $GLOBALS[$key] );
150 public static function provideExportExtractedDataGlobals() {
151 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
154 'Simple non-array values',
156 'mwtestFooBarConfig' => true,
157 'mwtestFooBarConfig2' => 'string',
160 'mwtestFooBarDefault' => 1234,
161 'mwtestFooBarConfig' => false,
164 'mwtestFooBarConfig' => true,
165 'mwtestFooBarConfig2' => 'string',
166 'mwtestFooBarDefault' => 1234,
170 'No global already set, simple array',
173 'mwtestDefaultOptions' => [
178 'mwtestDefaultOptions' => [
184 'Global already set, simple array',
186 'mwtestDefaultOptions' => [
192 'mwtestDefaultOptions' => [
198 'mwtestDefaultOptions' => [
206 'Global already set, 1d array that appends',
208 'mwAvailableRights' => [
214 'mwAvailableRights' => [
219 'mwAvailableRights' => [
227 'Global already set, array with integer keys',
229 'mwNamespacesFoo' => [
235 'mwNamespacesFoo' => [
238 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus',
242 'mwNamespacesFoo' => [
250 'No global already set, $wgHooks',
257 'FooBarClass::onFooBarEvent'
259 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive'
265 'FooBarClass::onFooBarEvent'
271 'Global already set, $wgHooks',
275 'FooBarClass::onFooBarEvent'
278 'FooBarClass::onBazBarEvent',
285 'BazBarClass::onFooBarEvent',
287 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive',
293 'FooBarClass::onFooBarEvent',
294 'BazBarClass::onFooBarEvent',
297 'FooBarClass::onBazBarEvent',
303 'Global already set, $wgGroupPermissions',
305 'wgGroupPermissions' => [
310 'somethingtwo' => true,
315 'wgGroupPermissions' => [
321 'somethingtwo' => false,
322 'nonduplicated' => true,
324 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus_2d',
328 'wgGroupPermissions' => [
336 'somethingtwo' => true,
338 'nonduplicated' => true,
344 'False local setting should not be overridden (T100767)',
346 'mwtestT100767' => false,
349 'mwtestT100767' => true,
352 'mwtestT100767' => false,
356 'test array_replace_recursive',
358 'mwtestJsonConfigs' => [
359 'JsonZeroConfig' => [
367 'mwtestJsonConfigs' => [
368 'JsonZeroConfig' => [
374 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_replace_recursive',
378 'mwtestJsonConfigs' => [
379 'JsonZeroConfig' => [
391 'global is null before',
393 'NullGlobal' => null,
396 'NullGlobal' => 'not-null'
405 public function testSetAttributeForTest() {
406 $registry = new ExtensionRegistry();
407 $registry->queue( "{$this->dataDir}/good.json" );
408 $registry->loadFromQueue();
409 // Sanity check that it worked
410 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
411 $reset = $registry->setAttributeForTest( 'FooBarAttr', [ 'override' ] );
412 // overridden properly
413 $this->assertSame( [ 'override' ], $registry->getAttribute( 'FooBarAttr' ) );
414 ScopedCallback
::consume( $reset );
416 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
420 * @expectedException Exception
421 * @expectedExceptionMessage The attribute 'foo' has already been overridden
423 public function testSetAttributeForTestDuplicate() {
424 $registry = new ExtensionRegistry();
425 $reset1 = $registry->setAttributeForTest( 'foo', [ 'val1' ] );
426 $reset2 = $registry->setAttributeForTest( 'foo', [ 'val2' ] );