A service for read-only mode
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / AuthManagerTest.php
index f57db11..802e4ae 100644 (file)
@@ -45,7 +45,9 @@ class AuthManagerTest extends \MediaWikiTestCase {
         */
        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" );
        }
@@ -762,9 +764,9 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
        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';
 
@@ -1083,10 +1085,6 @@ class AuthManagerTest extends \MediaWikiTestCase {
        }
 
        public function provideAuthentication() {
-               $user = \User::newFromName( 'UTSysop' );
-               $id = $user->getId();
-               $name = $user->getName();
-
                $rememberReq = new RememberMeAuthenticationRequest;
                $rememberReq->action = AuthManager::ACTION_LOGIN;
 
@@ -1108,6 +1106,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $restartResponse2->createRequest->action = AuthManager::ACTION_LOGIN;
                $restartResponse2->neededRequests = [ $rememberReq, $restartResponse2->createRequest ];
 
+               $userName = 'UTSysop';
+
                return [
                        'Failure in pre-auth' => [
                                StatusValue::newFatal( 'fail-from-pre' ),
@@ -1202,7 +1202,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        'Secondary fail' => [
                                StatusValue::newGood(),
                                [
-                                       AuthenticationResponse::newPass( $name ),
+                                       AuthenticationResponse::newPass( $userName ),
                                ],
                                $tmp = [
                                        AuthenticationResponse::newFail( $this->message( 'fail-in-secondary' ) ),
@@ -1212,7 +1212,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        'Secondary UI, then abstain' => [
                                StatusValue::newGood(),
                                [
-                                       AuthenticationResponse::newPass( $name ),
+                                       AuthenticationResponse::newPass( $userName ),
                                ],
                                [
                                        $tmp = AuthenticationResponse::newUI( [ $req ], $this->message( '...' ) ),
@@ -1220,19 +1220,19 @@ class AuthManagerTest extends \MediaWikiTestCase {
                                ],
                                [
                                        $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 ),
                                ]
                        ],
                ];
@@ -1404,12 +1404,13 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        $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 );
@@ -1597,7 +1598,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $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/' );
@@ -1605,7 +1607,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $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();
@@ -1770,14 +1772,15 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                $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 );
@@ -2468,7 +2471,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                // 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 );
@@ -2481,7 +2485,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        [ LogLevel::DEBUG, 'denied by wfReadOnly(): {reason}' ],
                ], $logger->getBuffer() );
                $logger->clearBuffer();
-               $this->setMwGlobals( [ 'wgReadOnly' => false ] );
+               $readOnlyMode->setReason( false );
 
                // Session blacklisted
                $session->clear();
@@ -2663,7 +2667,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                // 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 );
@@ -2684,7 +2689,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $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 );
@@ -2707,7 +2713,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                // 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 );
@@ -2815,7 +2822,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
         */
        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;
@@ -3018,7 +3025,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
        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;
@@ -3148,7 +3155,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
        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' ) );