Merge "Parser: Replace Linker::link() with LinkRenderer"
[lhc/web/wiklou.git] / tests / phpunit / includes / user / CentralIdLookupTest.php
1 <?php
2
3 /**
4 * @covers CentralIdLookup
5 * @group Database
6 */
7 class CentralIdLookupTest extends MediaWikiTestCase {
8
9 public function testFactory() {
10 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
11
12 $this->setMwGlobals( [
13 'wgCentralIdLookupProviders' => [
14 'local' => [ 'class' => 'LocalIdLookup' ],
15 'local2' => [ 'class' => 'LocalIdLookup' ],
16 'mock' => [ 'factory' => function () use ( $mock ) {
17 return $mock;
18 } ],
19 'bad' => [ 'class' => 'stdClass' ],
20 ],
21 'wgCentralIdLookupProvider' => 'mock',
22 ] );
23
24 $this->assertSame( $mock, CentralIdLookup::factory() );
25 $this->assertSame( $mock, CentralIdLookup::factory( 'mock' ) );
26 $this->assertSame( 'mock', $mock->getProviderId() );
27
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() );
33
34 $local2 = CentralIdLookup::factory( 'local2' );
35 $this->assertNotSame( $local, $local2 );
36 $this->assertInstanceOf( 'LocalIdLookup', $local2 );
37 $this->assertSame( 'local2', $local2->getProviderId() );
38
39 $this->assertNull( CentralIdLookup::factory( 'unconfigured' ) );
40 $this->assertNull( CentralIdLookup::factory( 'bad' ) );
41 }
42
43 public function testCheckAudience() {
44 $mock = TestingAccessWrapper::newFromObject(
45 $this->getMockForAbstractClass( 'CentralIdLookup' )
46 );
47
48 $user = static::getTestSysop()->getUser();
49 $this->assertSame( $user, $mock->checkAudience( $user ) );
50
51 $user = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC );
52 $this->assertInstanceOf( 'User', $user );
53 $this->assertSame( 0, $user->getId() );
54
55 $this->assertNull( $mock->checkAudience( CentralIdLookup::AUDIENCE_RAW ) );
56
57 try {
58 $mock->checkAudience( 100 );
59 $this->fail( 'Expected exception not thrown' );
60 } catch ( InvalidArgumentException $ex ) {
61 $this->assertSame( 'Invalid audience', $ex->getMessage() );
62 }
63 }
64
65 public function testNameFromCentralId() {
66 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
67 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
68 ->with(
69 $this->equalTo( [ 15 => null ] ),
70 $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
71 $this->equalTo( CentralIdLookup::READ_LATEST )
72 )
73 ->will( $this->returnValue( [ 15 => 'FooBar' ] ) );
74
75 $this->assertSame(
76 'FooBar',
77 $mock->nameFromCentralId( 15, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
78 );
79 }
80
81 /**
82 * @dataProvider provideLocalUserFromCentralId
83 * @param string $name
84 * @param bool $succeeds
85 */
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' )
91 ->with(
92 $this->equalTo( [ 42 => null ] ),
93 $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
94 $this->equalTo( CentralIdLookup::READ_LATEST )
95 )
96 ->will( $this->returnValue( [ 42 => $name ] ) );
97
98 $user = $mock->localUserFromCentralId(
99 42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
100 );
101 if ( $succeeds ) {
102 $this->assertInstanceOf( 'User', $user );
103 $this->assertSame( $name, $user->getName() );
104 } else {
105 $this->assertNull( $user );
106 }
107
108 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
109 $mock->expects( $this->any() )->method( 'isAttached' )
110 ->will( $this->returnValue( false ) );
111 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
112 ->with(
113 $this->equalTo( [ 42 => null ] ),
114 $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
115 $this->equalTo( CentralIdLookup::READ_LATEST )
116 )
117 ->will( $this->returnValue( [ 42 => $name ] ) );
118 $this->assertNull(
119 $mock->localUserFromCentralId( 42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
120 );
121 }
122
123 public static function provideLocalUserFromCentralId() {
124 return [
125 [ 'UTSysop', true ],
126 [ 'UTDoesNotExist', false ],
127 [ null, false ],
128 [ '', false ],
129 [ '<X>', false ],
130 ];
131 }
132
133 public function testCentralIdFromName() {
134 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
135 $mock->expects( $this->once() )->method( 'lookupUserNames' )
136 ->with(
137 $this->equalTo( [ 'FooBar' => 0 ] ),
138 $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
139 $this->equalTo( CentralIdLookup::READ_LATEST )
140 )
141 ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
142
143 $this->assertSame(
144 23,
145 $mock->centralIdFromName( 'FooBar', CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
146 );
147 }
148
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' )
154 ->with(
155 $this->equalTo( [ 'FooBar' => 0 ] ),
156 $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
157 $this->equalTo( CentralIdLookup::READ_LATEST )
158 )
159 ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
160
161 $this->assertSame(
162 23,
163 $mock->centralIdFromLocalUser(
164 User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
165 )
166 );
167
168 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
169 $mock->expects( $this->any() )->method( 'isAttached' )
170 ->will( $this->returnValue( false ) );
171 $mock->expects( $this->never() )->method( 'lookupUserNames' );
172
173 $this->assertSame(
174 0,
175 $mock->centralIdFromLocalUser(
176 User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
177 )
178 );
179 }
180
181 }