Merge "Unit tests: Whitelist global so LoggerFactory doesn't explode"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 6 Sep 2019 15:27:14 +0000 (15:27 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 6 Sep 2019 15:27:14 +0000 (15:27 +0000)
1  2 
tests/phpunit/MediaWikiUnitTestCase.php

@@@ -20,7 -20,6 +20,7 @@@
   */
  
  use PHPUnit\Framework\TestCase;
 +use PHPUnit\Framework\Exception;
  
  /**
   * Base class for unit tests.
@@@ -38,6 -37,25 +38,25 @@@ abstract class MediaWikiUnitTestCase ex
        private static $originalGlobals;
        private static $unitGlobals;
  
+       /**
+        * Whitelist of globals to allow in MediaWikiUnitTestCase.
+        *
+        * Please, keep this list to the bare minimum.
+        *
+        * @return string[]
+        */
+       private static function getGlobalsWhitelist() {
+               return [
+                       // The autoloader may change between bootstrap and the first test,
+                       // so (lazily) capture these here instead.
+                       'wgAutoloadClasses',
+                       'wgAutoloadLocalClasses',
+                       // Need for LoggerFactory. Default is NullSpi.
+                       'wgMWLoggerDefaultSpi',
+                       'wgAutoloadAttemptLowercase'
+               ];
+       }
        public static function setUpBeforeClass() {
                parent::setUpBeforeClass();
  
                }
  
                self::$unitGlobals =& TestSetup::$bootstrapGlobals;
-               // The autoloader may change between bootstrap and the first test,
-               // so (lazily) capture these here instead.
-               self::$unitGlobals['wgAutoloadClasses'] =& $GLOBALS['wgAutoloadClasses'];
-               self::$unitGlobals['wgAutoloadLocalClasses'] =& $GLOBALS['wgAutoloadLocalClasses'];
-               // This value should always be true.
-               self::$unitGlobals['wgAutoloadAttemptLowercase'] = true;
+               foreach ( self::getGlobalsWhitelist() as $global ) {
+                       self::$unitGlobals[ $global ] =& $GLOBALS[ $global ];
+               }
  
                // Would be nice if we coud simply replace $GLOBALS as a whole,
                // but unsetting or re-assigning that breaks the reference of this magic
                }
        }
  
 +      /**
 +       * @inheritDoc
 +       */
 +      protected function runTest() {
 +              try {
 +                      return parent::runTest();
 +              } catch ( ConfigException $exception ) {
 +                      throw new Exception(
 +                              'Config variables must be mocked, they cannot be accessed directly in tests which extend '
 +                              . self::class,
 +                              $exception->getCode(),
 +                              $exception
 +                      );
 +              }
 +      }
 +
        protected function tearDown() {
                if ( !defined( 'HHVM_VERSION' ) ) {
                        // Quick reset between tests