4 * @covers ExtensionRegistry
6 class ExtensionRegistryTest
extends MediaWikiTestCase
{
10 public function setUp() {
12 $this->dataDir
= __DIR__
. '/../../data/registration';
15 public function testQueue_invalid() {
16 $registry = new ExtensionRegistry();
17 $path = __DIR__
. '/doesnotexist.json';
18 $this->setExpectedException(
20 "$path does not exist!"
22 $registry->queue( $path );
25 public function testQueue() {
26 $registry = new ExtensionRegistry();
27 $path = "{$this->dataDir}/good.json";
28 $registry->queue( $path );
29 $this->assertArrayHasKey(
33 $registry->clearQueue();
34 $this->assertEmpty( $registry->getQueue() );
37 public function testLoadFromQueue_empty() {
38 $registry = new ExtensionRegistry();
39 $registry->loadFromQueue();
40 $this->assertEmpty( $registry->getAllThings() );
43 public function testLoadFromQueue_late() {
44 $registry = new ExtensionRegistry();
46 $registry->queue( "{$this->dataDir}/good.json" );
47 $this->setExpectedException(
49 "The following paths tried to load late: {$this->dataDir}/good.json"
51 $registry->loadFromQueue();
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' ) );
65 * @expectedException PHPUnit_Framework_Error
67 public function testReadFromQueue_nonexistent() {
68 $registry = new ExtensionRegistry();
69 $registry->readFromQueue( [
70 __DIR__
. '/doesnotexist.json' => 1
74 public function testReadFromQueueInitializeAutoloaderWithPsr4Namespaces() {
75 $registry = new ExtensionRegistry();
76 $registry->readFromQueue( [
77 "{$this->dataDir}/autoload_namespaces.json" => 1
80 class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ),
81 "Registry initializes Autoloader from AutoloadNamespaces"
86 * @dataProvider provideExportExtractedDataGlobals
88 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
89 // Set globals for test
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;
96 $this->setMwGlobals( $key, $value );
102 'globals' => $globals,
107 'autoloaderPaths' => []
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 );
119 // Remove mw prefixed globals
121 foreach ( $before as $key => $value ) {
122 if ( substr( $key, 0, 2 ) == 'mw' ) {
123 unset( $GLOBALS[$key] );
129 public static function provideExportExtractedDataGlobals() {
130 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
133 'Simple non-array values',
135 'mwtestFooBarConfig' => true,
136 'mwtestFooBarConfig2' => 'string',
139 'mwtestFooBarDefault' => 1234,
140 'mwtestFooBarConfig' => false,
143 'mwtestFooBarConfig' => true,
144 'mwtestFooBarConfig2' => 'string',
145 'mwtestFooBarDefault' => 1234,
149 'No global already set, simple array',
152 'mwtestDefaultOptions' => [
157 'mwtestDefaultOptions' => [
163 'Global already set, simple array',
165 'mwtestDefaultOptions' => [
171 'mwtestDefaultOptions' => [
177 'mwtestDefaultOptions' => [
185 'Global already set, 1d array that appends',
187 'mwAvailableRights' => [
193 'mwAvailableRights' => [
198 'mwAvailableRights' => [
206 'Global already set, array with integer keys',
208 'mwNamespacesFoo' => [
214 'mwNamespacesFoo' => [
217 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus',
221 'mwNamespacesFoo' => [
229 'No global already set, $wgHooks',
236 'FooBarClass::onFooBarEvent'
238 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive'
244 'FooBarClass::onFooBarEvent'
250 'Global already set, $wgHooks',
254 'FooBarClass::onFooBarEvent'
257 'FooBarClass::onBazBarEvent',
264 'BazBarClass::onFooBarEvent',
266 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive',
272 'FooBarClass::onFooBarEvent',
273 'BazBarClass::onFooBarEvent',
276 'FooBarClass::onBazBarEvent',
282 'Global already set, $wgGroupPermissions',
284 'wgGroupPermissions' => [
289 'somethingtwo' => true,
294 'wgGroupPermissions' => [
300 'somethingtwo' => false,
301 'nonduplicated' => true,
303 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus_2d',
307 'wgGroupPermissions' => [
315 'somethingtwo' => true,
317 'nonduplicated' => true,
323 'False local setting should not be overridden (T100767)',
325 'mwtestT100767' => false,
328 'mwtestT100767' => true,
331 'mwtestT100767' => false,
335 'test array_replace_recursive',
337 'mwtestJsonConfigs' => [
338 'JsonZeroConfig' => [
346 'mwtestJsonConfigs' => [
347 'JsonZeroConfig' => [
353 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_replace_recursive',
357 'mwtestJsonConfigs' => [
358 'JsonZeroConfig' => [
370 'global is null before',
372 'NullGlobal' => null,
375 'NullGlobal' => 'not-null'