Remove empty lines at end of functions
[lhc/web/wiklou.git] / tests / phpunit / includes / auth / LocalPasswordPrimaryAuthenticationProviderTest.php
index fa68dee..caf1680 100644 (file)
@@ -13,15 +13,6 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
        private $config = null;
        private $validity = null;
 
-       protected function setUp() {
-               global $wgDisableAuthManager;
-
-               parent::setUp();
-               if ( $wgDisableAuthManager ) {
-                       $this->markTestSkipped( '$wgDisableAuthManager is set' );
-               }
-       }
-
        /**
         * Get an instance of the provider
         *
@@ -62,6 +53,10 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
        }
 
        public function testBasics() {
+               $user = $this->getMutableTestUser()->getUser();
+               $userName = $user->getName();
+               $lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
+
                $provider = new LocalPasswordPrimaryAuthenticationProvider();
 
                $this->assertSame(
@@ -69,8 +64,8 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                        $provider->accountCreationType()
                );
 
-               $this->assertTrue( $provider->testUserExists( 'UTSysop' ) );
-               $this->assertTrue( $provider->testUserExists( 'uTSysop' ) );
+               $this->assertTrue( $provider->testUserExists( $userName ) );
+               $this->assertTrue( $provider->testUserExists( $lowerInitialUserName ) );
                $this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
                $this->assertFalse( $provider->testUserExists( '<invalid>' ) );
 
@@ -81,7 +76,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                        $provider->accountCreationType()
                );
 
-               $this->assertTrue( $provider->testUserExists( 'UTSysop' ) );
+               $this->assertTrue( $provider->testUserExists( $userName ) );
                $this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
 
                $req = new PasswordAuthenticationRequest;
@@ -91,12 +86,9 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
        }
 
        public function testTestUserCanAuthenticate() {
+               $user = $this->getMutableTestUser()->getUser();
+               $userName = $user->getName();
                $dbw = wfGetDB( DB_MASTER );
-               $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => 'UTSysop' ] );
-               $cb = new \ScopedCallback( function () use ( $dbw, $oldHash ) {
-                       $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => 'UTSysop' ] );
-               } );
-               $id = \User::idFromName( 'UTSysop' );
 
                $provider = $this->getProvider();
 
@@ -104,23 +96,24 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
                $this->assertFalse( $provider->testUserCanAuthenticate( 'DoesNotExist' ) );
 
-               $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
-               $this->assertTrue( $provider->testUserCanAuthenticate( 'uTSysop' ) );
+               $this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
+               $lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
+               $this->assertTrue( $provider->testUserCanAuthenticate( $lowerInitialUserName ) );
 
                $dbw->update(
                        'user',
                        [ 'user_password' => \PasswordFactory::newInvalidPassword()->toString() ],
-                       [ 'user_name' => 'UTSysop' ]
+                       [ 'user_name' => $userName ]
                );
-               $this->assertFalse( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+               $this->assertFalse( $provider->testUserCanAuthenticate( $userName ) );
 
                // Really old format
                $dbw->update(
                        'user',
                        [ 'user_password' => '0123456789abcdef0123456789abcdef' ],
-                       [ 'user_name' => 'UTSysop' ]
+                       [ 'user_name' => $userName ]
                );
-               $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+               $this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
        }
 
        public function testSetPasswordResetFlag() {
@@ -139,22 +132,24 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                $provider->setManager( $this->manager );
                $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
 
+               $user = $this->getMutableTestUser()->getUser();
+               $userName = $user->getName();
                $dbw = wfGetDB( DB_MASTER );
                $row = $dbw->selectRow(
                        'user',
                        '*',
-                       [ 'user_name' => 'UTSysop' ],
+                       [ 'user_name' => $userName ],
                        __METHOD__
                );
 
                $this->manager->removeAuthenticationSessionData( null );
                $row->user_password_expires = wfTimestamp( TS_MW, time() + 200 );
-               $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+               $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
                $this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
 
                $this->manager->removeAuthenticationSessionData( null );
                $row->user_password_expires = wfTimestamp( TS_MW, time() - 200 );
-               $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+               $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
                $ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
                $this->assertNotNull( $ret );
                $this->assertSame( 'resetpass-expired', $ret->msg->getKey() );
@@ -162,7 +157,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
                $this->manager->removeAuthenticationSessionData( null );
                $row->user_password_expires = wfTimestamp( TS_MW, time() - 1 );
-               $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+               $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
                $ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
                $this->assertNotNull( $ret );
                $this->assertSame( 'resetpass-expired-soft', $ret->msg->getKey() );
@@ -172,7 +167,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                $row->user_password_expires = null;
                $status = \Status::newGood();
                $status->error( 'testing' );
-               $providerPriv->setPasswordResetFlag( 'UTSysop', $status, $row );
+               $providerPriv->setPasswordResetFlag( $userName, $status, $row );
                $ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
                $this->assertNotNull( $ret );
                $this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
@@ -180,12 +175,11 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
        }
 
        public function testAuthentication() {
+               $testUser = $this->getMutableTestUser();
+               $userName = $testUser->getUser()->getName();
+
                $dbw = wfGetDB( DB_MASTER );
-               $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => 'UTSysop' ] );
-               $cb = new \ScopedCallback( function () use ( $dbw, $oldHash ) {
-                       $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => 'UTSysop' ] );
-               } );
-               $id = \User::idFromName( 'UTSysop' );
+               $id = \User::idFromName( $userName );
 
                $req = new PasswordAuthenticationRequest();
                $req->action = AuthManager::ACTION_LOGIN;
@@ -230,8 +224,8 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                );
 
                // Validation failure
-               $req->username = 'UTSysop';
-               $req->password = 'UTSysopPassword';
+               $req->username = $userName;
+               $req->password = $testUser->getPassword();
                $this->validity = \Status::newFatal( 'arbitrary-failure' );
                $ret = $provider->beginPrimaryAuthentication( $reqs );
                $this->assertEquals(
@@ -247,7 +241,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                $this->manager->removeAuthenticationSessionData( null );
                $this->validity = \Status::newGood();
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
                $this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
@@ -255,19 +249,19 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                // Successful auth after normalizing name
                $this->manager->removeAuthenticationSessionData( null );
                $this->validity = \Status::newGood();
-               $req->username = 'uTSysop';
+               $req->username = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
                $this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
-               $req->username = 'UTSysop';
+               $req->username = $userName;
 
                // Successful auth with reset
                $this->manager->removeAuthenticationSessionData( null );
                $this->validity->error( 'arbitrary-warning' );
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
                $this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
@@ -287,7 +281,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
                // Correct handling of legacy encodings
                $password = ':B:salt:' . md5( 'salt-' . md5( "\xe1\xe9\xed\xf3\xfa" ) );
-               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
                $req->password = 'áéíóú';
                $ret = $provider->beginPrimaryAuthentication( $reqs );
                $this->assertEquals(
@@ -301,7 +295,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
                $this->config->set( 'LegacyEncoding', true );
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
 
@@ -319,22 +313,21 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                // Correct handling of really old password hashes
                $this->config->set( 'PasswordSalt', false );
                $password = md5( 'FooBar' );
-               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
                $req->password = 'FooBar';
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
 
                $this->config->set( 'PasswordSalt', true );
                $password = md5( "$id-" . md5( 'FooBar' ) );
-               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+               $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
                $req->password = 'FooBar';
                $this->assertEquals(
-                       AuthenticationResponse::newPass( 'UTSysop' ),
+                       AuthenticationResponse::newPass( $userName ),
                        $provider->beginPrimaryAuthentication( $reqs )
                );
-
        }
 
        /**
@@ -407,29 +400,24 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
        /**
         * @dataProvider provideProviderChangeAuthenticationData
-        * @param string $user
+        * @param callable|bool $usernameTransform
         * @param string $type
         * @param bool $loginOnly
         * @param bool $changed
         */
