2015-11-12 23:21:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2015-11-12 23:21:19 +00:00
|
|
|
/**
|
|
|
|
|
* @covers CentralIdLookup
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class CentralIdLookupTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
public function testFactory() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgCentralIdLookupProviders' => [
|
2018-01-13 00:02:09 +00:00
|
|
|
'local' => [ 'class' => LocalIdLookup::class ],
|
|
|
|
|
'local2' => [ 'class' => LocalIdLookup::class ],
|
2016-02-17 09:09:32 +00:00
|
|
|
'mock' => [ 'factory' => function () use ( $mock ) {
|
2015-11-12 23:21:19 +00:00
|
|
|
return $mock;
|
2016-02-17 09:09:32 +00:00
|
|
|
} ],
|
2018-01-13 00:02:09 +00:00
|
|
|
'bad' => [ 'class' => stdClass::class ],
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-11-12 23:21:19 +00:00
|
|
|
'wgCentralIdLookupProvider' => 'mock',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$this->assertSame( $mock, CentralIdLookup::factory() );
|
|
|
|
|
$this->assertSame( $mock, CentralIdLookup::factory( 'mock' ) );
|
|
|
|
|
$this->assertSame( 'mock', $mock->getProviderId() );
|
|
|
|
|
|
|
|
|
|
$local = CentralIdLookup::factory( 'local' );
|
|
|
|
|
$this->assertNotSame( $mock, $local );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( LocalIdLookup::class, $local );
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertSame( $local, CentralIdLookup::factory( 'local' ) );
|
|
|
|
|
$this->assertSame( 'local', $local->getProviderId() );
|
|
|
|
|
|
|
|
|
|
$local2 = CentralIdLookup::factory( 'local2' );
|
|
|
|
|
$this->assertNotSame( $local, $local2 );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( LocalIdLookup::class, $local2 );
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertSame( 'local2', $local2->getProviderId() );
|
|
|
|
|
|
|
|
|
|
$this->assertNull( CentralIdLookup::factory( 'unconfigured' ) );
|
|
|
|
|
$this->assertNull( CentralIdLookup::factory( 'bad' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCheckAudience() {
|
|
|
|
|
$mock = TestingAccessWrapper::newFromObject(
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->getMockForAbstractClass( CentralIdLookup::class )
|
2015-11-12 23:21:19 +00:00
|
|
|
);
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = static::getTestSysop()->getUser();
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertSame( $user, $mock->checkAudience( $user ) );
|
|
|
|
|
|
|
|
|
|
$user = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( User::class, $user );
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertSame( 0, $user->getId() );
|
|
|
|
|
|
|
|
|
|
$this->assertNull( $mock->checkAudience( CentralIdLookup::AUDIENCE_RAW ) );
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$mock->checkAudience( 100 );
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
|
|
|
|
$this->assertSame( 'Invalid audience', $ex->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNameFromCentralId() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->once() )->method( 'lookupCentralIds' )
|
|
|
|
|
->with(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->equalTo( [ 15 => null ] ),
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
|
|
|
|
|
$this->equalTo( CentralIdLookup::READ_LATEST )
|
|
|
|
|
)
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [ 15 => 'FooBar' ] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'FooBar',
|
|
|
|
|
$mock->nameFromCentralId( 15, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideLocalUserFromCentralId
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param bool $succeeds
|
|
|
|
|
*/
|
|
|
|
|
public function testLocalUserFromCentralId( $name, $succeeds ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->any() )->method( 'isAttached' )
|
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
$mock->expects( $this->once() )->method( 'lookupCentralIds' )
|
|
|
|
|
->with(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->equalTo( [ 42 => null ] ),
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
|
|
|
|
|
$this->equalTo( CentralIdLookup::READ_LATEST )
|
|
|
|
|
)
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [ 42 => $name ] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$user = $mock->localUserFromCentralId(
|
|
|
|
|
42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
|
|
|
|
|
);
|
|
|
|
|
if ( $succeeds ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( User::class, $user );
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertSame( $name, $user->getName() );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertNull( $user );
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->any() )->method( 'isAttached' )
|
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
$mock->expects( $this->once() )->method( 'lookupCentralIds' )
|
|
|
|
|
->with(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->equalTo( [ 42 => null ] ),
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
|
|
|
|
|
$this->equalTo( CentralIdLookup::READ_LATEST )
|
|
|
|
|
)
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [ 42 => $name ] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->assertNull(
|
|
|
|
|
$mock->localUserFromCentralId( 42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideLocalUserFromCentralId() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'UTSysop', true ],
|
|
|
|
|
[ 'UTDoesNotExist', false ],
|
|
|
|
|
[ null, false ],
|
|
|
|
|
[ '', false ],
|
|
|
|
|
[ '<X>', false ],
|
|
|
|
|
];
|
2015-11-12 23:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCentralIdFromName() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->once() )->method( 'lookupUserNames' )
|
|
|
|
|
->with(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->equalTo( [ 'FooBar' => 0 ] ),
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
|
|
|
|
|
$this->equalTo( CentralIdLookup::READ_LATEST )
|
|
|
|
|
)
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
23,
|
|
|
|
|
$mock->centralIdFromName( 'FooBar', CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCentralIdFromLocalUser() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->any() )->method( 'isAttached' )
|
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
$mock->expects( $this->once() )->method( 'lookupUserNames' )
|
|
|
|
|
->with(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->equalTo( [ 'FooBar' => 0 ] ),
|
2015-11-12 23:21:19 +00:00
|
|
|
$this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
|
|
|
|
|
$this->equalTo( CentralIdLookup::READ_LATEST )
|
|
|
|
|
)
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
|
2015-11-12 23:21:19 +00:00
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
23,
|
|
|
|
|
$mock->centralIdFromLocalUser(
|
|
|
|
|
User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$mock = $this->getMockForAbstractClass( CentralIdLookup::class );
|
2015-11-12 23:21:19 +00:00
|
|
|
$mock->expects( $this->any() )->method( 'isAttached' )
|
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
$mock->expects( $this->never() )->method( 'lookupUserNames' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
0,
|
|
|
|
|
$mock->centralIdFromLocalUser(
|
|
|
|
|
User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|