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