From 2d9f29a84600af691e19eaedd01980dcc7b6a2b7 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 11 Sep 2017 16:37:18 -0400 Subject: [PATCH] Improve namespace handling in tests 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 | 62 +++++++++++++------ tests/parser/ParserTestRunner.php | 2 +- tests/phpunit/includes/EditPageTest.php | 10 ++- tests/phpunit/includes/PagePropsTest.php | 4 +- tests/phpunit/includes/PrefixSearchTest.php | 5 +- .../phpunit/includes/RevisionStorageTest.php | 4 +- tests/phpunit/includes/RevisionTest.php | 4 +- tests/phpunit/includes/TitleMethodsTest.php | 4 +- tests/phpunit/includes/XmlTest.php | 5 ++ .../phpunit/includes/api/ApiEditPageTest.php | 8 ++- .../includes/content/ContentHandlerTest.php | 4 +- 11 files changed, 78 insertions(+), 34 deletions(-) diff --git a/includes/MWNamespace.php b/includes/MWNamespace.php index 97dba26b95..f2f98ba29c 100644 --- a/includes/MWNamespace.php +++ b/includes/MWNamespace.php @@ -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; } /** diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index 5fe2177d11..0121d53e63 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -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; diff --git a/tests/phpunit/includes/EditPageTest.php b/tests/phpunit/includes/EditPageTest.php index 9507811ec2..7cf24bfa9a 100644 --- a/tests/phpunit/includes/EditPageTest.php +++ b/tests/phpunit/includes/EditPageTest.php @@ -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 diff --git a/tests/phpunit/includes/PagePropsTest.php b/tests/phpunit/includes/PagePropsTest.php index 29c9e228f2..89fd6e032f 100644 --- a/tests/phpunit/includes/PagePropsTest.php +++ b/tests/phpunit/includes/PagePropsTest.php @@ -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 } diff --git a/tests/phpunit/includes/PrefixSearchTest.php b/tests/phpunit/includes/PrefixSearchTest.php index a6cf14a3ad..2f3e569613 100644 --- a/tests/phpunit/includes/PrefixSearchTest.php +++ b/tests/phpunit/includes/PrefixSearchTest.php @@ -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; diff --git a/tests/phpunit/includes/RevisionStorageTest.php b/tests/phpunit/includes/RevisionStorageTest.php index e9f16dbd5c..8ed85fe550 100644 --- a/tests/phpunit/includes/RevisionStorageTest.php +++ b/tests/phpunit/includes/RevisionStorageTest.php @@ -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 } diff --git a/tests/phpunit/includes/RevisionTest.php b/tests/phpunit/includes/RevisionTest.php index c971a40cc9..386f219b4f 100644 --- a/tests/phpunit/includes/RevisionTest.php +++ b/tests/phpunit/includes/RevisionTest.php @@ -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(); diff --git a/tests/phpunit/includes/TitleMethodsTest.php b/tests/phpunit/includes/TitleMethodsTest.php index d9c01cb9f1..b760c22489 100644 --- a/tests/phpunit/includes/TitleMethodsTest.php +++ b/tests/phpunit/includes/TitleMethodsTest.php @@ -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 } diff --git a/tests/phpunit/includes/XmlTest.php b/tests/phpunit/includes/XmlTest.php index 6c059ec5fd..43112b61ac 100644 --- a/tests/phpunit/includes/XmlTest.php +++ b/tests/phpunit/includes/XmlTest.php @@ -34,6 +34,11 @@ class XmlTest extends MediaWikiTestCase { ] ); } + protected function tearDown() { + Language::factory( 'en' )->resetNamespaces(); + parent::tearDown(); + } + /** * @covers Xml::expandAttributes */ diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index e091153281..c82edf0399 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -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(); } diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index ee79ffa40f..2422e792cf 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -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' ); -- 2.20.1