3 namespace MediaWiki\Auth;
36 if ( $wgDisableAuthManager ) {
37 $this->markTestSkipped(
'$wgDisableAuthManager is set' );
50 protected function hook( $hook, $expect ) {
52 $mock = $this->getMock( __CLASS__, [
"on$hook" ] );
53 $wgHooks[$hook] = [ $mock ];
54 return $mock->expects( $expect )->method(
"on$hook" );
73 if ( $key === null ) {
78 $key = $key->getKey();
98 foreach ( [
'preauth',
'primaryauth',
'secondaryauth' ]
as $type ) {
99 $key = $type .
'Mocks';
100 foreach ( $this->$key
as $mock ) {
101 $config[
$type][$mock->getUniqueId()] = [
'factory' =>
function ()
use ( $mock ) {
107 $this->config->set(
'AuthManagerConfig',
$config );
108 $this->config->set(
'LanguageCode',
'en' );
109 $this->config->set(
'NewUserLog',
false );
117 if ( $regen || !$this->config ) {
118 $this->config = new \HashConfig();
120 if ( $regen || !$this->
request ) {
121 $this->
request = new \FauxRequest();
123 if ( !$this->logger ) {
124 $this->logger = new \TestLogger();
127 if ( $regen || !$this->config->has(
'AuthManagerConfig' ) ) {
131 $this->manager->setLogger( $this->logger );
142 if ( !$this->config ) {
143 $this->config = new \HashConfig();
146 $this->config->set(
'ObjectCacheSessionExpiry', 100 );
148 $methods[] =
'__toString';
149 $methods[] =
'describe';
150 if ( $canChangeUser !== null ) {
151 $methods[] =
'canChangeUser';
153 $provider = $this->getMockBuilder(
'DummySessionProvider' )
154 ->setMethods( $methods )
156 $provider->expects( $this->any() )->method(
'__toString' )
157 ->will( $this->returnValue(
'MockSessionProvider' ) );
158 $provider->expects( $this->any() )->method(
'describe' )
159 ->will( $this->returnValue(
'MockSessionProvider sessions' ) );
160 if ( $canChangeUser !== null ) {
161 $provider->expects( $this->any() )->method(
'canChangeUser' )
162 ->will( $this->returnValue( $canChangeUser ) );
164 $this->config->set(
'SessionProviders', [
165 [
'factory' =>
function ()
use ( $provider ) {
170 $manager = new \MediaWiki\Session\SessionManager( [
171 'config' => $this->config,
172 'logger' =>
new \Psr\Log\NullLogger(),
183 return [ $provider, $reset ];
190 $rProp->setAccessible(
true );
191 $old = $rProp->getValue();
192 $cb = new \ScopedCallback( [ $rProp,
'setValue' ], [ $old ] );
193 $rProp->setValue( null );
204 $this->
setMwGlobals( [
'wgDisableAuthManager' =>
true ] );
207 $this->fail(
'Expected exception not thrown' );
208 }
catch ( \BadMethodCallException $ex ) {
209 $this->assertSame(
'$wgDisableAuthManager is set', $ex->getMessage() );
217 $this->assertFalse( $this->manager->canAuthenticateNow() );
221 $this->assertTrue( $this->manager->canAuthenticateNow() );
232 foreach ( $mocks
as $key => $mock ) {
233 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue( $key ) );
235 $mocks[0]->expects( $this->once() )->method(
'providerNormalizeUsername' )
236 ->with( $this->identicalTo(
'XYZ' ) )
237 ->willReturn(
'Foo' );
238 $mocks[1]->expects( $this->once() )->method(
'providerNormalizeUsername' )
239 ->with( $this->identicalTo(
'XYZ' ) )
240 ->willReturn(
'Foo' );
241 $mocks[2]->expects( $this->once() )->method(
'providerNormalizeUsername' )
242 ->with( $this->identicalTo(
'XYZ' ) )
243 ->willReturn( null );
244 $mocks[3]->expects( $this->once() )->method(
'providerNormalizeUsername' )
245 ->with( $this->identicalTo(
'XYZ' ) )
246 ->willReturn(
'Bar!' );
248 $this->primaryauthMocks = $mocks;
252 $this->assertSame( [
'Foo',
'Bar!' ], $this->manager->normalizeUsername(
'XYZ' ) );
260 $this->logger = new \Psr\Log\NullLogger();
266 $mutableSession, [
'provideSessionInfo' ]
268 $provider->expects( $this->any() )->method(
'provideSessionInfo' )
269 ->will( $this->returnCallback(
function ()
use ( $provider, &$provideUser ) {
271 'provider' => $provider,
279 $this->config->set(
'ReauthenticateTime', [] );
280 $this->config->set(
'AllowSecuritySensitiveOperationIfCannotReauthenticate', [] );
282 $session = $provider->getManager()->getSessionForRequest( $this->
request );
283 $this->assertSame( 0, $session->getUser()->getId(),
'sanity check' );
286 $session->set(
'AuthManager:lastAuthId', 0 );
287 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
288 $this->assertSame( $reauth, $this->manager->securitySensitiveOperationStatus(
'foo' ) );
290 $provideUser =
$user;
291 $session = $provider->getManager()->getSessionForRequest( $this->
request );
292 $this->assertSame(
$user->getId(), $session->getUser()->getId(),
'sanity check' );
295 $session->set(
'AuthManager:lastAuthId',
$user->getId() + 1 );
296 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
298 $this->manager->securitySensitiveOperationStatus(
'foo' );
299 $this->fail(
'Expected exception not thrown' );
300 }
catch ( \UnexpectedValueException $ex ) {
303 ?
'$wgReauthenticateTime lacks a default'
304 :
'$wgAllowSecuritySensitiveOperationIfCannotReauthenticate lacks a default',
309 if ( $mutableSession ) {
310 $this->config->set(
'ReauthenticateTime', [
317 $session->set(
'AuthManager:lastAuthId',
$user->getId() + 1 );
318 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
330 $session->set(
'AuthManager:lastAuthId',
$user->getId() );
331 $session->set(
'AuthManager:lastAuthTimestamp', null );
343 $session->set(
'AuthManager:lastAuthTimestamp', time() - 5 );
349 $session->set(
'AuthManager:lastAuthTimestamp', time() - 20 );
358 $this->config->set(
'AllowSecuritySensitiveOperationIfCannotReauthenticate', [
377 ]
as $hook => $expect ) {
378 $this->
hook(
'SecuritySensitiveOperationStatus', $this->exactly( 2 ) )
382 $this->callback(
function (
$s )
use ( $session ) {
383 return $s->getId() === $session->getId();
385 $mutableSession ? $this->equalTo( 500, 1 ) : $this->equalTo( -1 )
387 ->
will( $this->returnCallback(
function ( &$v )
use ( $hook ) {
391 $session->set(
'AuthManager:lastAuthTimestamp', time() - 500 );
393 $expect, $this->manager->securitySensitiveOperationStatus(
'test' ),
"hook $hook"
396 $expect, $this->manager->securitySensitiveOperationStatus(
'test2' ),
"hook $hook"
398 $this->
unhook(
'SecuritySensitiveOperationStatus' );
422 $mock1->expects( $this->any() )->method(
'getUniqueId' )
423 ->will( $this->returnValue(
'primary1' ) );
424 $mock1->expects( $this->any() )->method(
'testUserCanAuthenticate' )
425 ->with( $this->equalTo(
'UTSysop' ) )
426 ->will( $this->returnValue( $primary1Can ) );
428 $mock2->expects( $this->any() )->method(
'getUniqueId' )
429 ->will( $this->returnValue(
'primary2' ) );
430 $mock2->expects( $this->any() )->method(
'testUserCanAuthenticate' )
431 ->with( $this->equalTo(
'UTSysop' ) )
432 ->will( $this->returnValue( $primary2Can ) );
433 $this->primaryauthMocks = [ $mock1, $mock2 ];
436 $this->assertSame( $expect, $this->manager->userCanAuthenticate(
'UTSysop' ) );
441 [
false,
false,
false ],
442 [
true,
false,
true ],
443 [
false,
true,
true ],
444 [
true,
true,
true ],
452 $mock->expects( $this->any() )->method(
'getUniqueId' )
453 ->will( $this->returnValue(
'primary' ) );
454 $mock->expects( $this->once() )->method(
'providerRevokeAccessForUser' )
455 ->with( $this->equalTo(
'UTSysop' ) );
456 $this->primaryauthMocks = [ $mock ];
459 $this->logger->setCollect(
true );
461 $this->manager->revokeAccessForUser(
'UTSysop' );
464 [ LogLevel::INFO,
'Revoking access for {user}' ],
465 ], $this->logger->getBuffer() );
474 foreach ( $mocks
as $key => $mock ) {
475 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue( $key ) );
476 $mock->expects( $this->once() )->method(
'setLogger' );
477 $mock->expects( $this->once() )->method(
'setManager' );
478 $mock->expects( $this->once() )->method(
'setConfig' );
480 $this->preauthMocks = [ $mocks[
'pre'] ];
481 $this->primaryauthMocks = [ $mocks[
'primary'] ];
482 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
488 $this->managerPriv->getAuthenticationProvider(
'primary' )
492 $this->managerPriv->getAuthenticationProvider(
'secondary' )
496 $this->managerPriv->getAuthenticationProvider(
'pre' )
499 [
'pre' => $mocks[
'pre'] ],
500 $this->managerPriv->getPreAuthenticationProviders()
503 [
'primary' => $mocks[
'primary'] ],
504 $this->managerPriv->getPrimaryAuthenticationProviders()
507 [
'secondary' => $mocks[
'secondary'] ],
508 $this->managerPriv->getSecondaryAuthenticationProviders()
514 $mock1->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
515 $mock2->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
516 $this->preauthMocks = [ $mock1 ];
517 $this->primaryauthMocks = [ $mock2 ];
518 $this->secondaryauthMocks = [];
521 $this->managerPriv->getAuthenticationProvider(
'Y' );
522 $this->fail(
'Expected exception not thrown' );
523 }
catch ( \RuntimeException $ex ) {
524 $class1 = get_class( $mock1 );
525 $class2 = get_class( $mock2 );
527 "Duplicate specifications for id X (classes $class1 and $class2)", $ex->getMessage()
533 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
534 $class = get_class( $mock );
535 $this->preauthMocks = [ $mock ];
536 $this->primaryauthMocks = [ $mock ];
537 $this->secondaryauthMocks = [ $mock ];
540 $this->managerPriv->getPreAuthenticationProviders();
541 $this->fail(
'Expected exception not thrown' );
542 }
catch ( \RuntimeException $ex ) {
544 "Expected instance of MediaWiki\\Auth\\PreAuthenticationProvider, got $class",
549 $this->managerPriv->getPrimaryAuthenticationProviders();
550 $this->fail(
'Expected exception not thrown' );
551 }
catch ( \RuntimeException $ex ) {
553 "Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got $class",
558 $this->managerPriv->getSecondaryAuthenticationProviders();
559 $this->fail(
'Expected exception not thrown' );
560 }
catch ( \RuntimeException $ex ) {
562 "Expected instance of MediaWiki\\Auth\\SecondaryAuthenticationProvider, got $class",
571 $mock1->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'A' ) );
572 $mock2->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
573 $mock3->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'C' ) );
574 $this->preauthMocks = [];
575 $this->primaryauthMocks = [ $mock1, $mock2, $mock3 ];
576 $this->secondaryauthMocks = [];
578 $config = $this->config->
get(
'AuthManagerConfig' );
582 [
'A' => $mock1,
'B' => $mock2,
'C' => $mock3 ],
583 $this->managerPriv->getPrimaryAuthenticationProviders(),
587 $config[
'primaryauth'][
'A'][
'sort'] = 100;
588 $config[
'primaryauth'][
'C'][
'sort'] = -1;
589 $this->config->set(
'AuthManagerConfig', $config );
592 [
'C' => $mock3,
'B' => $mock2,
'A' => $mock1 ],
593 $this->managerPriv->getPrimaryAuthenticationProviders()
606 $user->addToDatabase();
607 $oldToken =
$user->getToken();
608 $this->managerPriv->setDefaultUserOptions(
$user,
false );
609 $user->saveSettings();
610 $this->assertNotEquals( $oldToken,
$user->getToken() );
611 $this->assertSame(
'zh',
$user->getOption(
'language' ) );
612 $this->assertSame(
'zh',
$user->getOption(
'variant' ) );
615 $user->addToDatabase();
616 $oldToken = $user->getToken();
617 $this->managerPriv->setDefaultUserOptions( $user,
true );
618 $user->saveSettings();
619 $this->assertNotEquals( $oldToken, $user->getToken() );
620 $this->assertSame(
'de', $user->getOption(
'language' ) );
621 $this->assertSame(
'zh', $user->getOption(
'variant' ) );
626 $user->addToDatabase();
627 $oldToken = $user->getToken();
628 $this->managerPriv->setDefaultUserOptions( $user,
true );
629 $user->saveSettings();
630 $this->assertNotEquals( $oldToken, $user->getToken() );
631 $this->assertSame(
'de', $user->getOption(
'language' ) );
632 $this->assertSame( null, $user->getOption(
'variant' ) );
639 $mockA->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'A' ) );
640 $mockB->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
641 $mockB2->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'B' ) );
642 $this->primaryauthMocks = [ $mockA ];
644 $this->logger = new \TestLogger(
true );
648 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB ],
'testing' );
650 [
'B' => $mockB ], $this->managerPriv->getPrimaryAuthenticationProviders()
652 $this->assertSame( null, $this->managerPriv->getAuthenticationProvider(
'A' ) );
653 $this->assertSame( $mockB, $this->managerPriv->getAuthenticationProvider(
'B' ) );
655 [ LogLevel::WARNING,
'Overriding AuthManager primary authn because testing' ],
656 ], $this->logger->getBuffer() );
657 $this->logger->clearBuffer();
661 $this->assertSame( $mockA, $this->managerPriv->getAuthenticationProvider(
'A' ) );
662 $this->assertSame( null, $this->managerPriv->getAuthenticationProvider(
'B' ) );
663 $this->
request->getSession()->setSecret(
'AuthManager::authnState',
'test' );
664 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
'test' );
665 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB ],
'testing' );
667 [
'B' => $mockB ], $this->managerPriv->getPrimaryAuthenticationProviders()
669 $this->assertSame( null, $this->managerPriv->getAuthenticationProvider(
'A' ) );
670 $this->assertSame( $mockB, $this->managerPriv->getAuthenticationProvider(
'B' ) );
671 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::authnState' ) );
673 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
676 [ LogLevel::WARNING,
'Overriding AuthManager primary authn because testing' ],
679 'PrimaryAuthenticationProviders have already been accessed! I hope nothing breaks.'
681 ], $this->logger->getBuffer() );
682 $this->logger->clearBuffer();
687 $this->manager->forcePrimaryAuthenticationProviders( [ $mockB, $mockB2 ],
'testing' );
688 $this->fail(
'Expected exception not thrown' );
689 }
catch ( \RuntimeException $ex ) {
690 $class1 = get_class( $mockB );
691 $class2 = get_class( $mockB2 );
693 "Duplicate specifications for id B (classes $class2 and $class1)", $ex->getMessage()
699 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
700 $class = get_class( $mock );
702 $this->manager->forcePrimaryAuthenticationProviders( [ $mock ],
'testing' );
703 $this->fail(
'Expected exception not thrown' );
704 }
catch ( \RuntimeException $ex ) {
706 "Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got $class",
718 $this->
hook(
'UserLoggedIn', $this->never() );
719 $this->
request->getSession()->setSecret(
'AuthManager::authnState',
'test' );
721 $this->manager->beginAuthentication( [],
'http://localhost/' );
722 $this->fail(
'Expected exception not thrown' );
723 }
catch ( \LogicException $ex ) {
724 $this->assertSame(
'Authentication is not possible now', $ex->getMessage() );
726 $this->
unhook(
'UserLoggedIn' );
727 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::authnState' ) );
736 $this->
hook(
'UserLoggedIn', $this->never() );
738 $this->manager->beginAuthentication( $reqs,
'http://localhost/' );
739 $this->fail(
'Expected exception not thrown' );
740 }
catch ( \LogicException $ex ) {
742 'CreatedAccountAuthenticationRequests are only valid on the same AuthManager ' .
743 'that created the account',
747 $this->
unhook(
'UserLoggedIn' );
749 $this->
request->getSession()->clear();
750 $this->
request->getSession()->setSecret(
'AuthManager::authnState',
'test' );
751 $this->managerPriv->createdAccountAuthenticationRequests = [ $reqs[0] ];
752 $this->
hook(
'UserLoggedIn', $this->once() )
753 ->with( $this->callback(
function ( $u )
use (
$user ) {
754 return $user->getId() === $u->getId() &&
$user->getName() === $u->getName();
756 $this->
hook(
'AuthManagerLoginAuthenticateAudit', $this->once() );
757 $this->logger->setCollect(
true );
758 $ret = $this->manager->beginAuthentication( $reqs,
'http://localhost/' );
759 $this->logger->setCollect(
false );
760 $this->
unhook(
'UserLoggedIn' );
761 $this->
unhook(
'AuthManagerLoginAuthenticateAudit' );
763 $this->assertSame(
$user->getName(),
$ret->username );
764 $this->assertSame(
$user->getId(), $this->
request->getSessionData(
'AuthManager:lastAuthId' ) );
766 time(), $this->
request->getSessionData(
'AuthManager:lastAuthTimestamp' ),
769 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::authnState' ) );
770 $this->assertSame(
$user->getId(), $this->
request->getSession()->getUser()->getId() );
772 [ LogLevel::INFO,
'Logging in {user} after account creation' ],
773 ], $this->logger->getBuffer() );
782 $userReq->username =
'UTDummy';
784 $req1->returnToUrl =
'http://localhost/';
785 $req2->returnToUrl =
'http://localhost/';
786 $req3->returnToUrl =
'http://localhost/';
787 $req3->username =
'UTDummy';
788 $userReq->returnToUrl =
'http://localhost/';
792 $this->primaryauthMocks = [ $primary ];
795 $res->createRequest = $req1;
796 $primary->expects( $this->any() )->method(
'beginPrimaryAuthentication' )
797 ->will( $this->returnValue(
$res ) );
799 null, [ $req2->getUniqueId() => $req2 ]
801 $this->logger->setCollect(
true );
802 $ret = $this->manager->beginAuthentication( [ $createReq ],
'http://localhost/' );
803 $this->logger->setCollect(
false );
806 $this->assertSame( $req1,
$ret->createRequest->createRequest );
807 $this->assertEquals( [ $req2->getUniqueId() => $req2 ],
$ret->createRequest->maybeLink );
811 ->setMethods( [
'continuePrimaryAuthentication' ] )
812 ->getMockForAbstractClass();
813 $this->primaryauthMocks = [ $primary ];
815 $primary->expects( $this->any() )->method(
'beginPrimaryAuthentication' )
816 ->will( $this->returnValue(
820 $res->createRequest = $req2;
821 $primary->expects( $this->any() )->method(
'continuePrimaryAuthentication' )
822 ->will( $this->returnValue(
$res ) );
823 $this->logger->setCollect(
true );
824 $ret = $this->manager->beginAuthentication( [],
'http://localhost/' );
826 $ret = $this->manager->continueAuthentication( [] );
827 $this->logger->setCollect(
false );
830 $this->assertSame( $req2,
$ret->createRequest->createRequest );
831 $this->assertEquals( [],
$ret->createRequest->maybeLink );
835 $this->primaryauthMocks = [ $primary ];
838 $createReq->returnToUrl =
'http://localhost/';
839 $createReq->username =
'UTDummy';
841 $primary->expects( $this->any() )->method(
'beginPrimaryAccountCreation' )
842 ->with( $this->
anything(), $this->
anything(), [ $userReq, $createReq, $req3 ] )
843 ->will( $this->returnValue(
$res ) );
844 $primary->expects( $this->any() )->method(
'accountCreationType' )
846 $this->logger->setCollect(
true );
847 $ret = $this->manager->beginAccountCreation(
848 $user, [ $userReq, $createReq ],
'http://localhost/'
850 $this->logger->setCollect(
false );
852 $state = $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' );
853 $this->assertNotNull( $state );
854 $this->assertEquals( [ $userReq, $createReq, $req3 ], $state[
'reqs'] );
855 $this->assertEquals( [ $req2 ], $state[
'maybeLink'] );
872 $id =
$user->getId();
877 $req->rememberMe = (bool)rand( 0, 1 );
878 $req->pre = $preResponse;
879 $req->primary = $primaryResponses;
880 $req->secondary = $secondaryResponses;
882 foreach ( [
'pre',
'primary',
'secondary' ]
as $key ) {
883 $class = ucfirst( $key ) .
'AuthenticationProvider';
884 $mocks[$key] = $this->getMockForAbstractClass(
885 "MediaWiki\\Auth\\$class", [],
"Mock$class"
887 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
888 ->will( $this->returnValue( $key ) );
889 $mocks[$key .
'2'] = $this->getMockForAbstractClass(
890 "MediaWiki\\Auth\\$class", [],
"Mock$class"
892 $mocks[$key .
'2']->expects( $this->any() )->method(
'getUniqueId' )
893 ->will( $this->returnValue( $key .
'2' ) );
894 $mocks[$key .
'3'] = $this->getMockForAbstractClass(
895 "MediaWiki\\Auth\\$class", [],
"Mock$class"
897 $mocks[$key .
'3']->expects( $this->any() )->method(
'getUniqueId' )
898 ->will( $this->returnValue( $key .
'3' ) );
900 foreach ( $mocks
as $mock ) {
901 $mock->expects( $this->any() )->method(
'getAuthenticationRequests' )
902 ->will( $this->returnValue( [] ) );
905 $mocks[
'pre']->expects( $this->once() )->method(
'testForAuthentication' )
906 ->will( $this->returnCallback(
function ( $reqs )
use (
$req ) {
907 $this->assertContains(
$req, $reqs );
911 $ct = count(
$req->primary );
912 $callback = $this->returnCallback(
function ( $reqs )
use (
$req ) {
913 $this->assertContains(
$req, $reqs );
914 return array_shift(
$req->primary );
916 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
917 ->method(
'beginPrimaryAuthentication' )
919 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
920 ->method(
'continuePrimaryAuthentication' )
923 $mocks[
'primary']->expects( $this->any() )->method(
'accountCreationType' )
927 $ct = count(
$req->secondary );
928 $callback = $this->returnCallback(
function (
$user, $reqs )
use ( $id,
$name,
$req ) {
929 $this->assertSame( $id,
$user->getId() );
931 $this->assertContains(
$req, $reqs );
932 return array_shift(
$req->secondary );
934 $mocks[
'secondary']->expects( $this->exactly( min( 1, $ct ) ) )
935 ->method(
'beginSecondaryAuthentication' )
937 $mocks[
'secondary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
938 ->method(
'continueSecondaryAuthentication' )
942 $mocks[
'pre2']->expects( $this->atMost( 1 ) )->method(
'testForAuthentication' )
944 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAuthentication' )
945 ->will( $this->returnValue( $abstain ) );
946 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAuthentication' );
947 $mocks[
'secondary2']->expects( $this->atMost( 1 ) )->method(
'beginSecondaryAuthentication' )
948 ->will( $this->returnValue( $abstain ) );
949 $mocks[
'secondary2']->expects( $this->never() )->method(
'continueSecondaryAuthentication' );
950 $mocks[
'secondary3']->expects( $this->atMost( 1 ) )->method(
'beginSecondaryAuthentication' )
951 ->will( $this->returnValue( $abstain ) );
952 $mocks[
'secondary3']->expects( $this->never() )->method(
'continueSecondaryAuthentication' );
954 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
955 $this->primaryauthMocks = [ $mocks[
'primary'], $mocks[
'primary2'] ];
956 $this->secondaryauthMocks = [
957 $mocks[
'secondary3'], $mocks[
'secondary'], $mocks[
'secondary2'],
962 $this->logger->setCollect(
true );
964 $constraint = \PHPUnit_Framework_Assert::logicalOr(
968 $providers = array_filter(
970 $this->preauthMocks, $this->primaryauthMocks, $this->secondaryauthMocks
973 return is_callable( [ $p,
'expects' ] );
976 foreach ( $providers
as $p ) {
977 $p->postCalled =
false;
978 $p->expects( $this->atMost( 1 ) )->method(
'postAuthentication' )
980 if (
$user !== null ) {
981 $this->assertInstanceOf(
'User',
$user );
982 $this->assertSame(
'UTSysop',
$user->getName() );
985 $this->assertThat(
$response->status, $constraint );
990 $session = $this->
request->getSession();
991 $session->setRememberUser( !
$req->rememberMe );
997 $this->
hook(
'UserLoggedIn', $this->once() )
998 ->with( $this->callback(
function (
$user )
use ( $id,
$name ) {
1002 $this->
hook(
'UserLoggedIn', $this->never() );
1007 $response->message->getKey() !==
'authmanager-authn-not-in-progress' &&
1008 $response->message->getKey() !==
'authmanager-authn-no-primary'
1011 $this->
hook(
'AuthManagerLoginAuthenticateAudit', $this->once() );
1013 $this->
hook(
'AuthManagerLoginAuthenticateAudit', $this->never() );
1019 $ret = $this->manager->beginAuthentication( [
$req ],
'http://localhost/' );
1021 $ret = $this->manager->continueAuthentication( [
$req ] );
1023 if (
$response instanceof \Exception ) {
1024 $this->fail(
'Expected exception not thrown',
"Response $i" );
1026 }
catch ( \Exception $ex ) {
1027 if ( !
$response instanceof \Exception ) {
1030 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
1031 $this->assertNull( $session->getSecret(
'AuthManager::authnState' ),
1032 "Response $i, exception, session state" );
1033 $this->
unhook(
'UserLoggedIn' );
1034 $this->
unhook(
'AuthManagerLoginAuthenticateAudit' );
1038 $this->
unhook(
'UserLoggedIn' );
1039 $this->
unhook(
'AuthManagerLoginAuthenticateAudit' );
1041 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
1044 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
1046 $this->assertSame( $id, $session->getUser()->getId(),
1047 "Response $i, authn" );
1049 $this->assertSame( 0, $session->getUser()->getId(),
1050 "Response $i, authn" );
1053 $this->assertNull( $session->getSecret(
'AuthManager::authnState' ),
1054 "Response $i, session state" );
1055 foreach ( $providers
as $p ) {
1056 $this->assertSame(
$response->status, $p->postCalled,
1057 "Response $i, post-auth callback called" );
1060 $this->assertNotNull( $session->getSecret(
'AuthManager::authnState' ),
1061 "Response $i, session state" );
1062 foreach (
$ret->neededRequests
as $neededReq ) {
1064 "Response $i, neededRequest action" );
1066 $this->assertEquals(
1067 $ret->neededRequests,
1069 "Response $i, continuation check"
1071 foreach ( $providers
as $p ) {
1072 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
1076 $state = $session->getSecret(
'AuthManager::authnState' );
1077 $maybeLink = isset( $state[
'maybeLink'] ) ? $state[
'maybeLink'] : [];
1079 $this->assertEquals(
1082 "Response $i, maybeLink"
1085 $this->assertEquals( [], $maybeLink,
"Response $i, maybeLink" );
1090 $this->assertSame(
$req->rememberMe, $session->shouldRememberUser(),
1091 'rememberMe checkbox had effect' );
1093 $this->assertNotSame(
$req->rememberMe, $session->shouldRememberUser(),
1094 'rememberMe checkbox wasn\'t applied' );
1100 $id =
$user->getId();
1107 $req->foobar =
'baz';
1109 $this->
message(
'authmanager-authn-no-local-user' )
1111 $restartResponse->neededRequests = [ $rememberReq ];
1114 $restartResponse2Pass->linkRequest =
$req;
1116 $this->
message(
'authmanager-authn-no-local-user-link' )
1119 null, [
$req->getUniqueId() =>
$req ]
1122 $restartResponse2->neededRequests = [ $rememberReq, $restartResponse2->createRequest ];
1125 'Failure in pre-auth' => [
1132 $this->
message(
'authmanager-authn-not-in-progress' )
1136 'Failure in primary' => [
1144 'All primary abstain' => [
1154 'Primary UI, then redirect, then fail' => [
1164 'Primary redirect, then abstain' => [
1168 [
$req ],
'/foo.html', [
'foo' =>
'bar' ]
1175 new \DomainException(
1176 'MockPrimaryAuthenticationProvider::continuePrimaryAuthentication() returned ABSTAIN'
1180 'Primary UI, then pass with no local user' => [
1192 'Primary UI, then pass with no local user (link type)' => [
1196 $restartResponse2Pass,
1205 'Primary pass with invalid username' => [
1212 new \DomainException(
'MockPrimaryAuthenticationProvider returned an invalid username: <>' ),
1215 'Secondary fail' => [
1225 'Secondary UI, then abstain' => [
1239 'Secondary pass' => [
1262 $mock1->expects( $this->any() )->method(
'getUniqueId' )
1263 ->will( $this->returnValue(
'primary1' ) );
1264 $mock1->expects( $this->any() )->method(
'testUserExists' )
1265 ->with( $this->equalTo(
'UTSysop' ) )
1266 ->will( $this->returnValue( $primary1Exists ) );
1268 $mock2->expects( $this->any() )->method(
'getUniqueId' )
1269 ->will( $this->returnValue(
'primary2' ) );
1270 $mock2->expects( $this->any() )->method(
'testUserExists' )
1271 ->with( $this->equalTo(
'UTSysop' ) )
1272 ->will( $this->returnValue( $primary2Exists ) );
1273 $this->primaryauthMocks = [ $mock1, $mock2 ];
1276 $this->assertSame( $expect, $this->manager->userExists(
'UTSysop' ) );
1281 [
false,
false,
false ],
1282 [
true,
false,
true ],
1283 [
false,
true,
true ],
1284 [
true,
true,
true ],
1298 $mock1->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'1' ) );
1299 $mock1->expects( $this->any() )->method(
'providerAllowsAuthenticationDataChange' )
1300 ->with( $this->equalTo(
$req ) )
1301 ->will( $this->returnValue( $primaryReturn ) );
1303 $mock2->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'2' ) );
1304 $mock2->expects( $this->any() )->method(
'providerAllowsAuthenticationDataChange' )
1305 ->with( $this->equalTo(
$req ) )
1306 ->will( $this->returnValue( $secondaryReturn ) );
1308 $this->primaryauthMocks = [ $mock1 ];
1309 $this->secondaryauthMocks = [ $mock2 ];
1311 $this->assertEquals( $expect, $this->manager->allowsAuthenticationDataChange(
$req ) );
1316 $ignored->warning(
'authmanager-change-not-supported' );
1319 $okFromPrimary->warning(
'warning-from-primary' );
1321 $okFromSecondary->warning(
'warning-from-secondary' );
1369 $req->username =
'UTSysop';
1372 $mock1->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'1' ) );
1373 $mock1->expects( $this->once() )->method(
'providerChangeAuthenticationData' )
1374 ->with( $this->equalTo(
$req ) );
1376 $mock2->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'2' ) );
1377 $mock2->expects( $this->once() )->method(
'providerChangeAuthenticationData' )
1378 ->with( $this->equalTo(
$req ) );
1380 $this->primaryauthMocks = [ $mock1, $mock2 ];
1382 $this->logger->setCollect(
true );
1383 $this->manager->changeAuthenticationData(
$req );
1384 $this->assertSame( [
1385 [ LogLevel::INFO,
'Changing authentication data for {user} class {what}' ],
1386 ], $this->logger->getBuffer() );
1396 foreach ( $types
as $type => $can ) {
1398 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
$type ) );
1399 $mock->expects( $this->any() )->method(
'accountCreationType' )
1400 ->will( $this->returnValue(
$type ) );
1401 $this->primaryauthMocks = [ $mock ];
1403 $this->assertSame( $can, $this->manager->canCreateAccounts(),
$type );
1414 $wgGroupPermissions[
'*'][
'createaccount'] =
true;
1415 $this->assertEquals(
1417 $this->manager->checkAccountCreatePermissions(
new \
User )
1421 $this->assertEquals(
1423 $this->manager->checkAccountCreatePermissions(
new \
User )
1427 $wgGroupPermissions[
'*'][
'createaccount'] =
false;
1428 $status = $this->manager->checkAccountCreatePermissions(
new \
User );
1429 $this->assertFalse(
$status->isOK() );
1430 $this->assertTrue(
$status->hasMessage(
'badaccess-groups' ) );
1431 $wgGroupPermissions[
'*'][
'createaccount'] =
true;
1434 if (
$user->getID() == 0 ) {
1435 $user->addToDatabase();
1437 $user->saveSettings();
1442 $oldBlock->delete();
1445 'address' =>
'UTBlockee',
1446 'user' =>
$user->getID(),
1447 'reason' => __METHOD__,
1448 'expiry' => time() + 100500,
1449 'createAccount' =>
true,
1451 $block = new \Block( $blockOptions );
1453 $status = $this->manager->checkAccountCreatePermissions(
$user );
1454 $this->assertFalse(
$status->isOK() );
1455 $this->assertTrue(
$status->hasMessage(
'cantcreateaccount-text' ) );
1458 'address' =>
'127.0.0.0/24',
1459 'reason' => __METHOD__,
1460 'expiry' => time() + 100500,
1461 'createAccount' =>
true,
1463 $block = new \Block( $blockOptions );
1465 $scopeVariable = new \ScopedCallback( [ $block,
'delete' ] );
1466 $status = $this->manager->checkAccountCreatePermissions(
new \
User );
1467 $this->assertFalse(
$status->isOK() );
1468 $this->assertTrue(
$status->hasMessage(
'cantcreateaccount-range-text' ) );
1472 'wgEnableDnsBlacklist' =>
true,
1473 'wgDnsBlacklistUrls' => [
1474 'local.wmftest.net',
1476 'wgProxyWhitelist' => [],
1478 $status = $this->manager->checkAccountCreatePermissions(
new \
User );
1479 $this->assertFalse(
$status->isOK() );
1480 $this->assertTrue(
$status->hasMessage(
'sorbs_create_account_reason' ) );
1481 $this->
setMwGlobals(
'wgProxyWhitelist', [
'127.0.0.1' ] );
1482 $status = $this->manager->checkAccountCreatePermissions(
new \
User );
1483 $this->assertTrue(
$status->isGood() );
1493 $username =
"UTAuthManagerTestAccountCreation" . $uniq . ++$i;
1499 $username = self::usernameForCreation();
1502 $this->assertEquals(
1504 $this->manager->canCreateAccount(
$username )
1508 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1509 $mock->expects( $this->any() )->method(
'accountCreationType' )
1511 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
true ) );
1512 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1514 $this->primaryauthMocks = [ $mock ];
1517 $this->assertEquals(
1519 $this->manager->canCreateAccount(
$username )
1523 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1524 $mock->expects( $this->any() )->method(
'accountCreationType' )
1526 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1527 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1529 $this->primaryauthMocks = [ $mock ];
1532 $this->assertEquals(
1534 $this->manager->canCreateAccount(
$username .
'<>' )
1537 $this->assertEquals(
1539 $this->manager->canCreateAccount(
'UTSysop' )
1542 $this->assertEquals(
1544 $this->manager->canCreateAccount(
$username )
1548 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1549 $mock->expects( $this->any() )->method(
'accountCreationType' )
1551 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1552 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1554 $this->primaryauthMocks = [ $mock ];
1557 $this->assertEquals(
1559 $this->manager->canCreateAccount(
$username )
1566 $this->logger = new \TestLogger(
false,
function ( $message, $level ) {
1567 return $level === LogLevel::DEBUG ? null : $message;
1571 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
'test' );
1572 $this->
hook(
'LocalUserCreated', $this->never() );
1574 $this->manager->beginAccountCreation(
1575 $creator, [],
'http://localhost/'
1577 $this->fail(
'Expected exception not thrown' );
1578 }
catch ( \LogicException $ex ) {
1579 $this->assertEquals(
'Account creation is not possible', $ex->getMessage() );
1581 $this->
unhook(
'LocalUserCreated' );
1583 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1587 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1588 $mock->expects( $this->any() )->method(
'accountCreationType' )
1590 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
true ) );
1591 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1593 $this->primaryauthMocks = [ $mock ];
1596 $this->
hook(
'LocalUserCreated', $this->never() );
1597 $ret = $this->manager->beginAccountCreation( $creator, [],
'http://localhost/' );
1598 $this->
unhook(
'LocalUserCreated' );
1600 $this->assertSame(
'noname',
$ret->message->getKey() );
1602 $this->
hook(
'LocalUserCreated', $this->never() );
1603 $userReq->username = self::usernameForCreation();
1605 $userReq2->username = $userReq->username .
'X';
1606 $ret = $this->manager->beginAccountCreation(
1607 $creator, [ $userReq, $userReq2 ],
'http://localhost/'
1609 $this->
unhook(
'LocalUserCreated' );
1611 $this->assertSame(
'noname',
$ret->message->getKey() );
1614 $this->
hook(
'LocalUserCreated', $this->never() );
1615 $userReq->username = self::usernameForCreation();
1616 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1617 $this->
unhook(
'LocalUserCreated' );
1619 $this->assertSame(
'readonlytext',
$ret->message->getKey() );
1620 $this->assertSame( [
'Because' ],
$ret->message->getParams() );
1623 $this->
hook(
'LocalUserCreated', $this->never() );
1624 $userReq->username = self::usernameForCreation();
1625 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1626 $this->
unhook(
'LocalUserCreated' );
1628 $this->assertSame(
'userexists',
$ret->message->getKey() );
1631 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1632 $mock->expects( $this->any() )->method(
'accountCreationType' )
1634 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1635 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1637 $this->primaryauthMocks = [ $mock ];
1640 $this->
hook(
'LocalUserCreated', $this->never() );
1641 $userReq->username = self::usernameForCreation();
1642 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1643 $this->
unhook(
'LocalUserCreated' );
1645 $this->assertSame(
'fail',
$ret->message->getKey() );
1648 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1649 $mock->expects( $this->any() )->method(
'accountCreationType' )
1651 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1652 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1654 $this->primaryauthMocks = [ $mock ];
1657 $this->
hook(
'LocalUserCreated', $this->never() );
1658 $userReq->username = self::usernameForCreation() .
'<>';
1659 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1660 $this->
unhook(
'LocalUserCreated' );
1662 $this->assertSame(
'noname',
$ret->message->getKey() );
1664 $this->
hook(
'LocalUserCreated', $this->never() );
1665 $userReq->username = $creator->getName();
1666 $ret = $this->manager->beginAccountCreation( $creator, [ $userReq ],
'http://localhost/' );
1667 $this->
unhook(
'LocalUserCreated' );
1669 $this->assertSame(
'userexists',
$ret->message->getKey() );
1672 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1673 $mock->expects( $this->any() )->method(
'accountCreationType' )
1675 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1676 $mock->expects( $this->any() )->method(
'testUserForCreation' )
1678 $mock->expects( $this->any() )->method(
'testForAccountCreation' )
1680 $this->primaryauthMocks = [ $mock ];
1684 ->setMethods( [
'populateUser' ] )
1686 $req->expects( $this->any() )->method(
'populateUser' )
1688 $userReq->username = self::usernameForCreation();
1689 $ret = $this->manager->beginAccountCreation(
1690 $creator, [ $userReq,
$req ],
'http://localhost/'
1693 $this->assertSame(
'populatefail',
$ret->message->getKey() );
1696 $userReq->username = self::usernameForCreation();
1698 $ret = $this->manager->beginAccountCreation(
1699 $creator, [ $userReq, $req ],
'http://localhost/'
1702 $this->assertSame(
'fail',
$ret->message->getKey() );
1704 $this->manager->beginAccountCreation(
1705 \
User::newFromName( $userReq->username ), [ $userReq, $req ],
'http://localhost/'
1708 $this->assertSame(
'fail',
$ret->message->getKey() );
1713 $username = self::usernameForCreation();
1714 $this->logger = new \TestLogger(
false,
function ( $message, $level ) {
1715 return $level === LogLevel::DEBUG ? null : $message;
1726 'primaryResponse' => null,
1728 'ranPreTests' =>
true,
1731 $this->
hook(
'LocalUserCreated', $this->never() );
1733 $this->manager->continueAccountCreation( [] );
1734 $this->fail(
'Expected exception not thrown' );
1735 }
catch ( \LogicException $ex ) {
1736 $this->assertEquals(
'Account creation is not possible', $ex->getMessage() );
1738 $this->
unhook(
'LocalUserCreated' );
1741 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
1742 $mock->expects( $this->any() )->method(
'accountCreationType' )
1744 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
false ) );
1745 $mock->expects( $this->any() )->method(
'beginPrimaryAccountCreation' )->will(
1748 $this->primaryauthMocks = [ $mock ];
1751 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState', null );
1752 $this->
hook(
'LocalUserCreated', $this->never() );
1753 $ret = $this->manager->continueAccountCreation( [] );
1754 $this->
unhook(
'LocalUserCreated' );
1756 $this->assertSame(
'authmanager-create-not-in-progress',
$ret->message->getKey() );
1758 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1759 [
'username' =>
"$username<>" ] + $session );
1760 $this->
hook(
'LocalUserCreated', $this->never() );
1761 $ret = $this->manager->continueAccountCreation( [] );
1762 $this->
unhook(
'LocalUserCreated' );
1764 $this->assertSame(
'noname',
$ret->message->getKey() );
1766 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1769 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState', $session );
1770 $this->
hook(
'LocalUserCreated', $this->never() );
1773 $ret = $this->manager->continueAccountCreation( [] );
1775 $this->
unhook(
'LocalUserCreated' );
1777 $this->assertSame(
'usernameinprogress',
$ret->message->getKey() );
1781 $session, $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1784 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1785 [
'username' => $creator->getName() ] + $session );
1787 $this->
hook(
'LocalUserCreated', $this->never() );
1788 $ret = $this->manager->continueAccountCreation( [] );
1789 $this->
unhook(
'LocalUserCreated' );
1791 $this->assertSame(
'readonlytext',
$ret->message->getKey() );
1792 $this->assertSame( [
'Because' ],
$ret->message->getParams() );
1795 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1796 [
'username' => $creator->getName() ] + $session );
1797 $this->
hook(
'LocalUserCreated', $this->never() );
1798 $ret = $this->manager->continueAccountCreation( [] );
1799 $this->
unhook(
'LocalUserCreated' );
1801 $this->assertSame(
'userexists',
$ret->message->getKey() );
1803 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1806 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1807 [
'userid' => $creator->getId() ] + $session );
1808 $this->
hook(
'LocalUserCreated', $this->never() );
1810 $ret = $this->manager->continueAccountCreation( [] );
1811 $this->fail(
'Expected exception not thrown' );
1812 }
catch ( \UnexpectedValueException $ex ) {
1813 $this->assertEquals(
"User \"{$username}\" should exist now, but doesn't!", $ex->getMessage() );
1815 $this->
unhook(
'LocalUserCreated' );
1817 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1820 $id = $creator->getId();
1821 $name = $creator->getName();
1822 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1823 [
'username' =>
$name,
'userid' => $id + 1 ] + $session );
1824 $this->
hook(
'LocalUserCreated', $this->never() );
1826 $ret = $this->manager->continueAccountCreation( [] );
1827 $this->fail(
'Expected exception not thrown' );
1828 }
catch ( \UnexpectedValueException $ex ) {
1829 $this->assertEquals(
1830 "User \"{$name}\" exists, but ID $id != " . ( $id + 1 ) .
'!', $ex->getMessage()
1833 $this->
unhook(
'LocalUserCreated' );
1835 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1839 ->setMethods( [
'populateUser' ] )
1841 $req->expects( $this->any() )->method(
'populateUser' )
1843 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState',
1844 [
'reqs' => [ $req ] ] + $session );
1845 $ret = $this->manager->continueAccountCreation( [] );
1847 $this->assertSame(
'populatefail',
$ret->message->getKey() );
1849 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' )
1863 StatusValue $preTest, $primaryTest, $secondaryTest,
1864 array $primaryResponses,
array $secondaryResponses,
array $managerResponses
1867 $username = self::usernameForCreation();
1873 $req->preTest = $preTest;
1874 $req->primaryTest = $primaryTest;
1875 $req->secondaryTest = $secondaryTest;
1876 $req->primary = $primaryResponses;
1877 $req->secondary = $secondaryResponses;
1879 foreach ( [
'pre',
'primary',
'secondary' ]
as $key ) {
1880 $class = ucfirst( $key ) .
'AuthenticationProvider';
1881 $mocks[$key] = $this->getMockForAbstractClass(
1882 "MediaWiki\\Auth\\$class", [],
"Mock$class"
1884 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
1885 ->will( $this->returnValue( $key ) );
1886 $mocks[$key]->expects( $this->any() )->method(
'testUserForCreation' )
1888 $mocks[$key]->expects( $this->any() )->method(
'testForAccountCreation' )
1889 ->will( $this->returnCallback(
1890 function (
$user, $creatorIn, $reqs )
1894 $this->assertSame( $creator->getId(), $creatorIn->getId() );
1895 $this->assertSame( $creator->getName(), $creatorIn->getName() );
1897 foreach ( $reqs
as $r ) {
1898 $this->assertSame(
$username, $r->username );
1899 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1901 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1907 for ( $i = 2; $i <= 3; $i++ ) {
1908 $mocks[$key . $i] = $this->getMockForAbstractClass(
1909 "MediaWiki\\Auth\\$class", [],
"Mock$class"
1911 $mocks[$key . $i]->expects( $this->any() )->method(
'getUniqueId' )
1912 ->will( $this->returnValue( $key . $i ) );
1913 $mocks[$key . $i]->expects( $this->any() )->method(
'testUserForCreation' )
1915 $mocks[$key . $i]->expects( $this->atMost( 1 ) )->method(
'testForAccountCreation' )
1920 $mocks[
'primary']->expects( $this->any() )->method(
'accountCreationType' )
1922 $mocks[
'primary']->expects( $this->any() )->method(
'testUserExists' )
1923 ->will( $this->returnValue(
false ) );
1924 $ct = count(
$req->primary );
1927 $this->assertSame(
'UTSysop', $creator->getName() );
1929 foreach ( $reqs
as $r ) {
1930 $this->assertSame(
$username, $r->username );
1931 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1933 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1934 return array_shift(
$req->primary );
1936 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
1937 ->method(
'beginPrimaryAccountCreation' )
1938 ->will( $callback );
1939 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
1940 ->method(
'continuePrimaryAccountCreation' )
1941 ->will( $callback );
1943 $ct = count(
$req->secondary );
1946 $this->assertSame(
'UTSysop', $creator->getName() );
1948 foreach ( $reqs
as $r ) {
1949 $this->assertSame(
$username, $r->username );
1950 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
1952 $this->assertTrue( $foundReq,
'$reqs contains $req' );
1953 return array_shift(
$req->secondary );
1955 $mocks[
'secondary']->expects( $this->exactly( min( 1, $ct ) ) )
1956 ->method(
'beginSecondaryAccountCreation' )
1957 ->will( $callback );
1958 $mocks[
'secondary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
1959 ->method(
'continueSecondaryAccountCreation' )
1960 ->will( $callback );
1963 $mocks[
'primary2']->expects( $this->any() )->method(
'accountCreationType' )
1965 $mocks[
'primary2']->expects( $this->any() )->method(
'testUserExists' )
1966 ->will( $this->returnValue(
false ) );
1967 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAccountCreation' )
1968 ->will( $this->returnValue( $abstain ) );
1969 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAccountCreation' );
1970 $mocks[
'primary3']->expects( $this->any() )->method(
'accountCreationType' )
1972 $mocks[
'primary3']->expects( $this->any() )->method(
'testUserExists' )
1973 ->will( $this->returnValue(
false ) );
1974 $mocks[
'primary3']->expects( $this->never() )->method(
'beginPrimaryAccountCreation' );
1975 $mocks[
'primary3']->expects( $this->never() )->method(
'continuePrimaryAccountCreation' );
1976 $mocks[
'secondary2']->expects( $this->atMost( 1 ) )
1977 ->method(
'beginSecondaryAccountCreation' )
1978 ->will( $this->returnValue( $abstain ) );
1979 $mocks[
'secondary2']->expects( $this->never() )->method(
'continueSecondaryAccountCreation' );
1980 $mocks[
'secondary3']->expects( $this->atMost( 1 ) )
1981 ->method(
'beginSecondaryAccountCreation' )
1982 ->will( $this->returnValue( $abstain ) );
1983 $mocks[
'secondary3']->expects( $this->never() )->method(
'continueSecondaryAccountCreation' );
1985 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
1986 $this->primaryauthMocks = [ $mocks[
'primary3'], $mocks[
'primary'], $mocks[
'primary2'] ];
1987 $this->secondaryauthMocks = [
1988 $mocks[
'secondary3'], $mocks[
'secondary'], $mocks[
'secondary2']
1991 $this->logger = new \TestLogger(
true,
function ( $message, $level ) {
1992 return $level === LogLevel::DEBUG ? null : $message;
1997 $constraint = \PHPUnit_Framework_Assert::logicalOr(
2001 $providers = array_merge(
2002 $this->preauthMocks, $this->primaryauthMocks, $this->secondaryauthMocks
2004 foreach ( $providers
as $p ) {
2005 $p->postCalled =
false;
2006 $p->expects( $this->atMost( 1 ) )->method(
'postAccountCreation' )
2010 $this->assertInstanceOf(
'User',
$user );
2012 $this->assertSame(
'UTSysop', $creator->getName() );
2014 $this->assertThat(
$response->status, $constraint );
2021 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2025 foreach ( $managerResponses
as $i =>
$response ) {
2028 if ( $i ===
'created' ) {
2030 $this->
hook(
'LocalUserCreated', $this->once() )
2035 $this->equalTo(
false )
2037 $expectLog[] = [ LogLevel::INFO,
"Creating user {user} during account creation" ];
2039 $this->
hook(
'LocalUserCreated', $this->never() );
2047 $ret = $this->manager->beginAccountCreation(
2048 $creator, [ $userReq,
$req ],
'http://localhost/'
2051 $ret = $this->manager->continueAccountCreation( [
$req ] );
2053 if (
$response instanceof \Exception ) {
2054 $this->fail(
'Expected exception not thrown',
"Response $i" );
2056 }
catch ( \Exception $ex ) {
2057 if ( !
$response instanceof \Exception ) {
2060 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
2062 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2063 "Response $i, exception, session state"
2065 $this->
unhook(
'LocalUserCreated' );
2069 $this->
unhook(
'LocalUserCreated' );
2071 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
2074 $this->assertNotNull(
$ret->loginRequest,
"Response $i, login marker" );
2075 $this->assertContains(
2076 $ret->loginRequest, $this->managerPriv->createdAccountAuthenticationRequests,
2077 "Response $i, login marker"
2082 "MediaWiki\Auth\AuthManager::continueAccountCreation: Account creation succeeded for {user}"
2090 $this->assertNull(
$ret->loginRequest,
"Response $i, login marker" );
2091 $this->assertSame( [], $this->managerPriv->createdAccountAuthenticationRequests,
2092 "Response $i, login marker" );
2095 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
2098 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2099 "Response $i, session state"
2101 foreach ( $providers
as $p ) {
2102 $this->assertSame(
$response->status, $p->postCalled,
2103 "Response $i, post-auth callback called" );
2106 $this->assertNotNull(
2107 $this->
request->getSession()->getSecret(
'AuthManager::accountCreationState' ),
2108 "Response $i, session state"
2110 foreach (
$ret->neededRequests
as $neededReq ) {
2112 "Response $i, neededRequest action" );
2114 $this->assertEquals(
2115 $ret->neededRequests,
2117 "Response $i, continuation check"
2119 foreach ( $providers
as $p ) {
2120 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
2133 $this->assertSame( $expectLog, $this->logger->getBuffer() );
2137 $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] )
2146 'Pre-creation test fail in pre' => [
2154 'Pre-creation test fail in primary' => [
2162 'Pre-creation test fail in secondary' => [
2170 'Failure in primary' => [
2171 $good, $good, $good,
2178 'All primary abstain' => [
2179 $good, $good, $good,
2188 'Primary UI, then redirect, then fail' => [
2189 $good, $good, $good,
2198 'Primary redirect, then abstain' => [
2199 $good, $good, $good,
2202 [
$req ],
'/foo.html', [
'foo' =>
'bar' ]
2209 new \DomainException(
2210 'MockPrimaryAuthenticationProvider::continuePrimaryAccountCreation() returned ABSTAIN'
2214 'Primary UI, then pass; secondary abstain' => [
2215 $good, $good, $good,
2228 'Primary pass; secondary UI then pass' => [
2229 $good, $good, $good,
2242 'Primary pass; secondary fail' => [
2243 $good, $good, $good,
2251 'created' => new \DomainException(
2252 'MockSecondaryAuthenticationProvider::beginSecondaryAccountCreation() returned FAIL. ' .
2253 'Secondary providers are not allowed to fail account creation, ' .
2254 'that should have been done via testForAccountCreation().'
2268 $username = self::usernameForCreation();
2273 $mock = $this->getMockForAbstractClass(
2274 "MediaWiki\\Auth\\PrimaryAuthenticationProvider", []
2276 $mock->expects( $this->any() )->method(
'getUniqueId' )
2277 ->will( $this->returnValue(
'primary' ) );
2278 $mock->expects( $this->any() )->method(
'testUserForCreation' )
2280 $mock->expects( $this->any() )->method(
'testForAccountCreation' )
2282 $mock->expects( $this->any() )->method(
'accountCreationType' )
2284 $mock->expects( $this->any() )->method(
'testUserExists' )
2285 ->will( $this->returnValue(
false ) );
2286 $mock->expects( $this->any() )->method(
'beginPrimaryAccountCreation' )
2288 $mock->expects( $this->any() )->method(
'finishAccountCreation' )
2289 ->will( $this->returnValue( $logSubtype ) );
2291 $this->primaryauthMocks = [ $mock ];
2293 $this->logger->setCollect(
true );
2295 $this->config->set(
'NewUserLog',
true );
2298 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2303 $reasonReq->reason = $this->toString();
2304 $ret = $this->manager->beginAccountCreation(
2305 $creator, [ $userReq, $reasonReq ],
'http://localhost/'
2311 $this->assertNotEquals( 0,
$user->getId(),
'sanity check' );
2312 $this->assertNotEquals( $creator->getId(),
$user->getId(),
'sanity check' );
2315 $rows = iterator_to_array( $dbw->select(
2319 'log_id > ' . (
int)$maxLogId,
2320 'log_type' =>
'newusers'
2326 $this->assertCount( 1, $rows );
2329 $this->assertSame( $logSubtype ?: ( $isAnon ?
'create' :
'create2' ), $entry->getSubtype() );
2331 $isAnon ?
$user->getId() : $creator->getId(),
2332 $entry->getPerformer()->getId()
2335 $isAnon ?
$user->getName() : $creator->getName(),
2336 $entry->getPerformer()->getName()
2338 $this->assertSame(
$user->getUserPage()->getFullText(), $entry->getTarget()->getFullText() );
2339 $this->assertSame( [
'4::userid' =>
$user->getId() ], $entry->getParameters() );
2340 $this->assertSame( $this->toString(), $entry->getComment() );
2348 [
false,
'byemail' ],
2359 $workaroundPHPUnitBug =
false;
2361 $username = self::usernameForCreation();
2365 $wgGroupPermissions[
'*'][
'createaccount'] =
true;
2366 $wgGroupPermissions[
'*'][
'autocreateaccount'] =
false;
2369 $this->
setMwGlobals( [
'wgMainCacheType' => __METHOD__ ] );
2373 foreach ( [
'pre',
'primary',
'secondary' ]
as $key ) {
2374 $class = ucfirst( $key ) .
'AuthenticationProvider';
2375 $mocks[$key] = $this->getMockForAbstractClass(
2376 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2378 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
2379 ->will( $this->returnValue( $key ) );
2383 $callback = $this->callback(
function (
$user )
use ( &
$username, &$workaroundPHPUnitBug ) {
2387 $mocks[
'pre']->expects( $this->exactly( 12 ) )->method(
'testUserForCreation' )
2389 ->will( $this->onConsecutiveCalls(
2399 $mocks[
'primary']->expects( $this->any() )->method(
'accountCreationType' )
2401 $mocks[
'primary']->expects( $this->any() )->method(
'testUserExists' )
2402 ->will( $this->returnValue(
true ) );
2403 $mocks[
'primary']->expects( $this->exactly( 9 ) )->method(
'testUserForCreation' )
2405 ->will( $this->onConsecutiveCalls(
2413 $mocks[
'primary']->expects( $this->exactly( 3 ) )->method(
'autoCreatedAccount' )
2416 $mocks[
'secondary']->expects( $this->exactly( 8 ) )->method(
'testUserForCreation' )
2418 ->will( $this->onConsecutiveCalls(
2426 $mocks[
'secondary']->expects( $this->exactly( 3 ) )->method(
'autoCreatedAccount' )
2429 $this->preauthMocks = [ $mocks[
'pre'] ];
2430 $this->primaryauthMocks = [ $mocks[
'primary'] ];
2431 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
2433 $session = $this->
request->getSession();
2435 $logger = new \TestLogger(
true,
function ( $m ) {
2436 $m = str_replace(
'MediaWiki\\Auth\\AuthManager::autoCreateUser: ',
'', $m );
2439 $this->manager->setLogger(
$logger );
2443 $this->manager->autoCreateUser(
$user,
'InvalidSource',
true );
2444 $this->fail(
'Expected exception not thrown' );
2445 }
catch ( \InvalidArgumentException $ex ) {
2446 $this->assertSame(
'Unknown auto-creation source: InvalidSource', $ex->getMessage() );
2452 $this->
hook(
'LocalUserCreated', $this->never() );
2454 $this->
unhook(
'LocalUserCreated' );
2456 $expect->warning(
'userexists' );
2457 $this->assertEquals( $expect,
$ret );
2458 $this->assertNotEquals( 0,
$user->getId() );
2459 $this->assertSame(
'UTSysop',
$user->getName() );
2460 $this->assertEquals(
$user->getId(), $session->getUser()->getId() );
2461 $this->assertSame( [
2462 [ LogLevel::DEBUG,
'{username} already exists locally' ],
2468 $this->
hook(
'LocalUserCreated', $this->never() );
2470 $this->
unhook(
'LocalUserCreated' );
2472 $expect->warning(
'userexists' );
2473 $this->assertEquals( $expect,
$ret );
2474 $this->assertNotEquals( 0,
$user->getId() );
2475 $this->assertSame(
'UTSysop',
$user->getName() );
2476 $this->assertEquals( 0, $session->getUser()->getId() );
2477 $this->assertSame( [
2478 [ LogLevel::DEBUG,
'{username} already exists locally' ],
2486 $this->
hook(
'LocalUserCreated', $this->never() );
2488 $this->
unhook(
'LocalUserCreated' );
2490 $this->assertEquals( 0,
$user->getId() );
2492 $this->assertEquals( 0, $session->getUser()->getId() );
2493 $this->assertSame( [
2494 [ LogLevel::DEBUG,
'denied by wfReadOnly(): {reason}' ],
2501 $session->set(
'AuthManager::AutoCreateBlacklist',
'test' );
2503 $this->
hook(
'LocalUserCreated', $this->never() );
2505 $this->
unhook(
'LocalUserCreated' );
2507 $this->assertEquals( 0,
$user->getId() );
2509 $this->assertEquals( 0, $session->getUser()->getId() );
2510 $this->assertSame( [
2511 [ LogLevel::DEBUG,
'blacklisted in session {sessionid}' ],
2518 $this->
hook(
'LocalUserCreated', $this->never() );
2520 $this->
unhook(
'LocalUserCreated' );
2522 $this->assertEquals( 0,
$user->getId() );
2524 $this->assertEquals( 0, $session->getUser()->getId() );
2525 $this->assertSame( [
2526 [ LogLevel::DEBUG,
'blacklisted in session {sessionid}' ],
2533 $this->
hook(
'LocalUserCreated', $this->never() );
2535 $this->
unhook(
'LocalUserCreated' );
2537 $this->assertEquals( 0,
$user->getId() );
2539 $this->assertEquals( 0, $session->getUser()->getId() );
2540 $this->assertSame( [
2541 [ LogLevel::DEBUG,
'name "{username}" is not creatable' ],
2544 $this->assertSame(
'noname', $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2547 $wgGroupPermissions[
'*'][
'createaccount'] =
false;
2548 $wgGroupPermissions[
'*'][
'autocreateaccount'] =
false;
2551 $this->
hook(
'LocalUserCreated', $this->never() );
2553 $this->
unhook(
'LocalUserCreated' );
2555 $this->assertEquals( 0,
$user->getId() );
2557 $this->assertEquals( 0, $session->getUser()->getId() );
2558 $this->assertSame( [
2559 [ LogLevel::DEBUG,
'IP lacks the ability to create or autocreate accounts' ],
2563 'authmanager-autocreate-noperm', $session->get(
'AuthManager::AutoCreateBlacklist' )
2568 $wgGroupPermissions[
'*'][
'createaccount'] =
false;
2569 $wgGroupPermissions[
'*'][
'autocreateaccount'] =
true;
2572 $this->
hook(
'LocalUserCreated', $this->never() );
2574 $this->
unhook(
'LocalUserCreated' );
2577 $wgGroupPermissions[
'*'][
'createaccount'] =
true;
2578 $wgGroupPermissions[
'*'][
'autocreateaccount'] =
false;
2581 $this->
hook(
'LocalUserCreated', $this->never() );
2583 $this->
unhook(
'LocalUserCreated' );
2590 $this->
hook(
'LocalUserCreated', $this->never() );
2595 $this->
unhook(
'LocalUserCreated' );
2597 $this->assertEquals( 0,
$user->getId() );
2599 $this->assertEquals( 0, $session->getUser()->getId() );
2600 $this->assertSame( [
2601 [ LogLevel::DEBUG,
'Could not acquire account creation lock' ],
2608 $this->
hook(
'LocalUserCreated', $this->never() );
2610 $this->
unhook(
'LocalUserCreated' );
2612 $this->assertEquals( 0,
$user->getId() );
2614 $this->assertEquals( 0, $session->getUser()->getId() );
2615 $this->assertSame( [
2616 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2619 $this->assertEquals(
2625 $this->
hook(
'LocalUserCreated', $this->never() );
2627 $this->
unhook(
'LocalUserCreated' );
2629 $this->assertEquals( 0,
$user->getId() );
2631 $this->assertEquals( 0, $session->getUser()->getId() );
2632 $this->assertSame( [
2633 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2636 $this->assertEquals(
2642 $this->
hook(
'LocalUserCreated', $this->never() );
2644 $this->
unhook(
'LocalUserCreated' );
2646 $this->assertEquals( 0,
$user->getId() );
2648 $this->assertEquals( 0, $session->getUser()->getId() );
2649 $this->assertSame( [
2650 [ LogLevel::DEBUG,
'Provider denied creation of {username}: {reason}' ],
2653 $this->assertEquals(
2660 $cache->set( $backoffKey,
true );
2663 $this->
hook(
'LocalUserCreated', $this->never() );
2665 $this->
unhook(
'LocalUserCreated' );
2667 $this->assertEquals( 0,
$user->getId() );
2669 $this->assertEquals( 0, $session->getUser()->getId() );
2670 $this->assertSame( [
2671 [ LogLevel::DEBUG,
'{username} denied by prior creation attempt failures' ],
2674 $this->assertSame( null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2675 $cache->delete( $backoffKey );
2679 $user = $this->getMock(
'User', [
'addToDatabase' ] );
2680 $user->expects( $this->once() )->method(
'addToDatabase' )
2685 $this->assertEquals( 0,
$user->getId() );
2687 $this->assertEquals( 0, $session->getUser()->getId() );
2688 $this->assertSame( [
2689 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2690 [ LogLevel::ERROR,
'{username} failed with message {message}' ],
2693 $this->assertSame( null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2698 $this->assertFalse( $cache->get( $backoffKey ),
'sanity check' );
2700 $user = $this->getMock(
'User', [
'addToDatabase' ] );
2701 $user->expects( $this->once() )->method(
'addToDatabase' )
2702 ->will( $this->throwException(
new \Exception(
'Excepted' ) ) );
2706 $this->fail(
'Expected exception not thrown' );
2707 }
catch ( \Exception $ex ) {
2708 $this->assertSame(
'Excepted', $ex->getMessage() );
2710 $this->assertEquals( 0,
$user->getId() );
2711 $this->assertEquals( 0, $session->getUser()->getId() );
2712 $this->assertSame( [
2713 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2714 [ LogLevel::ERROR,
'{username} failed with exception {exception}' ],
2717 $this->assertSame( null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2718 $this->assertNotEquals(
false, $cache->get( $backoffKey ) );
2719 $cache->delete( $backoffKey );
2723 $user = $this->getMock(
'User', [
'addToDatabase' ] );
2724 $user->expects( $this->once() )->method(
'addToDatabase' )
2725 ->will( $this->returnCallback(
function ()
use (
$username ) {
2727 $this->assertTrue(
$status->isOK(),
'sanity check' );
2728 return \Status::newFatal(
'userexists' );
2733 $expect->warning(
'userexists' );
2734 $this->assertEquals( $expect,
$ret );
2735 $this->assertNotEquals( 0,
$user->getId() );
2737 $this->assertEquals(
$user->getId(), $session->getUser()->getId() );
2738 $this->assertSame( [
2739 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2740 [ LogLevel::INFO,
'{username} already exists locally (race)' ],
2743 $this->assertSame( null, $session->get(
'AuthManager::AutoCreateBlacklist' ) );
2747 $username = self::usernameForCreation();
2749 $this->
hook(
'AuthPluginAutoCreate', $this->once() )
2750 ->with( $callback );
2752 get_class( $wgHooks[
'AuthPluginAutoCreate'][0] ) .
'::onAuthPluginAutoCreate)' );
2753 $this->
hook(
'LocalUserCreated', $this->once() )
2754 ->with( $callback, $this->equalTo(
true ) );
2756 $this->
unhook(
'LocalUserCreated' );
2757 $this->
unhook(
'AuthPluginAutoCreate' );
2759 $this->assertNotEquals( 0,
$user->getId() );
2761 $this->assertEquals(
$user->getId(), $session->getUser()->getId() );
2762 $this->assertSame( [
2763 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2768 $maxLogId = $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] );
2770 $username = self::usernameForCreation();
2772 $this->
hook(
'LocalUserCreated', $this->once() )
2773 ->with( $callback, $this->equalTo(
true ) );
2775 $this->
unhook(
'LocalUserCreated' );
2777 $this->assertNotEquals( 0,
$user->getId() );
2779 $this->assertEquals( 0, $session->getUser()->getId() );
2780 $this->assertSame( [
2781 [ LogLevel::INFO,
'creating new user ({username}) - from: {from}' ],
2786 $dbw->selectField(
'logging',
'MAX(log_id)', [
'log_type' =>
'newusers' ] )
2789 $this->config->set(
'NewUserLog',
true );
2791 $username = self::usernameForCreation();
2798 $rows = iterator_to_array( $dbw->select(
2802 'log_id > ' . (
int)$maxLogId,
2803 'log_type' =>
'newusers'
2809 $this->assertCount( 1, $rows );
2812 $this->assertSame(
'autocreate', $entry->getSubtype() );
2813 $this->assertSame(
$user->getId(), $entry->getPerformer()->getId() );
2814 $this->assertSame(
$user->getName(), $entry->getPerformer()->getName() );
2815 $this->assertSame(
$user->getUserPage()->getFullText(), $entry->getTarget()->getFullText() );
2816 $this->assertSame( [
'4::userid' =>
$user->getId() ], $entry->getParameters() );
2818 $workaroundPHPUnitBug =
true;
2828 $makeReq =
function ( $key )
use (
$action ) {
2830 $req->expects( $this->any() )->method(
'getUniqueId' )
2831 ->will( $this->returnValue( $key ) );
2836 $cmpReqs =
function ( $a, $b ) {
2837 $ret = strcmp( get_class( $a ), get_class( $b ) );
2839 $ret = strcmp( $a->key, $b->key );
2847 foreach ( [
'pre',
'primary',
'secondary' ]
as $key ) {
2848 $class = ucfirst( $key ) .
'AuthenticationProvider';
2849 $mocks[$key] = $this->getMockForAbstractClass(
2850 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2852 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
2853 ->will( $this->returnValue( $key ) );
2854 $mocks[$key]->expects( $this->any() )->method(
'getAuthenticationRequests' )
2855 ->will( $this->returnCallback(
function (
$action )
use ( $key, $makeReq ) {
2856 return [ $makeReq(
"$key-$action" ), $makeReq(
'generic' ) ];
2858 $mocks[$key]->expects( $this->any() )->method(
'providerAllowsAuthenticationDataChange' )
2859 ->will( $this->returnValue( $good ) );
2868 $class =
'PrimaryAuthenticationProvider';
2869 $mocks[
"primary-$type"] = $this->getMockForAbstractClass(
2870 "MediaWiki\\Auth\\$class", [],
"Mock$class"
2872 $mocks[
"primary-$type"]->expects( $this->any() )->method(
'getUniqueId' )
2873 ->will( $this->returnValue(
"primary-$type" ) );
2874 $mocks[
"primary-$type"]->expects( $this->any() )->method(
'accountCreationType' )
2875 ->will( $this->returnValue( $type ) );
2876 $mocks[
"primary-$type"]->expects( $this->any() )->method(
'getAuthenticationRequests' )
2877 ->will( $this->returnCallback(
function (
$action )
use ( $type, $makeReq ) {
2878 return [ $makeReq(
"primary-$type-$action" ), $makeReq(
'generic' ) ];
2880 $mocks[
"primary-$type"]->expects( $this->any() )
2881 ->method(
'providerAllowsAuthenticationDataChange' )
2882 ->will( $this->returnValue( $good ) );
2883 $this->primaryauthMocks[] = $mocks[
"primary-$type"];
2886 $mocks[
'primary2'] = $this->getMockForAbstractClass(
2889 $mocks[
'primary2']->expects( $this->any() )->method(
'getUniqueId' )
2890 ->will( $this->returnValue(
'primary2' ) );
2891 $mocks[
'primary2']->expects( $this->any() )->method(
'accountCreationType' )
2893 $mocks[
'primary2']->expects( $this->any() )->method(
'getAuthenticationRequests' )
2894 ->will( $this->returnValue( [] ) );
2895 $mocks[
'primary2']->expects( $this->any() )
2896 ->method(
'providerAllowsAuthenticationDataChange' )
2897 ->will( $this->returnCallback(
function (
$req )
use ( $good ) {
2900 $this->primaryauthMocks[] = $mocks[
'primary2'];
2902 $this->preauthMocks = [ $mocks[
'pre'] ];
2903 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
2907 if ( isset( $state[
'continueRequests'] ) ) {
2908 $state[
'continueRequests'] = array_map( $makeReq, $state[
'continueRequests'] );
2911 $this->
request->getSession()->setSecret(
'AuthManager::authnState', $state );
2913 $this->
request->getSession()->setSecret(
'AuthManager::accountCreationState', $state );
2915 $this->
request->getSession()->setSecret(
'AuthManager::accountLinkState', $state );
2919 $expectReqs = array_map( $makeReq, $expect );
2924 $expectReqs[] =
$req;
2928 $expectReqs[] =
$req;
2932 $expectReqs[] =
$req;
2934 usort( $expectReqs, $cmpReqs );
2936 $actual = $this->manager->getAuthenticationRequests(
$action );
2937 foreach ( $actual
as $req ) {
2941 usort( $actual, $cmpReqs );
2943 $this->assertEquals( $expectReqs, $actual );
2950 $expectReqs[] =
$req;
2951 usort( $expectReqs, $cmpReqs );
2954 foreach ( $actual
as $req ) {
2958 usort( $actual, $cmpReqs );
2960 $this->assertEquals( $expectReqs, $actual );
2968 [
'pre-login',
'primary-none-login',
'primary-create-login',
2969 'primary-link-login',
'secondary-login',
'generic' ],
2973 [
'pre-create',
'primary-none-create',
'primary-create-create',
2974 'primary-link-create',
'secondary-create',
'generic' ],
2978 [
'primary-link-link',
'generic' ],
2982 [
'primary-none-change',
'primary-create-change',
'primary-link-change',
2983 'secondary-change' ],
2987 [
'primary-none-remove',
'primary-create-remove',
'primary-link-remove',
2988 'secondary-remove' ],
2992 [
'primary-link-remove' ],
3000 $reqs = [
'continue-login',
'foo',
'bar' ],
3002 'continueRequests' => $reqs,
3011 $reqs = [
'continue-create',
'foo',
'bar' ],
3013 'continueRequests' => $reqs,
3022 $reqs = [
'continue-link',
'foo',
'bar' ],
3024 'continueRequests' => $reqs,
3031 $makeReq =
function ( $key, $required ) {
3033 $req->expects( $this->any() )->method(
'getUniqueId' )
3034 ->will( $this->returnValue( $key ) );
3037 $req->required = $required;
3040 $cmpReqs =
function ( $a, $b ) {
3041 $ret = strcmp( get_class( $a ), get_class( $b ) );
3043 $ret = strcmp( $a->key, $b->key );
3051 $primary1->expects( $this->any() )->method(
'getUniqueId' )
3052 ->will( $this->returnValue(
'primary1' ) );
3053 $primary1->expects( $this->any() )->method(
'accountCreationType' )
3055 $primary1->expects( $this->any() )->method(
'getAuthenticationRequests' )
3056 ->will( $this->returnCallback(
function (
$action )
use ( $makeReq ) {
3068 $primary2->expects( $this->any() )->method(
'getUniqueId' )
3069 ->will( $this->returnValue(
'primary2' ) );
3070 $primary2->expects( $this->any() )->method(
'accountCreationType' )
3072 $primary2->expects( $this->any() )->method(
'getAuthenticationRequests' )
3073 ->will( $this->returnCallback(
function (
$action )
use ( $makeReq ) {
3082 $secondary->expects( $this->any() )->method(
'getUniqueId' )
3083 ->will( $this->returnValue(
'secondary' ) );
3084 $secondary->expects( $this->any() )->method(
'getAuthenticationRequests' )
3085 ->will( $this->returnCallback(
function (
$action )
use ( $makeReq ) {
3096 $this->primaryauthMocks = [ $primary1, $primary2 ];
3097 $this->secondaryauthMocks = [ $secondary ];
3112 usort( $actual, $cmpReqs );
3113 usort( $expected, $cmpReqs );
3114 $this->assertEquals( $expected, $actual );
3116 $this->primaryauthMocks = [ $primary1 ];
3117 $this->secondaryauthMocks = [ $secondary ];
3130 usort( $actual, $cmpReqs );
3131 usort( $expected, $cmpReqs );
3132 $this->assertEquals( $expected, $actual );
3137 foreach ( [
'primary',
'secondary' ]
as $key ) {
3138 $class = ucfirst( $key ) .
'AuthenticationProvider';
3139 $mocks[$key] = $this->getMockForAbstractClass(
3140 "MediaWiki\\Auth\\$class", [],
"Mock$class"
3142 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
3143 ->will( $this->returnValue( $key ) );
3144 $mocks[$key]->expects( $this->any() )->method(
'providerAllowsPropertyChange' )
3145 ->will( $this->returnCallback(
function ( $prop )
use ( $key ) {
3146 return $prop !== $key;
3150 $this->primaryauthMocks = [ $mocks[
'primary'] ];
3151 $this->secondaryauthMocks = [ $mocks[
'secondary'] ];
3154 $this->assertTrue( $this->manager->allowsPropertyChange(
'foo' ) );
3155 $this->assertFalse( $this->manager->allowsPropertyChange(
'primary' ) );
3156 $this->assertFalse( $this->manager->allowsPropertyChange(
'secondary' ) );
3160 $username = self::usernameForCreation();
3165 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'primary' ) );
3166 $mock->expects( $this->any() )->method(
'beginPrimaryAuthentication' )
3168 $mock->expects( $this->any() )->method(
'accountCreationType' )
3170 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
true ) );
3171 $mock->expects( $this->any() )->method(
'testUserForCreation' )
3175 $mock2->expects( $this->any() )->method(
'getUniqueId' )
3176 ->will( $this->returnValue(
'secondary' ) );
3177 $mock2->expects( $this->any() )->method(
'beginSecondaryAuthentication' )->will(
3182 $mock2->expects( $this->any() )->method(
'continueSecondaryAuthentication' )
3184 $mock2->expects( $this->any() )->method(
'testUserForCreation' )
3187 $this->primaryauthMocks = [ $mock ];
3188 $this->secondaryauthMocks = [ $mock2 ];
3190 $this->manager->setLogger(
new \Psr\Log\NullLogger() );
3191 $session = $this->
request->getSession();
3201 $this->
hook(
'UserLoggedIn', $this->never() );
3202 $this->
hook(
'LocalUserCreated', $this->once() )->with( $callback, $this->equalTo(
true ) );
3203 $ret = $this->manager->beginAuthentication( [],
'http://localhost/' );
3204 $this->
unhook(
'LocalUserCreated' );
3205 $this->
unhook(
'UserLoggedIn' );
3210 $this->assertSame( 0, $session->getUser()->getId() );
3212 $this->
hook(
'UserLoggedIn', $this->once() )->with( $callback );
3213 $this->
hook(
'LocalUserCreated', $this->never() );
3214 $ret = $this->manager->continueAuthentication( [] );
3215 $this->
unhook(
'LocalUserCreated' );
3216 $this->
unhook(
'UserLoggedIn' );
3219 $this->assertSame( $id, $session->getUser()->getId() );
3223 $username = self::usernameForCreation();
3225 $mock = $this->getMockForAbstractClass(
3227 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'primary' ) );
3228 $mock->expects( $this->any() )->method(
'beginPrimaryAuthentication' )
3230 $mock->expects( $this->any() )->method(
'accountCreationType' )
3232 $mock->expects( $this->any() )->method(
'testUserExists' )->will( $this->returnValue(
true ) );
3233 $mock->expects( $this->any() )->method(
'testUserForCreation' )
3236 $this->primaryauthMocks = [ $mock ];
3238 $this->manager->setLogger(
new \Psr\Log\NullLogger() );
3239 $session = $this->
request->getSession();
3242 $this->assertSame( 0, $session->getUser()->getId(),
3247 $this->
hook(
'UserLoggedIn', $this->never() );
3248 $this->
hook(
'LocalUserCreated', $this->never() );
3249 $ret = $this->manager->beginAuthentication( [],
'http://localhost/' );
3250 $this->
unhook(
'LocalUserCreated' );
3251 $this->
unhook(
'UserLoggedIn' );
3253 $this->assertSame(
'authmanager-authn-autocreate-failed',
$ret->message->getKey() );
3256 $this->assertSame( 0, $session->getUser()->getId() );
3262 $this->assertNull( $this->manager->getAuthenticationSessionData(
'foo' ) );
3263 $this->manager->setAuthenticationSessionData(
'foo',
'foo!' );
3264 $this->manager->setAuthenticationSessionData(
'bar',
'bar!' );
3265 $this->assertSame(
'foo!', $this->manager->getAuthenticationSessionData(
'foo' ) );
3266 $this->assertSame(
'bar!', $this->manager->getAuthenticationSessionData(
'bar' ) );
3267 $this->manager->removeAuthenticationSessionData(
'foo' );
3268 $this->assertNull( $this->manager->getAuthenticationSessionData(
'foo' ) );
3269 $this->assertSame(
'bar!', $this->manager->getAuthenticationSessionData(
'bar' ) );
3270 $this->manager->removeAuthenticationSessionData(
'bar' );
3271 $this->assertNull( $this->manager->getAuthenticationSessionData(
'bar' ) );
3273 $this->manager->setAuthenticationSessionData(
'foo',
'foo!' );
3274 $this->manager->setAuthenticationSessionData(
'bar',
'bar!' );
3275 $this->manager->removeAuthenticationSessionData( null );
3276 $this->assertNull( $this->manager->getAuthenticationSessionData(
'foo' ) );
3277 $this->assertNull( $this->manager->getAuthenticationSessionData(
'bar' ) );
3288 foreach ( $types
as $type => $can ) {
3290 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
$type ) );
3291 $mock->expects( $this->any() )->method(
'accountCreationType' )
3292 ->will( $this->returnValue(
$type ) );
3293 $this->primaryauthMocks = [ $mock ];
3295 $this->assertSame( $can, $this->manager->canCreateAccounts(),
$type );
3303 $this->
request->getSession()->setSecret(
'AuthManager::accountLinkState',
'test' );
3305 $this->manager->beginAccountLink(
$user, [],
'http://localhost/' );
3306 $this->fail(
'Expected exception not thrown' );
3307 }
catch ( \LogicException $ex ) {
3308 $this->assertEquals(
'Account linking is not possible', $ex->getMessage() );
3310 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ) );
3313 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
3314 $mock->expects( $this->any() )->method(
'accountCreationType' )
3316 $this->primaryauthMocks = [ $mock ];
3319 $ret = $this->manager->beginAccountLink(
new \
User, [],
'http://localhost/' );
3321 $this->assertSame(
'noname',
$ret->message->getKey() );
3323 $ret = $this->manager->beginAccountLink(
3327 $this->assertSame(
'authmanager-userdoesnotexist', $ret->message->getKey() );
3335 'userid' =>
$user->getId(),
3336 'username' =>
$user->getName(),
3341 $this->manager->continueAccountLink( [] );
3342 $this->fail(
'Expected exception not thrown' );
3343 }
catch ( \LogicException $ex ) {
3344 $this->assertEquals(
'Account linking is not possible', $ex->getMessage() );
3348 $mock->expects( $this->any() )->method(
'getUniqueId' )->will( $this->returnValue(
'X' ) );
3349 $mock->expects( $this->any() )->method(
'accountCreationType' )
3351 $mock->expects( $this->any() )->method(
'beginPrimaryAccountLink' )->will(
3354 $this->primaryauthMocks = [ $mock ];
3357 $this->
request->getSession()->setSecret(
'AuthManager::accountLinkState', null );
3358 $ret = $this->manager->continueAccountLink( [] );
3360 $this->assertSame(
'authmanager-link-not-in-progress',
$ret->message->getKey() );
3362 $this->
request->getSession()->setSecret(
'AuthManager::accountLinkState',
3363 [
'username' =>
$user->getName() .
'<>' ] + $session );
3364 $ret = $this->manager->continueAccountLink( [] );
3366 $this->assertSame(
'noname',
$ret->message->getKey() );
3367 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ) );
3369 $id =
$user->getId();
3370 $this->
request->getSession()->setSecret(
'AuthManager::accountLinkState',
3371 [
'userid' => $id + 1 ] + $session );
3373 $ret = $this->manager->continueAccountLink( [] );
3374 $this->fail(
'Expected exception not thrown' );
3375 }
catch ( \UnexpectedValueException $ex ) {
3376 $this->assertEquals(
3377 "User \"{$user->getName()}\" is valid, but ID $id != " . ( $id + 1 ) .
'!',
3381 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ) );
3399 $req->primary = $primaryResponses;
3402 foreach ( [
'pre',
'primary' ]
as $key ) {
3403 $class = ucfirst( $key ) .
'AuthenticationProvider';
3404 $mocks[$key] = $this->getMockForAbstractClass(
3405 "MediaWiki\\Auth\\$class", [],
"Mock$class"
3407 $mocks[$key]->expects( $this->any() )->method(
'getUniqueId' )
3408 ->will( $this->returnValue( $key ) );
3410 for ( $i = 2; $i <= 3; $i++ ) {
3411 $mocks[$key . $i] = $this->getMockForAbstractClass(
3412 "MediaWiki\\Auth\\$class", [],
"Mock$class"
3414 $mocks[$key . $i]->expects( $this->any() )->method(
'getUniqueId' )
3415 ->will( $this->returnValue( $key . $i ) );
3419 $mocks[
'pre']->expects( $this->any() )->method(
'testForAccountLink' )
3420 ->will( $this->returnCallback(
3424 $this->assertSame(
$user->getId(), $u->getId() );
3425 $this->assertSame(
$user->getName(), $u->getName() );
3430 $mocks[
'pre2']->expects( $this->atMost( 1 ) )->method(
'testForAccountLink' )
3433 $mocks[
'primary']->expects( $this->any() )->method(
'accountCreationType' )
3435 $ct = count(
$req->primary );
3436 $callback = $this->returnCallback(
function ( $u, $reqs )
use (
$user,
$req ) {
3437 $this->assertSame(
$user->getId(), $u->getId() );
3438 $this->assertSame(
$user->getName(), $u->getName() );
3440 foreach ( $reqs
as $r ) {
3441 $this->assertSame(
$user->getName(), $r->username );
3442 $foundReq = $foundReq || get_class( $r ) === get_class(
$req );
3444 $this->assertTrue( $foundReq,
'$reqs contains $req' );
3445 return array_shift(
$req->primary );
3447 $mocks[
'primary']->expects( $this->exactly( min( 1, $ct ) ) )
3448 ->method(
'beginPrimaryAccountLink' )
3449 ->will( $callback );
3450 $mocks[
'primary']->expects( $this->exactly( max( 0, $ct - 1 ) ) )
3451 ->method(
'continuePrimaryAccountLink' )
3452 ->will( $callback );
3455 $mocks[
'primary2']->expects( $this->any() )->method(
'accountCreationType' )
3457 $mocks[
'primary2']->expects( $this->atMost( 1 ) )->method(
'beginPrimaryAccountLink' )
3458 ->will( $this->returnValue( $abstain ) );
3459 $mocks[
'primary2']->expects( $this->never() )->method(
'continuePrimaryAccountLink' );
3460 $mocks[
'primary3']->expects( $this->any() )->method(
'accountCreationType' )
3462 $mocks[
'primary3']->expects( $this->never() )->method(
'beginPrimaryAccountLink' );
3463 $mocks[
'primary3']->expects( $this->never() )->method(
'continuePrimaryAccountLink' );
3465 $this->preauthMocks = [ $mocks[
'pre'], $mocks[
'pre2'] ];
3466 $this->primaryauthMocks = [ $mocks[
'primary3'], $mocks[
'primary2'], $mocks[
'primary'] ];
3467 $this->logger = new \TestLogger(
true,
function ( $message, $level ) {
3468 return $level === LogLevel::DEBUG ? null : $message;
3472 $constraint = \PHPUnit_Framework_Assert::logicalOr(
3476 $providers = array_merge( $this->preauthMocks, $this->primaryauthMocks );
3477 foreach ( $providers
as $p ) {
3478 $p->postCalled =
false;
3479 $p->expects( $this->atMost( 1 ) )->method(
'postAccountLink' )
3481 $this->assertInstanceOf(
'User',
$user );
3482 $this->assertSame(
'UTSysop',
$user->getName() );
3484 $this->assertThat(
$response->status, $constraint );
3492 foreach ( $managerResponses
as $i =>
$response ) {
3496 $expectLog[] = [ LogLevel::INFO,
'Account linked to {user} by primary' ];
3502 $ret = $this->manager->beginAccountLink(
$user, [
$req ],
'http://localhost/' );
3504 $ret = $this->manager->continueAccountLink( [
$req ] );
3506 if (
$response instanceof \Exception ) {
3507 $this->fail(
'Expected exception not thrown',
"Response $i" );
3509 }
catch ( \Exception $ex ) {
3510 if ( !
$response instanceof \Exception ) {
3513 $this->assertEquals(
$response->getMessage(), $ex->getMessage(),
"Response $i, exception" );
3514 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3515 "Response $i, exception, session state" );
3519 $this->assertSame(
'http://localhost/',
$req->returnToUrl );
3522 $this->assertEquals(
$response,
$ret,
"Response $i, response" );
3526 $this->assertNull( $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3527 "Response $i, session state" );
3528 foreach ( $providers
as $p ) {
3529 $this->assertSame(
$response->status, $p->postCalled,
3530 "Response $i, post-auth callback called" );
3533 $this->assertNotNull(
3534 $this->
request->getSession()->getSecret(
'AuthManager::accountLinkState' ),
3535 "Response $i, session state"
3537 foreach (
$ret->neededRequests
as $neededReq ) {
3539 "Response $i, neededRequest action" );
3541 $this->assertEquals(
3542 $ret->neededRequests,
3544 "Response $i, continuation check"
3546 foreach ( $providers
as $p ) {
3547 $this->assertFalse( $p->postCalled,
"Response $i, post-auth callback not called" );
3554 $this->assertSame( $expectLog, $this->logger->getBuffer() );
3562 'Pre-link test fail in pre' => [
3569 'Failure in primary' => [
3576 'All primary abstain' => [
3585 'Primary UI, then redirect, then fail' => [
3594 'Primary redirect, then abstain' => [
3598 [
$req ],
'/foo.html', [
'foo' =>
'bar' ]
3604 new \DomainException(
3605 'MockPrimaryAuthenticationProvider::continuePrimaryAccountLink() returned ABSTAIN'
3609 'Primary UI, then pass' => [
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
static wrap($sv)
Succinct helper method to wrap a StatusValue.
processing should stop and the error should be shown to the user * false
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static newFatal($message)
Factory function for fatal errors.
static getLocalClusterInstance()
Get the main cluster-local cache object.
$wgHooks['ArticleShow'][]
this hook is for auditing only $response
when a variable name is used in a it is silently declared as a new local masking the global
static newFatal($message)
Factory function for fatal errors.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
see documentation in includes Linker php for Linker::makeImageLink & $time
get($name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
$wgGroupPermissions
Permission keys given to users in each group.
the value to return A Title object or null for latest to be modified or replaced by the hook handler or if authentication is not possible after cache objects are set for highlighting & $link
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
static BagOStuff[] $instances
Map of (id => BagOStuff)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
static getMain()
Static methods.
static newFromTarget($specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"<
Generic operation result class Has warning/error list, boolean status and arbitrary value...
static newGood($value=null)
Factory function for good results.
getLanguage()
Get the Language object.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
static getSelectQueryData()
Returns array of information that is needed for querying log entries.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Simple store for keeping values in an associative array for the current process.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
this hook is for auditing only $req
this hook is for auditing only or null if authentication failed before getting that far $username
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
$wgDisableAuthManager
Disable AuthManager.
static idFromName($name, $flags=self::READ_NORMAL)
Get database id given a user name.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
static newFromRow($row)
Constructs new LogEntry from database result row.
static consume(ScopedCallback &$sc=null)
Trigger a scoped callback and destroy it.
wfMemcKey()
Make a cache key for the local wiki.
</td >< td > &</td >< td > t want your writing to be edited mercilessly and redistributed at will
static newFromObject($object)
Return the same object, without access restrictions.
this hook is for auditing only etc instead of letting the login form give the generic error message that the account does not exist For when the account has been renamed or deleted or an array to pass a message key and parameters create2 Corresponds to logging log_action database field and which is displayed in the UI similar to $comment this hook should only be used to add variables that depend on the current page request
static factory($code)
Get a cached or new language object for a given language code.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
static newGood($value=null)
Factory function for good results.
Allows to change the fields on the form that will be generated $name