Improve namespace handling in tests
authorBrad Jorsch <bjorsch@wikimedia.org>
Mon, 11 Sep 2017 20:37:18 +0000 (16:37 -0400)
committerLegoktm <legoktm@member.fsf.org>
Fri, 29 Sep 2017 05:21:47 +0000 (05:21 +0000)
MWNamespace has three internal caches, only one of which can be cleared
(and that somewhat oddly by passing a boolean to
MWNamespace::getCanonicalNamespaces()).

This change introduces a MWNamespace::clearCaches() method to clear all
three caches. It also adds some resetting in tests that seemed to be
missing it.

Change-Id: I1dcfcd8713888b3ff8fc75e95329ba72bd95d0c9

includes/MWNamespace.php
tests/parser/ParserTestRunner.php
tests/phpunit/includes/EditPageTest.php
tests/phpunit/includes/PagePropsTest.php
tests/phpunit/includes/PrefixSearchTest.php
tests/phpunit/includes/RevisionStorageTest.php
tests/phpunit/includes/RevisionTest.php
tests/phpunit/includes/TitleMethodsTest.php
tests/phpunit/includes/XmlTest.php
tests/phpunit/includes/api/ApiEditPageTest.php
tests/phpunit/includes/content/ContentHandlerTest.php

index 97dba26..f2f98ba 100644 (file)
@@ -38,6 +38,15 @@ class MWNamespace {
         */
        private static $alwaysCapitalizedNamespaces = [ NS_SPECIAL, NS_USER, NS_MEDIAWIKI ];
 
+       /** @var string[]|null Canonical namespaces cache */
+       private static $canonicalNamespaces = null;
+
+       /** @var array|false Canonical namespaces index cache */
+       private static $namespaceIndexes = false;
+
+       /** @var int[]|null Valid namespaces cache */
+       private static $validNamespaces = null;
+
        /**
         * Throw an exception when trying to get the subject or talk page
         * for a given namespace where it does not make sense.
@@ -57,6 +66,19 @@ class MWNamespace {
                return true;
        }
 
+       /**
+        * Clear internal caches
+        *
+        * For use in unit testing when namespace configuration is changed.
+        *
+        * @since 1.31
+        */
+       public static function clearCaches() {
+               self::$canonicalNamespaces = null;
+               self::$namespaceIndexes = false;
+               self::$validNamespaces = null;
+       }
+
        /**
         * Can pages in the given namespace be moved?
         *
@@ -200,23 +222,28 @@ class MWNamespace {
         * (English) names.
         *
         * @param bool $rebuild Rebuild namespace list (default = false). Used for testing.
+        *  Deprecated since 1.31, use self::clearCaches() instead.
         *
         * @return array
         * @since 1.17
         */
        public static function getCanonicalNamespaces( $rebuild = false ) {
-               static $namespaces = null;
-               if ( $namespaces === null || $rebuild ) {
+               if ( $rebuild ) {
+                       self::clearCaches();
+               }
+
+               if ( self::$canonicalNamespaces === null ) {
                        global $wgExtraNamespaces, $wgCanonicalNamespaceNames;
-                       $namespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
+                       self::$canonicalNamespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames;
                        // Add extension namespaces
-                       $namespaces += ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
+                       self::$canonicalNamespaces +=
+                               ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' );
                        if ( is_array( $wgExtraNamespaces ) ) {
-                               $namespaces += $wgExtraNamespaces;
+                               self::$canonicalNamespaces += $wgExtraNamespaces;
                        }
-                       Hooks::run( 'CanonicalNamespaces', [ &$namespaces ] );
+                       Hooks::run( 'CanonicalNamespaces', [ &self::$canonicalNamespaces ] );
                }
-               return $namespaces;
+               return self::$canonicalNamespaces;
        }
 
        /**
@@ -242,15 +269,14 @@ class MWNamespace {
         * @return int
         */
        public static function getCanonicalIndex( $name ) {
-               static $xNamespaces = false;
-               if ( $xNamespaces === false ) {
-                       $xNamespaces = [];
+               if ( self::$namespaceIndexes === false ) {
+                       self::$namespaceIndexes = [];
                        foreach ( self::getCanonicalNamespaces() as $i => $text ) {
-                               $xNamespaces[strtolower( $text )] = $i;
+                               self::$namespaceIndexes[strtolower( $text )] = $i;
                        }
                }
-               if ( array_key_exists( $name, $xNamespaces ) ) {
-                       return $xNamespaces[$name];
+               if ( array_key_exists( $name, self::$namespaceIndexes ) ) {
+                       return self::$namespaceIndexes[$name];
                } else {
                        return null;
                }
@@ -262,19 +288,17 @@ class MWNamespace {
         * @return array
         */
        public static function getValidNamespaces() {
-               static $mValidNamespaces = null;
-
-               if ( is_null( $mValidNamespaces ) ) {
+               if ( is_null( self::$validNamespaces ) ) {
                        foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) {
                                if ( $ns >= 0 ) {
-                                       $mValidNamespaces[] = $ns;
+                                       self::$validNamespaces[] = $ns;
                                }
                        }
                        // T109137: sort numerically
-                       sort( $mValidNamespaces, SORT_NUMERIC );
+                       sort( self::$validNamespaces, SORT_NUMERIC );
                }
 
-               return $mValidNamespaces;
+               return self::$validNamespaces;
        }
 
        /**
index 5fe2177..0121d53 100644 (file)
@@ -384,7 +384,7 @@ class ParserTestRunner {
                // Changing wgExtraNamespaces invalidates caches in MWNamespace and
                // any live Language object, both on setup and teardown
                $reset = function () {
-                       MWNamespace::getCanonicalNamespaces( true );
+                       MWNamespace::clearCaches();
                        $GLOBALS['wgContLang']->resetNamespaces();
                };
                $setup[] = $reset;
index 9507811..7cf24bf 100644 (file)
@@ -29,10 +29,18 @@ class EditPageTest extends MediaWikiLangTestCase {
                $wgNamespaceContentModels[12312] = "testing";
                $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
+       protected function tearDown() {
+               global $wgContLang;
+
+               MWNamespace::clearCaches();
+               $wgContLang->resetNamespaces(); # reset namespace cache
+               parent::tearDown();
+       }
+
        /**
         * @dataProvider provideExtractSectionTitle
         * @covers EditPage::extractSectionTitle
index 29c9e22..89fd6e0 100644 (file)
@@ -35,7 +35,7 @@ class TestPageProps extends MediaWikiLangTestCase {
                $wgNamespaceContentModels[12312] = 'DUMMY';
                $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
 
                if ( !$this->the_properties ) {
@@ -81,7 +81,7 @@ class TestPageProps extends MediaWikiLangTestCase {
                unset( $wgNamespaceContentModels[12312] );
                unset( $wgContentHandlers['DUMMY'] );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
index a6cf14a..2f3e569 100644 (file)
@@ -62,13 +62,16 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
 
                // Clear caches so that our new namespace appears
-               MWNamespace::getCanonicalNamespaces( true );
+               MWNamespace::clearCaches();
                Language::factory( 'en' )->resetNamespaces();
 
                SpecialPageFactory::resetList();
        }
 
        public function tearDown() {
+               MWNamespace::clearCaches();
+               Language::factory( 'en' )->resetNamespaces();
+
                parent::tearDown();
 
                TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = $this->originalHandlers;
index e9f16db..8ed85fe 100644 (file)
@@ -49,7 +49,7 @@ class RevisionStorageTest extends MediaWikiTestCase {
                $wgNamespaceContentModels[12312] = 'DUMMY';
                $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
                if ( !$this->the_page ) {
                        $this->the_page = $this->createPage(
@@ -73,7 +73,7 @@ class RevisionStorageTest extends MediaWikiTestCase {
                unset( $wgNamespaceContentModels[12312] );
                unset( $wgContentHandlers['DUMMY'] );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
index c971a40..386f219 100644 (file)
@@ -41,14 +41,14 @@ class RevisionTest extends MediaWikiTestCase {
                        ]
                );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
        function tearDown() {
                global $wgContLang;
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
 
                parent::tearDown();
index d9c01cb..b760c22 100644 (file)
@@ -29,7 +29,7 @@ class TitleMethodsTest extends MediaWikiLangTestCase {
                        ]
                );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
@@ -38,7 +38,7 @@ class TitleMethodsTest extends MediaWikiLangTestCase {
 
                parent::tearDown();
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
        }
 
index 6c059ec..43112b6 100644 (file)
@@ -34,6 +34,11 @@ class XmlTest extends MediaWikiTestCase {
                ] );
        }
 
+       protected function tearDown() {
+               Language::factory( 'en' )->resetNamespaces();
+               parent::tearDown();
+       }
+
        /**
         * @covers Xml::expandAttributes
         */
index e091153..c82edf0 100644 (file)
@@ -36,14 +36,18 @@ class ApiEditPageTest extends ApiTestCase {
                $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
                $wgContentHandlers["testing-nontext"] = 'DummyNonTextContentHandler';
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces(); # reset namespace cache
 
                $this->doLogin();
        }
 
        protected function tearDown() {
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               global $wgContLang;
+
+               MWNamespace::clearCaches();
+               $wgContLang->resetNamespaces(); # reset namespace cache
+
                parent::tearDown();
        }
 
index ee79ffa..2422e79 100644 (file)
@@ -35,7 +35,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
                ] );
 
                // Reset namespace cache
-               MWNamespace::getCanonicalNamespaces( true );
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces();
                // And LinkCache
                MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
@@ -45,7 +45,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
                global $wgContLang;
 
                // Reset namespace cache
-               MWNamespace::getCanonicalNamespaces( true );
+               MWNamespace::clearCaches();
                $wgContLang->resetNamespaces();
                // And LinkCache
                MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );