4 * @covers LocalIdLookup
7 class LocalIdLookupTest
extends MediaWikiTestCase
{
8 private $localUsers = [];
10 protected function setUp() {
11 global $wgGroupPermissions;
15 $this->stashMwGlobals( [ 'wgGroupPermissions' ] );
16 $wgGroupPermissions['local-id-lookup-test']['hideuser'] = true;
19 public function addDBData() {
20 for ( $i = 1; $i <= 4; $i++
) {
21 $this->localUsers
[] = $this->getMutableTestUser()->getUser();
24 $sysop = static::getTestSysop()->getUser();
27 'address' => $this->localUsers
[2]->getName(),
28 'by' => $sysop->getId(),
29 'reason' => __METHOD__
,
36 'address' => $this->localUsers
[3]->getName(),
37 'by' => $sysop->getId(),
38 'reason' => __METHOD__
,
45 public function getLookupUser() {
46 return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
49 public function testLookupCentralIds() {
50 $lookup = new LocalIdLookup();
52 $user1 = $this->getLookupUser();
53 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
55 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
56 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
58 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
61 foreach ( $this->localUsers
as $localUser ) {
62 $expect[$localUser->getId()] = $localUser->getName();
68 $expect2[$this->localUsers
[3]->getId()] = '';
70 $arg = array_fill_keys( array_keys( $expect ), 'X' );
72 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
73 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
74 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
75 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
78 public function testLookupUserNames() {
79 $lookup = new LocalIdLookup();
80 $user1 = $this->getLookupUser();
81 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
83 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
84 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
86 $this->assertSame( [], $lookup->lookupUserNames( [] ) );
89 foreach ( $this->localUsers
as $localUser ) {
90 $expect[$localUser->getName()] = $localUser->getId();
92 $expect['UTDoesNotExist'] = 'X';
96 $expect2[$this->localUsers
[3]->getName()] = 'X';
98 $arg = array_fill_keys( array_keys( $expect ), 'X' );
100 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
101 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
102 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
103 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
106 public function testIsAttached() {
107 $lookup = new LocalIdLookup();
108 $user1 = $this->getLookupUser();
109 $user2 = User
::newFromName( 'DoesNotExist' );
111 $this->assertTrue( $lookup->isAttached( $user1 ) );
112 $this->assertFalse( $lookup->isAttached( $user2 ) );
115 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
116 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
118 $wiki = 'not-' . wfWikiID();
119 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
120 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
124 * @dataProvider provideIsAttachedShared
125 * @param bool $sharedDB $wgSharedDB is set
126 * @param bool $sharedTable $wgSharedTables contains 'user'
127 * @param bool $localDBSet $wgLocalDatabases contains the shared DB
129 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
131 $this->setMwGlobals( [
132 'wgSharedDB' => $sharedDB ?
$wgDBName : null,
133 'wgSharedTables' => $sharedTable ?
[ 'user' ] : [],
134 'wgLocalDatabases' => $localDBSet ?
[ 'shared' ] : [],
137 $lookup = new LocalIdLookup();
139 $sharedDB && $sharedTable && $localDBSet,
140 $lookup->isAttached( $this->getLookupUser(), 'shared' )
144 public static function provideIsAttachedShared() {
146 for ( $i = 0; $i < 7; $i++
) {