Complete ExtensionProcessor test coverage
[lhc/web/wiklou.git] / tests / phpunit / includes / registration / ExtensionProcessorTest.php
index 4282652..ef19b2f 100644 (file)
@@ -2,6 +2,9 @@
 
 use Wikimedia\TestingAccessWrapper;
 
+/**
+ * @covers ExtensionProcessor
+ */
 class ExtensionProcessorTest extends MediaWikiTestCase {
 
        private $dir, $dirname;
@@ -21,9 +24,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                'name' => 'FooBar',
        ];
 
-       /**
-        * @covers ExtensionProcessor::extractInfo
-        */
        public function testExtractInfo() {
                // Test that attributes that begin with @ are ignored
                $processor = new ExtensionProcessor();
@@ -31,6 +31,8 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                        '@metadata' => [ 'foobarbaz' ],
                        'AnAttribute' => [ 'omg' ],
                        'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
+                       'SpecialPages' => [ 'Foo' => 'SpecialFoo' ],
+                       'callback' => 'FooBar::onRegistration',
                ], 1 );
 
                $extracted = $processor->getExtractedInfo();
@@ -38,12 +40,17 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertArrayHasKey( 'AnAttribute', $attributes );
                $this->assertArrayNotHasKey( '@metadata', $attributes );
                $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
+               $this->assertSame(
+                       [ 'FooBar' => 'FooBar::onRegistration' ],
+                       $extracted['callbacks']
+               );
+               $this->assertSame(
+                       [ 'Foo' => 'SpecialFoo' ],
+                       $extracted['globals']['wgSpecialPages']
+               );
        }
 
-       /**
-        * @covers ExtensionProcessor::extractInfo
-        */
-       public function testExtractInfo_namespaces() {
+       public function testExtractNamespaces() {
                // 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 );
@@ -56,13 +63,20 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                                        'id' => 332200,
                                        'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
                                        'name' => 'Test_A',
-                                       'content' => 'TestModel'
+                                       'defaultcontentmodel' => 'TestModel',
+                                       'gender' => [
+                                               'male' => 'Male test',
+                                               'female' => 'Female test',
+                                       ],
+                                       'subpages' => true,
+                                       'content' => true,
+                                       'protection' => 'userright',
                                ],
                                [ // Test_X will use ID 123456 not 334400
                                        'id' => 334400,
                                        'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
                                        'name' => 'Test_X',
-                                       'content' => 'TestModel'
+                                       'defaultcontentmodel' => 'TestModel'
                                ],
                        ]
                ], 1 );
@@ -90,6 +104,13 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
 
                $this->assertSame( 'Test_X', $extracted['attributes']['ExtensionNamespaces'][123456] );
                $this->assertSame( 'Test_A', $extracted['attributes']['ExtensionNamespaces'][332200] );
