Merge "Reset scoped session for upload jobs after deferred updates"
[lhc/web/wiklou.git] / tests / phpunit / includes / TestUser.php
index 754568d..b506cb8 100644 (file)
@@ -28,13 +28,15 @@ class TestUser {
 
        private function assertNotReal() {
                global $wgDBprefix;
-               if ( $wgDBprefix !== MediaWikiTestCase::DB_PREFIX && $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX ) {
+               if ( $wgDBprefix !== MediaWikiTestCase::DB_PREFIX &&
+                       $wgDBprefix !== MediaWikiTestCase::ORA_DB_PREFIX
+               ) {
                        throw new MWException( "Can't create user on real database" );
                }
        }
 
        public function __construct( $username, $realname = 'Real Name',
-               $email = 'sample@example.com', $groups = array()
+               $email = 'sample@example.com', $groups = []
        ) {
                $this->assertNotReal();
 
@@ -51,10 +53,10 @@ class TestUser {
                if ( !$this->user->isLoggedIn() ) {
                        // create the user
                        $this->user = User::createNew(
-                               $this->username, array(
+                               $this->username, [
                                        "email" => $email,
                                        "real_name" => $realname
-                               )
+                               ]
                        );
 
                        if ( !$this->user ) {
@@ -63,8 +65,8 @@ class TestUser {
                }
 
                // Update the user to use the password and other details
-               $change = $this->setPassword( $this->password ) ||
-                       $this->setEmail( $email ) ||
+               $this->setPassword( $this->password );
+               $change = $this->setEmail( $email ) ||
                        $this->setRealName( $realname );
 
                // Adjust groups by adding any missing ones and removing any extras
@@ -108,26 +110,36 @@ class TestUser {
 
        /**
         * @param string $password
-        * @return bool
         */
        private function setPassword( $password ) {
-               $passwordFactory = $this->user->getPasswordFactory();
-               $oldDefaultType = $passwordFactory->getDefaultType();
-
-               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
-               $passwordFactory->setDefaultType( 'A' );
-               $newPassword = $passwordFactory->newFromPlaintext( $password, $this->user->getPassword() );
+               self::setPasswordForUser( $this->user, $password );
+       }
 
-               $change = false;
-               if ( !$this->user->getPassword()->equals( $newPassword ) ) {
-                       // Password changed
-                       $this->user->setPassword( $password );
-                       $change = true;
+       /**
+        * Set the password on a testing user
+        *
+        * This assumes we're still using the generic AuthManager config from
+        * PHPUnitMaintClass::finalSetup(), and just sets the password in the
+        * database directly.
+        * @param User $user
+        * @param string $password
+        */
+       public static function setPasswordForUser( User $user, $password ) {
+               if ( !$user->getId() ) {
+                       throw new MWException( "Passed User has not been added to the database yet!" );
                }
 
-               $passwordFactory->setDefaultType( $oldDefaultType );
-
-               return $change;
+               $passwordFactory = new PasswordFactory();
+               $passwordFactory->init( RequestContext::getMain()->getConfig() );
+               // A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
+               $passwordFactory->setDefaultType( 'A' );
+               $pwhash = $passwordFactory->newFromPlaintext( $password );
+               wfGetDB( DB_MASTER )->update(
+                       'user',
+                       [ 'user_password' => $pwhash->toString() ],
+                       [ 'user_id' => $user->getId() ],
+                       __METHOD__
+               );
        }
 
        /**