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 $plugin = $this->getMock( 'AuthPlugin' );
465 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
466 $plugin->expects( $allow === null ?
$this->never() : $this->once() )
467 ->method( 'allowPasswordChange' )->will( $this->returnValue( $allow ) );
468 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
470 if ( $type === PasswordAuthenticationRequest
::class ) {
473 $req = $this->getMock( $type );
475 $req->action
= AuthManager
::ACTION_CHANGE
;
476 $req->username
= 'UTSysop';
477 $req->password
= 'Pa$$w0Rd!!!';
478 $req->retype
= 'Pa$$w0Rd!!!';
479 $this->assertEquals( $expect, $provider->providerAllowsAuthenticationDataChange( $req ) );
482 public static function provideProviderAllowsAuthenticationDataChange() {
484 [ AuthenticationRequest
::class, null, \StatusValue
::newGood( 'ignored' ) ],
485 [ PasswordAuthenticationRequest
::class, true, \StatusValue
::newGood() ],
487 PasswordAuthenticationRequest
::class,
489 \StatusValue
::newFatal( 'authmanager-authplugin-setpass-denied' )
494 public function testProviderChangeAuthenticationData() {
495 $plugin = $this->getMock( 'AuthPlugin' );
496 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
497 $plugin->expects( $this->never() )->method( 'setPassword' );
498 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
499 $provider->providerChangeAuthenticationData(
500 $this->getMock( AuthenticationRequest
::class )
503 $req = new PasswordAuthenticationRequest();
504 $req->action
= AuthManager
::ACTION_CHANGE
;
505 $req->username
= 'foo';
506 $req->password
= 'bar';
508 $plugin = $this->getMock( 'AuthPlugin' );
509 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
510 $plugin->expects( $this->once() )->method( 'setPassword' )
511 ->with( $this->callback( function ( $u ) {
512 return $u instanceof \User
&& $u->getName() === 'Foo';
513 } ), $this->equalTo( 'bar' ) )
514 ->will( $this->returnValue( true ) );
515 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
516 $provider->providerChangeAuthenticationData( $req );
518 $plugin = $this->getMock( 'AuthPlugin' );
519 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
520 $plugin->expects( $this->once() )->method( 'setPassword' )
521 ->with( $this->callback( function ( $u ) {
522 return $u instanceof \User
&& $u->getName() === 'Foo';
523 } ), $this->equalTo( 'bar' ) )
524 ->will( $this->returnValue( false ) );
525 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
527 $provider->providerChangeAuthenticationData( $req );
528 $this->fail( 'Expected exception not thrown' );
529 } catch ( \ErrorPageError
$e ) {
530 $this->assertSame( 'authmanager-authplugin-setpass-failed-title', $e->title
);
531 $this->assertSame( 'authmanager-authplugin-setpass-failed-message', $e->msg
);
534 $plugin = $this->getMock( 'AuthPlugin' );
535 $plugin->expects( $this->any() )->method( 'domainList' )
536 ->will( $this->returnValue( [ 'Domain1', 'Domain2' ] ) );
537 $plugin->expects( $this->any() )->method( 'validDomain' )
538 ->will( $this->returnCallback( function ( $domain ) {
539 return in_array( $domain, [ 'Domain1', 'Domain2' ] );
541 $plugin->expects( $this->once() )->method( 'setDomain' )
542 ->with( $this->equalTo( 'Domain2' ) );
543 $plugin->expects( $this->once() )->method( 'setPassword' )
544 ->with( $this->callback( function ( $u ) {
545 return $u instanceof \User
&& $u->getName() === 'Foo';
546 } ), $this->equalTo( 'bar' ) )
547 ->will( $this->returnValue( true ) );
548 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
549 list( $req ) = $provider->getAuthenticationRequests( AuthManager
::ACTION_CREATE
, [] );
550 $req->username
= 'foo';
551 $req->password
= 'bar';
552 $req->domain
= 'Domain2';
553 $provider->providerChangeAuthenticationData( $req );
557 * @dataProvider provideAccountCreationType
559 * @param string $expect
561 public function testAccountCreationType( $can, $expect ) {
562 $plugin = $this->getMock( 'AuthPlugin' );
563 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
564 $plugin->expects( $this->once() )
565 ->method( 'canCreateAccounts' )->will( $this->returnValue( $can ) );
566 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
568 $this->assertSame( $expect, $provider->accountCreationType() );
571 public static function provideAccountCreationType() {
573 [ true, PrimaryAuthenticationProvider
::TYPE_CREATE
],
574 [ false, PrimaryAuthenticationProvider
::TYPE_NONE
],
578 public function testTestForAccountCreation() {
579 $user = \User
::newFromName( 'foo' );
581 $plugin = $this->getMock( 'AuthPlugin' );
582 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
583 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
585 \StatusValue
::newGood(),
586 $provider->testForAccountCreation( $user, $user, [] )
590 public function testAccountCreation() {
591 $user = \User
::newFromName( 'foo' );
592 $user->setEmail( 'email' );
593 $user->setRealName( 'realname' );
595 $req = new PasswordAuthenticationRequest();
596 $req->action
= AuthManager
::ACTION_CREATE
;
597 $reqs = [ PasswordAuthenticationRequest
::class => $req ];
599 $plugin = $this->getMock( 'AuthPlugin' );
600 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
601 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
602 ->will( $this->returnValue( false ) );
603 $plugin->expects( $this->never() )->method( 'addUser' );
604 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
606 $provider->beginPrimaryAccountCreation( $user, $user, [] );
607 $this->fail( 'Expected exception was not thrown' );
608 } catch ( \BadMethodCallException
$ex ) {
610 'Shouldn\'t call this when accountCreationType() is NONE', $ex->getMessage()
614 $plugin = $this->getMock( 'AuthPlugin' );
615 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
616 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
617 ->will( $this->returnValue( true ) );
618 $plugin->expects( $this->never() )->method( 'addUser' );
619 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
622 AuthenticationResponse
::newAbstain(),
623 $provider->beginPrimaryAccountCreation( $user, $user, [] )
626 $req->username
= 'foo';
627 $req->password
= null;
629 AuthenticationResponse
::newAbstain(),
630 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
633 $req->username
= null;
634 $req->password
= 'bar';
636 AuthenticationResponse
::newAbstain(),
637 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
640 $req->username
= 'foo';
641 $req->password
= 'bar';
643 $plugin = $this->getMock( 'AuthPlugin' );
644 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
645 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
646 ->will( $this->returnValue( true ) );
647 $plugin->expects( $this->once() )->method( 'addUser' )
649 $this->callback( function ( $u ) {
650 return $u instanceof \User
&& $u->getName() === 'Foo';
652 $this->equalTo( 'bar' ),
653 $this->equalTo( 'email' ),
654 $this->equalTo( 'realname' )
656 ->will( $this->returnValue( true ) );
657 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
659 AuthenticationResponse
::newPass(),
660 $provider->beginPrimaryAccountCreation( $user, $user, $reqs )
663 $plugin = $this->getMock( 'AuthPlugin' );
664 $plugin->expects( $this->any() )->method( 'domainList' )->willReturn( [] );
665 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
666 ->will( $this->returnValue( true ) );
667 $plugin->expects( $this->once() )->method( 'addUser' )
669 $this->callback( function ( $u ) {
670 return $u instanceof \User
&& $u->getName() === 'Foo';
672 $this->equalTo( 'bar' ),
673 $this->equalTo( 'email' ),
674 $this->equalTo( 'realname' )
676 ->will( $this->returnValue( false ) );
677 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
678 $ret = $provider->beginPrimaryAccountCreation( $user, $user, $reqs );
679 $this->assertSame( AuthenticationResponse
::FAIL
, $ret->status
);
680 $this->assertSame( 'authmanager-authplugin-create-fail', $ret->message
->getKey() );
682 $plugin = $this->getMock( 'AuthPlugin' );
683 $plugin->expects( $this->any() )->method( 'canCreateAccounts' )
684 ->will( $this->returnValue( true ) );
685 $plugin->expects( $this->any() )->method( 'domainList' )
686 ->will( $this->returnValue( [ 'Domain1', 'Domain2' ] ) );
687 $plugin->expects( $this->any() )->method( 'validDomain' )
688 ->will( $this->returnCallback( function ( $domain ) {
689 return in_array( $domain, [ 'Domain1', 'Domain2' ] );
691 $plugin->expects( $this->once() )->method( 'setDomain' )
692 ->with( $this->equalTo( 'Domain2' ) );
693 $plugin->expects( $this->once() )->method( 'addUser' )
694 ->with( $this->callback( function ( $u ) {
695 return $u instanceof \User
&& $u->getName() === 'Foo';
696 } ), $this->equalTo( 'bar' ) )
697 ->will( $this->returnValue( true ) );
698 $provider = new AuthPluginPrimaryAuthenticationProvider( $plugin );
699 list( $req ) = $provider->getAuthenticationRequests( AuthManager
::ACTION_CREATE
, [] );
700 $req->username
= 'foo';
701 $req->password
= 'bar';
702 $req->domain
= 'Domain2';
703 $provider->beginPrimaryAccountCreation( $user, $user, [ $req ] );