X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FTestUser.php;h=b506cb87af01d939fb2f476a200bee3278381cdc;hb=d716155c8b2d6e4a51a4110195cee7a1794846e8;hp=96b2251d3ce7c18309d03b47589951e44a58e36d;hpb=638c4528259b71a5bc90439fad7cd8d110a86b06;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/TestUser.php b/tests/phpunit/includes/TestUser.php index 96b2251d3c..b506cb87af 100644 --- a/tests/phpunit/includes/TestUser.php +++ b/tests/phpunit/includes/TestUser.php @@ -36,7 +36,7 @@ class TestUser { } public function __construct( $username, $realname = 'Real Name', - $email = 'sample@example.com', $groups = array() + $email = 'sample@example.com', $groups = [] ) { $this->assertNotReal(); @@ -53,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 ) { @@ -65,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 @@ -110,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__ + ); } /**