diff options
author | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-16 02:16:45 +0200 |
---|---|---|
committer | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-31 00:46:13 +0000 |
commit | 485e47ff102fbf172f6f906e17b6bfa578978fd9 (patch) | |
tree | c82b406f3bc5f2b143be8a27071597e104a54148 | |
parent | b03fc0147e005d492f5ffda54f6ed7acef3868b1 (diff) | |
download | mediawikicore-485e47ff102fbf172f6f906e17b6bfa578978fd9.tar.gz mediawikicore-485e47ff102fbf172f6f906e17b6bfa578978fd9.zip |
tests: Avoid database usage when possible
We would like to remove DB access in non-database PHPUnit tests. As a
first step, avoid database usage in tested code when possible. In
particular:
- In NameTableStoreFactory, avoid domain ID normalization if the
provided ID is already false.
- In SpecialDoubleRedirects, do not acquire a DB connection until it's
needed (which is just one place).
- Use editPage() in TitleDefTest instead of a DIY implementation, and
add `@group Database` accordingly.
- Avoid parsing titles in ContentHandler tests that don't need to parse
titles. Among the many dependencies of parsing titles is the interwiki
lookup, which requires DB access.
- Also remove test cases that used the "Gadget" namespace; it doesn't
exist in core, so these pages were actually in the mainspace.
- Mock the database in CategoriesRdfTest. The only two methods that use
the database were already being mocked.
- Add `@group Database` to test classes that are intentionally using the
Database, mainly via getTestUser().
Bug: T155147
Change-Id: I9385fe14cfeb6b7b7378cc322d510034c4ee0711
14 files changed, 40 insertions, 43 deletions
diff --git a/includes/Storage/NameTableStoreFactory.php b/includes/Storage/NameTableStoreFactory.php index ddf659b7e064..f4d1f3ddc5ad 100644 --- a/includes/Storage/NameTableStoreFactory.php +++ b/includes/Storage/NameTableStoreFactory.php @@ -95,7 +95,7 @@ class NameTableStoreFactory { if ( !isset( $infos[$tableName] ) ) { throw new \InvalidArgumentException( "Invalid table name \$tableName" ); } - if ( $wiki === $this->lbFactory->getLocalDomainID() ) { + if ( $wiki !== false && $wiki === $this->lbFactory->getLocalDomainID() ) { $wiki = false; } diff --git a/includes/specials/SpecialDoubleRedirects.php b/includes/specials/SpecialDoubleRedirects.php index f3ba3005a864..6e73fcbc0951 100644 --- a/includes/specials/SpecialDoubleRedirects.php +++ b/includes/specials/SpecialDoubleRedirects.php @@ -47,9 +47,6 @@ class SpecialDoubleRedirects extends QueryPage { /** @var LinkBatchFactory */ private $linkBatchFactory; - /** @var IDatabase */ - private $dbr; - /** * @param IContentHandlerFactory $contentHandlerFactory * @param LinkBatchFactory $linkBatchFactory @@ -64,7 +61,6 @@ class SpecialDoubleRedirects extends QueryPage { $this->contentHandlerFactory = $contentHandlerFactory; $this->linkBatchFactory = $linkBatchFactory; $this->setDatabaseProvider( $dbProvider ); - $this->dbr = $dbProvider->getReplicaDatabase(); } public function isExpensive() { @@ -162,7 +158,7 @@ class SpecialDoubleRedirects extends QueryPage { $result->namespace, $result->title ); - $deep = $this->dbr->selectRow( + $deep = $this->getDatabaseProvider()->getReplicaDatabase()->selectRow( $qi['tables'], $qi['fields'], $qi['conds'], diff --git a/tests/phpunit/includes/ParamValidator/TypeDef/TitleDefTest.php b/tests/phpunit/includes/ParamValidator/TypeDef/TitleDefTest.php index 9e1d68d5bbd4..dd021113ae55 100644 --- a/tests/phpunit/includes/ParamValidator/TypeDef/TitleDefTest.php +++ b/tests/phpunit/includes/ParamValidator/TypeDef/TitleDefTest.php @@ -2,19 +2,16 @@ namespace MediaWiki\ParamValidator\TypeDef; -use CommentStoreComment; use MediaWiki\MainConfigNames; use MediaWiki\MediaWikiServices; -use MediaWiki\Revision\SlotRecord; use MediaWiki\Title\Title; use TitleValue; -use User; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\SimpleCallbacks; -use WikitextContent; /** * @covers \MediaWiki\ParamValidator\TypeDef\TitleDef + * @group Database */ class TitleDefTest extends TypeDefIntegrationTestCase { protected function getInstance( SimpleCallbacks $callbacks, array $options ) { @@ -33,12 +30,8 @@ class TitleDefTest extends TypeDefIntegrationTestCase { $value, $expect, array $settings = [], array $options = [], array $expectConds = [] ) { if ( $this->dataName() === 'must exist (success)' ) { - $updater = MediaWikiServices::getInstance()->getWikiPageFactory() - ->newFromTitle( Title::makeTitle( NS_MAIN, 'Exists' ) ) - ->newPageUpdater( new User ) - ->setContent( SlotRecord::MAIN, new WikitextContent( 'exists' ) ); - $updater->saveRevision( CommentStoreComment::newUnsavedComment( 'test' ) ); - $this->assertTrue( $updater->getStatus()->isOK() ); + $status = $this->editPage( Title::makeTitle( NS_MAIN, 'Exists' ), 'exists' ); + $this->assertTrue( $status->isOK() ); } parent::testValidate( $value, $expect, $settings, $options, $expectConds ); } diff --git a/tests/phpunit/includes/content/CssContentHandlerTest.php b/tests/phpunit/includes/content/CssContentHandlerTest.php index cedf38035d1c..ba024a0b8644 100644 --- a/tests/phpunit/includes/content/CssContentHandlerTest.php +++ b/tests/phpunit/includes/content/CssContentHandlerTest.php @@ -9,13 +9,13 @@ class CssContentHandlerTest extends MediaWikiLangTestCase { * @dataProvider provideMakeRedirectContent * @covers CssContentHandler::makeRedirectContent */ - public function testMakeRedirectContent( $title, $expected ) { + public function testMakeRedirectContent( int $namespace, string $title, $expected ) { $this->overrideConfigValues( [ MainConfigNames::Server => '//example.org', MainConfigNames::Script => '/w/index.php', ] ); $ch = new CssContentHandler(); - $content = $ch->makeRedirectContent( Title::newFromText( $title ) ); + $content = $ch->makeRedirectContent( Title::makeTitle( $namespace, $title ) ); $this->assertInstanceOf( CssContent::class, $content ); $this->assertEquals( $expected, $content->serialize( CONTENT_FORMAT_CSS ) ); } @@ -26,19 +26,18 @@ class CssContentHandlerTest extends MediaWikiLangTestCase { public static function provideMakeRedirectContent() { return [ [ - 'MediaWiki:MonoBook.css', + NS_MEDIAWIKI, + 'MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ], [ - 'User:FooBar/common.css', + NS_USER, + 'FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ], [ - 'Gadget:FooBaz.css', - "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" - ], - [ - 'User:😂/unicode.css', + NS_USER, + '😂/unicode.css', '/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);' ], ]; diff --git a/tests/phpunit/includes/content/CssContentTest.php b/tests/phpunit/includes/content/CssContentTest.php index 73a4b776a8f1..fd88414480ae 100644 --- a/tests/phpunit/includes/content/CssContentTest.php +++ b/tests/phpunit/includes/content/CssContentTest.php @@ -91,7 +91,6 @@ class CssContentTest extends TextContentTest { return [ [ 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ], [ 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ], - [ 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ], [ 'User:😂/unicode.css', '/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);' diff --git a/tests/phpunit/includes/content/WikitextContentHandlerTest.php b/tests/phpunit/includes/content/WikitextContentHandlerTest.php index 1c037c2a5353..c90b8895d3bd 100644 --- a/tests/phpunit/includes/content/WikitextContentHandlerTest.php +++ b/tests/phpunit/includes/content/WikitextContentHandlerTest.php @@ -1,8 +1,10 @@ <?php +use MediaWiki\Linker\LinkTarget; use MediaWiki\MainConfigNames; use MediaWiki\Page\PageReferenceValue; use MediaWiki\Title\Title; +use MediaWiki\User\UserIdentity; /** * See also unit tests at \MediaWiki\Tests\Unit\WikitextContentHandlerTest @@ -22,33 +24,30 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase { /** * @dataProvider provideMakeRedirectContent - * @param Title|string $title Title object or string for Title::newFromText() + * @param LinkTarget $target * @param string $expected Serialized form of the content object built * @covers WikitextContentHandler::makeRedirectContent */ - public function testMakeRedirectContent( $title, $expected ) { + public function testMakeRedirectContent( LinkTarget $target, $expected ) { $this->getServiceContainer()->resetServiceForTesting( 'ContentLanguage' ); $this->getServiceContainer()->resetServiceForTesting( 'MagicWordFactory' ); - if ( is_string( $title ) ) { - $title = Title::newFromText( $title ); - } - $content = $this->handler->makeRedirectContent( $title ); + $content = $this->handler->makeRedirectContent( Title::newFromLinkTarget( $target ) ); $this->assertEquals( $expected, $content->serialize() ); } public static function provideMakeRedirectContent() { return [ - [ 'Hello', '#REDIRECT [[Hello]]' ], - [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ], - [ 'Hello#section', '#REDIRECT [[Hello#section]]' ], - [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ], - [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ], - [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ], - [ Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ], - [ Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ], + [ new TitleValue( NS_MAIN, 'Hello' ), '#REDIRECT [[Hello]]' ], + [ new TitleValue( NS_TEMPLATE, 'Hello' ), '#REDIRECT [[Template:Hello]]' ], + [ new TitleValue( NS_MAIN, 'Hello', 'section' ), '#REDIRECT [[Hello#section]]' ], + [ new TitleValue( NS_USER, 'John doe', 'section' ), '#REDIRECT [[User:John doe#section]]' ], + [ new TitleValue( NS_MEDIAWIKI, 'FOOBAR' ), '#REDIRECT [[MediaWiki:FOOBAR]]' ], + [ new TitleValue( NS_CATEGORY, 'Foo' ), '#REDIRECT [[:Category:Foo]]' ], + [ new TitleValue( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ], + [ new TitleValue( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ], [ - Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), + new TitleValue( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ], ]; @@ -309,7 +308,7 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase { $pstContent = $contentTransformer->preSaveTransform( $content, $pageObj, - $this->getTestUser()->getUser(), + $this->createMock( UserIdentity::class ), ParserOptions::newFromAnon() ); diff --git a/tests/phpunit/includes/session/CsrfTokenSetTest.php b/tests/phpunit/includes/session/CsrfTokenSetTest.php index b7659d354157..fe2778e516d5 100644 --- a/tests/phpunit/includes/session/CsrfTokenSetTest.php +++ b/tests/phpunit/includes/session/CsrfTokenSetTest.php @@ -11,6 +11,7 @@ use WebRequest; /** * @covers \MediaWiki\Session\CsrfTokenSet * @package MediaWiki\Tests\Unit\Session + * @group Database */ class CsrfTokenSetTest extends MediaWikiIntegrationTestCase { diff --git a/tests/phpunit/includes/specials/SpecialConfirmEmailTest.php b/tests/phpunit/includes/specials/SpecialConfirmEmailTest.php index 13a082bb2652..4f3770229cfb 100644 --- a/tests/phpunit/includes/specials/SpecialConfirmEmailTest.php +++ b/tests/phpunit/includes/specials/SpecialConfirmEmailTest.php @@ -5,6 +5,7 @@ use MediaWiki\User\UserFactory; /** * @covers SpecialConfirmEmail + * @group Database */ class SpecialConfirmEmailTest extends SpecialPageTestBase { protected function newSpecialPage() { diff --git a/tests/phpunit/includes/specials/SpecialMuteTest.php b/tests/phpunit/includes/specials/SpecialMuteTest.php index 307a1bacdb69..5abefae067b3 100644 --- a/tests/phpunit/includes/specials/SpecialMuteTest.php +++ b/tests/phpunit/includes/specials/SpecialMuteTest.php @@ -8,6 +8,7 @@ use MediaWiki\User\UserOptionsManager; /** * @group SpecialPage + * @group Database * @covers MediaWiki\Specials\SpecialMute */ class SpecialMuteTest extends SpecialPageTestBase { diff --git a/tests/phpunit/includes/specials/SpecialUncategorizedCategoriesTest.php b/tests/phpunit/includes/specials/SpecialUncategorizedCategoriesTest.php index c4d3643bc7d2..c2cba06668d7 100644 --- a/tests/phpunit/includes/specials/SpecialUncategorizedCategoriesTest.php +++ b/tests/phpunit/includes/specials/SpecialUncategorizedCategoriesTest.php @@ -4,6 +4,8 @@ use MediaWiki\Language\RawMessage; /** * Tests for Special:UncategorizedCategories + * + * @group Database */ class SpecialUncategorizedCategoriesTest extends MediaWikiIntegrationTestCase { /** diff --git a/tests/phpunit/includes/user/ExternalUserNamesTest.php b/tests/phpunit/includes/user/ExternalUserNamesTest.php index ccb5752377fe..d8b66080b6ef 100644 --- a/tests/phpunit/includes/user/ExternalUserNamesTest.php +++ b/tests/phpunit/includes/user/ExternalUserNamesTest.php @@ -5,6 +5,7 @@ use MediaWiki\Title\Title; /** * @covers ExternalUserNames + * @group Database */ class ExternalUserNamesTest extends MediaWikiIntegrationTestCase { use DummyServicesTrait; diff --git a/tests/phpunit/integration/includes/Permissions/RateLimiterTest.php b/tests/phpunit/integration/includes/Permissions/RateLimiterTest.php index 73c90ac4b6e3..9ee7ea616d93 100644 --- a/tests/phpunit/integration/includes/Permissions/RateLimiterTest.php +++ b/tests/phpunit/integration/includes/Permissions/RateLimiterTest.php @@ -21,6 +21,7 @@ use Wikimedia\WRStats\WRStatsFactory; /** * @coversDefaultClass \MediaWiki\Permissions\RateLimiter + * @group Database */ class RateLimiterTest extends MediaWikiIntegrationTestCase { diff --git a/tests/phpunit/maintenance/CategoriesRdfTest.php b/tests/phpunit/maintenance/CategoriesRdfTest.php index 8311015ba371..0f48439d0136 100644 --- a/tests/phpunit/maintenance/CategoriesRdfTest.php +++ b/tests/phpunit/maintenance/CategoriesRdfTest.php @@ -3,6 +3,7 @@ namespace MediaWiki\Tests\Maintenance; use DumpCategoriesAsRdf; +use IMaintainableDatabase; use MediaWiki\MainConfigNames; use MediaWikiLangTestCase; @@ -64,9 +65,11 @@ class CategoriesRdfTest extends MediaWikiLangTestCase { $dumpScript = $this->getMockBuilder( DumpCategoriesAsRdf::class ) - ->onlyMethods( [ 'getCategoryIterator', 'getCategoryLinksIterator' ] ) + ->onlyMethods( [ 'getDB', 'getCategoryIterator', 'getCategoryLinksIterator' ] ) ->getMock(); + $dumpScript->method( 'getDB' ) + ->willReturn( $this->createNoOpMock( IMaintainableDatabase::class ) ); $dumpScript->expects( $this->once() ) ->method( 'getCategoryIterator' ) ->willReturn( $this->getCategoryIterator() ); diff --git a/tests/phpunit/structure/ApiStructureTest.php b/tests/phpunit/structure/ApiStructureTest.php index a20574c2f487..9f7779c43b6b 100644 --- a/tests/phpunit/structure/ApiStructureTest.php +++ b/tests/phpunit/structure/ApiStructureTest.php @@ -12,6 +12,7 @@ use Wikimedia\TestingAccessWrapper; * - do not have inconsistencies in the parameter definitions * * @group API + * @group Database * @coversNothing */ class ApiStructureTest extends MediaWikiIntegrationTestCase { |