You are here

protected function WebTestBase::drupalLogin in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/WebTestBase.php \Drupal\simpletest\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 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()

731 calls to WebTestBase::drupalLogin()
AccessDeniedTest::testAccessDenied in core/modules/system/src/Tests/System/AccessDeniedTest.php
AccessRoleTest::testAccessRole in core/modules/user/src/Tests/Views/AccessRoleTest.php
Tests role access plugin.
ActionUninstallTest::testActionUninstall in core/modules/action/src/Tests/ActionUninstallTest.php
Tests Action uninstall.
AdminPathEntityConverterLanguageTest::setUp in core/modules/language/src/Tests/AdminPathEntityConverterLanguageTest.php
Sets up a Drupal site for running functional and integration tests.
AdminTest::setUp in core/modules/system/src/Tests/System/AdminTest.php
Sets up a Drupal site for running functional and integration tests.

... See full list

File

core/modules/simpletest/src/WebTestBase.php, line 600
Contains \Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function drupalLogin(AccountInterface $account) {
  if ($this->loggedInUser) {
    $this
      ->drupalLogout();
  }
  $edit = array(
    'name' => $account
      ->getUsername(),
    '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), format_string('User %name successfully logged in.', array(
    '%name' => $account
      ->getUsername(),
  )), 'User login');
  if ($pass) {
    $this->loggedInUser = $account;
    $this->container
      ->get('current_user')
      ->setAccount($account);
  }
}