You are here

function UserLoginTest::testPasswordRehashOnLogin in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Tests/UserLoginTest.php \Drupal\user\Tests\UserLoginTest::testPasswordRehashOnLogin()

Test that user password is re-hashed upon login after changing $count_log2.

File

core/modules/user/src/Tests/UserLoginTest.php, line 116
Contains \Drupal\user\Tests\UserLoginTest.

Class

UserLoginTest
Ensure that login works as expected.

Namespace

Drupal\user\Tests

Code

function testPasswordRehashOnLogin() {

  // Determine default log2 for phpass hashing algorithm
  $default_count_log2 = 16;

  // Retrieve instance of password hashing algorithm
  $password_hasher = $this->container
    ->get('password');

  // Create a new user and authenticate.
  $account = $this
    ->drupalCreateUser(array());
  $password = $account->pass_raw;
  $this
    ->drupalLogin($account);
  $this
    ->drupalLogout();

  // Load the stored user. The password hash should reflect $default_count_log2.
  $user_storage = $this->container
    ->get('entity.manager')
    ->getStorage('user');
  $account = User::load($account
    ->id());
  $this
    ->assertIdentical($password_hasher
    ->getCountLog2($account
    ->getPassword()), $default_count_log2);

  // Change the required number of iterations by loading a test-module
  // containing the necessary container builder code and then verify that the
  // users password gets rehashed during the login.
  $overridden_count_log2 = 19;
  \Drupal::service('module_installer')
    ->install(array(
    'user_custom_phpass_params_test',
  ));
  $this
    ->resetAll();
  $account->pass_raw = $password;
  $this
    ->drupalLogin($account);

  // Load the stored user, which should have a different password hash now.
  $user_storage
    ->resetCache(array(
    $account
      ->id(),
  ));
  $account = $user_storage
    ->load($account
    ->id());
  $this
    ->assertIdentical($password_hasher
    ->getCountLog2($account
    ->getPassword()), $overridden_count_log2);
  $this
    ->assertTrue($password_hasher
    ->check($password, $account
    ->getPassword()));
}