Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / AuthManagerTest.php
index 6bdc569..fc6f688 100644 (file)
@@ -3,6 +3,7 @@
 namespace MediaWiki\Auth;
 
 use Config;
+use MediaWiki\Block\DatabaseBlock;
 use MediaWiki\Session\SessionInfo;
 use MediaWiki\Session\UserInfo;
 use Psr\Log\LoggerInterface;
@@ -15,7 +16,7 @@ use Wikimedia\TestingAccessWrapper;
 /**
  * @group AuthManager
  * @group Database
- * @covers MediaWiki\Auth\AuthManager
+ * @covers \MediaWiki\Auth\AuthManager
  */
 class AuthManagerTest extends \MediaWikiTestCase {
        /** @var WebRequest */
@@ -34,12 +35,6 @@ class AuthManagerTest extends \MediaWikiTestCase {
        /** @var TestingAccessWrapper */
        protected $managerPriv;
 
-       protected function setUp() {
-               parent::setUp();
-
-               $this->setMwGlobals( [ 'wgAuth' => null ] );
-       }
-
        /**
         * Sets a mock on a hook
         * @param string $hook
@@ -1436,7 +1431,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        \TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
                        $user->saveSettings();
                }
-               $oldBlock = \Block::newFromTarget( 'UTBlockee' );
+               $oldBlock = DatabaseBlock::newFromTarget( 'UTBlockee' );
                if ( $oldBlock ) {
                        // An old block will prevent our new one from saving.
                        $oldBlock->delete();
@@ -1449,8 +1444,9 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        'expiry' => time() + 100500,
                        'createAccount' => true,
                ];
-               $block = new \Block( $blockOptions );
+               $block = new DatabaseBlock( $blockOptions );
                $block->insert();
+               $this->resetServices();
                $status = $this->manager->checkAccountCreatePermissions( $user );
                $this->assertFalse( $status->isOK() );
                $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
@@ -1462,7 +1458,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        'expiry' => time() + 100500,
                        'createAccount' => true,
                ];
-               $block = new \Block( $blockOptions );
+               $block = new DatabaseBlock( $blockOptions );
                $block->insert();
                $scopeVariable = new ScopedCallback( [ $block, 'delete' ] );
                $status = $this->manager->checkAccountCreatePermissions( new \User );
@@ -1477,10 +1473,12 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        ],
                        'wgProxyWhitelist' => [],
                ] );
+               $this->resetServices();
                $status = $this->manager->checkAccountCreatePermissions( new \User );
                $this->assertFalse( $status->isOK() );
                $this->assertTrue( $status->hasMessage( 'sorbs_create_account_reason' ) );
                $this->setMwGlobals( 'wgProxyWhitelist', [ '127.0.0.1' ] );
+               $this->resetServices();
                $status = $this->manager->checkAccountCreatePermissions( new \User );
                $this->assertTrue( $status->isGood() );
        }
@@ -1831,7 +1829,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        $this->fail( 'Expected exception not thrown' );
                } catch ( \UnexpectedValueException $ex ) {
                        $this->assertEquals(
-                               "User \"{$name}\" exists, but ID $id != " . ( $id + 1 ) . '!', $ex->getMessage()
+                               "User \"{$name}\" exists, but ID $id !== " . ( $id + 1 ) . '!', $ex->getMessage()
                        );
                }
                $this->unhook( 'LocalUserCreated' );
@@ -2352,8 +2350,6 @@ class AuthManagerTest extends \MediaWikiTestCase {
        }
 
        public function testAutoAccountCreation() {
-               global $wgHooks;
-
                // PHPUnit seems to have a bug where it will call the ->with()
                // callbacks for our hooks again after the test is run (WTF?), which
                // breaks here because $username no longer matches $user by the end of
@@ -2361,6 +2357,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $workaroundPHPUnitBug = false;
 
                $username = self::usernameForCreation();
+               $expectedSource = AuthManager::AUTOCREATE_SOURCE_SESSION;
                $this->initializeManager();
 
                $this->setGroupPermissions( '*', 'createaccount', true );
@@ -2369,6 +2366,8 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $this->mergeMwGlobalArrayValue( 'wgObjectCaches',
                        [ __METHOD__ => [ 'class' => 'HashBagOStuff' ] ] );
                $this->setMwGlobals( [ 'wgMainCacheType' => __METHOD__ ] );
+               // Supply services with updated globals
+               $this->resetServices();
 
                // Set up lots of mocks...
                $mocks = [];
@@ -2380,14 +2379,20 @@ class AuthManagerTest extends \MediaWikiTestCase {
                }
 
                $good = StatusValue::newGood();
+               $ok = StatusValue::newFatal( 'ok' );
                $callback = $this->callback( function ( $user ) use ( &$username, &$workaroundPHPUnitBug ) {
                        return $workaroundPHPUnitBug || $user->getName() === $username;
                } );
+               $callback2 = $this->callback(
+                       function ( $source ) use ( &$expectedSource, &$workaroundPHPUnitBug ) {
+                               return $workaroundPHPUnitBug || $source === $expectedSource;
+                       }
+               );
 
-               $mocks['pre']->expects( $this->exactly( 12 ) )->method( 'testUserForCreation' )
-                       ->with( $callback, $this->identicalTo( AuthManager::AUTOCREATE_SOURCE_SESSION ) )
+               $mocks['pre']->expects( $this->exactly( 13 ) )->method( 'testUserForCreation' )
+                       ->with( $callback, $callback2 )
                        ->will( $this->onConsecutiveCalls(
-                               StatusValue::newFatal( 'ok' ), StatusValue::newFatal( 'ok' ), // For testing permissions
+                               $ok, $ok, $ok, // For testing permissions
                                StatusValue::newFatal( 'fail-in-pre' ), $good, $good,
                                $good, // backoff test
                                $good, // addToDatabase fails test
@@ -2401,7 +2406,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $mocks['primary']->expects( $this->any() )->method( 'testUserExists' )
                        ->will( $this->returnValue( true ) );
                $mocks['primary']->expects( $this->exactly( 9 ) )->method( 'testUserForCreation' )
-                       ->with( $callback, $this->identicalTo( AuthManager::AUTOCREATE_SOURCE_SESSION ) )
+                       ->with( $callback, $callback2 )
                        ->will( $this->onConsecutiveCalls(
                                StatusValue::newFatal( 'fail-in-primary' ), $good,
                                $good, // backoff test
@@ -2411,10 +2416,10 @@ class AuthManagerTest extends \MediaWikiTestCase {
                                $good, $good, $good
                        ) );
                $mocks['primary']->expects( $this->exactly( 3 ) )->method( 'autoCreatedAccount' )
-                       ->with( $callback, $this->identicalTo( AuthManager::AUTOCREATE_SOURCE_SESSION ) );
+                       ->with( $callback, $callback2 );
 
                $mocks['secondary']->expects( $this->exactly( 8 ) )->method( 'testUserForCreation' )
-                       ->with( $callback, $this->identicalTo( AuthManager::AUTOCREATE_SOURCE_SESSION ) )
+                       ->with( $callback, $callback2 )
                        ->will( $this->onConsecutiveCalls(
                                StatusValue::newFatal( 'fail-in-secondary' ),
                                $good, // backoff test
@@ -2424,7 +2429,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                                $good, $good, $good
                        ) );
                $mocks['secondary']->expects( $this->exactly( 3 ) )->method( 'autoCreatedAccount' )
-                       ->with( $callback, $this->identicalTo( AuthManager::AUTOCREATE_SOURCE_SESSION ) );
+                       ->with( $callback, $callback2 );
 
                $this->preauthMocks = [ $mocks['pre'] ];
                $this->primaryauthMocks = [ $mocks['primary'] ];
@@ -2564,8 +2569,20 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        'authmanager-autocreate-noperm', $session->get( 'AuthManager::AutoCreateBlacklist' )
                );
 
+               // maintenance scripts always work
+               $expectedSource = AuthManager::AUTOCREATE_SOURCE_MAINT;
+               $this->setGroupPermissions( '*', 'createaccount', false );
+               $this->setGroupPermissions( '*', 'autocreateaccount', false );
+               $session->clear();
+               $user = \User::newFromName( $username );
+               $this->hook( 'LocalUserCreated', $this->never() );
+               $ret = $this->manager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_MAINT, true );
+               $this->unhook( 'LocalUserCreated' );
+               $this->assertEquals( \Status::newFatal( 'ok' ), $ret );
+
                // Test that both permutations of permissions are allowed
                // (this hits the two "ok" entries in $mocks['pre'])
+               $expectedSource = AuthManager::AUTOCREATE_SOURCE_SESSION;
                $this->setGroupPermissions( '*', 'createaccount', false );
                $this->setGroupPermissions( '*', 'autocreateaccount', true );
                $session->clear();
@@ -2657,7 +2674,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                // Test backoff
                $cache = \ObjectCache::getLocalClusterInstance();
-               $backoffKey = wfMemcKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
+               $backoffKey = $cache->makeKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
                $cache->set( $backoffKey, true );
                $session->clear();
                $user = \User::newFromName( $username );
@@ -2696,7 +2713,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
 
                // Test addToDatabase throws an exception
                $cache = \ObjectCache::getLocalClusterInstance();
-               $backoffKey = wfMemcKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
+               $backoffKey = $cache->makeKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
                $this->assertFalse( $cache->get( $backoffKey ), 'sanity check' );
                $session->clear();
                $user = $this->getMockBuilder( \User::class )
@@ -2752,15 +2769,10 @@ class AuthManagerTest extends \MediaWikiTestCase {
                $session->clear();
                $username = self::usernameForCreation();
                $user = \User::newFromName( $username );
-               $this->hook( 'AuthPluginAutoCreate', $this->once() )
-                       ->with( $callback );
-               $this->hideDeprecated( 'AuthPluginAutoCreate hook (used in ' .
-                               get_class( $wgHooks['AuthPluginAutoCreate'][0] ) . '::onAuthPluginAutoCreate)' );
                $this->hook( 'LocalUserCreated', $this->once() )
                        ->with( $callback, $this->equalTo( true ) );
                $ret = $this->manager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_SESSION, true );
                $this->unhook( 'LocalUserCreated' );
-               $this->unhook( 'AuthPluginAutoCreate' );
                $this->assertEquals( \Status::newGood(), $ret );
                $this->assertNotEquals( 0, $user->getId() );
                $this->assertEquals( $username, $user->getName() );
@@ -3384,7 +3396,7 @@ class AuthManagerTest extends \MediaWikiTestCase {
                        $this->fail( 'Expected exception not thrown' );
                } catch ( \UnexpectedValueException $ex ) {
                        $this->assertEquals(
-                               "User \"{$user->getName()}\" is valid, but ID $id != " . ( $id + 1 ) . '!',
+                               "User \"{$user->getName()}\" is valid, but ID $id !== " . ( $id + 1 ) . '!',
                                $ex->getMessage()
                        );
                }