4 * @covers LocalIdLookup
7 class LocalIdLookupTest
extends MediaWikiTestCase
{
8 private $localUsers = array();
10 protected function setUp() {
11 global $wgGroupPermissions;
15 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
16 $wgGroupPermissions['local-id-lookup-test']['hideuser'] = true;
19 public function addDBData() {
20 for ( $i = 1; $i <= 4; $i++
) {
21 $user = User
::newFromName( "UTLocalIdLookup$i" );
22 if ( $user->getId() == 0 ) {
23 $user->addToDatabase();
25 $this->localUsers
["UTLocalIdLookup$i"] = $user->getId();
28 User
::newFromName( 'UTLocalIdLookup1' )->addGroup( 'local-id-lookup-test' );
30 $block = new Block( array(
31 'address' => 'UTLocalIdLookup3',
32 'by' => User
::idFromName( 'UTSysop' ),
33 'reason' => __METHOD__
,
39 $block = new Block( array(
40 'address' => 'UTLocalIdLookup4',
41 'by' => User
::idFromName( 'UTSysop' ),
42 'reason' => __METHOD__
,
49 public function testLookupCentralIds() {
50 $lookup = new LocalIdLookup();
51 $user1 = User
::newFromName( 'UTLocalIdLookup1' );
52 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
54 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
55 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
57 $this->assertSame( array(), $lookup->lookupCentralIds( array() ) );
59 $expect = array_flip( $this->localUsers
);
64 $expect2[$this->localUsers
['UTLocalIdLookup4']] = '';
66 $arg = array_fill_keys( array_keys( $expect ), 'X' );
68 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
69 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
70 $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
71 $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
74 public function testLookupUserNames() {
75 $lookup = new LocalIdLookup();
76 $user1 = User
::newFromName( 'UTLocalIdLookup1' );
77 $user2 = User
::newFromName( 'UTLocalIdLookup2' );
79 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
80 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
82 $this->assertSame( array(), $lookup->lookupUserNames( array() ) );
84 $expect = $this->localUsers
;
85 $expect['UTDoesNotExist'] = 'X';
89 $expect2['UTLocalIdLookup4'] = 'X';
91 $arg = array_fill_keys( array_keys( $expect ), 'X' );
93 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
94 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup
::AUDIENCE_RAW
) );
95 $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
96 $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
99 public function testIsAttached() {
100 $lookup = new LocalIdLookup();
101 $user1 = User
::newFromName( 'UTLocalIdLookup1' );
102 $user2 = User
::newFromName( 'DoesNotExist' );
104 $this->assertTrue( $lookup->isAttached( $user1 ) );
105 $this->assertFalse( $lookup->isAttached( $user2 ) );
108 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
109 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
111 $wiki = 'not-' . wfWikiId();
112 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
113 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
117 * @dataProvider provideIsAttachedShared
118 * @param bool $sharedDB $wgSharedDB is set
119 * @param bool $sharedTable $wgSharedTables contains 'user'
120 * @param bool $localDBSet $wgLocalDatabases contains the shared DB
122 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
124 $this->setMwGlobals( array(
125 'wgSharedDB' => $sharedDB ?
$wgDBName : null,
126 'wgSharedTables' => $sharedTable ?
array( 'user' ) : array(),
127 'wgLocalDatabases' => $localDBSet ?
array( 'shared' ) : array(),
130 $lookup = new LocalIdLookup();
132 $sharedDB && $sharedTable && $localDBSet,
133 $lookup->isAttached( User
::newFromName( 'UTLocalIdLookup1' ), 'shared' )
137 public static function provideIsAttachedShared() {
139 for ( $i = 0; $i < 7; $i++
) {