You are here

protected function WebTestBase::drupalLogin in Drupal 8

Log in a user with the internal browser.

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->pass_raw;
$account = User::load($account
  ->id());
$account->pass_raw = $pass_raw;

Parameters

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

See also

drupalCreateUser()

21 calls to WebTestBase::drupalLogin()
AggregatorTestBase::setUp in core/modules/aggregator/src/Tests/AggregatorTestBase.php
Sets up a Drupal site for running functional and integration tests.
BlockTestBase::setUp in core/modules/block/src/Tests/BlockTestBase.php
Sets up a Drupal site for running functional and integration tests.
BrokenSetUpTest::setUp in core/modules/simpletest/src/Tests/BrokenSetUpTest.php
Sets up a Drupal site for running functional and integration tests.
CommentTestBase::setUp in core/modules/comment/src/Tests/Views/CommentTestBase.php
Sets up a Drupal site for running functional and integration tests.
ContentTranslationTestBase::setupUsers in core/modules/content_translation/src/Tests/ContentTranslationTestBase.php
Creates and activates translator, editor and admin users.

... See full list

File

core/modules/simpletest/src/WebTestBase.php, line 289

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalLogin(AccountInterface $account) {
  if ($this->loggedInUser) {
    $this
      ->drupalLogout();
  }
  $edit = [
    'name' => $account
      ->getAccountName(),
    'pass' => $account->pass_raw,
  ];
  $this
    ->drupalPostForm('user/login', $edit, t('Log in'));

  // @see WebTestBase::drupalUserIsLoggedIn()
  if (isset($this->sessionId)) {
    $account->session_id = $this->sessionId;
  }
  $pass = $this
    ->assert($this
    ->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', [
    '%name' => $account
      ->getAccountName(),
  ]), 'User login');
  if ($pass) {
    $this->loggedInUser = $account;
    $this->container
      ->get('current_user')
      ->setAccount($account);
  }
}