251 $this->logger = new \Psr\Log\NullLogger();
252 $user = \User::newFromName(
'UTSysop' );
257 $mutableSession, [
'provideSessionInfo' ]
259 $provider->expects( $this->
any() )->method(
'provideSessionInfo' )
260 ->will( $this->returnCallback(
function () use ( $provider, &$provideUser ) {
261 return new SessionInfo( SessionInfo::MIN_PRIORITY, [
262 'provider' => $provider,
270 $this->config->set(
'ReauthenticateTime', [] );
271 $this->config->set(
'AllowSecuritySensitiveOperationIfCannotReauthenticate', [] );
272 $provideUser = new \User;
273 $session = $provider->getManager()->getSessionForRequest( $this->request );
274 $this->assertSame( 0, $session->getUser()->getId(),
'sanity check' );
277 $session->set(
'AuthManager:lastAuthId', 0 );
278 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
279 $this->assertSame( $reauth, $this->manager->securitySensitiveOperationStatus(
'foo' ) );
281 $provideUser =
$user;
282 $session = $provider->getManager()->getSessionForRequest( $this->request );
283 $this->assertSame( $user->getId(), $session->getUser()->getId(),
'sanity check' );
286 $session->set(
'AuthManager:lastAuthId', $user->getId() + 1 );
287 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
289 $this->manager->securitySensitiveOperationStatus(
'foo' );
290 $this->fail(
'Expected exception not thrown' );
291 }
catch ( \UnexpectedValueException $ex ) {
294 ?
'$wgReauthenticateTime lacks a default'
295 :
'$wgAllowSecuritySensitiveOperationIfCannotReauthenticate lacks a default',
300 if ( $mutableSession ) {
301 $this->config->set(
'ReauthenticateTime', [
308 $session->set(
'AuthManager:lastAuthId', $user->getId() + 1 );
309 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
321 $session->set(
'AuthManager:lastAuthId', $user->getId() );
322 $session->set(
'AuthManager:lastAuthTimestamp',
null );
334 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
340 $session->set(
'AuthManager:lastAuthTimestamp', time() - 20 );
349 $this->config->set(
'AllowSecuritySensitiveOperationIfCannotReauthenticate', [
368 ] as $hook => $expect ) {
369 $this->
hook(
'SecuritySensitiveOperationStatus', $this->exactly( 2 ) )
373 $this->callback(
function (
$s ) use ( $session ) {
374 return $s->getId() === $session->getId();
376 $mutableSession ? $this->equalTo( 500, 1 ) : $this->equalTo( -1 )
378 ->
will( $this->returnCallback(
function ( &$v ) use ( $hook ) {
382 $session->set(
'AuthManager:lastAuthTimestamp', time() - 500 );
384 $expect, $this->manager->securitySensitiveOperationStatus(
'test' ),
"hook $hook"
387 $expect, $this->manager->securitySensitiveOperationStatus(
'test2' ),
"hook $hook"
389 $this->
unhook(
'SecuritySensitiveOperationStatus' );
392 ScopedCallback::consume( $reset );
461 'pre' => $this->getMockForAbstractClass( PreAuthenticationProvider::class ),
462 'primary' => $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
463 'secondary' => $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class ),
465 foreach ( $mocks as $key => $mock ) {
466 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue( $key ) );
467 $mock->expects( $this->once() )->method(
'setLogger' );
468 $mock->expects( $this->once() )->method(
'setManager' );
469 $mock->expects( $this->once() )->method(
'setConfig' );
471 $this->preauthMocks = [ $mocks[
'pre'] ];
472 $this->primaryauthMocks = [ $mocks[
'primary'] ];
473 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
479 $this->managerPriv->getAuthenticationProvider(
'primary' )
483 $this->managerPriv->getAuthenticationProvider(
'secondary' )
487 $this->managerPriv->getAuthenticationProvider(
'pre' )
490 [
'pre' => $mocks[
'pre'] ],
491 $this->managerPriv->getPreAuthenticationProviders()
494 [
'primary' => $mocks[
'primary'] ],
495 $this->managerPriv->getPrimaryAuthenticationProviders()
498 [
'secondary' => $mocks[
'secondary'] ],
499 $this->managerPriv->getSecondaryAuthenticationProviders()
503 $mock1 = $this->getMockForAbstractClass( PreAuthenticationProvider::class );
504 $mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
505 $mock1->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
506 $mock2->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
507 $this->preauthMocks = [ $mock1 ];
508 $this->primaryauthMocks = [ $mock2 ];
509 $this->secondaryauthMocks = [];
512 $this->managerPriv->getAuthenticationProvider(
'Y' );
513 $this->fail(
'Expected exception not thrown' );
514 }
catch ( \RuntimeException $ex ) {
515 $class1 = get_class( $mock1 );
516 $class2 = get_class( $mock2 );
518 "Duplicate specifications for id X (classes $class1 and $class2)", $ex->getMessage()
523 $mock = $this->getMockForAbstractClass( AuthenticationProvider::class );
524 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
525 $class = get_class( $mock );
526 $this->preauthMocks = [ $mock ];
527 $this->primaryauthMocks = [ $mock ];
528 $this->secondaryauthMocks = [ $mock ];
531 $this->managerPriv->getPreAuthenticationProviders();
532 $this->fail(
'Expected exception not thrown' );
533 }
catch ( \RuntimeException $ex ) {
535 "Expected instance of MediaWiki\\Auth\\PreAuthenticationProvider, got $class",
540 $this->managerPriv->getPrimaryAuthenticationProviders();
541 $this->fail(
'Expected exception not thrown' );
542 }
catch ( \RuntimeException $ex ) {
544 "Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got $class",
549 $this->managerPriv->getSecondaryAuthenticationProviders();
550 $this->fail(
'Expected exception not thrown' );
551 }
catch ( \RuntimeException $ex ) {
553 "Expected instance of MediaWiki\\Auth\\SecondaryAuthenticationProvider, got $class",
559 $mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
560 $mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
561 $mock3 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
562 $mock1->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'A' ) );
563 $mock2->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
564 $mock3->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'C' ) );
565 $this->preauthMocks = [];
566 $this->primaryauthMocks = [ $mock1, $mock2, $mock3 ];
567 $this->secondaryauthMocks = [];
569 $config = $this->config->
get(
'AuthManagerConfig' );
573 [
'A' => $mock1,
'B' => $mock2,
'C' => $mock3 ],
574 $this->managerPriv->getPrimaryAuthenticationProviders(),
578 $config[
'primaryauth'][
'A'][
'sort'] = 100;
579 $config[
'primaryauth'][
'C'][
'sort'] = -1;
580 $this->config->set(
'AuthManagerConfig',
$config );
583 [
'C' => $mock3,
'B' => $mock2,
'A' => $mock1 ],
584 $this->managerPriv->getPrimaryAuthenticationProviders()
627 $mockA = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
628 $mockB = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
629 $mockB2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
630 $mockA->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'A' ) );
631 $mockB->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
632 $mockB2->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
633 $this->primaryauthMocks = [ $mockA ];
635 $this->logger = new \TestLogger(
true );
639 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB ],
'testing' );
641 [
'B' => $mockB ], $this->managerPriv->getPrimaryAuthenticationProviders()
643 $this->assertSame(
null, $this->managerPriv->getAuthenticationProvider(
'A' ) );
644 $this->assertSame( $mockB, $this->managerPriv->getAuthenticationProvider(
'B' ) );
646 [ LogLevel::WARNING,
'Overriding AuthManager primary authn because testing' ],
647 ], $this->logger->getBuffer() );
648 $this->logger->clearBuffer();
652 $this->assertSame( $mockA, $this->managerPriv->getAuthenticationProvider(
'A' ) );
653 $this->assertSame(
null, $this->managerPriv->getAuthenticationProvider(
'B' ) );
654 $this->request->getSession()->setSecret(
'AuthManager::authnState',
'test' );
655 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
'test' );
656 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB ],
'testing' );
658 [
'B' => $mockB ], $this->managerPriv->getPrimaryAuthenticationProviders()
660 $this->assertSame(
null, $this->managerPriv->getAuthenticationProvider(
'A' ) );
661 $this->assertSame( $mockB, $this->managerPriv->getAuthenticationProvider(
'B' ) );
662 $this->assertNull( $this->request->getSession()->getSecret(
'AuthManager::authnState' ) );
664 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
667 [ LogLevel::WARNING,
'Overriding AuthManager primary authn because testing' ],
670 'PrimaryAuthenticationProviders have already been accessed! I hope nothing breaks.'
672 ], $this->logger->getBuffer() );
673 $this->logger->clearBuffer();
678 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB, $mockB2 ],
'testing' );
679 $this->fail(
'Expected exception not thrown' );
680 }
catch ( \RuntimeException $ex ) {
681 $class1 = get_class( $mockB );
682 $class2 = get_class( $mockB2 );
684 "Duplicate specifications for id B (classes $class2 and $class1)", $ex->getMessage()
689 $mock = $this->getMockForAbstractClass( AuthenticationProvider::class );
690 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
691 $class = get_class( $mock );
693 $this->manager->forcePrimaryAuthenticationProviders( [ $mock ],
'testing' );
694 $this->fail(
'Expected exception not thrown' );
695 }
catch ( \RuntimeException $ex ) {
697 "Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got $class",
767 $user = \User::newFromName(
'UTSysop' );
768 $req1 = $this->createMock( AuthenticationRequest::class );
769 $req2 = $this->createMock( AuthenticationRequest::class );
770 $req3 = $this->createMock( AuthenticationRequest::class );
772 $userReq->username =
'UTDummy';
774 $req1->returnToUrl =
'http://localhost/';
775 $req2->returnToUrl =
'http://localhost/';
776 $req3->returnToUrl =
'http://localhost/';
777 $req3->username =
'UTDummy';
778 $userReq->returnToUrl =
'http://localhost/';
781 $primary = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
782 $this->primaryauthMocks = [ $primary ];
785 $res->createRequest = $req1;
786 $primary->expects( $this->
any() )->method(
'beginPrimaryAuthentication' )
787 ->will( $this->returnValue(
$res ) );
789 null, [ $req2->getUniqueId() => $req2 ]
791 $this->logger->setCollect(
true );
792 $ret = $this->manager->beginAuthentication( [ $createReq ],
'http://localhost/' );
793 $this->logger->setCollect(
false );
795 $this->assertInstanceOf( CreateFromLoginAuthenticationRequest::class,
$ret->createRequest );
796 $this->assertSame( $req1,
$ret->createRequest->createRequest );
797 $this->assertEquals( [ $req2->getUniqueId() => $req2 ],
$ret->createRequest->maybeLink );
800 $primary = $this->getMockBuilder( AbstractPrimaryAuthenticationProvider::class )
801 ->setMethods( [
'continuePrimaryAuthentication' ] )
802 ->getMockForAbstractClass();
803 $this->primaryauthMocks = [ $primary ];
805 $primary->expects( $this->
any() )->method(
'beginPrimaryAuthentication' )
806 ->will( $this->returnValue(
810 $res->createRequest = $req2;
811 $primary->expects( $this->
any() )->method(
'continuePrimaryAuthentication' )
812 ->will( $this->returnValue(
$res ) );
813 $this->logger->setCollect(
true );
814 $ret = $this->manager->beginAuthentication( [],
'http://localhost/' );
816 $ret = $this->manager->continueAuthentication( [] );
817 $this->logger->setCollect(
false );
819 $this->assertInstanceOf( CreateFromLoginAuthenticationRequest::class,
$ret->createRequest );
820 $this->assertSame( $req2,
$ret->createRequest->createRequest );
821 $this->assertEquals( [],
$ret->createRequest->maybeLink );
824 $primary = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
825 $this->primaryauthMocks = [ $primary ];
828 $createReq->returnToUrl =
'http://localhost/';
829 $createReq->username =
'UTDummy';
831 $primary->expects( $this->
any() )->method(
'beginPrimaryAccountCreation' )
832 ->with( $this->
anything(), $this->
anything(), [ $userReq, $createReq, $req3 ] )
833 ->will( $this->returnValue(
$res ) );
834 $primary->expects( $this->
any() )->method(
'accountCreationType' )
836 $this->logger->setCollect(
true );
837 $ret = $this->manager->beginAccountCreation(
838 $user, [ $userReq, $createReq ],
'http://localhost/'
840 $this->logger->setCollect(
false );
842 $state = $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' );
843 $this->assertNotNull( $state );
844 $this->assertEquals( [ $userReq, $createReq, $req3 ], $state[
'reqs'] );
845 $this->assertEquals( [ $req2 ], $state[
'maybeLink'] );
857 StatusValue $preResponse, array $primaryResponses, array $secondaryResponses,
858 array $managerResponses,
$link =
false
861 $user = \User::newFromName(
'UTSysop' );
862 $id = $user->getId();
863 $name = $user->getName();
867 $req->rememberMe = (bool)rand( 0, 1 );
868 $req->pre = $preResponse;
869 $req->primary = $primaryResponses;
870 $req->secondary = $secondaryResponses;
872 foreach ( [
'pre',
'primary',
'secondary' ] as $key ) {
873 $class = ucfirst( $key ) .
'AuthenticationProvider';
874 $mocks[$key] = $this->getMockForAbstractClass(
875 "MediaWiki\\Auth\\$class", [],
"Mock$class"
877 $mocks[$key]->expects( $this->
any() )->method(
'getUniqueId' )
878 ->will( $this->returnValue( $key ) );
879 $mocks[$key .
'2'] = $this->getMockForAbstractClass(
880 "MediaWiki\\Auth\\$class", [],
"Mock$class"
882 $mocks[$key .
'2']->expects( $this->
any() )->method(
'getUniqueId' )
883 ->will( $this->returnValue( $key .
'2' ) );
884 $mocks[$key .
'3'] = $this->getMockForAbstractClass(
885 "MediaWiki\\Auth\\$class", [],
"Mock$class"
887 $mocks[$key .
'3']->expects( $this->
any() )->method(
'getUniqueId' )
888 ->will( $this->returnValue( $key .
'3' ) );
890 foreach ( $mocks as $mock ) {
891 $mock->expects( $this->
any() )->method(
'getAuthenticationRequests' )
892 ->will( $this->returnValue( [] ) );
895 $mocks[
'pre']->expects( $this->once() )->method(
'testForAuthentication' )
896 ->will( $this->returnCallback(
function ( $reqs ) use (
$req ) {
897 $this->assertContains(
$req, $reqs );
901 $ct = count(
$req->primary );
902 $callback = $this->returnCallback(
function ( $reqs ) use (
$req ) {
903 $this->assertContains(
$req, $reqs );
904 return array_shift(
$req->primary );
906 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
907 ->method(
'beginPrimaryAuthentication' )
909 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
910 ->method(
'continuePrimaryAuthentication' )
913 $mocks[
'primary']->expects( $this->
any() )->method(
'accountCreationType' )
917 $ct = count(
$req->secondary );
918 $callback = $this->returnCallback(
function ( $user, $reqs ) use ( $id, $name,
$req ) {
919 $this->assertSame( $id, $user->getId() );
920 $this->assertSame( $name, $user->getName() );
921 $this->assertContains(
$req, $reqs );
922 return array_shift(
$req->secondary );
924 $mocks[
'secondary']->expects( $this->exactly( min( 1, $ct ) ) )
925 ->method(
'beginSecondaryAuthentication' )
927 $mocks[
'secondary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
928 ->method(
'continueSecondaryAuthentication' )
932 $mocks[
'pre2']->expects( $this->atMost( 1 ) )->method(
'testForAuthentication' )
933 ->will( $this->returnValue( StatusValue::newGood() ) );
934 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAuthentication' )
935 ->will( $this->returnValue( $abstain ) );
936 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAuthentication' );
937 $mocks[
'secondary2']->expects( $this->atMost( 1 ) )->method(
'beginSecondaryAuthentication' )
938 ->will( $this->returnValue( $abstain ) );
939 $mocks[
'secondary2']->expects( $this->never() )->method(
'continueSecondaryAuthentication' );
940 $mocks[
'secondary3']->expects( $this->atMost( 1 ) )->method(
'beginSecondaryAuthentication' )
941 ->will( $this->returnValue( $abstain ) );
942 $mocks[
'secondary3']->expects( $this->never() )->method(
'continueSecondaryAuthentication' );
944 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
945 $this->primaryauthMocks = [ $mocks[
'primary'], $mocks[
'primary2'] ];
946 $this->secondaryauthMocks = [
947 $mocks[
'secondary3'], $mocks[
'secondary'], $mocks[
'secondary2'],
952 $this->logger->setCollect(
true );
954 $constraint = \PHPUnit_Framework_Assert::logicalOr(
958 $providers = array_filter(
960 $this->preauthMocks, $this->primaryauthMocks, $this->secondaryauthMocks
963 return is_callable( [ $p,
'expects' ] );
966 foreach ( $providers as $p ) {
967 $p->postCalled =
false;
968 $p->expects( $this->atMost( 1 ) )->method(
'postAuthentication' )
969 ->willReturnCallback(
function ( $user,
$response )
use ( $constraint, $p ) {
970 if ( $user !==
null ) {
971 $this->assertInstanceOf(
'User', $user );
972 $this->assertSame(
'UTSysop', $user->getName() );
974 $this->assertInstanceOf( AuthenticationResponse::class,
$response );
975 $this->assertThat(
$response->status, $constraint );
980 $session = $this->request->getSession();
981 $session->setRememberUser( !
$req->rememberMe );
983 foreach ( $managerResponses as $i =>
$response ) {
987 $this->
hook(
'UserLoggedIn', $this->once() )
988 ->with( $this->callback(
function ( $user ) use ( $id, $name ) {
989 return $user->getId() === $id && $user->getName() === $name;
992 $this->
hook(
'UserLoggedIn', $this->never() );
997 $response->message->getKey() !==
'authmanager-authn-not-in-progress' &&
998 $response->message->getKey() !==
'authmanager-authn-no-primary'
1001 $this->
hook(
'AuthManagerLoginAuthenticateAudit', $this->once() );
1003 $this->
hook(
'AuthManagerLoginAuthenticateAudit', $this->never() );
1009 $ret = $this->manager->beginAuthentication( [
$req ],
'http://localhost/' );
1011 $ret = $this->manager->continueAuthentication( [
$req ] );
1013 if (
$response instanceof \Exception ) {
1014 $this->fail(
'Expected exception not thrown',
"Response $i" );
1016 }
catch ( \Exception $ex ) {
1017 if ( !
$response instanceof \Exception ) {
1020 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
1021 $this->assertNull( $session->getSecret(
'AuthManager::authnState' ),
1022 "Response $i, exception, session state" );
1023 $this->
unhook(
'UserLoggedIn' );
1024 $this->
unhook(
'AuthManagerLoginAuthenticateAudit' );
1028 $this->
unhook(
'UserLoggedIn' );
1029 $this->
unhook(
'AuthManagerLoginAuthenticateAudit' );
1031 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
1034 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
1036 $this->assertSame( $id, $session->getUser()->getId(),
1037 "Response $i, authn" );
1039 $this->assertSame( 0, $session->getUser()->getId(),
1040 "Response $i, authn" );
1043 $this->assertNull( $session->getSecret(
'AuthManager::authnState' ),
1044 "Response $i, session state" );
1045 foreach ( $providers as $p ) {
1046 $this->assertSame(
$response->status, $p->postCalled,
1047 "Response $i, post-auth callback called" );
1050 $this->assertNotNull( $session->getSecret(
'AuthManager::authnState' ),
1051 "Response $i, session state" );
1052 foreach (
$ret->neededRequests as $neededReq ) {
1054 "Response $i, neededRequest action" );
1056 $this->assertEquals(
1057 $ret->neededRequests,
1059 "Response $i, continuation check"
1061 foreach ( $providers as $p ) {
1062 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
1066 $state = $session->getSecret(
'AuthManager::authnState' );
1067 $maybeLink = isset( $state[
'maybeLink'] ) ? $state[
'maybeLink'] : [];
1069 $this->assertEquals(
1072 "Response $i, maybeLink"
1075 $this->assertEquals( [], $maybeLink,
"Response $i, maybeLink" );
1080 $this->assertSame(
$req->rememberMe, $session->shouldRememberUser(),
1081 'rememberMe checkbox had effect' );
1083 $this->assertNotSame(
$req->rememberMe, $session->shouldRememberUser(),
1084 'rememberMe checkbox wasn\'t applied' );
1553 $creator = \User::newFromName(
'UTSysop' );
1555 $this->logger = new \TestLogger(
false,
function ( $message, $level ) {
1556 return $level === LogLevel::DEBUG ? null : $message;
1560 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
'test' );
1561 $this->
hook(
'LocalUserCreated', $this->never() );
1563 $this->manager->beginAccountCreation(
1564 $creator, [],
'http://localhost/'
1566 $this->fail(
'Expected exception not thrown' );
1567 }
catch ( \LogicException $ex ) {
1568 $this->assertEquals(
'Account creation is not possible', $ex->getMessage() );
1570 $this->
unhook(
'LocalUserCreated' );
1572 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1575 $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
1576 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1577 $mock->expects( $this->
any() )->method(
'accountCreationType' )
1579 $mock->expects( $this->
any() )->method(
'testUserExists' )->will( $this->returnValue(
true ) );
1580 $mock->expects( $this->
any() )->method(
'testUserForCreation' )
1581 ->will( $this->returnValue( StatusValue::newGood() ) );
1582 $this->primaryauthMocks = [ $mock ];
1585 $this->
hook(
'LocalUserCreated', $this->never() );
1586 $ret = $this->manager->beginAccountCreation( $creator, [],
'http://localhost/' );
1587 $this->
unhook(
'LocalUserCreated' );
1589 $this->assertSame(
'noname',
$ret->message->getKey() );
1591 $this->
hook(
'LocalUserCreated', $this->never() );
1594 $userReq2->username = $userReq->username .
'X';
1595 $ret = $this->manager->beginAccountCreation(
1596 $creator, [ $userReq, $userReq2 ],
'http://localhost/'
1598 $this->
unhook(
'LocalUserCreated' );
1600 $this->assertSame(
'noname',
$ret->message->getKey() );
1602 $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
1603 $readOnlyMode->setReason(
'Because' );
1604 $this->
hook(
'LocalUserCreated', $this->never() );
1606 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1607 $this->
unhook(
'LocalUserCreated' );
1609 $this->assertSame(
'readonlytext',
$ret->message->getKey() );
1610 $this->assertSame( [
'Because' ],
$ret->message->getParams() );
1611 $readOnlyMode->setReason(
false );
1613 $this->
hook(
'LocalUserCreated', $this->never() );
1615 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1616 $this->
unhook(
'LocalUserCreated' );
1618 $this->assertSame(
'userexists',
$ret->message->getKey() );
1620 $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
1621 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1622 $mock->expects( $this->
any() )->method(
'accountCreationType' )
1624 $mock->expects( $this->
any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1625 $mock->expects( $this->
any() )->method(
'testUserForCreation' )
1626 ->will( $this->returnValue( StatusValue::newFatal(
'fail' ) ) );
1627 $this->primaryauthMocks = [ $mock ];
1630 $this->
hook(
'LocalUserCreated', $this->never() );
1632 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1633 $this->
unhook(
'LocalUserCreated' );
1635 $this->assertSame(
'fail',
$ret->message->getKey() );
1637 $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
1638 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1639 $mock->expects( $this->
any() )->method(
'accountCreationType' )
1641 $mock->expects( $this->
any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1642 $mock->expects( $this->
any() )->method(
'testUserForCreation' )
1643 ->will( $this->returnValue( StatusValue::newGood() ) );
1644 $this->primaryauthMocks = [ $mock ];
1647 $this->
hook(
'LocalUserCreated', $this->never() );
1649 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1650 $this->
unhook(
'LocalUserCreated' );
1652 $this->assertSame(
'noname',
$ret->message->getKey() );
1654 $this->
hook(
'LocalUserCreated', $this->never() );
1655 $userReq->username = $creator->getName();
1656 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1657 $this->
unhook(
'LocalUserCreated' );
1659 $this->assertSame(
'userexists',
$ret->message->getKey() );
1661 $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
1662 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1663 $mock->expects( $this->
any() )->method(
'accountCreationType' )
1665 $mock->expects( $this->
any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1666 $mock->expects( $this->
any() )->method(
'testUserForCreation' )
1667 ->will( $this->returnValue( StatusValue::newGood() ) );
1668 $mock->expects( $this->
any() )->method(
'testForAccountCreation' )
1669 ->will( $this->returnValue( StatusValue::newFatal(
'fail' ) ) );
1670 $this->primaryauthMocks = [ $mock ];
1673 $req = $this->getMockBuilder( UserDataAuthenticationRequest::class )
1674 ->setMethods( [
'populateUser' ] )
1676 $req->expects( $this->
any() )->method(
'populateUser' )
1677 ->willReturn( \StatusValue::newFatal(
'populatefail' ) );
1679 $ret = $this->manager->beginAccountCreation(
1680 $creator, [ $userReq,
$req ],
'http://localhost/'
1683 $this->assertSame(
'populatefail',
$ret->message->getKey() );
1688 $ret = $this->manager->beginAccountCreation(
1689 $creator, [ $userReq,
$req ],
'http://localhost/'
1692 $this->assertSame(
'fail',
$ret->message->getKey() );
1694 $this->manager->beginAccountCreation(
1695 \User::newFromName( $userReq->username ), [ $userReq,
$req ],
'http://localhost/'
1698 $this->assertSame(
'fail',
$ret->message->getKey() );
1702 $creator = \User::newFromName(
'UTSysop' );
1704 $this->logger = new \TestLogger(
false,
function ( $message, $level ) {
1705 return $level === LogLevel::DEBUG ? null : $message;
1716 'primaryResponse' =>
null,
1718 'ranPreTests' =>
true,
1721 $this->
hook(
'LocalUserCreated', $this->never() );
1723 $this->manager->continueAccountCreation( [] );
1724 $this->fail(
'Expected exception not thrown' );
1725 }
catch ( \LogicException $ex ) {
1726 $this->assertEquals(
'Account creation is not possible', $ex->getMessage() );
1728 $this->
unhook(
'LocalUserCreated' );
1730 $mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
1731 $mock->expects( $this->
any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1732 $mock->expects( $this->
any() )->method(
'accountCreationType' )
1734 $mock->expects( $this->
any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1735 $mock->expects( $this->
any() )->method(
'beginPrimaryAccountCreation' )->will(
1738 $this->primaryauthMocks = [ $mock ];
1741 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
null );
1742 $this->
hook(
'LocalUserCreated', $this->never() );
1743 $ret = $this->manager->continueAccountCreation( [] );
1744 $this->
unhook(
'LocalUserCreated' );
1746 $this->assertSame(
'authmanager-create-not-in-progress',
$ret->message->getKey() );
1748 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1749 [
'username' =>
"$username<>" ] + $session );
1750 $this->
hook(
'LocalUserCreated', $this->never() );
1751 $ret = $this->manager->continueAccountCreation( [] );
1752 $this->
unhook(
'LocalUserCreated' );
1754 $this->assertSame(
'noname',
$ret->message->getKey() );
1756 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1759 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState', $session );
1760 $this->
hook(
'LocalUserCreated', $this->never() );
1761 $cache = \ObjectCache::getLocalClusterInstance();
1763 $ret = $this->manager->continueAccountCreation( [] );
1765 $this->
unhook(
'LocalUserCreated' );
1767 $this->assertSame(
'usernameinprogress',
$ret->message->getKey() );
1771 $session, $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1774 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1775 [
'username' => $creator->getName() ] + $session );
1776 $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
1777 $readOnlyMode->setReason(
'Because' );
1778 $this->
hook(
'LocalUserCreated', $this->never() );
1779 $ret = $this->manager->continueAccountCreation( [] );
1780 $this->
unhook(
'LocalUserCreated' );
1782 $this->assertSame(
'readonlytext',
$ret->message->getKey() );
1783 $this->assertSame( [
'Because' ],
$ret->message->getParams() );
1784 $readOnlyMode->setReason(
false );
1786 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1787 [
'username' => $creator->getName() ] + $session );
1788 $this->
hook(
'LocalUserCreated', $this->never() );
1789 $ret = $this->manager->continueAccountCreation( [] );
1790 $this->
unhook(
'LocalUserCreated' );
1792 $this->assertSame(
'userexists',
$ret->message->getKey() );
1794 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1797 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1798 [
'userid' => $creator->getId() ] + $session );
1799 $this->
hook(
'LocalUserCreated', $this->never() );
1801 $ret = $this->manager->continueAccountCreation( [] );
1802 $this->fail(
'Expected exception not thrown' );
1803 }
catch ( \UnexpectedValueException $ex ) {
1804 $this->assertEquals(
"User \"{$username}\" should exist now, but doesn't!", $ex->getMessage() );
1806 $this->
unhook(
'LocalUserCreated' );
1808 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1811 $id = $creator->getId();
1812 $name = $creator->getName();
1813 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1814 [
'username' => $name,
'userid' => $id + 1 ] + $session );
1815 $this->
hook(
'LocalUserCreated', $this->never() );
1817 $ret = $this->manager->continueAccountCreation( [] );
1818 $this->fail(
'Expected exception not thrown' );
1819 }
catch ( \UnexpectedValueException $ex ) {
1820 $this->assertEquals(
1821 "User \"{$name}\" exists, but ID $id != " . ( $id + 1 ) .
'!', $ex->getMessage()
1824 $this->
unhook(
'LocalUserCreated' );
1826 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1829 $req = $this->getMockBuilder( UserDataAuthenticationRequest::class )
1830 ->setMethods( [
'populateUser' ] )
1832 $req->expects( $this->
any() )->method(
'populateUser' )
1833 ->willReturn( \StatusValue::newFatal(
'populatefail' ) );
1834 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState',
1835 [
'reqs' => [
$req ] ] + $session );
1836 $ret = $this->manager->continueAccountCreation( [] );
1838 $this->assertSame(
'populatefail',
$ret->message->getKey() );
1840 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1854 StatusValue $preTest, $primaryTest, $secondaryTest,
1855 array $primaryResponses, array $secondaryResponses, array $managerResponses
1857 $creator = \User::newFromName(
'UTSysop' );
1863 $req = $this->getMockForAbstractClass( AuthenticationRequest::class );
1864 $req->preTest = $preTest;
1865 $req->primaryTest = $primaryTest;
1866 $req->secondaryTest = $secondaryTest;
1867 $req->primary = $primaryResponses;
1868 $req->secondary = $secondaryResponses;
1870 foreach ( [
'pre',
'primary',
'secondary' ] as $key ) {
1871 $class = ucfirst( $key ) .
'AuthenticationProvider';
1872 $mocks[$key] = $this->getMockForAbstractClass(
1873 "MediaWiki\\Auth\\$class", [],
"Mock$class"
1875 $mocks[$key]->expects( $this->
any() )->method(
'getUniqueId' )
1876 ->will( $this->returnValue( $key ) );
1877 $mocks[$key]->expects( $this->
any() )->method(
'testUserForCreation' )
1878 ->will( $this->returnValue( StatusValue::newGood() ) );
1879 $mocks[$key]->expects( $this->
any() )->method(
'testForAccountCreation' )
1880 ->will( $this->returnCallback(
1881 function ( $user, $creatorIn, $reqs )
1884 $this->assertSame(
$username, $user->getName() );
1885 $this->assertSame( $creator->getId(), $creatorIn->getId() );
1886 $this->assertSame( $creator->getName(), $creatorIn->getName() );
1888 foreach ( $reqs as $r ) {
1889 $this->assertSame(
$username, $r->username );
1890 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1892 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1898 for ( $i = 2; $i <= 3; $i++ ) {
1899 $mocks[$key . $i] = $this->getMockForAbstractClass(
1900 "MediaWiki\\Auth\\$class", [],
"Mock$class"
1902 $mocks[$key . $i]->expects( $this->
any() )->method(
'getUniqueId' )
1903 ->will( $this->returnValue( $key . $i ) );
1904 $mocks[$key . $i]->expects( $this->
any() )->method(
'testUserForCreation' )
1905 ->will( $this->returnValue( StatusValue::newGood() ) );
1906 $mocks[$key . $i]->expects( $this->atMost( 1 ) )->method(
'testForAccountCreation' )
1907 ->will( $this->returnValue( StatusValue::newGood() ) );
1911 $mocks[
'primary']->expects( $this->
any() )->method(
'accountCreationType' )
1913 $mocks[
'primary']->expects( $this->
any() )->method(
'testUserExists' )
1914 ->will( $this->returnValue(
false ) );
1915 $ct = count(
$req->primary );
1916 $callback = $this->returnCallback(
function ( $user, $creator, $reqs ) use (
$username,
$req ) {
1917 $this->assertSame(
$username, $user->getName() );
1918 $this->assertSame(
'UTSysop', $creator->getName() );
1920 foreach ( $reqs as $r ) {
1921 $this->assertSame(
$username, $r->username );
1922 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1924 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1925 return array_shift(
$req->primary );
1927 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
1928 ->method(
'beginPrimaryAccountCreation' )
1929 ->will( $callback );
1930 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
1931 ->method(
'continuePrimaryAccountCreation' )
1932 ->will( $callback );
1934 $ct = count(
$req->secondary );
1935 $callback = $this->returnCallback(
function ( $user, $creator, $reqs ) use (
$username,
$req ) {
1936 $this->assertSame(
$username, $user->getName() );
1937 $this->assertSame(
'UTSysop', $creator->getName() );
1939 foreach ( $reqs as $r ) {
1940 $this->assertSame(
$username, $r->username );
1941 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1943 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1944 return array_shift(
$req->secondary );
1946 $mocks[
'secondary']->expects( $this->exactly( min( 1, $ct ) ) )
1947 ->method(
'beginSecondaryAccountCreation' )
1948 ->will( $callback );
1949 $mocks[
'secondary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
1950 ->method(
'continueSecondaryAccountCreation' )
1951 ->will( $callback );
1954 $mocks[
'primary2']->expects( $this->
any() )->method(
'accountCreationType' )
1956 $mocks[
'primary2']->expects( $this->
any() )->method(
'testUserExists' )
1957 ->will( $this->returnValue(
false ) );
1958 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAccountCreation' )
1959 ->will( $this->returnValue( $abstain ) );
1960 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAccountCreation' );
1961 $mocks[
'primary3']->expects( $this->
any() )->method(
'accountCreationType' )
1963 $mocks[
'primary3']->expects( $this->
any() )->method(
'testUserExists' )
1964 ->will( $this->returnValue(
false ) );
1965 $mocks[
'primary3']->expects( $this->never() )->method(
'beginPrimaryAccountCreation' );
1966 $mocks[
'primary3']->expects( $this->never() )->method(
'continuePrimaryAccountCreation' );
1967 $mocks[
'secondary2']->expects( $this->atMost( 1 ) )
1968 ->method(
'beginSecondaryAccountCreation' )
1969 ->will( $this->returnValue( $abstain ) );
1970 $mocks[
'secondary2']->expects( $this->never() )->method(
'continueSecondaryAccountCreation' );
1971 $mocks[
'secondary3']->expects( $this->atMost( 1 ) )
1972 ->method(
'beginSecondaryAccountCreation' )
1973 ->will( $this->returnValue( $abstain ) );
1974 $mocks[
'secondary3']->expects( $this->never() )->method(
'continueSecondaryAccountCreation' );
1976 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
1977 $this->primaryauthMocks = [ $mocks[
'primary3'], $mocks[
'primary'], $mocks[
'primary2'] ];
1978 $this->secondaryauthMocks = [
1979 $mocks[
'secondary3'], $mocks[
'secondary'], $mocks[
'secondary2']
1982 $this->logger = new \TestLogger(
true,
function ( $message, $level ) {
1983 return $level === LogLevel::DEBUG ? null : $message;
1988 $constraint = \PHPUnit_Framework_Assert::logicalOr(
1992 $providers = array_merge(
1993 $this->preauthMocks, $this->primaryauthMocks, $this->secondaryauthMocks
1995 foreach ( $providers as $p ) {
1996 $p->postCalled =
false;
1997 $p->expects( $this->atMost( 1 ) )->method(
'postAccountCreation' )
1998 ->willReturnCallback(
function ( $user, $creator,
$response )
2001 $this->assertInstanceOf(
'User', $user );
2002 $this->assertSame(
$username, $user->getName() );
2003 $this->assertSame(
'UTSysop', $creator->getName() );
2004 $this->assertInstanceOf( AuthenticationResponse::class,
$response );
2005 $this->assertThat(
$response->status, $constraint );
2012 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2016 foreach ( $managerResponses as $i =>
$response ) {
2019 if ( $i ===
'created' ) {
2021 $this->
hook(
'LocalUserCreated', $this->once() )
2023 $this->callback(
function ( $user ) use (
$username ) {
2026 $this->equalTo(
false )
2028 $expectLog[] = [ LogLevel::INFO,
"Creating user {user} during account creation" ];
2030 $this->
hook(
'LocalUserCreated', $this->never() );
2038 $ret = $this->manager->beginAccountCreation(
2039 $creator, [ $userReq,
$req ],
'http://localhost/'
2042 $ret = $this->manager->continueAccountCreation( [
$req ] );
2044 if (
$response instanceof \Exception ) {
2045 $this->fail(
'Expected exception not thrown',
"Response $i" );
2047 }
catch ( \Exception $ex ) {
2048 if ( !
$response instanceof \Exception ) {
2051 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
2053 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2054 "Response $i, exception, session state"
2056 $this->
unhook(
'LocalUserCreated' );
2060 $this->
unhook(
'LocalUserCreated' );
2062 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
2065 $this->assertNotNull(
$ret->loginRequest,
"Response $i, login marker" );
2066 $this->assertContains(
2067 $ret->loginRequest, $this->managerPriv->createdAccountAuthenticationRequests,
2068 "Response $i, login marker"
2073 "MediaWiki\Auth\AuthManager::continueAccountCreation: Account creation succeeded for {user}"
2081 $this->assertNull(
$ret->loginRequest,
"Response $i, login marker" );
2082 $this->assertSame( [], $this->managerPriv->createdAccountAuthenticationRequests,
2083 "Response $i, login marker" );
2086 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
2089 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2090 "Response $i, session state"
2092 foreach ( $providers as $p ) {
2093 $this->assertSame(
$response->status, $p->postCalled,
2094 "Response $i, post-auth callback called" );
2097 $this->assertNotNull(
2098 $this->request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2099 "Response $i, session state"
2101 foreach (
$ret->neededRequests as $neededReq ) {
2103 "Response $i, neededRequest action" );
2105 $this->assertEquals(
2106 $ret->neededRequests,
2108 "Response $i, continuation check"
2110 foreach ( $providers as $p ) {
2111 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
2116 $this->assertNotEquals( 0, \User::idFromName(
$username ) );
2118 $this->assertEquals( 0, \User::idFromName(
$username ) );
2124 $this->assertSame( $expectLog, $this->logger->getBuffer() );
2128 $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] )
2258 $creator = $isAnon ? new \User : \User::newFromName(
'UTSysop' );
2264 $mock = $this->getMockForAbstractClass(
2265 "MediaWiki\\Auth\\PrimaryAuthenticationProvider", []
2267 $mock->expects( $this->
any() )->method(
'getUniqueId' )
2268 ->will( $this->returnValue(
'primary' ) );
2269 $mock->expects( $this->
any() )->method(
'testUserForCreation' )
2270 ->will( $this->returnValue( StatusValue::newGood() ) );
2271 $mock->expects( $this->
any() )->method(
'testForAccountCreation' )
2272 ->will( $this->returnValue( StatusValue::newGood() ) );
2273 $mock->expects( $this->
any() )->method(
'accountCreationType' )
2275 $mock->expects( $this->
any() )->method(
'testUserExists' )
2276 ->will( $this->returnValue(
false ) );
2277 $mock->expects( $this->
any() )->method(
'beginPrimaryAccountCreation' )
2279 $mock->expects( $this->
any() )->method(
'finishAccountCreation' )
2280 ->will( $this->returnValue( $logSubtype ) );
2282 $this->primaryauthMocks = [ $mock ];
2284 $this->logger->setCollect(
true );
2286 $this->config->set(
'NewUserLog',
true );
2289 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2294 $reasonReq->reason = $this->toString();
2295 $ret = $this->manager->beginAccountCreation(
2296 $creator, [ $userReq, $reasonReq ],
'http://localhost/'
2301 $user = \User::newFromName(
$username );
2302 $this->assertNotEquals( 0, $user->getId(),
'sanity check' );
2303 $this->assertNotEquals( $creator->getId(), $user->getId(),
'sanity check' );
2305 $data = \DatabaseLogEntry::getSelectQueryData();
2306 $rows = iterator_to_array( $dbw->select(
2310 'log_id > ' . (
int)$maxLogId,
2311 'log_type' =>
'newusers'
2317 $this->assertCount( 1,
$rows );
2318 $entry = \DatabaseLogEntry::newFromRow( reset(
$rows ) );
2320 $this->assertSame( $logSubtype ?: ( $isAnon ?
'create' :
'create2' ), $entry->getSubtype() );
2322 $isAnon ? $user->getId() : $creator->getId(),
2323 $entry->getPerformer()->getId()
2326 $isAnon ? $user->getName() : $creator->getName(),
2327 $entry->getPerformer()->getName()
2329 $this->assertSame( $user->getUserPage()->getFullText(), $entry->getTarget()->getFullText() );
2330 $this->assertSame( [
'4::userid' => $user->getId() ], $entry->getParameters() );
2331 $this->assertSame( $this->toString(), $entry->getComment() );
2350 $workaroundPHPUnitBug =
false;
2356 $wgGroupPermissions[
'*'][
'createaccount'] =
true;
2360 $this->
setMwGlobals( [
'wgMainCacheType' => __METHOD__ ] );
2364 foreach ( [
'pre',
'primary',
'secondary' ] as $key ) {
2365 $class = ucfirst( $key ) .
'AuthenticationProvider';
2366 $mocks[$key] = $this->getMockForAbstractClass(
2367 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2369 $mocks[$key]->expects( $this->
any() )->method(
'getUniqueId' )
2370 ->will( $this->returnValue( $key ) );
2373 $good = StatusValue::newGood();
2374 $callback = $this->callback(
function ( $user ) use ( &
$username, &$workaroundPHPUnitBug ) {
2375 return $workaroundPHPUnitBug || $user->getName() ===
$username;
2378 $mocks[
'pre']->expects( $this->exactly( 12 ) )->method(
'testUserForCreation' )
2380 ->will( $this->onConsecutiveCalls(
2381 StatusValue::newFatal(
'ok' ), StatusValue::newFatal(
'ok' ),
2382 StatusValue::newFatal(
'fail-in-pre' ), $good, $good,
2390 $mocks[
'primary']->expects( $this->
any() )->method(
'accountCreationType' )
2392 $mocks[
'primary']->expects( $this->
any() )->method(
'testUserExists' )
2393 ->will( $this->returnValue(
true ) );
2394 $mocks[
'primary']->expects( $this->exactly( 9 ) )->method(
'testUserForCreation' )
2396 ->will( $this->onConsecutiveCalls(
2397 StatusValue::newFatal(
'fail-in-primary' ), $good,
2404 $mocks[
'primary']->expects( $this->exactly( 3 ) )->method(
'autoCreatedAccount' )
2407 $mocks[
'secondary']->expects( $this->exactly( 8 ) )->method(
'testUserForCreation' )
2409 ->will( $this->onConsecutiveCalls(
2410 StatusValue::newFatal(
'fail-in-secondary' ),
2417 $mocks[
'secondary']->expects( $this->exactly( 3 ) )->method(
'autoCreatedAccount' )
2420 $this->preauthMocks = [ $mocks[
'pre'] ];
2421 $this->primaryauthMocks = [ $mocks[
'primary'] ];
2422 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
2424 $session = $this->request->getSession();
2426 $logger = new \TestLogger(
true,
function ( $m ) {
2427 $m = str_replace(
'MediaWiki\\Auth\\AuthManager::autoCreateUser: ',
'', $m );
2430 $this->manager->setLogger(
$logger );
2433 $user = \User::newFromName(
'UTSysop' );
2434 $this->manager->autoCreateUser( $user,
'InvalidSource',
true );
2435 $this->fail(
'Expected exception not thrown' );
2436 }
catch ( \InvalidArgumentException $ex ) {
2437 $this->assertSame(
'Unknown auto-creation source: InvalidSource', $ex->getMessage() );
2442 $user = \User::newFromName(
'UTSysop' );
2443 $this->
hook(
'LocalUserCreated', $this->never() );
2445 $this->
unhook(
'LocalUserCreated' );
2446 $expect = \Status::newGood();
2447 $expect->warning(
'userexists' );
2448 $this->assertEquals( $expect,
$ret );
2449 $this->assertNotEquals( 0, $user->getId() );
2450 $this->assertSame(
'UTSysop', $user->getName() );
2451 $this->assertEquals( $user->getId(), $session->getUser()->getId() );
2452 $this->assertSame( [
2453 [ LogLevel::DEBUG,
'{username} already exists locally' ],
2458 $user = \User::newFromName(
'UTSysop' );
2459 $this->
hook(
'LocalUserCreated', $this->never() );
2461 $this->
unhook(
'LocalUserCreated' );
2462 $expect = \Status::newGood();
2463 $expect->warning(
'userexists' );
2464 $this->assertEquals( $expect,
$ret );
2465 $this->assertNotEquals( 0, $user->getId() );
2466 $this->assertSame(
'UTSysop', $user->getName() );
2467 $this->assertEquals( 0, $session->getUser()->getId() );
2468 $this->assertSame( [
2469 [ LogLevel::DEBUG,
'{username} already exists locally' ],
2475 $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
2476 $readOnlyMode->setReason(
'Because' );
2477 $user = \User::newFromName(
$username );
2478 $this->
hook(
'LocalUserCreated', $this->never() );
2480 $this->
unhook(
'LocalUserCreated' );
2481 $this->assertEquals( \Status::newFatal(
wfMessage(
'readonlytext',
'Because' ) ),
$ret );
2482 $this->assertEquals( 0, $user->getId() );
2483 $this->assertNotEquals(
$username, $user->getName() );
2484 $this->assertEquals( 0, $session->getUser()->getId() );
2485 $this->assertSame( [
2486 [ LogLevel::DEBUG,
'denied by wfReadOnly(): {reason}' ],
2489 $readOnlyMode->setReason(
false );
2493 $session->set(
'AuthManager::AutoCreateBlacklist',
'test' );
2494 $user = \User::newFromName(
$username );
2495 $this->
hook(
'LocalUserCreated', $this->never() );
2497 $this->
unhook(
'LocalUserCreated' );
2498 $this->assertEquals( \Status::newFatal(
'test' ),
$ret );
2499 $this->assertEquals( 0, $user->getId() );
2500 $this->assertNotEquals(
$username, $user->getName() );
2501 $this->assertEquals( 0, $session->getUser()->getId() );
2502 $this->assertSame( [
2503 [ LogLevel::DEBUG,
'blacklisted in session {sessionid}' ],
2508 $session->set(
'AuthManager::AutoCreateBlacklist', StatusValue::newFatal(
'test2' ) );
2509 $user = \User::newFromName(
$username );
2510 $this->
hook(
'LocalUserCreated', $this->never() );
2512 $this->
unhook(
'LocalUserCreated' );
2513 $this->assertEquals( \Status::newFatal(
'test2' ),
$ret );
2514 $this->assertEquals( 0, $user->getId() );
2515 $this->assertNotEquals(
$username, $user->getName() );
2516 $this->assertEquals( 0, $session->getUser()->getId() );
2517 $this->assertSame( [
2518 [ LogLevel::DEBUG,
'blacklisted in session {sessionid}' ],
2524 $user = \User::newFromName(
$username .
'@' );
2525 $this->
hook(
'LocalUserCreated', $this->never() );
2527 $this->
unhook(
'LocalUserCreated' );
2528 $this->assertEquals( \Status::newFatal(
'noname' ),
$ret );
2529 $this->assertEquals( 0, $user->getId() );
2530 $this->assertNotEquals(
$username .
'@', $user->getId() );
2531 $this->assertEquals( 0, $session->getUser()->getId() );
2532 $this->assertSame( [
2533 [ LogLevel::DEBUG,
'name "{username}" is not creatable' ],
2536 $this->assertSame(
'noname', $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2542 $user = \User::newFromName(
$username );
2543 $this->
hook(
'LocalUserCreated', $this->never() );
2545 $this->
unhook(
'LocalUserCreated' );
2546 $this->assertEquals( \Status::newFatal(
'authmanager-autocreate-noperm' ),
$ret );
2547 $this->assertEquals( 0, $user->getId() );
2548 $this->assertNotEquals(
$username, $user->getName() );
2549 $this->assertEquals( 0, $session->getUser()->getId() );
2550 $this->assertSame( [
2551 [ LogLevel::DEBUG,
'IP lacks the ability to create or autocreate accounts' ],
2555 'authmanager-autocreate-noperm', $session->get(
'AuthManager::AutoCreateBlacklist' )
2563 $user = \User::newFromName(
$username );
2564 $this->
hook(
'LocalUserCreated', $this->never() );
2566 $this->
unhook(
'LocalUserCreated' );
2567 $this->assertEquals( \Status::newFatal(
'ok' ),
$ret );
2572 $user = \User::newFromName(
$username );
2573 $this->
hook(
'LocalUserCreated', $this->never() );
2575 $this->
unhook(
'LocalUserCreated' );
2576 $this->assertEquals( \Status::newFatal(
'ok' ),
$ret );
2581 $user = \User::newFromName(
$username );
2582 $this->
hook(
'LocalUserCreated', $this->never() );
2583 $cache = \ObjectCache::getLocalClusterInstance();
2587 $this->
unhook(
'LocalUserCreated' );
2588 $this->assertEquals( \Status::newFatal(
'usernameinprogress' ),
$ret );
2589 $this->assertEquals( 0, $user->getId() );
2590 $this->assertNotEquals(
$username, $user->getName() );
2591 $this->assertEquals( 0, $session->getUser()->getId() );
2592 $this->assertSame( [
2593 [ LogLevel::DEBUG,
'Could not acquire account creation lock' ],
2599 $user = \User::newFromName(
$username );
2600 $this->
hook(
'LocalUserCreated', $this->never() );
2602 $this->
unhook(
'LocalUserCreated' );
2603 $this->assertEquals( \Status::newFatal(
'fail-in-pre' ),
$ret );
2604 $this->assertEquals( 0, $user->getId() );
2605 $this->assertNotEquals(
$username, $user->getName() );
2606 $this->assertEquals( 0, $session->getUser()->getId() );
2607 $this->assertSame( [
2608 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2611 $this->assertEquals(
2612 StatusValue::newFatal(
'fail-in-pre' ), $session->get(
'AuthManager::AutoCreateBlacklist' )
2616 $user = \User::newFromName(
$username );
2617 $this->
hook(
'LocalUserCreated', $this->never() );
2619 $this->
unhook(
'LocalUserCreated' );
2620 $this->assertEquals( \Status::newFatal(
'fail-in-primary' ),
$ret );
2621 $this->assertEquals( 0, $user->getId() );
2622 $this->assertNotEquals(
$username, $user->getName() );
2623 $this->assertEquals( 0, $session->getUser()->getId() );
2624 $this->assertSame( [
2625 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2628 $this->assertEquals(
2629 StatusValue::newFatal(
'fail-in-primary' ), $session->get(
'AuthManager::AutoCreateBlacklist' )
2633 $user = \User::newFromName(
$username );
2634 $this->
hook(
'LocalUserCreated', $this->never() );
2636 $this->
unhook(
'LocalUserCreated' );
2637 $this->assertEquals( \Status::newFatal(
'fail-in-secondary' ),
$ret );
2638 $this->assertEquals( 0, $user->getId() );
2639 $this->assertNotEquals(
$username, $user->getName() );
2640 $this->assertEquals( 0, $session->getUser()->getId() );
2641 $this->assertSame( [
2642 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2645 $this->assertEquals(
2646 StatusValue::newFatal(
'fail-in-secondary' ), $session->get(
'AuthManager::AutoCreateBlacklist' )
2650 $cache = \ObjectCache::getLocalClusterInstance();
2652 $cache->set( $backoffKey,
true );
2654 $user = \User::newFromName(
$username );
2655 $this->
hook(
'LocalUserCreated', $this->never() );
2657 $this->
unhook(
'LocalUserCreated' );
2658 $this->assertEquals( \Status::newFatal(
'authmanager-autocreate-exception' ),
$ret );
2659 $this->assertEquals( 0, $user->getId() );
2660 $this->assertNotEquals(
$username, $user->getName() );
2661 $this->assertEquals( 0, $session->getUser()->getId() );
2662 $this->assertSame( [
2663 [ LogLevel::DEBUG,
'{username} denied by prior creation attempt failures' ],
2666 $this->assertSame(
null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2667 $cache->delete( $backoffKey );
2671 $user = $this->getMockBuilder(
'User' )
2672 ->setMethods( [
'addToDatabase' ] )->getMock();
2673 $user->expects( $this->once() )->method(
'addToDatabase' )
2674 ->will( $this->returnValue( \Status::newFatal(
'because' ) ) );
2677 $this->assertEquals( \Status::newFatal(
'because' ),
$ret );
2678 $this->assertEquals( 0, $user->getId() );
2679 $this->assertNotEquals(
$username, $user->getName() );
2680 $this->assertEquals( 0, $session->getUser()->getId() );
2681 $this->assertSame( [
2682 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2683 [ LogLevel::ERROR,
'{username} failed with message {msg}' ],
2686 $this->assertSame(
null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2689 $cache = \ObjectCache::getLocalClusterInstance();
2691 $this->assertFalse(
$cache->get( $backoffKey ),
'sanity check' );
2693 $user = $this->getMockBuilder(
'User' )
2694 ->setMethods( [
'addToDatabase' ] )->getMock();
2695 $user->expects( $this->once() )->method(
'addToDatabase' )
2696 ->will( $this->throwException(
new \Exception(
'Excepted' ) ) );
2700 $this->fail(
'Expected exception not thrown' );
2701 }
catch ( \Exception $ex ) {
2702 $this->assertSame(
'Excepted', $ex->getMessage() );
2704 $this->assertEquals( 0, $user->getId() );
2705 $this->assertEquals( 0, $session->getUser()->getId() );
2706 $this->assertSame( [
2707 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2708 [ LogLevel::ERROR,
'{username} failed with exception {exception}' ],
2711 $this->assertSame(
null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2712 $this->assertNotEquals(
false,
$cache->get( $backoffKey ) );
2713 $cache->delete( $backoffKey );
2717 $user = $this->getMockBuilder(
'User' )
2718 ->setMethods( [
'addToDatabase' ] )->getMock();
2719 $user->expects( $this->once() )->method(
'addToDatabase' )
2720 ->will( $this->returnCallback(
function () use (
$username, &$user ) {
2721 $oldUser = \User::newFromName(
$username );
2722 $status = $oldUser->addToDatabase();
2723 $this->assertTrue(
$status->isOK(),
'sanity check' );
2724 $user->setId( $oldUser->getId() );
2725 return \Status::newFatal(
'userexists' );
2729 $expect = \Status::newGood();
2730 $expect->warning(
'userexists' );
2731 $this->assertEquals( $expect,
$ret );
2732 $this->assertNotEquals( 0, $user->getId() );
2733 $this->assertEquals(
$username, $user->getName() );
2734 $this->assertEquals( $user->getId(), $session->getUser()->getId() );
2735 $this->assertSame( [
2736 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2737 [ LogLevel::INFO,
'{username} already exists locally (race)' ],
2740 $this->assertSame(
null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2745 $user = \User::newFromName(
$username );
2746 $this->
hook(
'AuthPluginAutoCreate', $this->once() )
2747 ->with( $callback );
2749 get_class(
$wgHooks[
'AuthPluginAutoCreate'][0] ) .
'::onAuthPluginAutoCreate)' );
2750 $this->
hook(
'LocalUserCreated', $this->once() )
2751 ->with( $callback, $this->equalTo(
true ) );
2753 $this->
unhook(
'LocalUserCreated' );
2754 $this->
unhook(
'AuthPluginAutoCreate' );
2755 $this->assertEquals( \Status::newGood(),
$ret );
2756 $this->assertNotEquals( 0, $user->getId() );
2757 $this->assertEquals(
$username, $user->getName() );
2758 $this->assertEquals( $user->getId(), $session->getUser()->getId() );
2759 $this->assertSame( [
2760 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2765 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2768 $user = \User::newFromName(
$username );
2769 $this->
hook(
'LocalUserCreated', $this->once() )
2770 ->with( $callback, $this->equalTo(
true ) );
2772 $this->
unhook(
'LocalUserCreated' );
2773 $this->assertEquals( \Status::newGood(),
$ret );
2774 $this->assertNotEquals( 0, $user->getId() );
2775 $this->assertEquals(
$username, $user->getName() );
2776 $this->assertEquals( 0, $session->getUser()->getId() );
2777 $this->assertSame( [
2778 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2783 $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] )
2786 $this->config->set(
'NewUserLog',
true );
2789 $user = \User::newFromName(
$username );
2791 $this->assertEquals( \Status::newGood(),
$ret );
2794 $data = \DatabaseLogEntry::getSelectQueryData();
2795 $rows = iterator_to_array( $dbw->select(
2799 'log_id > ' . (
int)$maxLogId,
2800 'log_type' =>
'newusers'
2806 $this->assertCount( 1,
$rows );
2807 $entry = \DatabaseLogEntry::newFromRow( reset(
$rows ) );
2809 $this->assertSame(
'autocreate', $entry->getSubtype() );
2810 $this->assertSame( $user->getId(), $entry->getPerformer()->getId() );
2811 $this->assertSame( $user->getName(), $entry->getPerformer()->getName() );
2812 $this->assertSame( $user->getUserPage()->getFullText(), $entry->getTarget()->getFullText() );
2813 $this->assertSame( [
'4::userid' => $user->getId() ], $entry->getParameters() );
2815 $workaroundPHPUnitBug =
true;
2825 $makeReq =
function ( $key ) use ( $action ) {
2826 $req = $this->createMock( AuthenticationRequest::class );
2827 $req->expects( $this->
any() )->method(
'getUniqueId' )
2828 ->will( $this->returnValue( $key ) );
2833 $cmpReqs =
function ( $a, $b ) {
2834 $ret = strcmp( get_class( $a ), get_class( $b ) );
2836 $ret = strcmp( $a->key, $b->key );
2841 $good = StatusValue::newGood();
2844 foreach ( [
'pre',
'primary',
'secondary' ] as $key ) {
2845 $class = ucfirst( $key ) .
'AuthenticationProvider';
2846 $mocks[$key] = $this->getMockForAbstractClass(
2847 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2849 $mocks[$key]->expects( $this->
any() )->method(
'getUniqueId' )
2850 ->will( $this->returnValue( $key ) );
2851 $mocks[$key]->expects( $this->
any() )->method(
'getAuthenticationRequests' )
2852 ->will( $this->returnCallback(
function ( $action ) use ( $key, $makeReq ) {
2853 return [ $makeReq(
"$key-$action" ), $makeReq(
'generic' ) ];
2855 $mocks[$key]->expects( $this->
any() )->method(
'providerAllowsAuthenticationDataChange' )
2856 ->will( $this->returnValue( $good ) );
2865 $class =
'PrimaryAuthenticationProvider';
2866 $mocks[
"primary-$type"] = $this->getMockForAbstractClass(
2867 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2869 $mocks[
"primary-$type"]->expects( $this->
any() )->method(
'getUniqueId' )
2870 ->will( $this->returnValue(
"primary-$type" ) );
2871 $mocks[
"primary-$type"]->expects( $this->
any() )->method(
'accountCreationType' )
2872 ->will( $this->returnValue(
$type ) );
2873 $mocks[
"primary-$type"]->expects( $this->
any() )->method(
'getAuthenticationRequests' )
2874 ->will( $this->returnCallback(
function ( $action ) use (
$type, $makeReq ) {
2875 return [ $makeReq(
"primary-$type-$action" ), $makeReq(
'generic' ) ];
2877 $mocks[
"primary-$type"]->expects( $this->
any() )
2878 ->method(
'providerAllowsAuthenticationDataChange' )
2879 ->will( $this->returnValue( $good ) );
2880 $this->primaryauthMocks[] = $mocks[
"primary-$type"];
2883 $mocks[
'primary2'] = $this->getMockForAbstractClass(
2884 PrimaryAuthenticationProvider::class, [],
"MockPrimaryAuthenticationProvider"
2886 $mocks[
'primary2']->expects( $this->
any() )->method(
'getUniqueId' )
2887 ->will( $this->returnValue(
'primary2' ) );
2888 $mocks[
'primary2']->expects( $this->
any() )->method(
'accountCreationType' )
2890 $mocks[
'primary2']->expects( $this->
any() )->method(
'getAuthenticationRequests' )
2891 ->will( $this->returnValue( [] ) );
2892 $mocks[
'primary2']->expects( $this->
any() )
2893 ->method(
'providerAllowsAuthenticationDataChange' )
2894 ->will( $this->returnCallback(
function (
$req ) use ( $good ) {
2895 return $req->key ===
'generic' ? StatusValue::newFatal(
'no' ) : $good;
2897 $this->primaryauthMocks[] = $mocks[
'primary2'];
2899 $this->preauthMocks = [ $mocks[
'pre'] ];
2900 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
2904 if ( isset( $state[
'continueRequests'] ) ) {
2905 $state[
'continueRequests'] = array_map( $makeReq, $state[
'continueRequests'] );
2908 $this->request->getSession()->setSecret(
'AuthManager::authnState', $state );
2910 $this->request->getSession()->setSecret(
'AuthManager::accountCreationState', $state );
2912 $this->request->getSession()->setSecret(
'AuthManager::accountLinkState', $state );
2916 $expectReqs = array_map( $makeReq, $expect );
2919 $req->action = $action;
2921 $expectReqs[] =
$req;
2924 $req->action = $action;
2925 $expectReqs[] =
$req;
2927 $req->action = $action;
2929 $expectReqs[] =
$req;
2931 usort( $expectReqs, $cmpReqs );
2933 $actual = $this->manager->getAuthenticationRequests( $action );
2934 foreach ( $actual as
$req ) {
2938 usort( $actual, $cmpReqs );
2940 $this->assertEquals( $expectReqs, $actual );
2945 $req->action = $action;
2947 $expectReqs[] =
$req;
2948 usort( $expectReqs, $cmpReqs );
2950 $actual = $this->manager->getAuthenticationRequests( $action, \User::newFromName(
'UTSysop' ) );
2951 foreach ( $actual as
$req ) {
2955 usort( $actual, $cmpReqs );
2957 $this->assertEquals( $expectReqs, $actual );
3028 $makeReq =
function ( $key, $required ) {
3029 $req = $this->createMock( AuthenticationRequest::class );
3030 $req->expects( $this->
any() )->method(
'getUniqueId' )
3031 ->will( $this->returnValue( $key ) );
3034 $req->required = $required;
3037 $cmpReqs =
function ( $a, $b ) {
3038 $ret = strcmp( get_class( $a ), get_class( $b ) );
3040 $ret = strcmp( $a->key, $b->key );
3045 $good = StatusValue::newGood();
3047 $primary1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
3048 $primary1->expects( $this->
any() )->method(
'getUniqueId' )
3049 ->will( $this->returnValue(
'primary1' ) );
3050 $primary1->expects( $this->
any() )->method(
'accountCreationType' )
3052 $primary1->expects( $this->
any() )->method(
'getAuthenticationRequests' )
3053 ->will( $this->returnCallback(
function ( $action ) use ( $makeReq ) {
3064 $primary2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
3065 $primary2->expects( $this->
any() )->method(
'getUniqueId' )
3066 ->will( $this->returnValue(
'primary2' ) );
3067 $primary2->expects( $this->
any() )->method(
'accountCreationType' )
3069 $primary2->expects( $this->
any() )->method(
'getAuthenticationRequests' )
3070 ->will( $this->returnCallback(
function ( $action ) use ( $makeReq ) {
3078 $secondary = $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class );
3079 $secondary->expects( $this->
any() )->method(
'getUniqueId' )
3080 ->will( $this->returnValue(
'secondary' ) );
3081 $secondary->expects( $this->
any() )->method(
'getAuthenticationRequests' )
3082 ->will( $this->returnCallback(
function ( $action ) use ( $makeReq ) {
3093 $this->primaryauthMocks = [ $primary1, $primary2 ];
3094 $this->secondaryauthMocks = [ $secondary ];
3109 usort( $actual, $cmpReqs );
3110 usort( $expected, $cmpReqs );
3111 $this->assertEquals( $expected, $actual );
3113 $this->primaryauthMocks = [ $primary1 ];
3114 $this->secondaryauthMocks = [ $secondary ];
3127 usort( $actual, $cmpReqs );
3128 usort( $expected, $cmpReqs );
3129 $this->assertEquals( $expected, $actual );
3387 StatusValue $preTest, array $primaryResponses, array $managerResponses
3389 $user = \User::newFromName(
'UTSysop' );
3394 $req = $this->getMockForAbstractClass( AuthenticationRequest::class );
3395 $req->primary = $primaryResponses;
3398 foreach ( [
'pre',
'primary' ] as $key ) {
3399 $class = ucfirst( $key ) .
'AuthenticationProvider';
3400 $mocks[$key] = $this->getMockForAbstractClass(
3401 "MediaWiki\\Auth\\$class", [],
"Mock$class"
3403 $mocks[$key]->expects( $this->
any() )->method(
'getUniqueId' )
3404 ->will( $this->returnValue( $key ) );
3406 for ( $i = 2; $i <= 3; $i++ ) {
3407 $mocks[$key . $i] = $this->getMockForAbstractClass(
3408 "MediaWiki\\Auth\\$class", [],
"Mock$class"
3410 $mocks[$key . $i]->expects( $this->
any() )->method(
'getUniqueId' )
3411 ->will( $this->returnValue( $key . $i ) );
3415 $mocks[
'pre']->expects( $this->
any() )->method(
'testForAccountLink' )
3416 ->will( $this->returnCallback(
3418 use ( $user, $preTest )
3420 $this->assertSame( $user->getId(), $u->getId() );
3421 $this->assertSame( $user->getName(), $u->getName() );
3426 $mocks[
'pre2']->expects( $this->atMost( 1 ) )->method(
'testForAccountLink' )
3427 ->will( $this->returnValue( StatusValue::newGood() ) );
3429 $mocks[
'primary']->expects( $this->
any() )->method(
'accountCreationType' )
3431 $ct = count(
$req->primary );
3432 $callback = $this->returnCallback(
function ( $u, $reqs ) use ( $user,
$req ) {
3433 $this->assertSame( $user->getId(), $u->getId() );
3434 $this->assertSame( $user->getName(), $u->getName() );
3436 foreach ( $reqs as $r ) {
3437 $this->assertSame( $user->getName(), $r->username );
3438 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
3440 $this->assertTrue( $foundReq,
'$reqs contains $req' );
3441 return array_shift(
$req->primary );
3443 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
3444 ->method(
'beginPrimaryAccountLink' )
3445 ->will( $callback );
3446 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
3447 ->method(
'continuePrimaryAccountLink' )
3448 ->will( $callback );
3451 $mocks[
'primary2']->expects( $this->
any() )->method(
'accountCreationType' )
3453 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAccountLink' )
3454 ->will( $this->returnValue( $abstain ) );
3455 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAccountLink' );
3456 $mocks[
'primary3']->expects( $this->
any() )->method(
'accountCreationType' )
3458 $mocks[
'primary3']->expects( $this->never() )->method(
'beginPrimaryAccountLink' );
3459 $mocks[
'primary3']->expects( $this->never() )->method(
'continuePrimaryAccountLink' );
3461 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
3462 $this->primaryauthMocks = [ $mocks[
'primary3'], $mocks[
'primary2'], $mocks[
'primary'] ];
3463 $this->logger = new \TestLogger(
true,
function ( $message, $level ) {
3464 return $level === LogLevel::DEBUG ? null : $message;
3468 $constraint = \PHPUnit_Framework_Assert::logicalOr(
3472 $providers = array_merge( $this->preauthMocks, $this->primaryauthMocks );
3473 foreach ( $providers as $p ) {
3474 $p->postCalled =
false;
3475 $p->expects( $this->atMost( 1 ) )->method(
'postAccountLink' )
3476 ->willReturnCallback(
function ( $user,
$response ) use ( $constraint, $p ) {
3477 $this->assertInstanceOf(
'User', $user );
3478 $this->assertSame(
'UTSysop', $user->getName() );
3479 $this->assertInstanceOf( AuthenticationResponse::class,
$response );
3480 $this->assertThat(
$response->status, $constraint );
3488 foreach ( $managerResponses as $i =>
$response ) {
3492 $expectLog[] = [ LogLevel::INFO,
'Account linked to {user} by primary' ];
3498 $ret = $this->manager->beginAccountLink( $user, [
$req ],
'http://localhost/' );
3500 $ret = $this->manager->continueAccountLink( [
$req ] );
3502 if (
$response instanceof \Exception ) {
3503 $this->fail(
'Expected exception not thrown',
"Response $i" );
3505 }
catch ( \Exception $ex ) {
3506 if ( !
$response instanceof \Exception ) {
3509 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
3510 $this->assertNull( $this->request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3511 "Response $i, exception, session state" );
3515 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
3518 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
3522 $this->assertNull( $this->request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3523 "Response $i, session state" );
3524 foreach ( $providers as $p ) {
3525 $this->assertSame(
$response->status, $p->postCalled,
3526 "Response $i, post-auth callback called" );
3529 $this->assertNotNull(
3530 $this->request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3531 "Response $i, session state"
3533 foreach (
$ret->neededRequests as $neededReq ) {
3535 "Response $i, neededRequest action" );
3537 $this->assertEquals(
3538 $ret->neededRequests,
3540 "Response $i, continuation check"
3542 foreach ( $providers as $p ) {
3543 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
3550 $this->assertSame( $expectLog, $this->logger->getBuffer() );