+               $this->assertSame(
+                       [ 'male' => 'Male test', 'female' => 'Female test' ],
+                       $extracted['globals']['wgExtraGenderNamespaces'][332200]
+               );
+               // A has subpages, X does not
+               $this->assertTrue( $extracted['globals']['wgNamespacesWithSubpages'][332200] );
+               $this->assertArrayNotHasKey( 123456, $extracted['globals']['wgNamespacesWithSubpages'] );
        }
 
        public static function provideRegisterHooks() {
@@ -152,7 +173,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ExtensionProcessor::extractHooks
         * @dataProvider provideRegisterHooks
         */
        public function testRegisterHooks( $pre, $info, $expected ) {
@@ -162,9 +182,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
        }
 
-       /**
-        * @covers ExtensionProcessor::extractConfig1
-        */
        public function testExtractConfig1() {
                $processor = new ExtensionProcessor;
                $info = [
@@ -191,9 +208,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
        }
 
-       /**
-        * @covers ExtensionProcessor::extractConfig2
-        */
        public function testExtractConfig2() {
                $processor = new ExtensionProcessor;
                $info = [
@@ -201,6 +215,13 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                                'Bar' => [ 'value' => 'somevalue' ],
                                'Foo' => [ 'value' => 10 ],
                                'Path' => [ 'value' => 'foo.txt', 'path' => true ],
+                               'Namespaces' => [
+                                       'value' => [
+                                               '10' => true,
+                                               '12' => false,
+                                       ],
+                                       'merge_strategy' => 'array_plus',
+                               ],
                        ],
                ] + self::$default;
                $info2 = [
@@ -218,10 +239,13 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                $this->assertEquals( "{$this->dirname}/foo.txt", $extracted['globals']['wgPath'] );
                // Custom prefix:
                $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
+               $this->assertSame(
+                       [ 10 => true, 12 => false, ExtensionRegistry::MERGE_STRATEGY => 'array_plus' ],
+                       $extracted['globals']['wgNamespaces']
+               );
        }
 
        /**
-        * @covers ExtensionProcessor::addConfigGlobal()
         * @expectedException RuntimeException
         */
        public function testDuplicateConfigKey1() {
@@ -242,7 +266,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ExtensionProcessor::addConfigGlobal()
         * @expectedException RuntimeException
         */
        public function testDuplicateConfigKey2() {
@@ -287,7 +310,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ExtensionProcessor::extractExtensionMessagesFiles
         * @dataProvider provideExtractExtensionMessagesFiles
         */
        public function testExtractExtensionMessagesFiles( $input, $expected ) {
@@ -314,7 +336,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ExtensionProcessor::extractMessagesDirs
         * @dataProvider provideExtractMessagesDirs
         */
        public function testExtractMessagesDirs( $input, $expected ) {
@@ -326,9 +347,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                }
        }
 
-       /**
-        * @covers ExtensionProcessor::extractCredits
-        */
        public function testExtractCredits() {
                $processor = new ExtensionProcessor();
                $processor->extractInfo( $this->dir, self::$default, 1 );
@@ -337,7 +355,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers ExtensionProcessor::extractResourceLoaderModules
         * @dataProvider provideExtractResourceLoaderModules
         */
        public function testExtractResourceLoaderModules( $input, $expected ) {
@@ -380,8 +397,8 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                                // Input
                                [
                                        'ResourceFileModulePaths' => [
-                                               'localBasePath' => '',
-                                               'remoteExtPath' => 'FooBar',
+                                               'localBasePath' => 'modules',
+                                               'remoteExtPath' => 'FooBar/modules',
                                        ],
                                        'ResourceModules' => [
                                                // No paths
@@ -412,8 +429,8 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                                        'wgResourceModules' => [
                                                'test.foo' => [
                                                        'styles' => 'foo.js',
-                                                       'localBasePath' => $dir,
-                                                       'remoteExtPath' => 'FooBar',
+                                                       'localBasePath' => "$dir/modules",
+                                                       'remoteExtPath' => 'FooBar/modules',
                                                ],
                                                'test.bar' => [
                                                        'styles' => 'bar.js',
@@ -423,14 +440,14 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                                                'test.class' => [
                                                        'class' => 'FooBarModule',
                                                        'extra' => 'argument',
-                                                       'localBasePath' => $dir,
-                                                       'remoteExtPath' => 'FooBar',
+                                                       'localBasePath' => "$dir/modules",
+                                                       'remoteExtPath' => 'FooBar/modules',
                                                ],
                                                'test.class.with.path' => [
                                                        'class' => 'FooBarPathModule',
                                                        'extra' => 'argument',
                                                        'localBasePath' => $dir,
-                                                       'remoteExtPath' => 'FooBar',
+                                                       'remoteExtPath' => 'FooBar/modules',
                                                ]
                                        ],
                                ],
@@ -543,9 +560,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
 
        /**
         * Attributes under manifest_version 2
-        *
-        * @covers ExtensionProcessor::extractAttributes
-        * @covers ExtensionProcessor::getExtractedInfo
         */
        public function testExtractAttributes() {
                $processor = new ExtensionProcessor();
@@ -581,8 +595,6 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
 
        /**
         * Attributes under manifest_version 1
-        *
-        * @covers ExtensionProcessor::extractInfo
         */
        public function testAttributes1() {
                $processor = new ExtensionProcessor();
@@ -599,12 +611,97 @@ class ExtensionProcessorTest extends MediaWikiTestCase {
                        ],
                        1
                );
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'name' => 'FooBar2',
+                               'FizzBuzzMorePlugins' => [
+                                       'ext.bar.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'] );
+               $this->assertSame(
+                       [ 'ext.baz.fizzbuzz', 'ext.bar.fizzbuzz' ],
+                       $info['attributes']['FizzBuzzMorePlugins']
+               );
+       }
+
+       public function testAttributes1_notarray() {
+               $processor = new ExtensionProcessor();
+               $this->setExpectedException(
+                       InvalidArgumentException::class,
+                       "The value for 'FooBarPlugins' should be an array (from {$this->dir})"
+               );
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'FooBarPlugins' => 'ext.baz.foobar',
+                       ] + self::$default,
+                       1
+               );
+       }
+
+       public function testExtractSimplePathBased() {
+               $processor = new ExtensionProcessor();
+               $processor->extractInfo(
+                       $this->dir,
+                       [
+                               'ParserTestFiles' => [
+                                       'tests/parserTests.txt',
+                                       'tests/extraParserTests.txt',
+                               ],
+                               'ServiceWiringFiles' => [
+                                       'includes/ServiceWiring.php'
+                               ],
+                       ] + self::$default,
+                       1
+               );
+               $globals = $processor->getExtractedInfo()['globals'];
+               $this->assertArrayHasKey( 'wgParserTestFiles', $globals );
+               $this->assertSame( [
+                       "{$this->dirname}/tests/parserTests.txt",
+                       "{$this->dirname}/tests/extraParserTests.txt"
+               ], $globals['wgParserTestFiles'] );
+               $this->assertArrayHasKey( 'wgServiceWiringFiles', $globals );
+               $this->assertSame( [
+                       "{$this->dirname}/includes/ServiceWiring.php"
+               ], $globals['wgServiceWiringFiles'] );
+       }
+
+       public function testGetRequirements() {
+               $info = self::$default + [
+                       'requires' => [
+                               'MediaWiki' => '>= 1.25.0',
+                               'extensions' => [
+                                       'Bar' => '*'
+                               ]
+                       ]
+               ];
+               $processor = new ExtensionProcessor();
+               $this->assertSame(
+                       $info['requires'],
+                       $processor->getRequirements( $info )
+               );
+               $this->assertSame(
+                       [],
+                       $processor->getRequirements( [] )
+               );
+       }
+
+       public function testGetExtraAutoloaderPaths() {
+               $processor = new ExtensionProcessor();
+               $this->assertSame(
+                       [ "{$this->dirname}/vendor/autoload.php" ],
+                       $processor->getExtraAutoloaderPaths( $this->dirname, [
+                               'load_composer_autoloader' => true,
+                       ] )
+               );
        }
 
        /**