From 3bedffa87b68bbf9b270b06cfba369a7d9989e8b Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sun, 16 Jul 2023 14:46:56 +0200 Subject: [PATCH] phpunit: Do not call addCoreDBData if the test doesn't need the DB This is a small step towards T155147. For now we clone the database even if the test doesn't say it needs it, but we stop adding core data. If a non-database test depends on this data, it means that it really should be in the database group, or that it's otherwise doing something wrong. For example, both MediaWikiTest and RefreshSecondaryDataUpdateTest were depending on the 'UTPage' page being created by the test framework, although they did that in such a way that the needsDB() check in getExistingTestPage() weren't triggered. Bug: T155147 Depends-On: I1a94c6ca2f335ee5a2d7d57df6dc46b65ca1f767 Change-Id: Ia04ff528628a7ae8b350e3122f5a06dbc1ff753b --- tests/phpunit/MediaWikiIntegrationTestCase.php | 12 ++++++++---- tests/phpunit/includes/MediaWikiTest.php | 3 +++ .../deferred/RefreshSecondaryDataUpdateTest.php | 3 ++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/MediaWikiIntegrationTestCase.php b/tests/phpunit/MediaWikiIntegrationTestCase.php index 5a6ab7bc326..cba5733f950 100644 --- a/tests/phpunit/MediaWikiIntegrationTestCase.php +++ b/tests/phpunit/MediaWikiIntegrationTestCase.php @@ -502,7 +502,9 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { self::setupAllTestDBs( $this->db, $this->dbPrefix(), $useTemporaryTables ); - $this->addCoreDBData(); + if ( $this->needsDB() ) { + $this->addCoreDBData(); + } } // TODO: the DB setup should be done in setUpBeforeClass(), so the test DB @@ -1633,7 +1635,7 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { // Make 1 page with 1 revision $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( Title::makeTitle( NS_MAIN, 'UTPage' ) ); - if ( $page->getId() == 0 ) { + if ( !$page->exists() ) { $page->doUserEditContent( new WikitextContent( 'UTContent' ), $user, @@ -2117,8 +2119,10 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase { if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) { // Reset services that may contain information relating to the truncated tables $this->overrideMwServices(); - // Re-add core DB data that was deleted - $this->addCoreDBData(); + if ( $this->needsDB() ) { + // Re-add core DB data that was deleted + $this->addCoreDBData(); + } } } } diff --git a/tests/phpunit/includes/MediaWikiTest.php b/tests/phpunit/includes/MediaWikiTest.php index afcbd4fdd73..ccb2463ef13 100644 --- a/tests/phpunit/includes/MediaWikiTest.php +++ b/tests/phpunit/includes/MediaWikiTest.php @@ -6,6 +6,9 @@ use MediaWiki\Request\WebResponse; use MediaWiki\Title\Title; use Wikimedia\TestingAccessWrapper; +/** + * @group Database + */ class MediaWikiTest extends MediaWikiIntegrationTestCase { private $oldServer, $oldGet, $oldPost; diff --git a/tests/phpunit/includes/deferred/RefreshSecondaryDataUpdateTest.php b/tests/phpunit/includes/deferred/RefreshSecondaryDataUpdateTest.php index 75686ea649f..3dfac95e75a 100644 --- a/tests/phpunit/includes/deferred/RefreshSecondaryDataUpdateTest.php +++ b/tests/phpunit/includes/deferred/RefreshSecondaryDataUpdateTest.php @@ -8,6 +8,7 @@ use Wikimedia\ScopedCallback; /** * @covers RefreshSecondaryDataUpdate + * @group Database */ class RefreshSecondaryDataUpdateTest extends MediaWikiIntegrationTestCase { public function testSuccess() { @@ -206,7 +207,7 @@ class RefreshSecondaryDataUpdateTest extends MediaWikiIntegrationTestCase { return [ $goodUpdate ]; } ); - $wikiPage = $services->getWikiPageFactory()->newFromTitle( Title::makeTitle( NS_MAIN, 'UTPage' ) ); + $wikiPage = $this->getExistingTestPage(); $update = new RefreshSecondaryDataUpdate( $lbFactory, $user,