You are here

public function UserRegistrationPassword::testLoginWithUrpLinkWhileBlocked in User registration password 8

Implements testLoginWithUrpLinkWhileBlocked().

File

tests/src/Functional/UserRegistrationPassword.php, line 150

Class

UserRegistrationPassword
Functionality tests for User registration password module.

Namespace

Drupal\Tests\user_registrationpassword\Functional

Code

public function testLoginWithUrpLinkWhileBlocked() {
  $timestamp = \Drupal::time()
    ->getRequestTime() + 5000;

  // Register a new account.
  $edit = [];
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomMachineName();
  $edit['pass[pass2]'] = $new_pass;
  $this
    ->drupalPostForm('user/register', $edit, 'Create new account');

  // Load the new user.
  $accounts = \Drupal::entityQuery('user')
    ->condition('name', $name)
    ->condition('mail', $mail)
    ->condition('status', 0)
    ->execute();

  /** @var \Drupal\user\UserInterface $account */
  $account = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->load(reset($accounts));

  // Attempt to use the activation link.
  $this
    ->drupalGet("user/registrationpassword/" . $account
    ->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $this
    ->assertSession()
    ->pageTextContains('You have just used your one-time login link. Your account is now active and you are authenticated.');
  $this
    ->drupalLogout();

  // Block the user.
  $account
    ->setLastLoginTime(\Drupal::time()
    ->getRequestTime())
    ->block()
    ->save();

  // Then try to use the link.
  $this
    ->drupalGet("user/registrationpassword/" . $account
    ->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $this
    ->assertSession()
    ->pageTextContains('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.');

  // Try to request a new activation email.
  $edit2['name'] = $edit['name'];
  $this
    ->drupalPostForm('user/password', $edit2, 'Submit');
  $this
    ->assertSession()
    ->pageTextContains($edit2['name'] . ' is blocked or has not been activated yet.');
}