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
156 lines
4.8 KiB
PHP
156 lines
4.8 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
|
use MediaWiki\Config\HashConfig;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
|
use MediaWiki\User\CentralId\CentralIdLookup;
|
|
use MediaWiki\User\CentralId\LocalIdLookup;
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
/**
|
|
* @covers LocalIdLookup
|
|
* @group Database
|
|
*/
|
|
class LocalIdLookupTest extends MediaWikiIntegrationTestCase {
|
|
use MockAuthorityTrait;
|
|
|
|
private $localUsers = [];
|
|
|
|
public function addDBData() {
|
|
for ( $i = 1; $i <= 4; $i++ ) {
|
|
$this->localUsers[] = $this->getMutableTestUser()->getUserIdentity();
|
|
}
|
|
|
|
$sysop = static::getTestSysop()->getUserIdentity();
|
|
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
|
|
|
|
$block = new DatabaseBlock( [
|
|
'address' => $this->localUsers[2]->getName(),
|
|
'by' => $sysop,
|
|
'reason' => __METHOD__,
|
|
'expiry' => '1 day',
|
|
'hideName' => false,
|
|
] );
|
|
$blockStore->insertBlock( $block );
|
|
|
|
$block = new DatabaseBlock( [
|
|
'address' => $this->localUsers[3]->getName(),
|
|
'by' => $sysop,
|
|
'reason' => __METHOD__,
|
|
'expiry' => '1 day',
|
|
'hideName' => true,
|
|
] );
|
|
$blockStore->insertBlock( $block );
|
|
}
|
|
|
|
private function newLookup( array $configOverride = [] ) {
|
|
return new LocalIdLookup(
|
|
new HashConfig( [
|
|
MainConfigNames::SharedDB => null,
|
|
MainConfigNames::SharedTables => [],
|
|
MainConfigNames::LocalDatabases => [],
|
|
] + $configOverride ),
|
|
$this->getServiceContainer()->getDBLoadBalancerFactory(),
|
|
$this->getServiceContainer()->getHideUserUtils()
|
|
);
|
|
}
|
|
|
|
public function testLookupCentralIds() {
|
|
$lookup = $this->newLookup();
|
|
$permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] );
|
|
$nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] );
|
|
|
|
$this->assertSame( [], $lookup->lookupCentralIds( [] ) );
|
|
|
|
$expect = [];
|
|
foreach ( $this->localUsers as $localUser ) {
|
|
$expect[$localUser->getId()] = $localUser->getName();
|
|
}
|
|
$expect[12345] = 'X';
|
|
ksort( $expect );
|
|
|
|
$expect2 = $expect;
|
|
$expect2[$this->localUsers[3]->getId()] = '';
|
|
|
|
$arg = array_fill_keys( array_keys( $expect ), 'X' );
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
|
|
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
|
|
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $permitted ) );
|
|
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $nonPermitted ) );
|
|
}
|
|
|
|
public function testLookupUserNames() {
|
|
$lookup = $this->newLookup();
|
|
$permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] );
|
|
$nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] );
|
|
|
|
$this->assertSame( [], $lookup->lookupUserNames( [] ) );
|
|
|
|
$expect = [];
|
|
foreach ( $this->localUsers as $localUser ) {
|
|
$expect[$localUser->getName()] = $localUser->getId();
|
|
}
|
|
$expect['UTDoesNotExist'] = 'X';
|
|
ksort( $expect );
|
|
|
|
$expect2 = $expect;
|
|
$expect2[$this->localUsers[3]->getName()] = 'X';
|
|
|
|
$arg = array_fill_keys( array_keys( $expect ), 'X' );
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
|
|
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
|
|
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, $permitted ) );
|
|
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $nonPermitted ) );
|
|
}
|
|
|
|
public function testIsAttached() {
|
|
$lookup = $this->newLookup();
|
|
$user1 = UserIdentityValue::newRegistered( 42, 'Test' );
|
|
$user2 = UserIdentityValue::newAnonymous( 'DoesNotExist' );
|
|
|
|
$this->assertTrue( $lookup->isAttached( $user1 ) );
|
|
$this->assertFalse( $lookup->isAttached( $user2 ) );
|
|
|
|
$wiki = UserIdentityValue::LOCAL;
|
|
$this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
|
|
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
|
|
|
|
$wiki = 'some_other_wiki';
|
|
$this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
|
|
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideIsAttachedShared
|
|
* @param bool $sharedDB $wgSharedDB is set
|
|
* @param bool $sharedTable $wgSharedTables contains 'user'
|
|
* @param bool $localDBSet $wgLocalDatabases contains the shared DB
|
|
*/
|
|
public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
|
|
$lookup = $this->newLookup( [
|
|
MainConfigNames::SharedDB => $sharedDB ? "dummy" : null,
|
|
MainConfigNames::SharedTables => $sharedTable ? [ 'user' ] : [],
|
|
MainConfigNames::LocalDatabases => $localDBSet ? [ 'shared' ] : [],
|
|
] );
|
|
$this->assertSame(
|
|
$sharedDB && $sharedTable && $localDBSet,
|
|
$lookup->isAttached( UserIdentityValue::newRegistered( 42, 'Test' ), 'shared' )
|
|
);
|
|
}
|
|
|
|
public static function provideIsAttachedShared() {
|
|
$ret = [];
|
|
for ( $i = 0; $i < 7; $i++ ) {
|
|
$ret[] = [
|
|
(bool)( $i & 1 ),
|
|
(bool)( $i & 2 ),
|
|
(bool)( $i & 4 ),
|
|
];
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
}
|