This enforces the DB_* role checks of DBConnRef in more places
Depends-on: I9328e709fe5d81099338a31deef24d34db22d784
Change-Id: I0d7dacee3ec4ef67dc0b0f6551ad046c74dc47dc
* @todo Replace calls to wfGetDB with calls to LoadBalancer::getConnection()
* on an injected instance of LoadBalancer.
*
* @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 ) {
*/
function wfGetDB( $db, $groups = [], $wiki = false ) {
- return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
+ return wfGetLB( $wiki )->getMaintenanceConnectionRef( $db, $groups, $wiki );
use MediaWiki\Revision\SlotRoleRegistry;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Storage\SqlBlobStore;
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;
use MediaWikiTestCase;
use MWException;
use Title;
->disableOriginalConstructor()->getMock();
}
->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
*/
/**
* @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
*/
$this->setService( 'DBLoadBalancer', $mockLoadBalancer );
$db = $this->getMockDatabase();
$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() )
$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 ) )
// First call to Title::newFromID, faking no result (db lag?)
$db->expects( $this->at( 0 ) )
$this->setService( 'DBLoadBalancer', $mockLoadBalancer );
$db = $this->getMockDatabase();
$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
// 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' )
$mockLoadBalancer->expects( $this->atLeastOnce() )
->method( 'getConnectionRef' )
+ ->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 ) )
// First call to Title::newFromID, faking no result (db lag?)
$db->expects( $this->at( 0 ) )
$this->setService( 'DBLoadBalancer', $mockLoadBalancer );
$db = $this->getMockDatabase();
$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' )
$mockLoadBalancer->expects( $this->atLeastOnce() )
->method( 'getConnectionRef' )
+ ->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 ) )
// First call to Title::newFromID, faking no result (db lag?)
$db->expects( $this->at( 0 ) )
$this->setService( 'DBLoadBalancer', $mockLoadBalancer );
$db = $this->getMockDatabase();
$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
// 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' )
$mockLoadBalancer->expects( $this->atLeastOnce() )
->method( 'getConnectionRef' )
+ ->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 ) )
// First call to Title::newFromID, faking no result (db lag?)
$db->expects( $this->at( 0 ) )
$this->setService( 'DBLoadBalancer', $mockLoadBalancer );
$db = $this->getMockDatabase();
$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
// 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 ) {
$mockLoadBalancer->expects( $this->exactly( 2 ) )
->method( $method )
->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {