3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\Config\HashConfig
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
7 use MediaWiki\User\CentralId\CentralIdLookup
;
8 use MediaWiki\User\CentralId\LocalIdLookup
;
9 use MediaWiki\User\UserIdentity
;
10 use MediaWiki\User\UserIdentityValue
;
13 * @covers \MediaWiki\User\CentralId\LocalIdLookup
16 class LocalIdLookupTest
extends MediaWikiIntegrationTestCase
{
17 use MockAuthorityTrait
;
19 /** @var UserIdentity[] */
20 private $localUsers = [];
22 public function addDBData() {
23 for ( $i = 1; $i <= 4; $i++
) {
24 $this->localUsers
[] = $this->getMutableTestUser()->getUserIdentity();
27 $sysop = static::getTestSysop()->getUserIdentity();
28 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
30 $block = new DatabaseBlock( [
31 'address' => $this->localUsers
[2]->getName(),
33 'reason' => __METHOD__
,
37 $blockStore->insertBlock( $block );
39 $block = new DatabaseBlock( [
40 'address' => $this->localUsers
[3]->getName(),
42 'reason' => __METHOD__
,
46 $blockStore->insertBlock( $block );
49 private function newLookup( array $configOverride = [] ) {
50 $lookup = new LocalIdLookup(
52 MainConfigNames
::SharedDB
=> null,
53 MainConfigNames
::SharedTables
=> [],
54 MainConfigNames
::LocalDatabases
=> [],
55 ] +
$configOverride ),
56 $this->getServiceContainer()->getConnectionProvider(),
57 $this->getServiceContainer()->getHideUserUtils()
61 $this->getServiceContainer()->getUserIdentityLookup(),
62 $this->getServiceContainer()->getUserFactory()
67 public function testLookupCentralIds() {
68 $lookup = $this->newLookup();
69 $permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] );
70 $nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] );
72 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
75 foreach ( $this->localUsers
as $localUser ) {
76 $expect[$localUser->getId()] = $localUser->getName();
82 $expect2[$this->localUsers
[3]->getId()] = '';
84 $arg = array_fill_keys( array_keys( $expect ), 'X' );
86 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
87 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
88 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $permitted ) );
89 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $nonPermitted ) );
92 public function testLookupUserNames() {
93 $lookup = $this->newLookup();
94 $permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] );
95 $nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] );
97 $this->assertSame( [], $lookup->lookupUserNames( [] ) );
100 foreach ( $this->localUsers
as $localUser ) {
101 $expect[$localUser->getName()] = $localUser->getId();
103 $expect['UTDoesNotExist'] = 'X';
107 $expect2[$this->localUsers
[3]->getName()] = 'X';
109 $arg = array_fill_keys( array_keys( $expect ), 'X' );
111 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
112 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
113 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $permitted ) );
114 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $nonPermitted ) );
117 public function testIsAttached() {
118 $lookup = $this->newLookup();
119 $user1 = UserIdentityValue
::newRegistered( 42, 'Test' );
120 $user2 = UserIdentityValue
::newAnonymous( 'DoesNotExist' );
122 $this->assertTrue( $lookup->isAttached( $user1 ) );
123 $this->assertFalse( $lookup->isAttached( $user2 ) );
125 $wiki = UserIdentityValue
::LOCAL
;
126 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
127 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
129 $wiki = 'some_other_wiki';
130 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
131 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
135 * @dataProvider provideIsAttachedShared
136 * @param bool $sharedDB $wgSharedDB is set
137 * @param bool $sharedTable $wgSharedTables contains 'user'
138 * @param bool $localDBSet $wgLocalDatabases contains the shared DB
140 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
141 $lookup = $this->newLookup( [
142 MainConfigNames
::SharedDB
=> $sharedDB ?
"dummy" : null,
143 MainConfigNames
::SharedTables
=> $sharedTable ?
[ 'user' ] : [],
144 MainConfigNames
::LocalDatabases
=> $localDBSet ?
[ 'shared' ] : [],
147 $sharedDB && $sharedTable && $localDBSet,
148 $lookup->isAttached( UserIdentityValue
::newRegistered( 42, 'Test' ), 'shared' )
152 public static function provideIsAttachedShared() {
154 for ( $i = 0; $i < 7; $i++
) {