3 use MediaWiki\Block\DatabaseBlock
;
6 * @covers LocalIdLookup
9 class LocalIdLookupTest
extends MediaWikiTestCase
{
10 private $localUsers = [];
12 protected function setUp() {
15 $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
18 public function addDBData() {
19 for ( $i = 1; $i <= 4; $i++
) {
20 $this->localUsers
[] = $this->getMutableTestUser()->getUser();
23 $sysop = static::getTestSysop()->getUser();
25 $block = new DatabaseBlock( [
26 'address' => $this->localUsers
[2]->getName(),
27 'by' => $sysop->getId(),
28 'reason' => __METHOD__
,
34 $block = new DatabaseBlock( [
35 'address' => $this->localUsers
[3]->getName(),
36 'by' => $sysop->getId(),
37 'reason' => __METHOD__
,
44 public function getLookupUser() {
45 return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
48 public function testLookupCentralIds() {
49 $lookup = new LocalIdLookup();
51 $user1 = $this->getLookupUser();
52 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
54 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
55 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
57 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
60 foreach ( $this->localUsers
as $localUser ) {
61 $expect[$localUser->getId()] = $localUser->getName();
67 $expect2[$this->localUsers
[3]->getId()] = '';
69 $arg = array_fill_keys( array_keys( $expect ), 'X' );
71 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
72 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
73 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
74 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
77 public function testLookupUserNames() {
78 $lookup = new LocalIdLookup();
79 $user1 = $this->getLookupUser();
80 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
82 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
83 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
85 $this->assertSame( [], $lookup->lookupUserNames( [] ) );
88 foreach ( $this->localUsers
as $localUser ) {
89 $expect[$localUser->getName()] = $localUser->getId();
91 $expect['UTDoesNotExist'] = 'X';
95 $expect2[$this->localUsers
[3]->getName()] = 'X';
97 $arg = array_fill_keys( array_keys( $expect ), 'X' );
99 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
100 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
101 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
102 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
105 public function testIsAttached() {
106 $lookup = new LocalIdLookup();
107 $user1 = $this->getLookupUser();
108 $user2 = User
::newFromName( 'DoesNotExist' );
110 $this->assertTrue( $lookup->isAttached( $user1 ) );
111 $this->assertFalse( $lookup->isAttached( $user2 ) );
114 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
115 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
117 $wiki = 'not-' . wfWikiID();
118 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
119 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
123 * @dataProvider provideIsAttachedShared
124 * @param bool $sharedDB $wgSharedDB is set
125 * @param bool $sharedTable $wgSharedTables contains 'user'
126 * @param bool $localDBSet $wgLocalDatabases contains the shared DB
128 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
129 $this->setMwGlobals( [
130 'wgSharedDB' => $sharedDB ?
"dummy" : null,
131 'wgSharedTables' => $sharedTable ?
[ 'user' ] : [],
132 'wgLocalDatabases' => $localDBSet ?
[ 'shared' ] : [],
135 $lookup = new LocalIdLookup();
137 $sharedDB && $sharedTable && $localDBSet,
138 $lookup->isAttached( $this->getLookupUser(), 'shared' )
142 public static function provideIsAttachedShared() {
144 for ( $i = 0; $i < 7; $i++
) {