You are here

public function UserLoginTest::doPasswordLengthLogin in Drupal 10

Helper to test log in with a maximum length password.

Parameters

\Drupal\user\UserInterface $account: An object containing the user account.

string $current_password: The current password associated with the user.

int $length: The length of the password.

Return value

string The new password associated with the user.

File

core/modules/user/tests/src/Functional/UserLoginTest.php, line 191

Class

UserLoginTest
Ensure that login works as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function doPasswordLengthLogin(UserInterface $account, string $current_password, int $length) {
  $new_password = \Drupal::service('password_generator')
    ->generate($length);
  $uid = $account
    ->id();
  $edit = [
    'current_pass' => $current_password,
    'mail' => $account
      ->getEmail(),
    'pass[pass1]' => $new_password,
    'pass[pass2]' => $new_password,
  ];

  // Change the password.
  $this
    ->drupalGet("user/{$uid}/edit");
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The changes have been saved.');
  $this
    ->drupalLogout();

  // Login with new password.
  $this
    ->drupalGet('user/login');
  $edit = [
    'name' => $account
      ->getAccountName(),
    'pass' => $new_password,
  ];
  $this
    ->submitForm($edit, 'Log in');
  return $new_password;
}