1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
<?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\UserIdentity;
use MediaWiki\User\UserIdentityValue;
/**
* @covers \MediaWiki\User\CentralId\LocalIdLookup
* @group Database
*/
class LocalIdLookupTest extends MediaWikiIntegrationTestCase {
use MockAuthorityTrait;
/** @var UserIdentity[] */
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 = [] ) {
$lookup = new LocalIdLookup(
new HashConfig( [
MainConfigNames::SharedDB => null,
MainConfigNames::SharedTables => [],
MainConfigNames::LocalDatabases => [],
] + $configOverride ),
$this->getServiceContainer()->getConnectionProvider(),
$this->getServiceContainer()->getHideUserUtils()
);
$lookup->init(
'test',
$this->getServiceContainer()->getUserIdentityLookup(),
$this->getServiceContainer()->getUserFactory()
);
return $lookup;
}
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;
}
}
|