diff options
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | 2023-07-17 04:38:34 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@wikimedia.org> | 2023-07-17 04:38:34 +0000 |
commit | b34939c6e478a487e44b007e92c7ce281803820c (patch) | |
tree | b70addd2738fccc7e207b9d4ed005f0393a1974d | |
parent | d00687831f2781398c51220613b6417584f69ae2 (diff) | |
parent | 0e55aa602a28d26bb9fd7fb951d3a6781ed1ca99 (diff) | |
download | mediawikicore-b34939c6e478a487e44b007e92c7ce281803820c.tar.gz mediawikicore-b34939c6e478a487e44b007e92c7ce281803820c.zip |
Merge "Inject extension namespaces into NamespaceInfo"
-rw-r--r-- | includes/ServiceWiring.php | 4 | ||||
-rw-r--r-- | includes/title/NamespaceInfo.php | 25 | ||||
-rw-r--r-- | tests/phpunit/includes/language/LanguageIntegrationTest.php | 4 | ||||
-rw-r--r-- | tests/phpunit/includes/title/NamespaceInfoTest.php | 52 | ||||
-rw-r--r-- | tests/phpunit/mocks/DummyServicesTrait.php | 11 | ||||
-rw-r--r-- | tests/phpunit/unit/includes/page/DeletePageTest.php | 2 | ||||
-rw-r--r-- | tests/phpunit/unit/includes/page/UndeletePageTest.php | 2 |
7 files changed, 39 insertions, 61 deletions
diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 127b654f0617..9b90418b940e 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -1235,7 +1235,9 @@ return [ 'NamespaceInfo' => static function ( MediaWikiServices $services ): NamespaceInfo { return new NamespaceInfo( new ServiceOptions( NamespaceInfo::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ), - $services->getHookContainer() + $services->getHookContainer(), + ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' ), + ExtensionRegistry::getInstance()->getAttribute( 'ImmovableNamespaces' ) ); }, diff --git a/includes/title/NamespaceInfo.php b/includes/title/NamespaceInfo.php index 14162f86137c..d7046b513a9b 100644 --- a/includes/title/NamespaceInfo.php +++ b/includes/title/NamespaceInfo.php @@ -56,6 +56,10 @@ class NamespaceInfo { /** @var HookRunner */ private $hookRunner; + private array $extensionNamespaces; + + private array $extensionImmovableNamespaces; + /** * Definitions of the NS_ constants are in Defines.php * @@ -100,11 +104,20 @@ class NamespaceInfo { /** * @param ServiceOptions $options * @param HookContainer $hookContainer - */ - public function __construct( ServiceOptions $options, HookContainer $hookContainer ) { + * @param array $extensionNamespaces + * @param array $extensionImmovableNamespaces + */ + public function __construct( + ServiceOptions $options, + HookContainer $hookContainer, + array $extensionNamespaces, + array $extensionImmovableNamespaces + ) { $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); $this->options = $options; $this->hookRunner = new HookRunner( $hookContainer ); + $this->extensionNamespaces = $extensionNamespaces; + $this->extensionImmovableNamespaces = $extensionImmovableNamespaces; } /** @@ -158,10 +171,7 @@ class NamespaceInfo { * @return bool */ public function isMovable( $index ) { - $extensionRegistry = ExtensionRegistry::getInstance(); - $extNamespaces = $extensionRegistry->getAttribute( 'ImmovableNamespaces' ); - - $result = $index >= NS_MAIN && !in_array( $index, $extNamespaces ); + $result = $index >= NS_MAIN && !in_array( $index, $this->extensionImmovableNamespaces ); /** * @since 1.20 @@ -375,8 +385,7 @@ class NamespaceInfo { if ( $this->canonicalNamespaces === null ) { $this->canonicalNamespaces = [ NS_MAIN => '' ] + $this->options->get( MainConfigNames::CanonicalNamespaceNames ); - $this->canonicalNamespaces += - ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' ); + $this->canonicalNamespaces += $this->extensionNamespaces; if ( is_array( $this->options->get( MainConfigNames::ExtraNamespaces ) ) ) { $this->canonicalNamespaces += $this->options->get( MainConfigNames::ExtraNamespaces ); } diff --git a/tests/phpunit/includes/language/LanguageIntegrationTest.php b/tests/phpunit/includes/language/LanguageIntegrationTest.php index c6113d710cc0..18ac8c6f136b 100644 --- a/tests/phpunit/includes/language/LanguageIntegrationTest.php +++ b/tests/phpunit/includes/language/LanguageIntegrationTest.php @@ -1960,7 +1960,9 @@ class LanguageIntegrationTest extends LanguageClassesTestCase { ]; $nsInfo = new NamespaceInfo( new ServiceOptions( NamespaceInfo::CONSTRUCTOR_OPTIONS, $config, $services->getMainConfig() ), - $services->getHookContainer() + $services->getHookContainer(), + ExtensionRegistry::getInstance()->getAttribute( 'ExtensionNamespaces' ), + ExtensionRegistry::getInstance()->getAttribute( 'ImmovableNamespaces' ) ); /** @var Language $lang */ $lang = new $langClass( diff --git a/tests/phpunit/includes/title/NamespaceInfoTest.php b/tests/phpunit/includes/title/NamespaceInfoTest.php index 8ddefe221f6f..f96dd760f4aa 100644 --- a/tests/phpunit/includes/title/NamespaceInfoTest.php +++ b/tests/phpunit/includes/title/NamespaceInfoTest.php @@ -9,32 +9,17 @@ use MediaWiki\Config\ServiceOptions; use MediaWiki\HookContainer\HookContainer; use MediaWiki\Linker\LinkTarget; use MediaWiki\Title\Title; -use Wikimedia\ScopedCallback; class NamespaceInfoTest extends MediaWikiIntegrationTestCase { use TestAllServiceOptionsUsed; + private const TEST_EXT_NAMESPACES = [ NS_MAIN => 'No effect', NS_TALK => 'No effect', 12345 => 'Extended' ]; + /********************************************************************************************** * Shared code * %{ */ - /** @var ScopedCallback */ - private $scopedCallback; - - protected function setUp(): void { - parent::setUp(); - - $this->scopedCallback = - ExtensionRegistry::getInstance()->setAttributeForTest( 'ExtensionNamespaces', [] ); - } - - protected function tearDown(): void { - $this->scopedCallback = null; - - parent::tearDown(); - } - private const DEFAULT_OPTIONS = [ 'CanonicalNamespaceNames' => [ NS_TALK => 'Talk', @@ -64,14 +49,16 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { return $this->getServiceContainer()->getHookContainer(); } - private function newObj( array $options = [] ): NamespaceInfo { + private function newObj( array $options = [], array $extensionNamespaces = [] ): NamespaceInfo { return new NamespaceInfo( new LoggedServiceOptions( self::$serviceOptionsAccessLog, NamespaceInfo::CONSTRUCTOR_OPTIONS, $options, self::DEFAULT_OPTIONS ), - $this->getHookContainer() + $this->getHookContainer(), + $extensionNamespaces, + [] ); } @@ -93,7 +80,7 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { $this->expectException( \Wikimedia\Assert\PreconditionException::class ); $this->expectExceptionMessage( $expectedExceptionText ); } - new NamespaceInfo( $options, $this->getHookContainer() ); + new NamespaceInfo( $options, $this->getHookContainer(), [], [] ); $this->assertTrue( true ); } @@ -945,23 +932,14 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { // Test extension namespaces // %{ - private function setupExtensionNamespaces() { - $this->scopedCallback = null; - $this->scopedCallback = ExtensionRegistry::getInstance()->setAttributeForTest( - 'ExtensionNamespaces', - [ NS_MAIN => 'No effect', NS_TALK => 'No effect', 12345 => 'Extended' ] - ); - } /** * @covers NamespaceInfo::getCanonicalNamespaces */ public function testGetCanonicalNamespaces_ExtensionNamespaces() { - $this->setupExtensionNamespaces(); - $this->assertSame( $this->getDefaultNamespaces() + [ 12345 => 'Extended' ], - $this->newObj()->getCanonicalNamespaces() + $this->newObj( [], self::TEST_EXT_NAMESPACES )->getCanonicalNamespaces() ); } @@ -969,8 +947,7 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { * @covers NamespaceInfo::getCanonicalName */ public function testGetCanonicalName_ExtensionNamespaces() { - $this->setupExtensionNamespaces(); - $obj = $this->newObj(); + $obj = $this->newObj( [], self::TEST_EXT_NAMESPACES ); $this->assertSame( '', $obj->getCanonicalName( NS_MAIN ) ); $this->assertSame( 'Talk', $obj->getCanonicalName( NS_TALK ) ); @@ -981,8 +958,7 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { * @covers NamespaceInfo::getCanonicalIndex */ public function testGetCanonicalIndex_ExtensionNamespaces() { - $this->setupExtensionNamespaces(); - $obj = $this->newObj(); + $obj = $this->newObj( [], self::TEST_EXT_NAMESPACES ); $this->assertSame( NS_MAIN, $obj->getCanonicalIndex( '' ) ); $this->assertSame( NS_TALK, $obj->getCanonicalIndex( 'talk' ) ); @@ -993,11 +969,9 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { * @covers NamespaceInfo::getValidNamespaces */ public function testGetValidNamespaces_ExtensionNamespaces() { - $this->setupExtensionNamespaces(); - $this->assertSame( [ NS_MAIN, NS_TALK, NS_USER, NS_USER_TALK, 12345 ], - $this->newObj()->getValidNamespaces() + $this->newObj( [], self::TEST_EXT_NAMESPACES )->getValidNamespaces() ); } @@ -1138,7 +1112,6 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { $obj->getCanonicalNamespaces(); // Now try to alter them through nefarious means - $this->setupExtensionNamespaces(); $this->setupHookNamespaces(); // Should have no effect @@ -1155,7 +1128,6 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { $obj->getCanonicalName( NS_MAIN ); // Now try to alter them through nefarious means - $this->setupExtensionNamespaces(); $this->setupHookNamespaces(); // Should have no effect @@ -1175,7 +1147,6 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { $obj->getCanonicalIndex( '' ); // Now try to alter them through nefarious means - $this->setupExtensionNamespaces(); $this->setupHookNamespaces(); // Should have no effect @@ -1195,7 +1166,6 @@ class NamespaceInfoTest extends MediaWikiIntegrationTestCase { $obj->getValidNamespaces(); // Now try to alter through nefarious means - $this->setupExtensionNamespaces(); $this->setupHookNamespaces(); // Should have no effect diff --git a/tests/phpunit/mocks/DummyServicesTrait.php b/tests/phpunit/mocks/DummyServicesTrait.php index 41a023b3a7aa..dbae20b4deda 100644 --- a/tests/phpunit/mocks/DummyServicesTrait.php +++ b/tests/phpunit/mocks/DummyServicesTrait.php @@ -388,13 +388,6 @@ trait DummyServicesTrait { * @return NamespaceInfo */ private function getDummyNamespaceInfo( array $options = [] ): NamespaceInfo { - // Rather than trying to use a complicated mock, it turns out that almost - // all of the NamespaceInfo service works fine in unit tests. The only issue: - // - in two places, NamespaceInfo tries to read extension attributes through - // ExtensionRegistry::getInstance()->getAttribute() - this should work fine - // in unit tests, it just won't include any extension info since those are - // not loaded - // configuration is based on the defaults in MainConfigSchema $serviceOptions = new ServiceOptions( NamespaceInfo::CONSTRUCTOR_OPTIONS, @@ -403,7 +396,9 @@ trait DummyServicesTrait { ); return new NamespaceInfo( $serviceOptions, - $options['hookContainer'] ?? $this->createHookContainer() + $options['hookContainer'] ?? $this->createHookContainer(), + [], + [] ); } diff --git a/tests/phpunit/unit/includes/page/DeletePageTest.php b/tests/phpunit/unit/includes/page/DeletePageTest.php index 9a48e746d92b..d169266a179f 100644 --- a/tests/phpunit/unit/includes/page/DeletePageTest.php +++ b/tests/phpunit/unit/includes/page/DeletePageTest.php @@ -292,7 +292,7 @@ class DeletePageTest extends MediaWikiUnitTestCase { ); return $wpFactory; }; - $nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer() ); + $nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer(), [], [] ); $talkPage = new PageIdentityValue( 42, NS_TALK, 'Test talk page', PageIdentity::LOCAL ); yield 'Talk page' => [ $talkPage, $getWpFactory( false ), $nsInfo, 'delete-error-associated-alreadytalk' ]; diff --git a/tests/phpunit/unit/includes/page/UndeletePageTest.php b/tests/phpunit/unit/includes/page/UndeletePageTest.php index a76c1018ba31..d3e5aaf7b2f0 100644 --- a/tests/phpunit/unit/includes/page/UndeletePageTest.php +++ b/tests/phpunit/unit/includes/page/UndeletePageTest.php @@ -111,7 +111,7 @@ class UndeletePageTest extends MediaWikiUnitTestCase { $ret->method( 'hasArchivedRevisions' )->willReturn( $hasDeletedRevs ); return $ret; }; - $nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer() ); + $nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer(), [], [] ); $talkPage = new PageIdentityValue( 42, NS_TALK, 'Test talk page', PageIdentity::LOCAL ); yield 'Talk page' => [ |