Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / user / LocalIdLookupTest.php
blob459418c2c2f6ff0dbbae838e4a32402cce064f7d
1 <?php
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;
12 /**
13 * @covers \MediaWiki\User\CentralId\LocalIdLookup
14 * @group Database
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(),
32 'by' => $sysop,
33 'reason' => __METHOD__,
34 'expiry' => '1 day',
35 'hideName' => false,
36 ] );
37 $blockStore->insertBlock( $block );
39 $block = new DatabaseBlock( [
40 'address' => $this->localUsers[3]->getName(),
41 'by' => $sysop,
42 'reason' => __METHOD__,
43 'expiry' => '1 day',
44 'hideName' => true,
45 ] );
46 $blockStore->insertBlock( $block );
49 private function newLookup( array $configOverride = [] ) {
50 $lookup = new LocalIdLookup(
51 new HashConfig( [
52 MainConfigNames::SharedDB => null,
53 MainConfigNames::SharedTables => [],
54 MainConfigNames::LocalDatabases => [],
55 ] + $configOverride ),
56 $this->getServiceContainer()->getConnectionProvider(),
57 $this->getServiceContainer()->getHideUserUtils()
59 $lookup->init(
60 'test',
61 $this->getServiceContainer()->getUserIdentityLookup(),
62 $this->getServiceContainer()->getUserFactory()
64 return $lookup;
67 public function testLookupCentralIds() {
68 $lookup = $this->newLookup();
69 $permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] );
70 $nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] );
72 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
74 $expect = [];
75 foreach ( $this->localUsers as $localUser ) {
76 $expect[$localUser->getId()] = $localUser->getName();
78 $expect[12345] = 'X';
79 ksort( $expect );
81 $expect2 = $expect;
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( [] ) );
99 $expect = [];
100 foreach ( $this->localUsers as $localUser ) {
101 $expect[$localUser->getName()] = $localUser->getId();
103 $expect['UTDoesNotExist'] = 'X';
104 ksort( $expect );
106 $expect2 = $expect;
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' ] : [],
145 ] );
146 $this->assertSame(
147 $sharedDB && $sharedTable && $localDBSet,
148 $lookup->isAttached( UserIdentityValue::newRegistered( 42, 'Test' ), 'shared' )
152 public static function provideIsAttachedShared() {
153 $ret = [];
154 for ( $i = 0; $i < 7; $i++ ) {
155 $ret[] = [
156 (bool)( $i & 1 ),
157 (bool)( $i & 2 ),
158 (bool)( $i & 4 ),
161 return $ret;