You are here

public function MassPasswordChange::testOtherUsersMassPasswordChange in Mass Password Change 8

Test Change password function for other users.

File

tests/src/Functional/MassPasswordChange.php, line 43

Class

MassPasswordChange
Test the change password function.

Namespace

Drupal\Tests\mass_password_change\Functional

Code

public function testOtherUsersMassPasswordChange() {
  $accounts = $this->accounts;
  unset($accounts[1], $accounts[$this->adminUser
    ->id()]);
  $this
    ->drupalGet('/admin/people');
  $edit = [
    'action' => 'mass_password_change_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 change password these user accounts?');

  // Check user names.
  foreach ($accounts as $account) {
    $this
      ->assertText($account
      ->getAccountName());
  }

  // Generate new password and submit confirmation form.
  $new_password = $this
    ->randomString();
  $edit = [
    'password[pass1]' => $new_password,
    'password[pass2]' => $new_password,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Change password');

  // Check new passwords.
  foreach ($accounts as $account) {
    $this
      ->drupalLogout();
    $this
      ->drupalGet('/user/login');
    $edit = [
      'name' => $account
        ->getAccountName(),
      'pass' => $new_password,
    ];
    $this
      ->drupalPostForm(NULL, $edit, 'Log in');
    $this
      ->assertUrl('/user/' . $account
      ->id());
    $this
      ->assertText($account
      ->getAccountName());
  }
}