use Psr\Log\LogLevel;
use StatusValue;
use Wikimedia\ScopedCallback;
+use Wikimedia\TestingAccessWrapper;
/**
* @group AuthManager
*/
protected function hook( $hook, $expect ) {
global $wgHooks;
- $mock = $this->getMock( __CLASS__, [ "on$hook" ] );
+ $mock = $this->getMockBuilder( __CLASS__ )
+ ->setMethods( [ "on$hook" ] )
+ ->getMock();
$wgHooks[$hook] = [ $mock ];
return $mock->expects( $expect )->method( "on$hook" );
}
}
$this->manager = new AuthManager( $this->request, $this->config );
$this->manager->setLogger( $this->logger );
- $this->managerPriv = \TestingAccessWrapper::newFromObject( $this->manager );
+ $this->managerPriv = TestingAccessWrapper::newFromObject( $this->manager );
}
/**
'logger' => new \Psr\Log\NullLogger(),
'store' => new \HashBagOStuff(),
] );
- \TestingAccessWrapper::newFromObject( $manager )->getProvider( (string)$provider );
+ TestingAccessWrapper::newFromObject( $manager )->getProvider( (string)$provider );
$reset = \MediaWiki\Session\TestUtils::setSessionManagerSingleton( $manager );
$this->assertSame( \RequestContext::getMain()->getRequest(), $singleton->getRequest() );
$this->assertSame(
\RequestContext::getMain()->getConfig(),
- \TestingAccessWrapper::newFromObject( $singleton )->config
+ TestingAccessWrapper::newFromObject( $singleton )->config
);
}
public function testCreateFromLogin() {
$user = \User::newFromName( 'UTSysop' );
- $req1 = $this->getMock( AuthenticationRequest::class );
- $req2 = $this->getMock( AuthenticationRequest::class );
- $req3 = $this->getMock( AuthenticationRequest::class );
+ $req1 = $this->createMock( AuthenticationRequest::class );
+ $req2 = $this->createMock( AuthenticationRequest::class );
+ $req3 = $this->createMock( AuthenticationRequest::class );
$userReq = new UsernameAuthenticationRequest;
$userReq->username = 'UTDummy';
}
public function provideAuthentication() {
- $user = \User::newFromName( 'UTSysop' );
- $id = $user->getId();
- $name = $user->getName();
-
$rememberReq = new RememberMeAuthenticationRequest;
$rememberReq->action = AuthManager::ACTION_LOGIN;
$restartResponse2->createRequest->action = AuthManager::ACTION_LOGIN;
$restartResponse2->neededRequests = [ $rememberReq, $restartResponse2->createRequest ];
+ $userName = 'UTSysop';
+
return [
'Failure in pre-auth' => [
StatusValue::newFatal( 'fail-from-pre' ),
'Secondary fail' => [
StatusValue::newGood(),
[
- AuthenticationResponse::newPass( $name ),
+ AuthenticationResponse::newPass( $userName ),
],
$tmp = [
AuthenticationResponse::newFail( $this->message( 'fail-in-secondary' ) ),
'Secondary UI, then abstain' => [
StatusValue::newGood(),
[
- AuthenticationResponse::newPass( $name ),
+ AuthenticationResponse::newPass( $userName ),
],
[
$tmp = AuthenticationResponse::newUI( [ $req ], $this->message( '...' ) ),
],
[
$tmp,
- AuthenticationResponse::newPass( $name ),
+ AuthenticationResponse::newPass( $userName ),
]
],
'Secondary pass' => [
StatusValue::newGood(),
[
- AuthenticationResponse::newPass( $name ),
+ AuthenticationResponse::newPass( $userName ),
],
[
AuthenticationResponse::newPass()
],
[
- AuthenticationResponse::newPass( $name ),
+ AuthenticationResponse::newPass( $userName ),
]
],
];
$this->manager->checkAccountCreatePermissions( new \User )
);
- $this->setMwGlobals( [ 'wgReadOnly' => 'Because' ] );
+ $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
+ $readOnlyMode->setReason( 'Because' );
$this->assertEquals(
\Status::newFatal( 'readonlytext', 'Because' ),
$this->manager->checkAccountCreatePermissions( new \User )
);
- $this->setMwGlobals( [ 'wgReadOnly' => false ] );
+ $readOnlyMode->setReason( false );
$wgGroupPermissions['*']['createaccount'] = false;
$status = $this->manager->checkAccountCreatePermissions( new \User );
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'noname', $ret->message->getKey() );
- $this->setMwGlobals( [ 'wgReadOnly' => 'Because' ] );
+ $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
+ $readOnlyMode->setReason( 'Because' );
$this->hook( 'LocalUserCreated', $this->never() );
$userReq->username = self::usernameForCreation();
$ret = $this->manager->beginAccountCreation( $creator, [ $userReq ], 'http://localhost/' );
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'readonlytext', $ret->message->getKey() );
$this->assertSame( [ 'Because' ], $ret->message->getParams() );
- $this->setMwGlobals( [ 'wgReadOnly' => false ] );
+ $readOnlyMode->setReason( false );
$this->hook( 'LocalUserCreated', $this->never() );
$userReq->username = self::usernameForCreation();
$this->request->getSession()->setSecret( 'AuthManager::accountCreationState',
[ 'username' => $creator->getName() ] + $session );
- $this->setMwGlobals( [ 'wgReadOnly' => 'Because' ] );
+ $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
+ $readOnlyMode->setReason( 'Because' );
$this->hook( 'LocalUserCreated', $this->never() );
$ret = $this->manager->continueAccountCreation( [] );
$this->unhook( 'LocalUserCreated' );
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'readonlytext', $ret->message->getKey() );
$this->assertSame( [ 'Because' ], $ret->message->getParams() );
- $this->setMwGlobals( [ 'wgReadOnly' => false ] );
+ $readOnlyMode->setReason( false );
$this->request->getSession()->setSecret( 'AuthManager::accountCreationState',
[ 'username' => $creator->getName() ] + $session );
// Wiki is read-only
$session->clear();
- $this->setMwGlobals( [ 'wgReadOnly' => 'Because' ] );
+ $readOnlyMode = \MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode();
+ $readOnlyMode->setReason( 'Because' );
$user = \User::newFromName( $username );
$this->hook( 'LocalUserCreated', $this->never() );
$ret = $this->manager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_SESSION, true );
[ LogLevel::DEBUG, 'denied by wfReadOnly(): {reason}' ],
], $logger->getBuffer() );
$logger->clearBuffer();
- $this->setMwGlobals( [ 'wgReadOnly' => false ] );
+ $readOnlyMode->setReason( false );
// Session blacklisted
$session->clear();
// Test addToDatabase fails
$session->clear();
- $user = $this->getMock( 'User', [ 'addToDatabase' ] );
+ $user = $this->getMockBuilder( 'User' )
+ ->setMethods( [ 'addToDatabase' ] )->getMock();
$user->expects( $this->once() )->method( 'addToDatabase' )
->will( $this->returnValue( \Status::newFatal( 'because' ) ) );
$user->setName( $username );
$backoffKey = wfMemcKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
$this->assertFalse( $cache->get( $backoffKey ), 'sanity check' );
$session->clear();
- $user = $this->getMock( 'User', [ 'addToDatabase' ] );
+ $user = $this->getMockBuilder( 'User' )
+ ->setMethods( [ 'addToDatabase' ] )->getMock();
$user->expects( $this->once() )->method( 'addToDatabase' )
->will( $this->throwException( new \Exception( 'Excepted' ) ) );
$user->setName( $username );
// Test addToDatabase fails because the user already exists.
$session->clear();
- $user = $this->getMock( 'User', [ 'addToDatabase' ] );
+ $user = $this->getMockBuilder( 'User' )
+ ->setMethods( [ 'addToDatabase' ] )->getMock();
$user->expects( $this->once() )->method( 'addToDatabase' )
->will( $this->returnCallback( function () use ( $username, &$user ) {
$oldUser = \User::newFromName( $username );
*/
public function testGetAuthenticationRequests( $action, $expect, $state = [] ) {
$makeReq = function ( $key ) use ( $action ) {
- $req = $this->getMock( AuthenticationRequest::class );
+ $req = $this->createMock( AuthenticationRequest::class );
$req->expects( $this->any() )->method( 'getUniqueId' )
->will( $this->returnValue( $key ) );
$req->action = $action === AuthManager::ACTION_UNLINK ? AuthManager::ACTION_REMOVE : $action;
public function testGetAuthenticationRequestsRequired() {
$makeReq = function ( $key, $required ) {
- $req = $this->getMock( AuthenticationRequest::class );
+ $req = $this->createMock( AuthenticationRequest::class );
$req->expects( $this->any() )->method( 'getUniqueId' )
->will( $this->returnValue( $key ) );
$req->action = AuthManager::ACTION_LOGIN;
public function testAutoCreateOnLogin() {
$username = self::usernameForCreation();
- $req = $this->getMock( AuthenticationRequest::class );
+ $req = $this->createMock( AuthenticationRequest::class );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock->expects( $this->any() )->method( 'getUniqueId' )->will( $this->returnValue( 'primary' ) );