You are here

public function PasswordHashingTest::testPasswordRehashing in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php \Drupal\Tests\Core\Password\PasswordHashingTest::testPasswordRehashing()

Tests password rehashing.

@covers ::hash @covers ::getCountLog2 @covers ::check @covers ::needsRehash

File

core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php, line 115
Contains \Drupal\Tests\Core\Password\PasswordHashingTest.

Class

PasswordHashingTest
Unit tests for password hashing API.

Namespace

Drupal\Tests\Core\Password

Code

public function testPasswordRehashing() {

  // Increment the log2 iteration to MIN + 1.
  $password_hasher = new PhpassHashedPassword(PhpassHashedPassword::MIN_HASH_COUNT + 1);
  $this
    ->assertTrue($password_hasher
    ->needsRehash($this->hashedPassword), 'Needs a new hash after incrementing the log2 count.');

  // Re-hash the password.
  $rehashed_password = $password_hasher
    ->hash($this->password);
  $this
    ->assertSame(PhpassHashedPassword::MIN_HASH_COUNT + 1, $password_hasher
    ->getCountLog2($rehashed_password), 'Re-hashed password has the correct number of log2 iterations.');
  $this
    ->assertNotEquals($rehashed_password, $this->hashedPassword, 'Password hash changed again.');

  // Now the hash should be OK.
  $this
    ->assertFalse($password_hasher
    ->needsRehash($rehashed_password), 'Re-hashed password does not need a new hash.');
  $this
    ->assertTrue($password_hasher
    ->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password.');
  $this
    ->assertTrue($this->passwordHasher
    ->check($this->password, $rehashed_password), 'Password check succeeds with re-hashed password with original hasher.');
}