4 * @covers CentralIdLookup
7 class CentralIdLookupTest
extends MediaWikiTestCase
{
9 public function testFactory() {
10 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
12 $this->setMwGlobals( array(
13 'wgCentralIdLookupProviders' => array(
14 'local' => array( 'class' => 'LocalIdLookup' ),
15 'local2' => array( 'class' => 'LocalIdLookup' ),
16 'mock' => array( 'factory' => function () use ( $mock ) {
19 'bad' => array( 'class' => 'stdClass' ),
21 'wgCentralIdLookupProvider' => 'mock',
24 $this->assertSame( $mock, CentralIdLookup
::factory() );
25 $this->assertSame( $mock, CentralIdLookup
::factory( 'mock' ) );
26 $this->assertSame( 'mock', $mock->getProviderId() );
28 $local = CentralIdLookup
::factory( 'local' );
29 $this->assertNotSame( $mock, $local );
30 $this->assertInstanceOf( 'LocalIdLookup', $local );
31 $this->assertSame( $local, CentralIdLookup
::factory( 'local' ) );
32 $this->assertSame( 'local', $local->getProviderId() );
34 $local2 = CentralIdLookup
::factory( 'local2' );
35 $this->assertNotSame( $local, $local2 );
36 $this->assertInstanceOf( 'LocalIdLookup', $local2 );
37 $this->assertSame( 'local2', $local2->getProviderId() );
39 $this->assertNull( CentralIdLookup
::factory( 'unconfigured' ) );
40 $this->assertNull( CentralIdLookup
::factory( 'bad' ) );
43 public function testCheckAudience() {
44 $mock = TestingAccessWrapper
::newFromObject(
45 $this->getMockForAbstractClass( 'CentralIdLookup' )
48 $user = User
::newFromName( 'UTSysop' );
49 $this->assertSame( $user, $mock->checkAudience( $user ) );
51 $user = $mock->checkAudience( CentralIdLookup
::AUDIENCE_PUBLIC
);
52 $this->assertInstanceOf( 'User', $user );
53 $this->assertSame( 0, $user->getId() );
55 $this->assertNull( $mock->checkAudience( CentralIdLookup
::AUDIENCE_RAW
) );
58 $mock->checkAudience( 100 );
59 $this->fail( 'Expected exception not thrown' );
60 } catch ( InvalidArgumentException
$ex ) {
61 $this->assertSame( 'Invalid audience', $ex->getMessage() );
65 public function testNameFromCentralId() {
66 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
67 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
69 $this->equalTo( array( 15 => null ) ),
70 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
71 $this->equalTo( CentralIdLookup
::READ_LATEST
)
73 ->will( $this->returnValue( array( 15 => 'FooBar' ) ) );
77 $mock->nameFromCentralId( 15, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
82 * @dataProvider provideLocalUserFromCentralId
84 * @param bool $succeeds
86 public function testLocalUserFromCentralId( $name, $succeeds ) {
87 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
88 $mock->expects( $this->any() )->method( 'isAttached' )
89 ->will( $this->returnValue( true ) );
90 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
92 $this->equalTo( array( 42 => null ) ),
93 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
94 $this->equalTo( CentralIdLookup
::READ_LATEST
)
96 ->will( $this->returnValue( array( 42 => $name ) ) );
98 $user = $mock->localUserFromCentralId(
99 42, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
102 $this->assertInstanceOf( 'User', $user );
103 $this->assertSame( $name, $user->getName() );
105 $this->assertNull( $user );
108 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
109 $mock->expects( $this->any() )->method( 'isAttached' )
110 ->will( $this->returnValue( false ) );
111 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
113 $this->equalTo( array( 42 => null ) ),
114 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
115 $this->equalTo( CentralIdLookup
::READ_LATEST
)
117 ->will( $this->returnValue( array( 42 => $name ) ) );
119 $mock->localUserFromCentralId( 42, CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
123 public static function provideLocalUserFromCentralId() {
125 array( 'UTSysop', true ),
126 array( 'UTDoesNotExist', false ),
127 array( null, false ),
129 array( '<X>', false ),
133 public function testCentralIdFromName() {
134 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
135 $mock->expects( $this->once() )->method( 'lookupUserNames' )
137 $this->equalTo( array( 'FooBar' => 0 ) ),
138 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
139 $this->equalTo( CentralIdLookup
::READ_LATEST
)
141 ->will( $this->returnValue( array( 'FooBar' => 23 ) ) );
145 $mock->centralIdFromName( 'FooBar', CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
)
149 public function testCentralIdFromLocalUser() {
150 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
151 $mock->expects( $this->any() )->method( 'isAttached' )
152 ->will( $this->returnValue( true ) );
153 $mock->expects( $this->once() )->method( 'lookupUserNames' )
155 $this->equalTo( array( 'FooBar' => 0 ) ),
156 $this->equalTo( CentralIdLookup
::AUDIENCE_RAW
),
157 $this->equalTo( CentralIdLookup
::READ_LATEST
)
159 ->will( $this->returnValue( array( 'FooBar' => 23 ) ) );
163 $mock->centralIdFromLocalUser(
164 User
::newFromName( 'FooBar' ), CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST
168 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
169 $mock->expects( $this->any() )->method( 'isAttached' )
170 ->will( $this->returnValue( false ) );
171 $mock->expects( $this->never() )->method( 'lookupUserNames' );
175 $mock->centralIdFromLocalUser(
176 User
::newFromName( 'FooBar' ), CentralIdLookup
::AUDIENCE_RAW
, CentralIdLookup
::READ_LATEST