3 namespace MediaWiki\Auth
;
7 * @covers MediaWiki\Auth\AuthPluginPrimaryAuthenticationProvider
9 class AuthPluginPrimaryAuthenticationProviderTest
extends \MediaWikiTestCase
{
10 protected function setUp() {
11 global $wgDisableAuthManager;
14 if ( $wgDisableAuthManager ) {
15 $this->markTestSkipped( '$wgDisableAuthManager is set' );
19 public function testConstruction() {
20 $plugin = new AuthManagerAuthPlugin();
22 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
23 $this->fail( 'Expected exception not thrown' );
24 } catch ( \InvalidArgumentException
$ex ) {
26 'Trying to wrap AuthManagerAuthPlugin in AuthPluginPrimaryAuthenticationProvider ' .
32 $plugin = $this->getMock( 'AuthPlugin' );
33 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
35 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
37 [ new PasswordAuthenticationRequest
],
38 $provider->getAuthenticationRequests( AuthManager
::ACTION_LOGIN
, [] )
41 $req = $this->getMock( PasswordAuthenticationRequest
::class );
42 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin, get_class( $req ) );
45 $provider->getAuthenticationRequests( AuthManager
::ACTION_LOGIN
, [] )
48 $reqType = get_class( $this->getMock( AuthenticationRequest
::class ) );
50 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin, $reqType );
51 $this->fail( 'Expected exception not thrown' );
52 } catch ( \InvalidArgumentException
$ex ) {
54 "$reqType is not a MediaWiki\\Auth\\PasswordAuthenticationRequest",
60 public function testOnUserSaveSettings() {
61 $user = \User
::newFromName( 'UTSysop' );
63 $plugin = $this->getMock( 'AuthPlugin' );
64 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
65 $plugin->expects( $this->once() )->method( 'updateExternalDB' )
66 ->with( $this->identicalTo( $user ) );
67 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
69 \Hooks
::run( 'UserSaveSettings', [ $user ] );
72 public function testOnUserGroupsChanged() {
73 $user = \User
::newFromName( 'UTSysop' );
75 $plugin = $this->getMock( 'AuthPlugin' );
76 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
77 $plugin->expects( $this->once() )->method( 'updateExternalDBGroups' )
79 $this->identicalTo( $user ),
80 $this->identicalTo( [ 'added' ] ),
81 $this->identicalTo( [ 'removed' ] )
83 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
85 \Hooks
::run( 'UserGroupsChanged', [ $user, [ 'added' ], [ 'removed' ] ] );
88 public function testOnUserLoggedIn() {
89 $user = \User
::newFromName( 'UTSysop' );
91 $plugin = $this->getMock( 'AuthPlugin' );
92 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
93 $plugin->expects( $this->exactly( 2 ) )->method( 'updateUser' )
94 ->with( $this->identicalTo( $user ) );
95 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
96 \Hooks
::run( 'UserLoggedIn', [ $user ] );
98 $plugin = $this->getMock( 'AuthPlugin' );
99 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
100 $plugin->expects( $this->once() )->method( 'updateUser' )
101 ->will( $this->returnCallback( function ( &$user ) {
102 $user = \User
::newFromName( 'UTSysop' );
104 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
106 \Hooks
::run( 'UserLoggedIn', [ $user ] );
107 $this->fail( 'Expected exception not thrown' );
108 } catch ( \UnexpectedValueException
$ex ) {
110 get_class( $plugin ) . '::updateUser() tried to replace $user!',
116 public function testOnLocalUserCreated() {
117 $user = \User
::newFromName( 'UTSysop' );
119 $plugin = $this->getMock( 'AuthPlugin' );
120 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
121 $plugin->expects( $this->exactly( 2 ) )->method( 'initUser' )
122 ->with( $this->identicalTo( $user ), $this->identicalTo( false ) );
123 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
124 \Hooks
::run( 'LocalUserCreated', [ $user, false ] );
126 $plugin = $this->getMock( 'AuthPlugin' );
127 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
128 $plugin->expects( $this->once() )->method( 'initUser' )
129 ->will( $this->returnCallback( function ( &$user ) {
130 $user = \User
::newFromName( 'UTSysop' );
132 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
134 \Hooks
::run( 'LocalUserCreated', [ $user, false ] );
135 $this->fail( 'Expected exception not thrown' );
136 } catch ( \UnexpectedValueException
$ex ) {
138 get_class( $plugin ) . '::initUser() tried to replace $user!',
144 public function testGetUniqueId() {
145 $plugin = $this->getMock( 'AuthPlugin' );
146 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
147 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
149 'MediaWiki\\Auth\\AuthPluginPrimaryAuthenticationProvider:' . get_class( $plugin ),
150 $provider->getUniqueId()
155 * @dataProvider provideGetAuthenticationRequests
156 * @param string $action
157 * @param array $response
158 * @param bool $allowPasswordChange
160 public function testGetAuthenticationRequests( $action, $response, $allowPasswordChange ) {
161 $plugin = $this->getMock( 'AuthPlugin' );
162 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
163 $plugin->expects( $this->any() )->method( 'allowPasswordChange' )
164 ->will( $this->returnValue( $allowPasswordChange ) );
165 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
166 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
169 public static function provideGetAuthenticationRequests() {
170 $arr = [ new PasswordAuthenticationRequest() ];
172 [ AuthManager
::ACTION_LOGIN
, $arr, true ],
173 [ AuthManager
::ACTION_LOGIN
, $arr, false ],
174 [ AuthManager
::ACTION_CREATE
, $arr, true ],
175 [ AuthManager
::ACTION_CREATE
, $arr, false ],
176 [ AuthManager
::ACTION_LINK
, [], true ],
177 [ AuthManager
::ACTION_LINK
, [], false ],
178 [ AuthManager
::ACTION_CHANGE
, $arr, true ],
179 [ AuthManager
::ACTION_CHANGE
, [], false ],
180 [ AuthManager
::ACTION_REMOVE
, $arr, true ],
181 [ AuthManager
::ACTION_REMOVE
, [], false ],
185 public function testAuthentication() {
186 $req = new PasswordAuthenticationRequest();
187 $req->action
= AuthManager
::ACTION_LOGIN
;
188 $reqs = [ PasswordAuthenticationRequest
::class => $req ];
190 $plugin = $this->getMockBuilder( 'AuthPlugin' )
191 ->setMethods( [ 'authenticate' ] )
193 $plugin->expects( $this->never() )->method( 'authenticate' );
194 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
197 AuthenticationResponse
::newAbstain(),
198 $provider->beginPrimaryAuthentication( [] )
201 $req->username
= 'foo';
202 $req->password
= null;
204 AuthenticationResponse
::newAbstain(),
205 $provider->beginPrimaryAuthentication( $reqs )
208 $req->username
= null;
209 $req->password
= 'bar';
211 AuthenticationResponse
::newAbstain(),
212 $provider->beginPrimaryAuthentication( $reqs )
215 $req->username
= 'foo';
216 $req->password
= 'bar';
218 $plugin = $this->getMockBuilder( 'AuthPlugin' )
219 ->setMethods( [ 'userExists', 'authenticate' ] )
221 $plugin->expects( $this->once() )->method( 'userExists' )
222 ->will( $this->returnValue( true ) );
223 $plugin->expects( $this->once() )->method( 'authenticate' )
224 ->with( $this->equalTo( 'Foo' ), $this->equalTo( 'bar' ) )
225 ->will( $this->returnValue( true ) );
226 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
228 AuthenticationResponse
::newPass( 'Foo', $req ),
229 $provider->beginPrimaryAuthentication( $reqs )
232 $plugin = $this->getMockBuilder( 'AuthPlugin' )
233 ->setMethods( [ 'userExists', 'authenticate' ] )
235 $plugin->expects( $this->once() )->method( 'userExists' )
236 ->will( $this->returnValue( false ) );
237 $plugin->expects( $this->never() )->method( 'authenticate' );
238 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
240 AuthenticationResponse
::newAbstain(),
241 $provider->beginPrimaryAuthentication( $reqs )
244 $pluginUser = $this->getMockBuilder( 'AuthPluginUser' )
245 ->setMethods( [ 'isLocked' ] )
246 ->disableOriginalConstructor()
248 $pluginUser->expects( $this->once() )->method( 'isLocked' )
249 ->will( $this->returnValue( true ) );
250 $plugin = $this->getMockBuilder( 'AuthPlugin' )
251 ->setMethods( [ 'userExists', 'getUserInstance', 'authenticate' ] )
253 $plugin->expects( $this->once() )->method( 'userExists' )
254 ->will( $this->returnValue( true ) );
255 $plugin->expects( $this->once() )->method( 'getUserInstance' )
256 ->will( $this->returnValue( $pluginUser ) );
257 $plugin->expects( $this->never() )->method( 'authenticate' );
258 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
260 AuthenticationResponse
::newAbstain(),
261 $provider->beginPrimaryAuthentication( $reqs )
264 $plugin = $this->getMockBuilder( 'AuthPlugin' )
265 ->setMethods( [ 'userExists', 'authenticate' ] )
267 $plugin->expects( $this->once() )->method( 'userExists' )
268 ->will( $this->returnValue( true ) );
269 $plugin->expects( $this->once() )->method( 'authenticate' )
270 ->with( $this->equalTo( 'Foo' ), $this->equalTo( 'bar' ) )
271 ->will( $this->returnValue( false ) );
272 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
274 AuthenticationResponse
::newAbstain(),
275 $provider->beginPrimaryAuthentication( $reqs )
278 $plugin = $this->getMockBuilder( 'AuthPlugin' )
279 ->setMethods( [ 'userExists', 'authenticate', 'strict' ] )
281 $plugin->expects( $this->once() )->method( 'userExists' )
282 ->will( $this->returnValue( true ) );
283 $plugin->expects( $this->once() )->method( 'authenticate' )
284 ->with( $this->equalTo( 'Foo' ), $this->equalTo( 'bar' ) )
285 ->will( $this->returnValue( false ) );
286 $plugin->expects( $this->any() )->method( 'strict' )->will( $this->returnValue( true ) );
287 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
288 $ret = $provider->beginPrimaryAuthentication( $reqs );
289 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
290 $this->assertSame( 'wrongpassword', $ret->message
->getKey() );
292 $plugin = $this->getMockBuilder( 'AuthPlugin' )
293 ->setMethods( [ 'userExists', 'authenticate', 'strictUserAuth' ] )
295 $plugin->expects( $this->once() )->method( 'userExists' )
296 ->will( $this->returnValue( true ) );
297 $plugin->expects( $this->once() )->method( 'authenticate' )
298 ->with( $this->equalTo( 'Foo' ), $this->equalTo( 'bar' ) )
299 ->will( $this->returnValue( false ) );
300 $plugin->expects( $this->any() )->method( 'strictUserAuth' )
301 ->with( $this->equalTo( 'Foo' ) )
302 ->will( $this->returnValue( true ) );
303 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
304 $ret = $provider->beginPrimaryAuthentication( $reqs );
305 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
306 $this->assertSame( 'wrongpassword', $ret->message
->getKey() );
308 $plugin = $this->getMockBuilder( 'AuthPlugin' )
309 ->setMethods( [ 'domainList', 'validDomain', 'setDomain', 'userExists', 'authenticate' ] )
311 $plugin->expects( $this->any() )->method( 'domainList' )
312 ->will( $this->returnValue( [ 'Domain1', 'Domain2' ] ) );
313 $plugin->expects( $this->any() )->method( 'validDomain' )
314 ->will( $this->returnCallback( function ( $domain ) {
315 return in_array( $domain, [ 'Domain1', 'Domain2' ] );
317 $plugin->expects( $this->once() )->method( 'setDomain' )
318 ->with( $this->equalTo( 'Domain2' ) );
319 $plugin->expects( $this->once() )->method( 'userExists' )
320 ->will( $this->returnValue( true ) );
321 $plugin->expects( $this->once() )->method( 'authenticate' )
322 ->with( $this->equalTo( 'Foo' ), $this->equalTo( 'bar' ) )
323 ->will( $this->returnValue( true ) );
324 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
325 list( $req ) = $provider->getAuthenticationRequests( AuthManager
::ACTION_LOGIN
, [] );
326 $req->username
= 'foo';
327 $req->password
= 'bar';
328 $req->domain
= 'Domain2';
329 $provider->beginPrimaryAuthentication( [ $req ] );
332 public function testTestUserExists() {
333 $plugin = $this->getMock( 'AuthPlugin' );
334 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
335 $plugin->expects( $this->once() )->method( 'userExists' )
336 ->with( $this->equalTo( 'Foo' ) )
337 ->will( $this->returnValue( true ) );
338 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
340 $this->assertTrue( $provider->testUserExists( 'foo' ) );
342 $plugin = $this->getMock( 'AuthPlugin' );
343 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
344 $plugin->expects( $this->once() )->method( 'userExists' )
345 ->with( $this->equalTo( 'Foo' ) )
346 ->will( $this->returnValue( false ) );
347 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
349 $this->assertFalse( $provider->testUserExists( 'foo' ) );
352 public function testTestUserCanAuthenticate() {
353 $plugin = $this->getMock( 'AuthPlugin' );
354 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
355 $plugin->expects( $this->once() )->method( 'userExists' )
356 ->with( $this->equalTo( 'Foo' ) )
357 ->will( $this->returnValue( false ) );
358 $plugin->expects( $this->never() )->method( 'getUserInstance' );
359 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
360 $this->assertFalse( $provider->testUserCanAuthenticate( 'foo' ) );
362 $pluginUser = $this->getMockBuilder( 'AuthPluginUser' )
363 ->disableOriginalConstructor()
365 $pluginUser->expects( $this->once() )->method( 'isLocked' )
366 ->will( $this->returnValue( true ) );
367 $plugin = $this->getMock( 'AuthPlugin' );
368 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
369 $plugin->expects( $this->once() )->method( 'userExists' )
370 ->with( $this->equalTo( 'Foo' ) )
371 ->will( $this->returnValue( true ) );
372 $plugin->expects( $this->once() )->method( 'getUserInstance' )
373 ->with( $this->callback( function ( $user ) {
374 $this->assertInstanceOf( 'User', $user );
375 $this->assertEquals( 'Foo', $user->getName() );
378 ->will( $this->returnValue( $pluginUser ) );
379 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
380 $this->assertFalse( $provider->testUserCanAuthenticate( 'foo' ) );
382 $pluginUser = $this->getMockBuilder( 'AuthPluginUser' )
383 ->disableOriginalConstructor()
385 $pluginUser->expects( $this->once() )->method( 'isLocked' )
386 ->will( $this->returnValue( false ) );
387 $plugin = $this->getMock( 'AuthPlugin' );
388 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
389 $plugin->expects( $this->once() )->method( 'userExists' )
390 ->with( $this->equalTo( 'Foo' ) )
391 ->will( $this->returnValue( true ) );
392 $plugin->expects( $this->once() )->method( 'getUserInstance' )
393 ->with( $this->callback( function ( $user ) {
394 $this->assertInstanceOf( 'User', $user );
395 $this->assertEquals( 'Foo', $user->getName() );
398 ->will( $this->returnValue( $pluginUser ) );
399 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
400 $this->assertTrue( $provider->testUserCanAuthenticate( 'foo' ) );
403 public function testProviderRevokeAccessForUser() {
404 $plugin = $this->getMockBuilder( 'AuthPlugin' )
405 ->setMethods( [ 'userExists', 'setPassword' ] )
407 $plugin->expects( $this->once() )->method( 'userExists' )->willReturn( true );
408 $plugin->expects( $this->once() )->method( 'setPassword' )
409 ->with( $this->callback( function ( $u ) {
410 return $u instanceof \User
&& $u->getName() === 'Foo';
411 } ), $this->identicalTo( null ) )
412 ->willReturn( true );
413 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
414 $provider->providerRevokeAccessForUser( 'foo' );
416 $plugin = $this->getMockBuilder( 'AuthPlugin' )
417 ->setMethods( [ 'domainList', 'userExists', 'setPassword' ] )
419 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [ 'D1', 'D2', 'D3' ] );
420 $plugin->expects( $this->exactly( 3 ) )->method( 'userExists' )
421 ->willReturnCallback( function () use ( $plugin ) {
422 return $plugin->getDomain() !== 'D2';
424 $plugin->expects( $this->exactly( 2 ) )->method( 'setPassword' )
425 ->with( $this->callback( function ( $u ) {
426 return $u instanceof \User
&& $u->getName() === 'Foo';
427 } ), $this->identicalTo( null ) )
428 ->willReturnCallback( function () use ( $plugin ) {
429 $this->assertNotEquals( 'D2', $plugin->getDomain() );
430 return $plugin->getDomain() !== 'D1';
432 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
434 $provider->providerRevokeAccessForUser( 'foo' );
435 $this->fail( 'Expected exception not thrown' );
436 } catch ( \UnexpectedValueException
$ex ) {
438 'AuthPlugin failed to reset password for Foo in the following domains: D1',
444 public function testProviderAllowsPropertyChange() {
445 $plugin = $this->getMock( 'AuthPlugin' );
446 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
447 $plugin->expects( $this->any() )->method( 'allowPropChange' )
448 ->will( $this->returnCallback( function ( $prop ) {
449 return $prop === 'allow';
451 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
453 $this->assertTrue( $provider->providerAllowsPropertyChange( 'allow' ) );
454 $this->assertFalse( $provider->providerAllowsPropertyChange( 'deny' ) );
458 * @dataProvider provideProviderAllowsAuthenticationDataChange
459 * @param string $type
460 * @param bool|null $allow
461 * @param StatusValue $expect
463 public function testProviderAllowsAuthenticationDataChange( $type, $allow, $expect ) {
464 $domains = $type instanceof PasswordDomainAuthenticationRequest ?
[ 'foo', 'bar' ] : [];
465 $plugin = $this->getMock( 'AuthPlugin' );
466 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( $domains );
467 $plugin->expects( $allow === null ?
$this->never() : $this->once() )
468 ->method( 'allowPasswordChange' )->will( $this->returnValue( $allow ) );
469 $plugin->expects( $this->any() )->method( 'validDomain' )
470 ->willReturnCallback( function ( $d ) use ( $domains ) {
471 return in_array( $d, $domains, true );
473 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
475 if ( is_object( $type ) ) {
478 $req = $this->getMock( $type );
480 $req->action
= AuthManager
::ACTION_CHANGE
;
481 $req->username
= 'UTSysop';
482 $req->password
= 'Pa$$w0Rd!!!';
483 $req->retype
= 'Pa$$w0Rd!!!';
484 $this->assertEquals( $expect, $provider->providerAllowsAuthenticationDataChange( $req ) );
487 public static function provideProviderAllowsAuthenticationDataChange() {
488 $domains = [ 'foo', 'bar' ];
489 $reqNoDomain = new PasswordDomainAuthenticationRequest( $domains );
490 $reqValidDomain = new PasswordDomainAuthenticationRequest( $domains );
491 $reqValidDomain->domain
= 'foo';
492 $reqInvalidDomain = new PasswordDomainAuthenticationRequest( $domains );
493 $reqInvalidDomain->domain
= 'invalid';
496 [ AuthenticationRequest
::class, null, \StatusValue
::newGood( 'ignored' ) ],
497 [ new PasswordAuthenticationRequest
, true, \StatusValue
::newGood() ],
499 new PasswordAuthenticationRequest
,
501 \StatusValue
::newFatal( 'authmanager-authplugin-setpass-denied' )
503 [ $reqNoDomain, true, \StatusValue
::newGood( 'ignored' ) ],
504 [ $reqValidDomain, true, \StatusValue
::newGood() ],
508 \StatusValue
::newFatal( 'authmanager-authplugin-setpass-bad-domain' )
513 public function testProviderChangeAuthenticationData() {
514 $plugin = $this->getMock( 'AuthPlugin' );
515 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
516 $plugin->expects( $this->never() )->method( 'setPassword' );
517 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
518 $provider->providerChangeAuthenticationData(
519 $this->getMock( AuthenticationRequest
::class )
522 $req = new PasswordAuthenticationRequest();
523 $req->action
= AuthManager
::ACTION_CHANGE
;
524 $req->username
= 'foo';
525 $req->password
= 'bar';
527 $plugin = $this->getMock( 'AuthPlugin' );
528 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
529 $plugin->expects( $this->once() )->method( 'setPassword' )
530 ->with( $this->callback( function ( $u ) {
531 return $u instanceof \User
&& $u->getName() === 'Foo';
532 } ), $this->equalTo( 'bar' ) )
533 ->will( $this->returnValue( true ) );
534 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
535 $provider->providerChangeAuthenticationData( $req );
537 $plugin = $this->getMock( 'AuthPlugin' );
538 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
539 $plugin->expects( $this->once() )->method( 'setPassword' )
540 ->with( $this->callback( function ( $u ) {
541 return $u instanceof \User
&& $u->getName() === 'Foo';
542 } ), $this->equalTo( 'bar' ) )
543 ->will( $this->returnValue( false ) );
544 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
546 $provider->providerChangeAuthenticationData( $req );
547 $this->fail( 'Expected exception not thrown' );
548 } catch ( \ErrorPageError
$e ) {
549 $this->assertSame( 'authmanager-authplugin-setpass-failed-title', $e->title
);
550 $this->assertSame( 'authmanager-authplugin-setpass-failed-message', $e->msg
);
553 $plugin = $this->getMock( 'AuthPlugin' );
554 $plugin->expects( $this->any() )->method( 'domainList' )
555 ->will( $this->returnValue( [ 'Domain1', 'Domain2' ] ) );
556 $plugin->expects( $this->any() )->method( 'validDomain' )
557 ->will( $this->returnCallback( function ( $domain ) {
558 return in_array( $domain, [ 'Domain1', 'Domain2' ] );
560 $plugin->expects( $this->once() )->method( 'setDomain' )
561 ->with( $this->equalTo( 'Domain2' ) );
562 $plugin->expects( $this->once() )->method( 'setPassword' )
563 ->with( $this->callback( function ( $u ) {
564 return $u instanceof \User
&& $u->getName() === 'Foo';
565 } ), $this->equalTo( 'bar' ) )
566 ->will( $this->returnValue( true ) );
567 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
568 list( $req ) = $provider->getAuthenticationRequests( AuthManager
::ACTION_CREATE
, [] );
569 $req->username
= 'foo';
570 $req->password
= 'bar';
571 $req->domain
= 'Domain2';
572 $provider->providerChangeAuthenticationData( $req );
576 * @dataProvider provideAccountCreationType
578 * @param string $expect
580 public function testAccountCreationType( $can, $expect ) {
581 $plugin = $this->getMock( 'AuthPlugin' );
582 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
583 $plugin->expects( $this->once() )
584 ->method( 'canCreateAccounts' )->will( $this->returnValue( $can ) );
585 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
587 $this->assertSame( $expect, $provider->accountCreationType() );
590 public static function provideAccountCreationType() {
592 [ true, PrimaryAuthenticationProvider
::TYPE_CREATE
],
593 [ false, PrimaryAuthenticationProvider
::TYPE_NONE
],
597 public function testTestForAccountCreation() {
598 $user = \User
::newFromName( 'foo' );
600 $plugin = $this->getMock( 'AuthPlugin' );
601 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
602 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
604 \StatusValue
::newGood(),
605 $provider->testForAccountCreation( $user, $user, [] )
609 public function testAccountCreation() {
610 $user = \User
::newFromName( 'foo' );
611 $user->setEmail( 'email' );
612 $user->setRealName( 'realname' );
614 $req = new PasswordAuthenticationRequest();
615 $req->action
= AuthManager
::ACTION_CREATE
;
616 $reqs = [ PasswordAuthenticationRequest
::class => $req ];
618 $plugin = $this->getMock( 'AuthPlugin' );
619 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
620 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
621 ->will( $this->returnValue( false ) );
622 $plugin->expects( $this->never() )->method( 'addUser' );
623 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
625 $provider->beginPrimaryAccountCreation( $user, $user, [] );
626 $this->fail( 'Expected exception was not thrown' );
627 } catch ( \BadMethodCallException
$ex ) {
629 'Shouldn\'t call this when accountCreationType() is NONE', $ex->getMessage()
633 $plugin = $this->getMock( 'AuthPlugin' );
634 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
635 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
636 ->will( $this->returnValue( true ) );
637 $plugin->expects( $this->never() )->method( 'addUser' );
638 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
641 AuthenticationResponse
::newAbstain(),
642 $provider->beginPrimaryAccountCreation( $user, $user, [] )
645 $req->username
= 'foo';
646 $req->password
= null;
648 AuthenticationResponse
::newAbstain(),
649 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
652 $req->username
= null;
653 $req->password
= 'bar';
655 AuthenticationResponse
::newAbstain(),
656 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
659 $req->username
= 'foo';
660 $req->password
= 'bar';
662 $plugin = $this->getMock( 'AuthPlugin' );
663 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
664 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
665 ->will( $this->returnValue( true ) );
666 $plugin->expects( $this->once() )->method( 'addUser' )
668 $this->callback( function ( $u ) {
669 return $u instanceof \User
&& $u->getName() === 'Foo';
671 $this->equalTo( 'bar' ),
672 $this->equalTo( 'email' ),
673 $this->equalTo( 'realname' )
675 ->will( $this->returnValue( true ) );
676 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
678 AuthenticationResponse
::newPass(),
679 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
682 $plugin = $this->getMock( 'AuthPlugin' );
683 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
684 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
685 ->will( $this->returnValue( true ) );
686 $plugin->expects( $this->once() )->method( 'addUser' )
688 $this->callback( function ( $u ) {
689 return $u instanceof \User
&& $u->getName() === 'Foo';
691 $this->equalTo( 'bar' ),
692 $this->equalTo( 'email' ),
693 $this->equalTo( 'realname' )
695 ->will( $this->returnValue( false ) );
696 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
697 $ret = $provider->beginPrimaryAccountCreation( $user, $user, $reqs );
698 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
699 $this->assertSame( 'authmanager-authplugin-create-fail', $ret->message
->getKey() );
701 $plugin = $this->getMock( 'AuthPlugin' );
702 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
703 ->will( $this->returnValue( true ) );
704 $plugin->expects( $this->any() )->method( 'domainList' )
705 ->will( $this->returnValue( [ 'Domain1', 'Domain2' ] ) );
706 $plugin->expects( $this->any() )->method( 'validDomain' )
707 ->will( $this->returnCallback( function ( $domain ) {
708 return in_array( $domain, [ 'Domain1', 'Domain2' ] );
710 $plugin->expects( $this->once() )->method( 'setDomain' )
711 ->with( $this->equalTo( 'Domain2' ) );
712 $plugin->expects( $this->once() )->method( 'addUser' )
713 ->with( $this->callback( function ( $u ) {
714 return $u instanceof \User
&& $u->getName() === 'Foo';
715 } ), $this->equalTo( 'bar' ) )
716 ->will( $this->returnValue( true ) );
717 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
718 list( $req ) = $provider->getAuthenticationRequests( AuthManager
::ACTION_CREATE
, [] );
719 $req->username
= 'foo';
720 $req->password
= 'bar';
721 $req->domain
= 'Domain2';
722 $provider->beginPrimaryAccountCreation( $user, $user, [ $req ] );