Merge "Add meta=userinfo&uiprop=latestcontrib"
[lhc/web/wiklou.git] / includes / registration / ExtensionRegistry.php
index e462a0b..e3df499 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use Composer\Semver\Semver;
+use Wikimedia\ScopedCallback;
 
 /**
  * ExtensionRegistry class
@@ -76,6 +77,13 @@ class ExtensionRegistry {
         */
        protected $attributes = [];
 
+       /**
+        * Attributes for testing
+        *
+        * @var array
+        */
+       protected $testAttributes = [];
+
        /**
         * @var ExtensionRegistry
         */
@@ -366,7 +374,7 @@ class ExtensionRegistry {
                                }
                                throw new UnexpectedValueException( "callback '$cb' is not callable" );
                        }
-                       call_user_func( $cb, $info['credits'][$name] );
+                       $cb( $info['credits'][$name] );
                }
        }
 
@@ -412,7 +420,31 @@ class ExtensionRegistry {
         * @return array
         */
        public function getAttribute( $name ) {
-               return $this->attributes[$name] ?? [];
+               return $this->testAttributes[$name] ??
+                       $this->attributes[$name] ?? [];
+       }
+
+       /**
+        * Force override the value of an attribute during tests
+        *
+        * @param string $name Name of attribute to override
+        * @param array $value Value to set
+        * @return ScopedCallback to reset
+        * @since 1.33
+        */
+       public function setAttributeForTest( $name, array $value ) {
+               // @codeCoverageIgnoreStart
+               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+                       throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
+               }
+               // @codeCoverageIgnoreEnd
+               if ( isset( $this->testAttributes[$name] ) ) {
+                       throw new Exception( "The attribute '$name' has already been overridden" );
+               }
+               $this->testAttributes[$name] = $value;
+               return new ScopedCallback( function () use ( $name ) {
+                       unset( $this->testAttributes[$name] );
+               } );
        }
 
        /**