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