You are here

protected function DemoUmamiProfileTest::drupalLoginWithPassword in Drupal 8

Same name and namespace in other branches
  1. 9 core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php \Drupal\Tests\demo_umami\Functional\DemoUmamiProfileTest::drupalLoginWithPassword()

Logs in a user using the Mink controlled browser using a password.

If a user is already logged in, then the current user is logged out before logging in the specified user.

Please note that neither the current user nor the passed-in user object is populated with data of the logged in user. If you need full access to the user object after logging in, it must be updated manually. If you also need access to the plain-text password of the user (set by drupalCreateUser()), e.g. to log in the same user again, then it must be re-assigned manually. For example:

// Create a user.
$account = $this
  ->drupalCreateUser(array());
$this
  ->drupalLogin($account);

// Load real user object.
$pass_raw = $account->passRaw;
$account = User::load($account
  ->id());
$account->passRaw = $pass_raw;

Parameters

\Drupal\Core\Session\AccountInterface $account: User object representing the user to log in.

string $password: The password to authenticate the user with.

See also

drupalCreateUser()

1 call to DemoUmamiProfileTest::drupalLoginWithPassword()
DemoUmamiProfileTest::testUser in core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php
Tests that the users can log in with the admin password selected at install.

File

core/profiles/demo_umami/tests/src/Functional/DemoUmamiProfileTest.php, line 222

Class

DemoUmamiProfileTest
Tests demo_umami profile.

Namespace

Drupal\Tests\demo_umami\Functional

Code

protected function drupalLoginWithPassword(AccountInterface $account, $password) {
  if ($this->loggedInUser) {
    $this
      ->drupalLogout();
  }
  $this
    ->drupalGet('user/login');
  $this
    ->submitForm([
    'name' => $account
      ->getAccountName(),
    'pass' => $password,
  ], t('Log in'));

  // @see ::drupalUserIsLoggedIn()
  $account->sessionId = $this
    ->getSession()
    ->getCookie(\Drupal::service('session_configuration')
    ->getOptions(\Drupal::request())['name']);
  $this
    ->assertTrue($this
    ->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', [
    '%name' => $account
      ->getAccountName(),
  ]));
  $this->loggedInUser = $account;
  $this->container
    ->get('current_user')
    ->setAccount($account);
}