protected function WebTestBase::drupalLogin

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 global $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->uid);
$account->pass_raw = $pass_raw;

Parameters

\Drupal\user\UserInterface $account: User object representing the user to log in.

See also

drupalCreateUser()

438 calls to WebTestBase::drupalLogin()
AccessDeniedTest::testAccessDenied in drupal/core/modules/system/lib/Drupal/system/Tests/System/AccessDeniedTest.php
AdminTest::setUp in drupal/core/modules/system/lib/Drupal/system/Tests/System/AdminTest.php
Sets up a Drupal site for running functional and integration tests.
AdminTest::testAdminPages in drupal/core/modules/system/lib/Drupal/system/Tests/System/AdminTest.php
Tests output on administrative listing pages.
AggregatorRenderingTest::testBlockLinks in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorRenderingTest.php
Adds a feed block to the page and checks its links.
AggregatorTestBase::setUp in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorTestBase.php
Sets up a Drupal site for running functional and integration tests.

... See full list

1 method overrides WebTestBase::drupalLogin()
RESTTestBase::drupalLogin in drupal/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php
Overrides WebTestBase::drupalLogin().

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php, line 634
Definition of Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalLogin($account) {
  if ($this->loggedInUser) {
    $this
      ->drupalLogout();
  }
  $edit = array(
    'name' => $account->name,
    'pass' => $account->pass_raw,
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));

  // @see WebTestBase::drupalUserIsLoggedIn()
  if (isset($this->session_id)) {
    $account->session_id = $this->session_id;
  }
  $pass = $this
    ->assert($this
    ->drupalUserIsLoggedIn($account), format_string('User %name successfully logged in.', array(
    '%name' => $account->name,
  )), 'User login');
  if ($pass) {
    $this->loggedInUser = $account;
  }
}