3 use Wikimedia\TestingAccessWrapper
;
6 * @covers CentralIdLookup
9 class CentralIdLookupTest
extends MediaWikiTestCase
{
11 public function testFactory() {
12 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
14 $this->setMwGlobals( [
15 'wgCentralIdLookupProviders' => [
16 'local' => [ 'class' => 'LocalIdLookup' ],
17 'local2' => [ 'class' => 'LocalIdLookup' ],
18 'mock' => [ 'factory' => function () use ( $mock ) {
21 'bad' => [ 'class' => 'stdClass' ],
23 'wgCentralIdLookupProvider' => 'mock',
26 $this->assertSame( $mock, CentralIdLookup
::factory() );
27 $this->assertSame( $mock, CentralIdLookup
::factory( 'mock' ) );
28 $this->assertSame( 'mock', $mock->getProviderId() );
30 $local = CentralIdLookup
::factory( 'local' );
31 $this->assertNotSame( $mock, $local );
32 $this->assertInstanceOf( 'LocalIdLookup', $local );
33 $this->assertSame( $local, CentralIdLookup
::factory( 'local' ) );
34 $this->assertSame( 'local', $local->getProviderId() );
36 $local2 = CentralIdLookup
::factory( 'local2' );
37 $this->assertNotSame( $local, $local2 );
38 $this->assertInstanceOf( 'LocalIdLookup', $local2 );
39 $this->assertSame( 'local2', $local2->getProviderId() );
41 $this->assertNull( CentralIdLookup
::factory( 'unconfigured' ) );
42 $this->assertNull( CentralIdLookup
::factory( 'bad' ) );
45 public function testCheckAudience() {
46 $mock = TestingAccessWrapper
::newFromObject(
47 $this->getMockForAbstractClass( 'CentralIdLookup' )
50 $user = static::getTestSysop()->getUser();
51 $this->assertSame( $user, $mock->checkAudience( $user ) );
53 $user = $mock->checkAudience( CentralIdLookup
::AUDIENCE_PUBLIC
);
54 $this->assertInstanceOf( 'User', $user );
55 $this->assertSame( 0, $user->getId() );
57 $this->assertNull( $mock->checkAudience( CentralIdLookup
::AUDIENCE_RAW
) );
60 $mock->checkAudience( 100 );
61 $this->fail( 'Expected exception not thrown' );
62 } catch ( InvalidArgumentException
$ex ) {
63 $this->assertSame( 'Invalid audience', $ex->getMessage() );
67 public function testNameFromCentralId() {
68 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
69 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
71 $this->equalTo( [ 15 => null ] ),
72 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
73 $this->equalTo( CentralIdLookup
::READ_LATEST
)
75 ->will( $this->returnValue( [ 15 => 'FooBar' ] ) );
79 $mock->nameFromCentralId( 15, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
84 * @dataProvider provideLocalUserFromCentralId
86 * @param bool $succeeds
88 public function testLocalUserFromCentralId( $name, $succeeds ) {
89 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
90 $mock->expects( $this->any() )->method( 'isAttached' )
91 ->will( $this->returnValue( true ) );
92 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
94 $this->equalTo( [ 42 => null ] ),
95 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
96 $this->equalTo( CentralIdLookup
::READ_LATEST
)
98 ->will( $this->returnValue( [ 42 => $name ] ) );
100 $user = $mock->localUserFromCentralId(
101 42, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
104 $this->assertInstanceOf( 'User', $user );
105 $this->assertSame( $name, $user->getName() );
107 $this->assertNull( $user );
110 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
111 $mock->expects( $this->any() )->method( 'isAttached' )
112 ->will( $this->returnValue( false ) );
113 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
115 $this->equalTo( [ 42 => null ] ),
116 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
117 $this->equalTo( CentralIdLookup
::READ_LATEST
)
119 ->will( $this->returnValue( [ 42 => $name ] ) );
121 $mock->localUserFromCentralId( 42, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
125 public static function provideLocalUserFromCentralId() {
128 [ 'UTDoesNotExist', false ],
135 public function testCentralIdFromName() {
136 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
137 $mock->expects( $this->once() )->method( 'lookupUserNames' )
139 $this->equalTo( [ 'FooBar' => 0 ] ),
140 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
141 $this->equalTo( CentralIdLookup
::READ_LATEST
)
143 ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
147 $mock->centralIdFromName( 'FooBar', CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
151 public function testCentralIdFromLocalUser() {
152 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
153 $mock->expects( $this->any() )->method( 'isAttached' )
154 ->will( $this->returnValue( true ) );
155 $mock->expects( $this->once() )->method( 'lookupUserNames' )
157 $this->equalTo( [ 'FooBar' => 0 ] ),
158 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
159 $this->equalTo( CentralIdLookup
::READ_LATEST
)
161 ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
165 $mock->centralIdFromLocalUser(
166 User
::newFromName( 'FooBar' ), CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
170 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
171 $mock->expects( $this->any() )->method( 'isAttached' )
172 ->will( $this->returnValue( false ) );
173 $mock->expects( $this->never() )->method( 'lookupUserNames' );
177 $mock->centralIdFromLocalUser(
178 User
::newFromName( 'FooBar' ), CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST