Merge "registration: Only allow one extension to set a specific config setting"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 22 Aug 2017 19:35:26 +0000 (19:35 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 22 Aug 2017 19:35:26 +0000 (19:35 +0000)
includes/registration/ExtensionProcessor.php
tests/phpunit/includes/registration/ExtensionProcessorTest.php

index ce262bd..14d8222 100644 (file)
@@ -450,7 +450,7 @@ class ExtensionProcessor implements Processor {
                        }
                        foreach ( $info['config'] as $key => $val ) {
                                if ( $key[0] !== '@' ) {
-                                       $this->globals["$prefix$key"] = $val;
+                                       $this->addConfigGlobal( "$prefix$key", $val );
                                }
                        }
                }
@@ -478,11 +478,26 @@ class ExtensionProcessor implements Processor {
                                if ( isset( $data['path'] ) && $data['path'] ) {
                                        $value = "$dir/$value";
                                }
-                               $this->globals["$prefix$key"] = $value;
+                               $this->addConfigGlobal( "$prefix$key", $value );
                        }
                }
        }
 
+       /**
+        * Helper function to set a value to a specific global, if it isn't set already.
+        *
+        * @param string $key The config key with the prefix and anything
+        * @param mixed $value The value of the config
+        */
+       private function addConfigGlobal( $key, $value ) {
+               if ( array_key_exists( $key, $this->globals ) ) {
+                       throw new RuntimeException(
+                               "The configuration setting '$key' was already set by another extension,"
+                               . " and cannot be set again." );
+               }
+               $this->globals[$key] = $value;
+       }
+
        protected function extractServiceWiringFiles( $dir, array $info ) {
                if ( isset( $info['ServiceWiringFiles'] ) ) {
                        foreach ( $info['ServiceWiringFiles'] as $path ) {
index 7b56def..5ef30e8 100644 (file)
@@ -220,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 [