You are here

public function MassPasswordReset::testOtherUsersMassPasswordReset in Mass Password Change 8

Test Password reset function for other users.

File

tests/src/Functional/MassPasswordReset.php, line 49

Class

MassPasswordReset
Test the password reset function.

Namespace

Drupal\Tests\mass_password_change\Functional

Code

public function testOtherUsersMassPasswordReset() {
  $accounts = $this->accounts;
  unset($accounts[1], $accounts[$this->adminUser
    ->id()]);
  $this
    ->drupalGet('/admin/people');
  $edit = [
    'action' => 'mass_password_reset_action',
  ];
  foreach ($accounts as $uid => $account) {
    $weight = $this
      ->getUserWeightFromAccountsArray($this->accounts, $uid);
    $edit["user_bulk_form[{$weight}]"] = TRUE;
  }
  $this
    ->drupalPostForm(NULL, $edit, 'Apply to selected items');

  // Check confirmation text.
  $this
    ->assertText('Are you sure you want to reset password these user accounts?');

  // Check user names.
  foreach ($accounts as $account) {
    $this
      ->assertText($account
      ->getAccountName());
  }
  $this
    ->drupalPostForm(NULL, [], 'Password reset');

  // Check password reset urls from email.
  $emails = $this
    ->drupalGetMails();
  $reset_urls = [];
  while ($reset_url = $this
    ->getResetUrl($emails)) {
    $reset_urls[$reset_url['uid']] = $reset_url;
  }
  $this
    ->assertTrue(count($accounts) == count($reset_urls), 'Password reset email sending successful.');
  foreach ($reset_urls as $reset_url) {
    $this
      ->assertTrue(array_key_exists($reset_url['uid'], $accounts), sprintf("Valid user password reset URL with User Id: %d", $reset_url['uid']));
  }
}