3 namespace MediaWiki\Auth
;
7 * @covers MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProvider
9 class ConfirmLinkSecondaryAuthenticationProviderTest
extends \MediaWikiTestCase
{
10 protected function setUp() {
11 global $wgDisableAuthManager;
14 if ( $wgDisableAuthManager ) {
15 $this->markTestSkipped( '$wgDisableAuthManager is set' );
20 * @dataProvider provideGetAuthenticationRequests
21 * @param string $action
22 * @param array $response
24 public function testGetAuthenticationRequests( $action, $response ) {
25 $provider = new ConfirmLinkSecondaryAuthenticationProvider();
27 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
30 public static function provideGetAuthenticationRequests() {
32 [ AuthManager
::ACTION_LOGIN
, [] ],
33 [ AuthManager
::ACTION_CREATE
, [] ],
34 [ AuthManager
::ACTION_LINK
, [] ],
35 [ AuthManager
::ACTION_CHANGE
, [] ],
36 [ AuthManager
::ACTION_REMOVE
, [] ],
40 public function testBeginSecondaryAuthentication() {
41 $user = \User
::newFromName( 'UTSysop' );
44 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
45 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
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' );
52 $this->assertSame( $obj, $mock->beginSecondaryAuthentication( $user, [] ) );
55 public function testContinueSecondaryAuthentication() {
56 $user = \User
::newFromName( 'UTSysop' );
58 $reqs = [ new \stdClass
];
60 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
61 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
63 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
64 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
66 $this->identicalTo( $user ),
67 $this->identicalTo( 'AuthManager::authnState' ),
68 $this->identicalTo( $reqs )
70 ->will( $this->returnValue( $obj ) );
72 $this->assertSame( $obj, $mock->continueSecondaryAuthentication( $user, $reqs ) );
75 public function testBeginSecondaryAccountCreation() {
76 $user = \User
::newFromName( 'UTSysop' );
79 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
80 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
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' );
87 $this->assertSame( $obj, $mock->beginSecondaryAccountCreation( $user, $user, [] ) );
90 public function testContinueSecondaryAccountCreation() {
91 $user = \User
::newFromName( 'UTSysop' );
93 $reqs = [ new \stdClass
];
95 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
96 ->setMethods( [ 'beginLinkAttempt', 'continueLinkAttempt' ] )
98 $mock->expects( $this->never() )->method( 'beginLinkAttempt' );
99 $mock->expects( $this->once() )->method( 'continueLinkAttempt' )
101 $this->identicalTo( $user ),
102 $this->identicalTo( 'AuthManager::accountCreationState' ),
103 $this->identicalTo( $reqs )
105 ->will( $this->returnValue( $obj ) );
107 $this->assertSame( $obj, $mock->continueSecondaryAccountCreation( $user, $user, $reqs ) );
111 * Get requests for testing
112 * @return AuthenticationRequest[]
114 private function getLinkRequests() {
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" ) );
124 $reqs[$req->getUniqueId()] = $req;
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" ) );
137 $user = \User
::newFromName( 'UTSysop' );
138 $provider = \TestingAccessWrapper
::newFromObject(
139 new ConfirmLinkSecondaryAuthenticationProvider
141 $request = new \
FauxRequest();
142 $manager = $this->getMockBuilder( AuthManager
::class )
143 ->setMethods( [ 'allowsAuthenticationDataChange' ] )
144 ->setConstructorArgs( [ $request, \RequestContext
::getMain()->getConfig() ] )
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' );
152 $provider->setManager( $manager );
155 AuthenticationResponse
::newAbstain(),
156 $provider->beginLinkAttempt( $user, 'state' )
159 $request->getSession()->setSecret( 'state', [
163 AuthenticationResponse
::newAbstain(),
164 $provider->beginLinkAttempt( $user, 'state' )
167 $reqs = $this->getLinkRequests();
168 $request->getSession()->setSecret( 'state', [
169 'maybeLink' => $reqs +
[ 'BadReq' => $badReq ]
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 $expectReqs = $this->getLinkRequests();
179 foreach ( $expectReqs as $r ) {
180 $r->action
= AuthManager
::ACTION_CHANGE
;
181 $r->username
= $user->getName();
183 $this->assertEquals( $expectReqs, \TestingAccessWrapper
::newFromObject( $req )->linkRequests
);
186 public function testContinueLinkAttempt() {
187 $user = \User
::newFromName( 'UTSysop' );
188 $obj = new \stdClass
;
189 $reqs = $this->getLinkRequests();
191 $done = [ false, false, false ];
193 // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
194 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
195 ->setMethods( [ 'beginLinkAttempt' ] )
197 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
198 ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
199 ->will( $this->returnValue( $obj ) );
202 \TestingAccessWrapper
::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
205 // Now test the actual functioning
206 $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
208 'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
209 'providerChangeAuthenticationData'
212 $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
213 $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
214 ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
215 return $req->getUniqueId() === 'Request3'
216 ? \StatusValue
::newFatal( 'foo' ) : \StatusValue
::newGood();
218 $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
219 ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
220 $done[$req->id
] = true;
222 $config = new \
HashConfig( [
223 'AuthManagerConfig' => [
227 [ 'factory' => function () use ( $provider ) {
233 $request = new \
FauxRequest();
234 $manager = new AuthManager( $request, $config );
235 $provider->setManager( $manager );
236 $provider = \TestingAccessWrapper
::newFromObject( $provider );
238 $req = new ConfirmLinkAuthenticationRequest( $reqs );
241 AuthenticationResponse
::newAbstain(),
242 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
245 $request->getSession()->setSecret( 'state', [
249 AuthenticationResponse
::newAbstain(),
250 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
253 $request->getSession()->setSecret( 'state', [
257 AuthenticationResponse
::newPass(),
258 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
260 $this->assertSame( [ false, false, false ], $done );
262 $request->getSession()->setSecret( 'state', [
263 'maybeLink' => [ $reqs['Request2'] ],
265 $req->confirmedLinkIDs
= [ 'Request1', 'Request2' ];
266 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
267 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
268 $this->assertSame( [ false, true, false ], $done );
269 $done = [ false, false, false ];
271 $request->getSession()->setSecret( 'state', [
272 'maybeLink' => $reqs,
274 $req->confirmedLinkIDs
= [ 'Request1', 'Request2' ];
275 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
276 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
277 $this->assertSame( [ true, true, false ], $done );
278 $done = [ false, false, false ];
280 $request->getSession()->setSecret( 'state', [
281 'maybeLink' => $reqs,
283 $req->confirmedLinkIDs
= [ 'Request1', 'Request3' ];
284 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
285 $this->assertEquals( AuthenticationResponse
::UI
, $res->status
);
286 $this->assertCount( 1, $res->neededRequests
);
287 $this->assertInstanceOf( ButtonAuthenticationRequest
::class, $res->neededRequests
[0] );
288 $this->assertSame( [ true, false, false ], $done );
289 $done = [ false, false, false ];
291 $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests
[0] ] );
292 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
293 $this->assertSame( [ false, false, false ], $done );