2015-11-12 23:21:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers LocalIdLookup
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class LocalIdLookupTest extends MediaWikiTestCase {
|
2016-02-17 09:09:32 +00:00
|
|
|
private $localUsers = [];
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2018-10-07 12:26:18 +00:00
|
|
|
$this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
|
2015-11-12 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addDBData() {
|
|
|
|
|
for ( $i = 1; $i <= 4; $i++ ) {
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->localUsers[] = $this->getMutableTestUser()->getUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$sysop = static::getTestSysop()->getUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$block = new Block( [
|
2016-05-18 09:19:20 +00:00
|
|
|
'address' => $this->localUsers[2]->getName(),
|
|
|
|
|
'by' => $sysop->getId(),
|
2015-11-12 23:21:19 +00:00
|
|
|
'reason' => __METHOD__,
|
|
|
|
|
'expiry' => '1 day',
|
|
|
|
|
'hideName' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-12 23:21:19 +00:00
|
|
|
$block->insert();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$block = new Block( [
|
2016-05-18 09:19:20 +00:00
|
|
|
'address' => $this->localUsers[3]->getName(),
|
|
|
|
|
'by' => $sysop->getId(),
|
2015-11-12 23:21:19 +00:00
|
|
|
'reason' => __METHOD__,
|
|
|
|
|
'expiry' => '1 day',
|
|
|
|
|
'hideName' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-12 23:21:19 +00:00
|
|
|
$block->insert();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
public function getLookupUser() {
|
|
|
|
|
return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-12 23:21:19 +00:00
|
|
|
public function testLookupCentralIds() {
|
|
|
|
|
$lookup = new LocalIdLookup();
|
2016-05-18 09:19:20 +00:00
|
|
|
|
|
|
|
|
$user1 = $this->getLookupUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
$user2 = User::newFromName( 'UTLocalIdLookup2' );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
|
|
|
|
|
$this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $lookup->lookupCentralIds( [] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$expect = [];
|
|
|
|
|
foreach ( $this->localUsers as $localUser ) {
|
|
|
|
|
$expect[$localUser->getId()] = $localUser->getName();
|
|
|
|
|
}
|
|
|
|
|
$expect[12345] = 'X';
|
2015-11-12 23:21:19 +00:00
|
|
|
ksort( $expect );
|
|
|
|
|
|
|
|
|
|
$expect2 = $expect;
|
2016-05-18 09:19:20 +00:00
|
|
|
$expect2[$this->localUsers[3]->getId()] = '';
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$arg = array_fill_keys( array_keys( $expect ), 'X' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
|
|
|
|
|
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
|
|
|
|
|
$this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
|
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLookupUserNames() {
|
|
|
|
|
$lookup = new LocalIdLookup();
|
2016-05-18 09:19:20 +00:00
|
|
|
$user1 = $this->getLookupUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
$user2 = User::newFromName( 'UTLocalIdLookup2' );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
|
|
|
|
|
$this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [], $lookup->lookupUserNames( [] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$expect = [];
|
|
|
|
|
foreach ( $this->localUsers as $localUser ) {
|
|
|
|
|
$expect[$localUser->getName()] = $localUser->getId();
|
|
|
|
|
}
|
2015-11-12 23:21:19 +00:00
|
|
|
$expect['UTDoesNotExist'] = 'X';
|
|
|
|
|
ksort( $expect );
|
|
|
|
|
|
|
|
|
|
$expect2 = $expect;
|
2016-05-18 09:19:20 +00:00
|
|
|
$expect2[$this->localUsers[3]->getName()] = 'X';
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$arg = array_fill_keys( array_keys( $expect ), 'X' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
|
|
|
|
|
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
|
|
|
|
|
$this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
|
|
|
|
|
$this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIsAttached() {
|
|
|
|
|
$lookup = new LocalIdLookup();
|
2016-05-18 09:19:20 +00:00
|
|
|
$user1 = $this->getLookupUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
$user2 = User::newFromName( 'DoesNotExist' );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $lookup->isAttached( $user1 ) );
|
|
|
|
|
$this->assertFalse( $lookup->isAttached( $user2 ) );
|
|
|
|
|
|
2016-03-18 13:55:54 +00:00
|
|
|
$wiki = wfWikiID();
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
|
|
|
|
|
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
|
|
|
|
|
|
2016-03-18 13:55:54 +00:00
|
|
|
$wiki = 'not-' . wfWikiID();
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
|
|
|
|
|
$this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsAttachedShared
|
|
|
|
|
* @param bool $sharedDB $wgSharedDB is set
|
|
|
|
|
* @param bool $sharedTable $wgSharedTables contains 'user'
|
|
|
|
|
* @param bool $localDBSet $wgLocalDatabases contains the shared DB
|
|
|
|
|
*/
|
|
|
|
|
public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2018-11-27 15:47:07 +00:00
|
|
|
'wgSharedDB' => $sharedDB ? "dummy" : null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
|
|
|
|
|
'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
|
|
|
|
|
] );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$lookup = new LocalIdLookup();
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$sharedDB && $sharedTable && $localDBSet,
|
2016-05-18 09:19:20 +00:00
|
|
|
$lookup->isAttached( $this->getLookupUser(), 'shared' )
|
2015-11-12 23:21:19 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideIsAttachedShared() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2015-11-12 23:21:19 +00:00
|
|
|
for ( $i = 0; $i < 7; $i++ ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret[] = [
|
2015-11-12 23:21:19 +00:00
|
|
|
(bool)( $i & 1 ),
|
|
|
|
|
(bool)( $i & 2 ),
|
|
|
|
|
(bool)( $i & 4 ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-11-12 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|