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 $user = \User
::newFromName( 'UTSysop' );
132 $provider = \TestingAccessWrapper
::newFromObject(
133 new ConfirmLinkSecondaryAuthenticationProvider
135 $request = new \
FauxRequest();
136 $manager = new AuthManager( $request, \RequestContext
::getMain()->getConfig() );
137 $provider->setManager( $manager );
140 AuthenticationResponse
::newAbstain(),
141 $provider->beginLinkAttempt( $user, 'state' )
144 $request->getSession()->setSecret( 'state', [
148 AuthenticationResponse
::newAbstain(),
149 $provider->beginLinkAttempt( $user, 'state' )
152 $reqs = $this->getLinkRequests();
153 $request->getSession()->setSecret( 'state', [
156 $res = $provider->beginLinkAttempt( $user, 'state' );
157 $this->assertInstanceOf( AuthenticationResponse
::class, $res );
158 $this->assertSame( AuthenticationResponse
::UI
, $res->status
);
159 $this->assertSame( 'authprovider-confirmlink-message', $res->message
->getKey() );
160 $this->assertCount( 1, $res->neededRequests
);
161 $req = $res->neededRequests
[0];
162 $this->assertInstanceOf( ConfirmLinkAuthenticationRequest
::class, $req );
163 $this->assertEquals( $reqs, \TestingAccessWrapper
::newFromObject( $req )->linkRequests
);
166 public function testContinueLinkAttempt() {
167 $user = \User
::newFromName( 'UTSysop' );
168 $obj = new \stdClass
;
169 $reqs = $this->getLinkRequests();
171 $done = [ false, false, false ];
173 // First, test the pass-through for not containing the ConfirmLinkAuthenticationRequest
174 $mock = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
175 ->setMethods( [ 'beginLinkAttempt' ] )
177 $mock->expects( $this->once() )->method( 'beginLinkAttempt' )
178 ->with( $this->identicalTo( $user ), $this->identicalTo( 'state' ) )
179 ->will( $this->returnValue( $obj ) );
182 \TestingAccessWrapper
::newFromObject( $mock )->continueLinkAttempt( $user, 'state', $reqs )
185 // Now test the actual functioning
186 $provider = $this->getMockBuilder( ConfirmLinkSecondaryAuthenticationProvider
::class )
188 'beginLinkAttempt', 'providerAllowsAuthenticationDataChange',
189 'providerChangeAuthenticationData'
192 $provider->expects( $this->never() )->method( 'beginLinkAttempt' );
193 $provider->expects( $this->any() )->method( 'providerAllowsAuthenticationDataChange' )
194 ->will( $this->returnCallback( function ( $req ) use ( $reqs ) {
195 return $req->getUniqueId() === 'Request3'
196 ? \StatusValue
::newFatal( 'foo' ) : \StatusValue
::newGood();
198 $provider->expects( $this->any() )->method( 'providerChangeAuthenticationData' )
199 ->will( $this->returnCallback( function ( $req ) use ( &$done ) {
200 $done[$req->id
] = true;
202 $config = new \
HashConfig( [
203 'AuthManagerConfig' => [
207 [ 'factory' => function () use ( $provider ) {
213 $request = new \
FauxRequest();
214 $manager = new AuthManager( $request, $config );
215 $provider->setManager( $manager );
216 $provider = \TestingAccessWrapper
::newFromObject( $provider );
218 $req = new ConfirmLinkAuthenticationRequest( $reqs );
221 AuthenticationResponse
::newAbstain(),
222 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
225 $request->getSession()->setSecret( 'state', [
229 AuthenticationResponse
::newAbstain(),
230 $provider->continueLinkAttempt( $user, 'state', [ $req ] )
233 $request->getSession()->setSecret( 'state', [
237 AuthenticationResponse
::newPass(),
238 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] )
240 $this->assertSame( [ false, false, false ], $done );
242 $request->getSession()->setSecret( 'state', [
243 'maybeLink' => [ $reqs['Request2'] ],
245 $req->confirmedLinkIDs
= [ 'Request1', 'Request2' ];
246 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
247 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
248 $this->assertSame( [ false, true, false ], $done );
249 $done = [ false, false, false ];
251 $request->getSession()->setSecret( 'state', [
252 'maybeLink' => $reqs,
254 $req->confirmedLinkIDs
= [ 'Request1', 'Request2' ];
255 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
256 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
257 $this->assertSame( [ true, true, false ], $done );
258 $done = [ false, false, false ];
260 $request->getSession()->setSecret( 'state', [
261 'maybeLink' => $reqs,
263 $req->confirmedLinkIDs
= [ 'Request1', 'Request3' ];
264 $res = $provider->continueLinkAttempt( $user, 'state', [ $req ] );
265 $this->assertEquals( AuthenticationResponse
::UI
, $res->status
);
266 $this->assertCount( 1, $res->neededRequests
);
267 $this->assertInstanceOf( ButtonAuthenticationRequest
::class, $res->neededRequests
[0] );
268 $this->assertSame( [ true, false, false ], $done );
269 $done = [ false, false, false ];
271 $res = $provider->continueLinkAttempt( $user, 'state', [ $res->neededRequests
[0] ] );
272 $this->assertEquals( AuthenticationResponse
::newPass(), $res );
273 $this->assertSame( [ false, false, false ], $done );