wiki.techinc.nl/tests/phpunit/includes/Storage/NameTableStoreFactoryTest.php
Aaron Schulz fe0af6cad5 rdbms: Database::selectDB() update the domain and handle failure better
LoadBalancer uses Database::getDomainId() for deciding which keys to use
in the foreign connection handle arrays. This method should reflect any
changes made to the DB selection.

If the query fails, then do not change domain field. This is the sort of
approach that LoadBalancer is expects in openForeignConnection(). Also,
throw an exception when selectDB() fails.

The db/schema/prefix fields of Database no longer exist in favor of just
using the newer currentDomain field.

Also:
* Add IDatabase::selectDomain() method and made selectDB() wrap it.
* Extract the DB name from sqlite files if not explicitly provided.
* Fix inconsistent open() return values from Database subclasses.
* Make a relationSchemaQualifier() method to handle the concern of
  omitting schema names in queries. The means that getDomainId() can
  still return the right value, rather than confusingly omitt the schema.
* Make RevisionStore::checkDatabaseWikiId() account for the domain schema.
  Unlike d2a4d614fc, this does not incorrectly assume the storage is
  always for the current wiki domain. Also, LBFactorySingle sets the local
  domain so it is defined even in install.php.
* Make RevisionStoreDbTestBase actually set the LoadBalancer local domain.
* Make RevisionTest::testLoadFromTitle() account for the domain schema.

Bug: T193565
Change-Id: I6e51cd54c6da78830b38906b8c46789c79498ab5
2018-10-10 12:03:30 -07:00

122 lines
3.2 KiB
PHP

<?php
namespace MediaWiki\Tests\Storage;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Storage\NameTableStore;
use MediaWiki\Storage\NameTableStoreFactory;
use MediaWikiTestCase;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Rdbms\ILoadBalancer;
/**
* @covers MediaWiki\Storage\NameTableStoreFactory
* @group Database
*/
class NameTableStoreFactoryTest extends MediaWikiTestCase {
/**
* @return \PHPUnit_Framework_MockObject_MockObject|ILoadBalancer
*/
private function getMockLoadBalancer( $localDomain ) {
$mock = $this->getMockBuilder( ILoadBalancer::class )
->disableOriginalConstructor()->getMock();
$mock->expects( $this->any() )
->method( 'getLocalDomainID' )
->willReturn( $localDomain );
return $mock;
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject|ILBFactory
*/
private function getMockLoadBalancerFactory( $expectedWiki ) {
$mock = $this->getMockBuilder( ILBFactory::class )
->disableOriginalConstructor()->getMock();
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
$localDomain = $lbFactory->getLocalDomainID();
$mock->expects( $this->any() )->method( 'getLocalDomainID' )->willReturn( $localDomain );
$mock->expects( $this->once() )
->method( 'getMainLB' )
->with( $this->equalTo( $expectedWiki ) )
->willReturnCallback( function ( $domain ) use ( $localDomain ) {
return $this->getMockLoadBalancer( $localDomain );
} );
return $mock;
}
public static function provideTestGet() {
return [
[
'change_tag_def',
false,
false,
],
[
'content_models',
false,
false,
],
[
'slot_roles',
false,
false,
],
[
'change_tag_def',
'test7245',
'test7245',
],
];
}
/** @dataProvider provideTestGet */
public function testGet( $tableName, $wiki, $expectedWiki ) {
$services = MediaWikiServices::getInstance();
$wiki2 = ( $wiki === false )
? $services->getDBLoadBalancerFactory()->getLocalDomainID()
: $wiki;
$names = new NameTableStoreFactory(
$this->getMockLoadBalancerFactory( $expectedWiki ),
$services->getMainWANObjectCache(),
LoggerFactory::getInstance( 'NameTableStoreFactory' )
);
$table = $names->get( $tableName, $wiki );
$table2 = $names->get( $tableName, $wiki2 );
$this->assertSame( $table, $table2 );
$this->assertInstanceOf( NameTableStore::class, $table );
}
/*
* The next three integration tests verify that the schema information is correct by loading
* the relevant information from the database.
*/
public function testIntegratedGetChangeTagDef() {
$services = MediaWikiServices::getInstance();
$factory = $services->getNameTableStoreFactory();
$store = $factory->getChangeTagDef();
$this->assertType( 'array', $store->getMap() );
}
public function testIntegratedGetContentModels() {
$services = MediaWikiServices::getInstance();
$factory = $services->getNameTableStoreFactory();
$store = $factory->getContentModels();
$this->assertType( 'array', $store->getMap() );
}
public function testIntegratedGetSlotRoles() {
$services = MediaWikiServices::getInstance();
$factory = $services->getNameTableStoreFactory();
$store = $factory->getSlotRoles();
$this->assertType( 'array', $store->getMap() );
}
}