registration: Only allow one extension to set a specific config setting
[lhc/web/wiklou.git] / tests / phpunit / includes / registration / ExtensionProcessorTest.php
index 71f760d..5ef30e8 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 class ExtensionProcessorTest extends MediaWikiTestCase {
 
        private $dir, $dirname;
@@ -38,6 +40,58 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
        }
 
+       /**
+        * @covers ExtensionProcessor::extractInfo
+        */
+       public function testExtractInfo_namespaces() {
+               // Test that namespace IDs can be overwritten
+               if ( !defined( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X' ) ) {
+                       define( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X', 123456 );
+               }
+
+               $processor = new ExtensionProcessor();
+               $processor->extractInfo( $this->dir, self::$default + [
+                       'namespaces' => [
+                               [
+                                       'id' => 332200,
+                                       'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
+                                       'name' => 'Test_A',
+                                       'content' => 'TestModel'
+                               ],
+                               [ // Test_X will use ID 123456 not 334400
+                                       'id' => 334400,
+                                       'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
+                                       'name' => 'Test_X',
+                                       'content' => 'TestModel'
+                               ],
+                       ]
+               ], 1 );
+
+               $extracted = $processor->getExtractedInfo();
+
+               $this->assertArrayHasKey(
+                       'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
+                       $extracted['defines']
+               );
+               $this->assertArrayNotHasKey(
+                       'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
+                       $extracted['defines']
+               );
+
+               $this->assertSame(
+                       $extracted['defines']['MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A'],
+                       332200
+               );
+
+               $this->assertArrayHasKey( 'ExtensionNamespaces', $extracted['attributes'] );
+               $this->assertArrayHasKey( 123456, $extracted['attributes']['ExtensionNamespaces'] );
+               $this->assertArrayHasKey( 332200, $extracted['attributes']['ExtensionNamespaces'] );
+               $this->assertArrayNotHasKey( 334400, $extracted['attributes']['ExtensionNamespaces'] );
+
+               $this->assertSame( 'Test_X', $extracted['attributes']['ExtensionNamespaces'][123456] );
+               $this->assertSame( 'Test_A', $extracted['attributes']['ExtensionNamespaces'][332200] );
+       }
+
        public static function provideRegisterHooks() {
                $merge = [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ];
                // Format:
@@ -166,6 +220,48 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
        }
 
+       /**
+        * @covers ExtensionProcessor::addConfigGlobal()
+        * @expectedException RuntimeException
+        */
+       public function testDuplicateConfigKey1() {
+               $processor = new ExtensionProcessor;
+               $info = [
+                       'config' => [
+                               'Bar' => '',
+                       ]
+               ] + self::$default;
+               $info2 = [
+                       'config' => [
+                               'Bar' => 'g',
+                       ],
+                       'name' => 'FooBar2',
+               ];
+               $processor->extractInfo( $this->dir, $info, 1 );
+               $processor->extractInfo( $this->dir, $info2, 1 );
+       }
+
+       /**
+        * @covers ExtensionProcessor::addConfigGlobal()
+        * @expectedException RuntimeException
+        */
+       public function testDuplicateConfigKey2() {
+               $processor = new ExtensionProcessor;
+               $info = [
+                       'config' => [
+                               'Bar' => [ 'value' => 'somevalue' ],
+                       ]
+               ] + self::$default;
+               $info2 = [
+                       'config' => [
+                               'Bar' => [ 'value' => 'somevalue' ],
+                       ],
+                       'name' => 'FooBar2',
+               ];
+               $processor->extractInfo( $this->dir, $info, 2 );
+               $processor->extractInfo( $this->dir, $info2, 2 );
+       }
+
        public static function provideExtractExtensionMessagesFiles() {
                $dir = __DIR__ . '/FooBar/';
                return [
@@ -445,6 +541,72 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                ];
        }
 
+       /**
+        * Attributes under manifest_version 2
+        *
+        * @covers ExtensionProcessor::extractAttributes
+        * @covers ExtensionProcessor::getExtractedInfo
+        */
+       public function testExtractAttributes() {
+               $processor = new ExtensionProcessor();
+               // Load FooBar extension
+               $processor->extractInfo( $this->dir, [ 'name' => 'FooBar' ], 2 );
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'name' => 'Baz',
+                               'attributes' => [
+                                       // Loaded
+                                       'FooBar' => [
+                                               'Plugins' => [
+                                                       'ext.baz.foobar',
+                                               ],
+                                       ],
+                                       // Not loaded
+                                       'FizzBuzz' => [
+                                               'MorePlugins' => [
+                                                       'ext.baz.fizzbuzz',
+                                               ],
+                                       ],
+                               ],
+                       ],
+                       2
+               );
+
+               $info = $processor->getExtractedInfo();
+               $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
+               $this->assertArrayNotHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
+       }
+
+       /**
+        * Attributes under manifest_version 1
+        *
+        * @covers ExtensionProcessor::extractInfo
+        */
+       public function testAttributes1() {
+               $processor = new ExtensionProcessor();
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'name' => 'FooBar',
+                               'FooBarPlugins' => [
+                                       'ext.baz.foobar',
+                               ],
+                               'FizzBuzzMorePlugins' => [
+                                       'ext.baz.fizzbuzz',
+                               ],
+                       ],
+                       1
+               );
+
+               $info = $processor->getExtractedInfo();
+               $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
+               $this->assertArrayHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
+               $this->assertSame( [ 'ext.baz.fizzbuzz' ], $info['attributes']['FizzBuzzMorePlugins'] );
+       }
+
        public function testGlobalSettingsDocumentedInSchema() {
                global $IP;
                $globalSettings = TestingAccessWrapper::newFromClass(