Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / user / LocalIdLookupTest.php
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4
5 /**
6 * @covers LocalIdLookup
7 * @group Database
8 */
9 class LocalIdLookupTest extends MediaWikiTestCase {
10 private $localUsers = [];
11
12 protected function setUp() {
13 parent::setUp();
14
15 $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
16 }
17
18 public function addDBData() {
19 for ( $i = 1; $i <= 4; $i++ ) {
20 $this->localUsers[] = $this->getMutableTestUser()->getUser();
21 }
22
23 $sysop = static::getTestSysop()->getUser();
24
25 $block = new DatabaseBlock( [
26 'address' => $this->localUsers[2]->getName(),
27 'by' => $sysop->getId(),
28 'reason' => __METHOD__,
29 'expiry' => '1 day',
30 'hideName' => false,
31 ] );
32 $block->insert();
33
34 $block = new DatabaseBlock( [
35 'address' => $this->localUsers[3]->getName(),
36 'by' => $sysop->getId(),
37 'reason' => __METHOD__,
38 'expiry' => '1 day',
39 'hideName' => true,
40 ] );
41 $block->insert();
42 }
43
44 public function getLookupUser() {
45 return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
46 }
47
48 public function testLookupCentralIds() {
49 $lookup = new LocalIdLookup();
50
51 $user1 = $this->getLookupUser();
52 $user2 = User::newFromName( 'UTLocalIdLookup2' );
53
54 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
55 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
56
57 $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
58
59 $expect = [];
60 foreach ( $this->localUsers as $localUser ) {
61 $expect[$localUser->getId()] = $localUser->getName();
62 }
63 $expect[12345] = 'X';
64 ksort( $expect );
65
66 $expect2 = $expect;
67 $expect2[$this->localUsers[3]->getId()] = '';
68
69 $arg = array_fill_keys( array_keys( $expect ), 'X' );
70
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 ) );
75 }
76
77 public function testLookupUserNames() {
78 $lookup = new LocalIdLookup();
79 $user1 = $this->getLookupUser();
80 $user2 = User::newFromName( 'UTLocalIdLookup2' );
81
82 $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
83 $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
84
85 $this->assertSame( [], $lookup->lookupUserNames( [] ) );
86
87 $expect = [];
88 foreach ( $this->localUsers as $localUser ) {
89 $expect[$localUser->getName()] = $localUser->getId();
90 }
91 $expect['UTDoesNotExist'] = 'X';
92 ksort( $expect );
93
94 $expect2 = $expect;
95 $expect2[$this->localUsers[3]->getName()] = 'X';
96
97 $arg = array_fill_keys( array_keys( $expect ), 'X' );
98
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 ) );
103 }
104
105 public function testIsAttached() {
106 $lookup = new LocalIdLookup();
107 $user1 = $this->getLookupUser();
108 $user2 = User::newFromName( 'DoesNotExist' );
109
110 $this->assertTrue( $lookup->isAttached( $user1 ) );
111 $this->assertFalse( $lookup->isAttached( $user2 ) );
112
113 $wiki = wfWikiID();
114 $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
115 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
116
117 $wiki = 'not-' . wfWikiID();
118 $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
119 $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
120 }
121
122 /**
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
127 */
128 public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
129 $this->setMwGlobals( [
130 'wgSharedDB' => $sharedDB ? "dummy" : null,
131 'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
132 'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
133 ] );
134
135 $lookup = new LocalIdLookup();
136 $this->assertSame(
137 $sharedDB && $sharedTable && $localDBSet,
138 $lookup->isAttached( $this->getLookupUser(), 'shared' )
139 );
140 }
141
142 public static function provideIsAttachedShared() {
143 $ret = [];
144 for ( $i = 0; $i < 7; $i++ ) {
145 $ret[] = [
146 (bool)( $i & 1 ),
147 (bool)( $i & 2 ),
148 (bool)( $i & 4 ),
149 ];
150 }
151 return $ret;
152 }
153
154 }