function UiHelperTrait::drupalLogin

Same name in other branches
  1. 9 core/tests/Drupal/Tests/UiHelperTrait.php \Drupal\Tests\UiHelperTrait::drupalLogin()
  2. 8.9.x core/tests/Drupal/Tests/UiHelperTrait.php \Drupal\Tests\UiHelperTrait::drupalLogin()
  3. 10 core/tests/Drupal/Tests/UiHelperTrait.php \Drupal\Tests\UiHelperTrait::drupalLogin()

Logs in a user using the Mink controlled browser.

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

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([]);
$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.

See also

drupalCreateUser()

1322 calls to UiHelperTrait::drupalLogin()
AccessAnnouncementTest::testAnnounceFirstLogin in core/modules/announcements_feed/tests/src/FunctionalJavascript/AccessAnnouncementTest.php
Test of viewing announcements by a user with appropriate permission.
AccessAnnouncementTest::testAnnounceWithoutPermission in core/modules/announcements_feed/tests/src/FunctionalJavascript/AccessAnnouncementTest.php
Testing announce icon without announce permission.
AccessDeniedTest::testAccessDenied in core/modules/system/tests/src/Functional/System/AccessDeniedTest.php
AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied in core/modules/system/tests/src/Functional/System/AccessDeniedTest.php
Tests that an inaccessible custom 403 page falls back to the default.
AccessRoleTest::testAccessRole in core/modules/user/tests/src/Functional/Views/AccessRoleTest.php
Tests role access plugin.

... See full list

File

core/tests/Drupal/Tests/UiHelperTrait.php, line 159

Class

UiHelperTrait
Provides UI helper methods.

Namespace

Drupal\Tests

Code

protected function drupalLogin(AccountInterface $account) {
    if ($this->loggedInUser) {
        $this->drupalLogout();
    }
    if ($this->useOneTimeLoginLinks) {
        // Reload to get latest login timestamp.
        $storage = \Drupal::entityTypeManager()->getStorage('user');
        
        /** @var \Drupal\user\UserInterface $accountUnchanged */
        $accountUnchanged = $storage->loadUnchanged($account->id());
        $login = user_pass_reset_url($accountUnchanged) . '/login?destination=user/' . $account->id();
        $this->drupalGet($login);
    }
    else {
        $this->drupalGet(Url::fromRoute('user.login'));
        $this->submitForm([
            'name' => $account->getAccountName(),
            'pass' => $account->passRaw,
        ], 'Log in');
    }
    // @see ::drupalUserIsLoggedIn()
    $account->sessionId = $this->getSession()
        ->getCookie(\Drupal::service('session_configuration')->getOptions(\Drupal::request())['name']);
    $this->assertTrue($this->drupalUserIsLoggedIn($account), "User {$account->getAccountName()} successfully logged in.");
    $this->loggedInUser = $account;
    $this->container
        ->get('current_user')
        ->setAccount($account);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.