Merge "HTMLMultiSelectField: Use CheckboxMultiselectInputWidget"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / ConfirmLinkSecondaryAuthenticationProviderTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 /**
6 * @group AuthManager
7 * @covers MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProvider
8 */
9 class ConfirmLinkSecondaryAuthenticationProviderTest extends \MediaWikiTestCase {
10 protected function setUp() {
11 global $wgDisableAuthManager;
12
13 parent::setUp();
14 if ( $wgDisableAuthManager ) {
15 $this->markTestSkipped( '$wgDisableAuthManager is set' );
16 }
17 }
18
19 /**
20 * @dataProvider provideGetAuthenticationRequests
21 * @param string $action
22 * @param array $response
23 */
24 public function testGetAuthenticationRequests( $action, $response ) {
25 $provider = new ConfirmLinkSecondaryAuthenticationProvider();
26
27 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
28 }
29
30 public static function provideGetAuthenticationRequests() {
31 return [
32 [ AuthManager::ACTION_LOGIN, [] ],
33 [ AuthManager::ACTION_CREATE, [] ],
34 [ AuthManager::ACTION_LINK, [] ],
35 [ AuthManager::ACTION_CHANGE, [] ],
36 [ AuthManager::ACTION_REMOVE, [] ],
37 ];
38 }
39
40 public function testBeginSecondaryAuthentication() {
41 $user = \User::newFromName( 'UTSysop' );
42 $obj = new \stdClass;
43
44 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
45 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
46 ->getMock();
47 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
48 ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::authnState' ) )
49 ->will( $this->returnValue( $obj ) );
50 $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
51
52 $this->assertSame( $obj, $mock->beginSecondaryAuthentication( $user, [] ) );
53 }
54
55 public function testContinueSecondaryAuthentication() {
56 $user = \User::newFromName( 'UTSysop' );
57 $obj = new \stdClass;
58 $reqs = [ new \stdClass ];
59
60 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
61 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
62 ->getMock();
63 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
64 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
65 ->with(
66 $this->identicalTo( $user ),
67 $this->identicalTo( 'AuthManager::authnState' ),
68 $this->identicalTo( $reqs )
69 )
70 ->will( $this->returnValue( $obj ) );
71
72 $this->assertSame( $obj, $mock->continueSecondaryAuthentication( $user, $reqs ) );
73 }
74
75 public function testBeginSecondaryAccountCreation() {
76 $user = \User::newFromName( 'UTSysop' );
77 $obj = new \stdClass;
78
79 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
80 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
81 ->getMock();
82 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
83 ->with( $this->identicalTo( $user ), $this->identicalTo( 'AuthManager::accountCreationState' ) )
84 ->will( $this->returnValue( $obj ) );
85 $mock->expects( $this->never() )->method( 'continueLinkAttempt' );
86
87 $this->assertSame( $obj, $mock->beginSecondaryAccountCreation( $user, $user, [] ) );
88 }
89
90 public function testContinueSecondaryAccountCreation() {
91 $user = \User::newFromName( 'UTSysop' );
92 $obj = new \stdClass;
93 $reqs = [ new \stdClass ];
94
95 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
96 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
97 ->getMock();
98 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
99 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
100 ->with(
101 $this->identicalTo( $user ),
102 $this->identicalTo( 'AuthManager::accountCreationState' ),
103 $this->identicalTo( $reqs )
104 )
105 ->will( $this->returnValue( $obj ) );
106
107 $this->assertSame( $obj, $mock->continueSecondaryAccountCreation( $user, $user, $reqs ) );
108 }
109
110 /**
111 * Get requests for testing
112 * @return AuthenticationRequest[]
113 */
114 private function getLinkRequests() {
115 $reqs = [];
116
117 $mb = $this->getMockBuilder( AuthenticationRequest::class )
118 ->setMethods( [ 'getUniqueId' ] );
119 for ( $i = 1; $i <= 3; $i++ ) {
120 $req = $mb->getMockForAbstractClass();
121 $req->expects( $this->any() )->method( 'getUniqueId' )
122 ->will( $this->returnValue( "Request$i" ) );
123 $req->id = $i - 1;
124 $reqs[$req->getUniqueId()] = $req;
125 }
126
127 return $reqs;
128 }
129
130 public function testBeginLinkAttempt() {
131 $badReq = $this->getMockBuilder( AuthenticationRequest::class )
132 ->setMethods( [ 'getUniqueId' ] )
133 ->getMockForAbstractClass();
134 $badReq->expects( $this->any() )->method( 'getUniqueId' )
135 ->will( $this->returnValue( "BadReq" ) );
136
137 $user = \User::newFromName( 'UTSysop' );
138 $provider = \TestingAccessWrapper::newFromObject(
139 new ConfirmLinkSecondaryAuthenticationProvider
140 );
141 $request = new \FauxRequest();
142 $manager = $this->getMockBuilder( AuthManager::class )
143 ->setMethods( [ 'allowsAuthenticationDataChange' ] )
144 ->setConstructorArgs( [ $request, \RequestContext::getMain()->getConfig() ] )
145 ->getMock();
146 $manager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
147 ->will( $this->returnCallback( function ( $req ) {
148 return $req->getUniqueId() !== 'BadReq'
149 ? \StatusValue::newGood()
150 : \StatusValue::newFatal( 'no' );
151 } ) );
152 $provider->setManager( $manager );
153
154 $this->assertEquals(
155 AuthenticationResponse::newAbstain(),
156 $provider->beginLinkAttempt( $user, 'state' )
157 );
158
159 $request->getSession()->setSecret( 'state', [
160 'maybeLink' => [],
161 ] );
162 $this->assertEquals(
163 AuthenticationResponse::newAbstain(),
164 $provider->beginLinkAttempt( $user, 'state' )
165 );
166
167 $reqs = $this->getLinkRequests();
168 $request->getSession()->setSecret( 'state', [
169 'maybeLink' => $reqs + [ 'BadReq' => $badReq ]
170 ] );
171 $res = $provider->beginLinkAttempt( $user, 'state' );
172 $this->assertInstanceOf( AuthenticationResponse::class, $res );
173 $this->assertSame( AuthenticationResponse::UI, $res->status );
174 $this->assertSame( 'authprovider-confirmlink-message', $res->message->getKey() );
175 $this->assertCount( 1, $res->neededRequests );
176 $req = $res->neededRequests[0];
177 $this->assertInstanceOf( ConfirmLinkAuthenticationRequest::class, $req );
178 $this->assertEquals( $reqs, \TestingAccessWrapper::newFromObject( $req )->linkRequests );
179 }
180
181 public function testContinueLinkAttempt() {
182 $user = \User::newFromName( 'UTSysop' );
183 $obj = new \stdClass;
184 $reqs = $this->getLinkRequests();
185
186 $done = [ false, false, false ];
187
188 // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
189 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
190 ->setMethods( [ 'beginLinkAttempt' ] )
191 ->getMock();
192 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
193 ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
194 ->will( $this->returnValue( $obj ) );
195 $this->assertSame(
196 $obj,
197 \TestingAccessWrapper::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
198 );
199
200 // Now test the actual functioning
201 $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider::class )
202 ->setMethods( [
203 'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
204 'providerChangeAuthenticationData'
205 ] )
206 ->getMock();
207 $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
208 $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
209 ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
210 return $req->getUniqueId() === 'Request3'
211 ? \StatusValue::newFatal( 'foo' ) : \StatusValue::newGood();
212 } ) );
213 $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
214 ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
215 $done[$req->id] = true;
216 } ) );
217 $config = new \HashConfig( [
218 'AuthManagerConfig' => [
219 'preauth' => [],
220 'primaryauth' => [],
221 'secondaryauth' => [
222 [ 'factory' => function () use ( $provider ) {
223 return $provider;
224 } ],
225 ],
226 ],
227 ] );
228 $request = new \FauxRequest();
229 $manager = new AuthManager( $request, $config );
230 $provider->setManager( $manager );
231 $provider = \TestingAccessWrapper::newFromObject( $provider );
232
233 $req = new ConfirmLinkAuthenticationRequest( $reqs );
234
235 $this->assertEquals(
236 AuthenticationResponse::newAbstain(),
237 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
238 );
239
240 $request->getSession()->setSecret( 'state', [
241 'maybeLink' => [],
242 ] );
243 $this->assertEquals(
244 AuthenticationResponse::newAbstain(),
245 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
246 );
247
248 $request->getSession()->setSecret( 'state', [
249 'maybeLink' => $reqs
250 ] );
251 $this->assertEquals(
252 AuthenticationResponse::newPass(),
253 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
254 );
255 $this->assertSame( [ false, false, false ], $done );
256
257 $request->getSession()->setSecret( 'state', [
258 'maybeLink' => [ $reqs['Request2'] ],
259 ] );
260 $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
261 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
262 $this->assertEquals( AuthenticationResponse::newPass(), $res );
263 $this->assertSame( [ false, true, false ], $done );
264 $done = [ false, false, false ];
265
266 $request->getSession()->setSecret( 'state', [
267 'maybeLink' => $reqs,
268 ] );
269 $req->confirmedLinkIDs = [ 'Request1', 'Request2' ];
270 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
271 $this->assertEquals( AuthenticationResponse::newPass(), $res );
272 $this->assertSame( [ true, true, false ], $done );
273 $done = [ false, false, false ];
274
275 $request->getSession()->setSecret( 'state', [
276 'maybeLink' => $reqs,
277 ] );
278 $req->confirmedLinkIDs = [ 'Request1', 'Request3' ];
279 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
280 $this->assertEquals( AuthenticationResponse::UI, $res->status );
281 $this->assertCount( 1, $res->neededRequests );
282 $this->assertInstanceOf( ButtonAuthenticationRequest::class, $res->neededRequests[0] );
283 $this->assertSame( [ true, false, false ], $done );
284 $done = [ false, false, false ];
285
286 $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests[0] ] );
287 $this->assertEquals( AuthenticationResponse::newPass(), $res );
288 $this->assertSame( [ false, false, false ], $done );
289 }
290
291 }