| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\DatabaseBlock; |
| 4 |
use MediaWiki\Config\HashConfig; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 7 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 8 |
use MediaWiki\User\CentralId\LocalIdLookup; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
use MediaWiki\User\UserIdentityValue; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class LocalIdLookupTest extends MediaWikiIntegrationTestCase { |
| 17 |
use MockAuthorityTrait; |
| 18 |
|
| 19 |
|
| 20 |
private $localUsers = []; |
| 21 |
|
| 22 |
public function addDBData() { |
| 23 |
for ( $i = 1; $i <= 4; $i++ ) { |
| 24 |
$this->localUsers[] = $this->getMutableTestUser()->getUserIdentity(); |
| 25 |
} |
| 26 |
|
| 27 |
$sysop = static::getTestSysop()->getUserIdentity(); |
| 28 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 29 |
|
| 30 |
$block = new DatabaseBlock( [ |
| 31 |
'address' => $this->localUsers[2]->getName(), |
| 32 |
'by' => $sysop, |
| 33 |
'reason' => __METHOD__, |
| 34 |
'expiry' => '1 day', |
| 35 |
'hideName' => false, |
| 36 |
] ); |
| 37 |
$blockStore->insertBlock( $block ); |
| 38 |
|
| 39 |
$block = new DatabaseBlock( [ |
| 40 |
'address' => $this->localUsers[3]->getName(), |
| 41 |
'by' => $sysop, |
| 42 |
'reason' => __METHOD__, |
| 43 |
'expiry' => '1 day', |
| 44 |
'hideName' => true, |
| 45 |
] ); |
| 46 |
$blockStore->insertBlock( $block ); |
| 47 |
} |
| 48 |
|
| 49 |
private function newLookup( array $configOverride = [] ) { |
| 50 |
$lookup = new LocalIdLookup( |
| 51 |
new HashConfig( [ |
| 52 |
MainConfigNames::SharedDB => null, |
| 53 |
MainConfigNames::SharedTables => [], |
| 54 |
MainConfigNames::LocalDatabases => [], |
| 55 |
] + $configOverride ), |
| 56 |
$this->getServiceContainer()->getConnectionProvider(), |
| 57 |
$this->getServiceContainer()->getHideUserUtils() |
| 58 |
); |
| 59 |
$lookup->init( |
| 60 |
'test', |
| 61 |
$this->getServiceContainer()->getUserIdentityLookup(), |
| 62 |
$this->getServiceContainer()->getUserFactory() |
| 63 |
); |
| 64 |
return $lookup; |
| 65 |
} |
| 66 |
|
| 67 |
public function testLookupCentralIds() { |
| 68 |
$lookup = $this->newLookup(); |
| 69 |
$permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] ); |
| 70 |
$nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] ); |
| 71 |
|
| 72 |
$this->assertSame( [], $lookup->lookupCentralIds( [] ) ); |
| 73 |
|
| 74 |
$expect = []; |
| 75 |
foreach ( $this->localUsers as $localUser ) { |
| 76 |
$expect[$localUser->getId()] = $localUser->getName(); |
| 77 |
} |
| 78 |
$expect[12345] = 'X'; |
| 79 |
ksort( $expect ); |
| 80 |
|
| 81 |
$expect2 = $expect; |
| 82 |
$expect2[$this->localUsers[3]->getId()] = ''; |
| 83 |
|
| 84 |
$arg = array_fill_keys( array_keys( $expect ), 'X' ); |
| 85 |
|
| 86 |
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) ); |
| 87 |
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) ); |
| 88 |
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $permitted ) ); |
| 89 |
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $nonPermitted ) ); |
| 90 |
} |
| 91 |
|
| 92 |
public function testLookupUserNames() { |
| 93 |
$lookup = $this->newLookup(); |
| 94 |
$permitted = $this->mockAnonAuthorityWithPermissions( [ 'hideuser' ] ); |
| 95 |
$nonPermitted = $this->mockAnonAuthorityWithoutPermissions( [ 'hideuser' ] ); |
| 96 |
|
| 97 |
$this->assertSame( [], $lookup->lookupUserNames( [] ) ); |
| 98 |
|
| 99 |
$expect = []; |
| 100 |
foreach ( $this->localUsers as $localUser ) { |
| 101 |
$expect[$localUser->getName()] = $localUser->getId(); |
| 102 |
} |
| 103 |
$expect['UTDoesNotExist'] = 'X'; |
| 104 |
ksort( $expect ); |
| 105 |
|
| 106 |
$expect2 = $expect; |
| 107 |
$expect2[$this->localUsers[3]->getName()] = 'X'; |
| 108 |
|
| 109 |
$arg = array_fill_keys( array_keys( $expect ), 'X' ); |
| 110 |
|
| 111 |
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) ); |
| 112 |
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) ); |
| 113 |
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, $permitted ) ); |
| 114 |
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $nonPermitted ) ); |
| 115 |
} |
| 116 |
|
| 117 |
public function testIsAttached() { |
| 118 |
$lookup = $this->newLookup(); |
| 119 |
$user1 = UserIdentityValue::newRegistered( 42, 'Test' ); |
| 120 |
$user2 = UserIdentityValue::newAnonymous( 'DoesNotExist' ); |
| 121 |
|
| 122 |
$this->assertTrue( $lookup->isAttached( $user1 ) ); |
| 123 |
$this->assertFalse( $lookup->isAttached( $user2 ) ); |
| 124 |
|
| 125 |
$wiki = UserIdentityValue::LOCAL; |
| 126 |
$this->assertTrue( $lookup->isAttached( $user1, $wiki ) ); |
| 127 |
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) ); |
| 128 |
|
| 129 |
$wiki = 'some_other_wiki'; |
| 130 |
$this->assertFalse( $lookup->isAttached( $user1, $wiki ) ); |
| 131 |
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) { |
| 141 |
$lookup = $this->newLookup( [ |
| 142 |
MainConfigNames::SharedDB => $sharedDB ? "dummy" : null, |
| 143 |
MainConfigNames::SharedTables => $sharedTable ? [ 'user' ] : [], |
| 144 |
MainConfigNames::LocalDatabases => $localDBSet ? [ 'shared' ] : [], |
| 145 |
] ); |
| 146 |
$this->assertSame( |
| 147 |
$sharedDB && $sharedTable && $localDBSet, |
| 148 |
$lookup->isAttached( UserIdentityValue::newRegistered( 42, 'Test' ), 'shared' ) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
public static function provideIsAttachedShared() { |
| 153 |
$ret = []; |
| 154 |
for ( $i = 0; $i < 7; $i++ ) { |
| 155 |
$ret[] = [ |
| 156 |
(bool)( $i & 1 ), |
| 157 |
(bool)( $i & 2 ), |
| 158 |
(bool)( $i & 4 ), |
| 159 |
]; |
| 160 |
} |
| 161 |
return $ret; |
| 162 |
} |
| 163 |
|
| 164 |
} |