Support migration stages when reading and writing blocks. I tried to set it up for an easy next stage, in which support for the old schema is removed. I tried to avoid factoring out of shared code between the two schemas, so that the old schema cases can simply be deleted without the need to revert unnecessary abstractions. However, I added HideUserUtils to factor out ipb_deleted queries. Code review showed that this was already quite complex, with multiple approaches to the problem, so it benefits from refactoring even without the schema abstraction. HideUserUtils is a service rather than a standalone class to support unit tests, since unit tests do not allow global config access. When the migration stage config is removed, it will be a service with no constructor parameters -- an unnecessary abstraction which should ideally be resolved at that time. When interpreting result rows, it is possible to share code by using field aliases. But when constructing WHERE conditions, the actual field names need to be used, so the migration is more intrusive in ApiQueryBlocks and SpecialBlockList, where complex conditions are used. Bug: T346293 Bug: T51504 Bug: T349883 Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
128 lines
4 KiB
PHP
128 lines
4 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\User;
|
|
|
|
use MediaWiki\Block\HideUserUtils;
|
|
use MediaWiki\Config\ServiceOptions;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\User\ActorNormalization;
|
|
use MediaWiki\User\ActorStore;
|
|
use MediaWiki\User\ActorStoreFactory;
|
|
use MediaWiki\User\TempUser\TempUserConfig;
|
|
use MediaWiki\User\UserIdentityLookup;
|
|
use MediaWiki\User\UserNameUtils;
|
|
use MediaWikiUnitTestCase;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Psr\Log\NullLogger;
|
|
use Wikimedia\Rdbms\ILBFactory;
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\ActorStoreFactory
|
|
* @covers \MediaWiki\User\ActorStore
|
|
* @package MediaWiki\Tests\User
|
|
*/
|
|
class ActorStoreFactoryTest extends MediaWikiUnitTestCase {
|
|
|
|
public static function provideGetActorStore() {
|
|
yield 'local, no shared' => [
|
|
false, // $domain,
|
|
[
|
|
MainConfigNames::SharedDB => false,
|
|
MainConfigNames::SharedTables => false
|
|
], // $config
|
|
false, // $expectedDomain
|
|
];
|
|
yield 'foreign, no shared' => [
|
|
'acmewiki', // $domain,
|
|
[
|
|
MainConfigNames::SharedDB => false,
|
|
MainConfigNames::SharedTables => false
|
|
], //
|
|
'acmewiki', // $expectedDomain
|
|
];
|
|
yield 'local, shared' => [
|
|
false, // $domain,
|
|
[
|
|
MainConfigNames::SharedDB => [ 'sharedwiki' ],
|
|
MainConfigNames::SharedTables => [ 'user', 'actor' ]
|
|
], // $config
|
|
false, // $expectedDomain
|
|
];
|
|
yield 'foreign, shared' => [
|
|
'acmewiki', // $domain,
|
|
[
|
|
MainConfigNames::SharedDB => [ 'sharedwiki' ],
|
|
MainConfigNames::SharedTables => [ 'user', 'actor' ]
|
|
], // $config
|
|
false, // $expectedDomain
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideGetActorStore
|
|
*/
|
|
public function testGetActorStore( $domain, array $config, $expectedDomain ) {
|
|
$factory = new ActorStoreFactory(
|
|
new ServiceOptions( ActorStoreFactory::CONSTRUCTOR_OPTIONS, $config ),
|
|
$this->getMockLoadBalancerFactory( $expectedDomain ),
|
|
$this->createNoOpMock( UserNameUtils::class ),
|
|
$this->createNoOpMock( TempUserConfig::class ),
|
|
new NullLogger(),
|
|
new HideUserUtils( SCHEMA_COMPAT_READ_OLD )
|
|
);
|
|
$notFromCache = $factory->getActorStore( $domain );
|
|
$this->assertInstanceOf( ActorStore::class, $notFromCache );
|
|
$this->assertSame( $notFromCache, $factory->getActorStore( $domain ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideGetActorStore
|
|
*/
|
|
public function testGetActorNormalization( $domain, array $config, $expectedDomain ) {
|
|
$factory = new ActorStoreFactory(
|
|
new ServiceOptions( ActorStoreFactory::CONSTRUCTOR_OPTIONS, $config ),
|
|
$this->getMockLoadBalancerFactory( $expectedDomain ),
|
|
$this->createNoOpMock( UserNameUtils::class ),
|
|
$this->createNoOpMock( TempUserConfig::class ),
|
|
new NullLogger(),
|
|
$this->createNoOpMock( HideUserUtils::class )
|
|
);
|
|
$notFromCache = $factory->getActorNormalization( $domain );
|
|
$this->assertInstanceOf( ActorNormalization::class, $notFromCache );
|
|
$this->assertSame( $notFromCache, $factory->getActorNormalization( $domain ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideGetActorStore
|
|
*/
|
|
public function testGetUserIdentityLookup( $domain, array $config, $expectedDomain ) {
|
|
$factory = new ActorStoreFactory(
|
|
new ServiceOptions( ActorStoreFactory::CONSTRUCTOR_OPTIONS, $config ),
|
|
$this->getMockLoadBalancerFactory( $expectedDomain ),
|
|
$this->createNoOpMock( UserNameUtils::class ),
|
|
$this->createNoOpMock( TempUserConfig::class ),
|
|
new NullLogger(),
|
|
$this->createNoOpMock( HideUserUtils::class )
|
|
);
|
|
$notFromCache = $factory->getUserIdentityLookup( $domain );
|
|
$this->assertInstanceOf( UserIdentityLookup::class, $notFromCache );
|
|
$this->assertSame( $notFromCache, $factory->getUserIdentityLookup( $domain ) );
|
|
}
|
|
|
|
/**
|
|
* @param string|false $expectDomain
|
|
* @return MockObject|ILBFactory
|
|
*/
|
|
private function getMockLoadBalancerFactory( $expectDomain ) {
|
|
$mock = $this->createMock( ILBFactory::class );
|
|
|
|
$mock->method( 'getMainLB' )
|
|
->willReturnCallback( function ( $domain ) use ( $expectDomain ) {
|
|
$this->assertSame( $expectDomain, $domain );
|
|
return $this->createMock( ILoadBalancer::class );
|
|
} );
|
|
|
|
return $mock;
|
|
}
|
|
}
|