protected function WebTestBase::drupalLogin in SimpleTest 8.3
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()
6 calls to WebTestBase::drupalLogin()
- BrokenSetUpTest::setUp in src/
Tests/ BrokenSetUpTest.php - Sets up a Drupal site for running functional and integration tests.
- MissingCheckedRequirementsTest::setUp in src/
Tests/ MissingCheckedRequirementsTest.php - Sets up a Drupal site for running functional and integration tests.
- SimpleTestBrowserTest::setUp in src/
Tests/ SimpleTestBrowserTest.php - Sets up a Drupal site for running functional and integration tests.
- SimpleTestBrowserTest::testInternalBrowser in src/
Tests/ SimpleTestBrowserTest.php - Test the internal browsers functionality.
- SimpleTestTest::setUp in src/
Tests/ SimpleTestTest.php - Sets up a Drupal site for running functional and integration tests.
File
- src/
WebTestBase.php, line 289
Class
- WebTestBase
- Test case for typical Drupal tests.
Namespace
Drupal\simpletestCode
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);
}
}