-       public function testProviderChangeAuthenticationData( $user, $type, $loginOnly, $changed ) {
+       public function testProviderChangeAuthenticationData(
+                       $usernameTransform, $type, $loginOnly, $changed ) {
+               $testUser = $this->getMutableTestUser();
+               $user = $testUser->getUser()->getName();
+               if ( is_callable( $usernameTransform ) ) {
+                       $user = call_user_func( $usernameTransform, $user );
+               }
                $cuser = ucfirst( $user );
-               $oldpass = 'UTSysopPassword';
+               $oldpass = $testUser->getPassword();
                $newpass = 'NewPassword';
 
                $dbw = wfGetDB( DB_MASTER );
-               $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => $cuser ] );
                $oldExpiry = $dbw->selectField( 'user', 'user_password_expires', [ 'user_name' => $cuser ] );
-               $cb = new \ScopedCallback( function () use ( $dbw, $cuser, $oldHash, $oldExpiry ) {
-                       $dbw->update(
-                               'user',
-                               [
-                                       'user_password' => $oldHash,
-                                       'user_password_expires' => $oldExpiry,
-                               ],
-                               [ 'user_name' => $cuser ]
-                       );
-               } );
 
                $this->mergeMwGlobalArrayValue( 'wgHooks', [
                        'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
@@ -525,12 +513,12 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
 
        public static function provideProviderChangeAuthenticationData() {
                return [
-                       [ 'UTSysop', AuthenticationRequest::class, false, false ],
-                       [ 'UTSysop', PasswordAuthenticationRequest::class, false, true ],
-                       [ 'UTSysop', AuthenticationRequest::class, true, false ],
-                       [ 'UTSysop', PasswordAuthenticationRequest::class, true, true ],
-                       [ 'uTSysop', PasswordAuthenticationRequest::class, false, true ],
-                       [ 'uTSysop', PasswordAuthenticationRequest::class, true, true ],
+                       [ false, AuthenticationRequest::class, false, false ],
+                       [ false, PasswordAuthenticationRequest::class, false, true ],
+                       [ false, AuthenticationRequest::class, true, false ],
+                       [ false, PasswordAuthenticationRequest::class, true, true ],
+                       [ 'ucfirst', PasswordAuthenticationRequest::class, false, true ],
+                       [ 'ucfirst', PasswordAuthenticationRequest::class, true, true ],
                ];
        }
 
@@ -640,10 +628,6 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                // We have to cheat a bit to avoid having to add a new user to
                // the database to test the actual setting of the password works right
                $dbw = wfGetDB( DB_MASTER );
-               $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => $user ] );
-               $cb = new \ScopedCallback( function () use ( $dbw, $user, $oldHash ) {
-                       $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => $user ] );
-               } );
 
                $user = \User::newFromName( 'UTSysop' );
                $req->username = $user->getName();
@@ -660,7 +644,6 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase
                $this->assertNull( $provider->finishAccountCreation( $user, $user, $res2 ) );
                $ret = $provider->beginPrimaryAuthentication( $reqs );
                $this->assertEquals( AuthenticationResponse::PASS, $ret->status, 'new password is set' );
-
        }
 
 }