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->assertTrue( $registry->isLoaded( 'FooBar', '*' ) );
61 $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
62 $this->assertSame( [], $registry->getAttribute( 'NotLoadedAttr' ) );
65 public function testLoadFromQueueWithConstraintWithVersion() {
66 $registry = new ExtensionRegistry();
67 $registry->queue( "{$this->dataDir}/good_with_version.json" );
68 $registry->loadFromQueue();
69 $this->assertTrue( $registry->isLoaded( 'FooBar', '>= 1.2.0' ) );
70 $this->assertFalse( $registry->isLoaded( 'FooBar', '^1.3.0' ) );
74 * @expectedException LogicException
76 public function testLoadFromQueueWithConstraintWithoutVersion() {
77 $registry = new ExtensionRegistry();
78 $registry->queue( "{$this->dataDir}/good.json" );
79 $registry->loadFromQueue();
80 $registry->isLoaded( 'FooBar', '>= 1.2.0' );
84 * @expectedException PHPUnit_Framework_Error
86 public function testReadFromQueue_nonexistent() {
87 $registry = new ExtensionRegistry();
88 $registry->readFromQueue( [
89 __DIR__
. '/doesnotexist.json' => 1
93 public function testReadFromQueueInitializeAutoloaderWithPsr4Namespaces() {
94 $registry = new ExtensionRegistry();
95 $registry->readFromQueue( [
96 "{$this->dataDir}/autoload_namespaces.json" => 1
99 class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ),
100 "Registry initializes Autoloader from AutoloadNamespaces"
105 * @dataProvider provideExportExtractedDataGlobals
107 public function testExportExtractedDataGlobals( $desc, $before, $globals, $expected ) {
108 // Set globals for test
110 foreach ( $before as $key => $value ) {
111 // mw prefixed globals does not exist normally
112 if ( substr( $key, 0, 2 ) == 'mw' ) {
113 $GLOBALS[$key] = $value;
115 $this->setMwGlobals( $key, $value );
121 'globals' => $globals,
126 'autoloaderPaths' => []
128 $registry = new ExtensionRegistry();
129 $class = new ReflectionClass( ExtensionRegistry
::class );
130 $method = $class->getMethod( 'exportExtractedData' );
131 $method->setAccessible( true );
132 $method->invokeArgs( $registry, [ $info ] );
133 foreach ( $expected as $name => $value ) {
134 $this->assertArrayHasKey( $name, $GLOBALS, $desc );
135 $this->assertEquals( $value, $GLOBALS[$name], $desc );
138 // Remove mw prefixed globals
140 foreach ( $before as $key => $value ) {
141 if ( substr( $key, 0, 2 ) == 'mw' ) {
142 unset( $GLOBALS[$key] );
148 public static function provideExportExtractedDataGlobals() {
149 // "mwtest" prefix used instead of "$wg" to avoid potential conflicts
152 'Simple non-array values',
154 'mwtestFooBarConfig' => true,
155 'mwtestFooBarConfig2' => 'string',
158 'mwtestFooBarDefault' => 1234,
159 'mwtestFooBarConfig' => false,
162 'mwtestFooBarConfig' => true,
163 'mwtestFooBarConfig2' => 'string',
164 'mwtestFooBarDefault' => 1234,
168 'No global already set, simple array',
171 'mwtestDefaultOptions' => [
176 'mwtestDefaultOptions' => [
182 'Global already set, simple array',
184 'mwtestDefaultOptions' => [
190 'mwtestDefaultOptions' => [
196 'mwtestDefaultOptions' => [
204 'Global already set, 1d array that appends',
206 'mwAvailableRights' => [
212 'mwAvailableRights' => [
217 'mwAvailableRights' => [
225 'Global already set, array with integer keys',
227 'mwNamespacesFoo' => [
233 'mwNamespacesFoo' => [
236 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus',
240 'mwNamespacesFoo' => [
248 'No global already set, $wgHooks',
255 'FooBarClass::onFooBarEvent'
257 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive'
263 'FooBarClass::onFooBarEvent'
269 'Global already set, $wgHooks',
273 'FooBarClass::onFooBarEvent'
276 'FooBarClass::onBazBarEvent',
283 'BazBarClass::onFooBarEvent',
285 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_merge_recursive',
291 'FooBarClass::onFooBarEvent',
292 'BazBarClass::onFooBarEvent',
295 'FooBarClass::onBazBarEvent',
301 'Global already set, $wgGroupPermissions',
303 'wgGroupPermissions' => [
308 'somethingtwo' => true,
313 'wgGroupPermissions' => [
319 'somethingtwo' => false,
320 'nonduplicated' => true,
322 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_plus_2d',
326 'wgGroupPermissions' => [
334 'somethingtwo' => true,
336 'nonduplicated' => true,
342 'False local setting should not be overridden (T100767)',
344 'mwtestT100767' => false,
347 'mwtestT100767' => true,
350 'mwtestT100767' => false,
354 'test array_replace_recursive',
356 'mwtestJsonConfigs' => [
357 'JsonZeroConfig' => [
365 'mwtestJsonConfigs' => [
366 'JsonZeroConfig' => [
372 ExtensionRegistry
::MERGE_STRATEGY
=> 'array_replace_recursive',
376 'mwtestJsonConfigs' => [
377 'JsonZeroConfig' => [
389 'global is null before',
391 'NullGlobal' => null,
394 'NullGlobal' => 'not-null'