Anomie has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/377496 )
Change subject: Improve namespace handling in tests ...................................................................... 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 --- M includes/MWNamespace.php M tests/parser/ParserTestRunner.php M tests/phpunit/includes/EditPageTest.php M tests/phpunit/includes/PagePropsTest.php M tests/phpunit/includes/PrefixSearchTest.php M tests/phpunit/includes/RevisionStorageTest.php M tests/phpunit/includes/RevisionTest.php M tests/phpunit/includes/TitleMethodsTest.php M tests/phpunit/includes/XmlTest.php M tests/phpunit/includes/api/ApiEditPageTest.php M tests/phpunit/includes/content/ContentHandlerTest.php 11 files changed, 80 insertions(+), 38 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/96/377496/1 diff --git a/includes/MWNamespace.php b/includes/MWNamespace.php index 97dba26..849a744 100644 --- a/includes/MWNamespace.php +++ b/includes/MWNamespace.php @@ -38,6 +38,15 @@ */ 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. @@ -55,6 +64,17 @@ throw new MWException( "$method does not make any sense for given namespace $index" ); } return true; + } + + /** + * Clear internal caches + * + * For use in unit testing when namespace configuration is changed. + */ + public static function clearCaches() { + self::$canonicalNamespaces = null; + self::$namespaceIndexes = false; + self::$validNamespaces = null; } /** @@ -200,23 +220,28 @@ * (English) names. * * @param bool $rebuild Rebuild namespace list (default = false). Used for testing. + * Deprecated since 1.30, use self::clearCaches() instead. * * @return array * @since 1.17 */ public static function getCanonicalNamespaces( $rebuild = false ) { - static $namespaces = null; - if ( $namespaces === null || $rebuild ) { - global $wgExtraNamespaces, $wgCanonicalNamespaceNames; - $namespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames; - // Add extension namespaces - $namespaces += ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' ); - if ( is_array( $wgExtraNamespaces ) ) { - $namespaces += $wgExtraNamespaces; - } - Hooks::run( 'CanonicalNamespaces', [ &$namespaces ] ); + if ( $rebuild ) { + self::clearCaches(); } - return $namespaces; + + if ( self::$canonicalNamespaces === null ) { + global $wgExtraNamespaces, $wgCanonicalNamespaceNames; + self::$canonicalNamespaces = [ NS_MAIN => '' ] + $wgCanonicalNamespaceNames; + // Add extension namespaces + self::$canonicalNamespaces += + ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' ); + if ( is_array( $wgExtraNamespaces ) ) { + self::$canonicalNamespaces += $wgExtraNamespaces; + } + Hooks::run( 'CanonicalNamespaces', [ &self::$canonicalNamespaces ] ); + } + return self::$canonicalNamespaces; } /** @@ -242,15 +267,14 @@ * @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 +286,17 @@ * @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 5fe2177..0121d53 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -384,7 +384,7 @@ // 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 9507811..7cf24bf 100644 --- a/tests/phpunit/includes/EditPageTest.php +++ b/tests/phpunit/includes/EditPageTest.php @@ -29,10 +29,18 @@ $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 29c9e22..89fd6e0 100644 --- a/tests/phpunit/includes/PagePropsTest.php +++ b/tests/phpunit/includes/PagePropsTest.php @@ -35,7 +35,7 @@ $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 @@ 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 a6cf14a..2f3e569 100644 --- a/tests/phpunit/includes/PrefixSearchTest.php +++ b/tests/phpunit/includes/PrefixSearchTest.php @@ -62,13 +62,16 @@ 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 e9f16db..8ed85fe 100644 --- a/tests/phpunit/includes/RevisionStorageTest.php +++ b/tests/phpunit/includes/RevisionStorageTest.php @@ -49,7 +49,7 @@ $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 @@ 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 c971a40..386f219 100644 --- a/tests/phpunit/includes/RevisionTest.php +++ b/tests/phpunit/includes/RevisionTest.php @@ -41,14 +41,14 @@ ] ); - 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 d9c01cb..b760c22 100644 --- a/tests/phpunit/includes/TitleMethodsTest.php +++ b/tests/phpunit/includes/TitleMethodsTest.php @@ -29,7 +29,7 @@ ] ); - MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache + MWNamespace::clearCaches(); $wgContLang->resetNamespaces(); # reset namespace cache } @@ -38,7 +38,7 @@ 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 6c059ec..43112b61 100644 --- a/tests/phpunit/includes/XmlTest.php +++ b/tests/phpunit/includes/XmlTest.php @@ -34,6 +34,11 @@ ] ); } + 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 e091153..c82edf0 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -36,14 +36,18 @@ $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 ee79ffa..2422e79 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -35,7 +35,7 @@ ] ); // Reset namespace cache - MWNamespace::getCanonicalNamespaces( true ); + MWNamespace::clearCaches(); $wgContLang->resetNamespaces(); // And LinkCache MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' ); @@ -45,7 +45,7 @@ global $wgContLang; // Reset namespace cache - MWNamespace::getCanonicalNamespaces( true ); + MWNamespace::clearCaches(); $wgContLang->resetNamespaces(); // And LinkCache MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' ); -- To view, visit https://gerrit.wikimedia.org/r/377496 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1dcfcd8713888b3ff8fc75e95329ba72bd95d0c9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Anomie <bjor...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits