4 * @covers LocalIdLookup
7 class LocalIdLookupTest
extends MediaWikiTestCase
{
8 private $localUsers = [];
10 protected function setUp() {
13 $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
16 public function addDBData() {
17 for ( $i = 1; $i <= 4; $i++
) {
18 $this->localUsers
[] = $this->getMutableTestUser()->getUser();
21 $sysop = static::getTestSysop()->getUser();
24 'address' => $this->localUsers
[2]->getName(),
25 'by' => $sysop->getId(),
26 'reason' => __METHOD__
,
33 'address' => $this->localUsers
[3]->getName(),
34 'by' => $sysop->getId(),
35 'reason' => __METHOD__
,
42 public function getLookupUser() {
43 return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
46 public function testLookupCentralIds() {
47 $lookup = new LocalIdLookup();
49 $user1 = $this->getLookupUser();
50 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
52 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
53 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
55 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
58 foreach ( $this->localUsers
as $localUser ) {
59 $expect[$localUser->getId()] = $localUser->getName();
65 $expect2[$this->localUsers
[3]->getId()] = '';
67 $arg = array_fill_keys( array_keys( $expect ), 'X' );
69 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
70 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
71 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
72 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
75 public function testLookupUserNames() {
76 $lookup = new LocalIdLookup();
77 $user1 = $this->getLookupUser();
78 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
80 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
81 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
83 $this->assertSame( [], $lookup->lookupUserNames( [] ) );
86 foreach ( $this->localUsers
as $localUser ) {
87 $expect[$localUser->getName()] = $localUser->getId();
89 $expect['UTDoesNotExist'] = 'X';
93 $expect2[$this->localUsers
[3]->getName()] = 'X';
95 $arg = array_fill_keys( array_keys( $expect ), 'X' );
97 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
98 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
99 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
100 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
103 public function testIsAttached() {
104 $lookup = new LocalIdLookup();
105 $user1 = $this->getLookupUser();
106 $user2 = User
::newFromName( 'DoesNotExist' );
108 $this->assertTrue( $lookup->isAttached( $user1 ) );
109 $this->assertFalse( $lookup->isAttached( $user2 ) );
112 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
113 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
115 $wiki = 'not-' . wfWikiID();
116 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
117 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
121 * @dataProvider provideIsAttachedShared
122 * @param bool $sharedDB $wgSharedDB is set
123 * @param bool $sharedTable $wgSharedTables contains 'user'
124 * @param bool $localDBSet $wgLocalDatabases contains the shared DB
126 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
128 $this->setMwGlobals( [
129 'wgSharedDB' => $sharedDB ?
$wgDBName : null,
130 'wgSharedTables' => $sharedTable ?
[ 'user' ] : [],
131 'wgLocalDatabases' => $localDBSet ?
[ 'shared' ] : [],
134 $lookup = new LocalIdLookup();
136 $sharedDB && $sharedTable && $localDBSet,
137 $lookup->isAttached( $this->getLookupUser(), 'shared' )
141 public static function provideIsAttachedShared() {
143 for ( $i = 0; $i < 7; $i++
) {