From: Krinkle Date: Wed, 24 Jul 2019 19:29:56 +0000 (+0000) Subject: Make wfGetDB() return a MaintainableDBConnRef instance (take 2) X-Git-Tag: 1.34.0-rc.0~885 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22calendrier%22%2C%22type=semaine%22%29%20.%20%22?a=commitdiff_plain;h=74068ce880e245f84844bd78d49214994202441a;p=lhc%2Fweb%2Fwiklou.git Make wfGetDB() return a MaintainableDBConnRef instance (take 2) This enforces the DB_* role checks of DBConnRef in more places. This is a re-submission of 335066505a3ea, which was reverted due to T228928. Change-Id: I556627dc6486e6f6539d1d2dd5aa6f009eff341e --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 7b4b502905..1741958681 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2563,10 +2563,10 @@ function wfWikiID() { * @todo Replace calls to wfGetDB with calls to LoadBalancer::getConnection() * on an injected instance of LoadBalancer. * - * @return \Wikimedia\Rdbms\Database + * @return \Wikimedia\Rdbms\DBConnRef */ function wfGetDB( $db, $groups = [], $wiki = false ) { - return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki ); + return wfGetLB( $wiki )->getMaintenanceConnectionRef( $db, $groups, $wiki ); } /** diff --git a/tests/phpunit/includes/Revision/RevisionStoreTest.php b/tests/phpunit/includes/Revision/RevisionStoreTest.php index 0648bfce6e..83872e3a37 100644 --- a/tests/phpunit/includes/Revision/RevisionStoreTest.php +++ b/tests/phpunit/includes/Revision/RevisionStoreTest.php @@ -12,6 +12,8 @@ use MediaWiki\Revision\RevisionStore; use MediaWiki\Revision\SlotRoleRegistry; use MediaWiki\Revision\SlotRecord; use MediaWiki\Storage\SqlBlobStore; +use Wikimedia\Rdbms\ILoadBalancer; +use Wikimedia\Rdbms\MaintainableDBConnRef; use MediaWikiTestCase; use MWException; use Title; @@ -77,6 +79,17 @@ class RevisionStoreTest extends MediaWikiTestCase { ->disableOriginalConstructor()->getMock(); } + /** + * @param ILoadBalancer $mockLoadBalancer + * @param Database $db + * @return callable + */ + private function getMockDBConnRefCallback( ILoadBalancer $mockLoadBalancer, IDatabase $db ) { + return function ( $i, $g, $domain, $flg ) use ( $mockLoadBalancer, $db ) { + return new MaintainableDBConnRef( $mockLoadBalancer, $db, $i ); + }; + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore */ @@ -158,10 +171,14 @@ class RevisionStoreTest extends MediaWikiTestCase { $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); $db = $this->getMockDatabase(); - // Title calls wfGetDB() which uses a regular Connection + // RevisionStore uses getConnectionRef + $mockLoadBalancer->expects( $this->any() ) + ->method( 'getConnectionRef' ) + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); + // Title calls wfGetDB() which uses getMaintenanceConnectionRef $mockLoadBalancer->expects( $this->atLeastOnce() ) - ->method( 'getConnection' ) - ->willReturn( $db ); + ->method( 'getMaintenanceConnectionRef' ) + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); // First call to Title::newFromID, faking no result (db lag?) $db->expects( $this->at( 0 ) ) @@ -192,15 +209,15 @@ class RevisionStoreTest extends MediaWikiTestCase { $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); $db = $this->getMockDatabase(); - // Title calls wfGetDB() which uses a regular Connection + // Title calls wfGetDB() which uses getMaintenanceConnectionRef // Assert that the first call uses a REPLICA and the second falls back to master - $mockLoadBalancer->expects( $this->exactly( 2 ) ) - ->method( 'getConnection' ) - ->willReturn( $db ); - // RevisionStore getTitle uses a ConnectionRef $mockLoadBalancer->expects( $this->atLeastOnce() ) ->method( 'getConnectionRef' ) - ->willReturn( $db ); + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); + // Title calls wfGetDB() which uses getMaintenanceConnectionRef + $mockLoadBalancer->expects( $this->exactly( 2 ) ) + ->method( 'getMaintenanceConnectionRef' ) + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); // First call to Title::newFromID, faking no result (db lag?) $db->expects( $this->at( 0 ) ) @@ -251,14 +268,14 @@ class RevisionStoreTest extends MediaWikiTestCase { $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); $db = $this->getMockDatabase(); - // Title calls wfGetDB() which uses a regular Connection - $mockLoadBalancer->expects( $this->atLeastOnce() ) - ->method( 'getConnection' ) - ->willReturn( $db ); - // RevisionStore getTitle uses a ConnectionRef $mockLoadBalancer->expects( $this->atLeastOnce() ) ->method( 'getConnectionRef' ) - ->willReturn( $db ); + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); + // Title calls wfGetDB() which uses getMaintenanceConnectionRef + // RevisionStore getTitle uses getMaintenanceConnectionRef + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getMaintenanceConnectionRef' ) + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); // First call to Title::newFromID, faking no result (db lag?) $db->expects( $this->at( 0 ) ) @@ -299,15 +316,15 @@ class RevisionStoreTest extends MediaWikiTestCase { $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); $db = $this->getMockDatabase(); - // Title calls wfGetDB() which uses a regular Connection // Assert that the first call uses a REPLICA and the second falls back to master - $mockLoadBalancer->expects( $this->exactly( 2 ) ) - ->method( 'getConnection' ) - ->willReturn( $db ); - // RevisionStore getTitle uses a ConnectionRef + // RevisionStore uses getMaintenanceConnectionRef $mockLoadBalancer->expects( $this->atLeastOnce() ) ->method( 'getConnectionRef' ) - ->willReturn( $db ); + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); + // Title calls wfGetDB() which uses getMaintenanceConnectionRef + $mockLoadBalancer->expects( $this->exactly( 2 ) ) + ->method( 'getMaintenanceConnectionRef' ) + ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) ); // First call to Title::newFromID, faking no result (db lag?) $db->expects( $this->at( 0 ) ) @@ -368,12 +385,14 @@ class RevisionStoreTest extends MediaWikiTestCase { $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); $db = $this->getMockDatabase(); - // Title calls wfGetDB() which uses a regular Connection + // Title calls wfGetDB() which uses getMaintenanceConnectionRef // Assert that the first call uses a REPLICA and the second falls back to master // RevisionStore getTitle uses getConnectionRef - // Title::newFromID uses getConnection - foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) { + // Title::newFromID uses getMaintenanceConnectionRef + foreach ( [ + 'getConnectionRef', 'getMaintenanceConnectionRef' + ] as $method ) { $mockLoadBalancer->expects( $this->exactly( 2 ) ) ->method( $method ) ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) { diff --git a/tests/phpunit/suites/ParserTestTopLevelSuite.php b/tests/phpunit/suites/ParserTestTopLevelSuite.php index 28547d1f84..f318df1dd7 100644 --- a/tests/phpunit/suites/ParserTestTopLevelSuite.php +++ b/tests/phpunit/suites/ParserTestTopLevelSuite.php @@ -1,5 +1,6 @@ getDBLoadBalancer(); + $db = $lb->getConnection( DB_MASTER ); $type = $db->getType(); $prefix = $type === 'oracle' ? MediaWikiTestCase::ORA_DB_PREFIX : MediaWikiTestCase::DB_PREFIX;