1 backdrop_web_test_case.php protected BackdropWebTestCase::backdropLogin($account, $by_email = FALSE)

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 backdropCreateUser()), e.g. to log in the same user again, then it must be re-assigned manually. For example:

  // Create a user.
  $account = $this->backdropCreateUser(array());
  $this->backdropLogin($account);
  // Load real user object.
  $pass_raw = $account->pass_raw;
  $account = user_load($account->uid);
  $account->pass_raw = $pass_raw;

Parameters

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

$by_email: Whether to use email for login instead of username.

See also

backdropCreateUser()

File

core/modules/simpletest/backdrop_web_test_case.php, line 1443

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function backdropLogin($account, $by_email = FALSE) {
  global $user;
  if ($this->loggedInUser) {
    $this->backdropLogout();
  }
  // Sometimes $this->loggedInUser is FALSE even when user is logged in.
  elseif ($user->uid) {
    $this->backdropLogout();
  }

  $edit = array(
    'name' => $by_email ? $account->mail : $account->name,
    'pass' => $account->pass_raw
  );
  $this->backdropPost('user/login', $edit, t('Log in'));

  // Check for the logged-in class.
  $result = $this->xpath('/html/body[contains(@class, "logged-in")]');
  $pass = $this->assertEqual(count($result), 1, t('User %name successfully logged in.', array('%name' => $account->name)), t('User login'));

  if ($pass) {
    $this->loggedInUser = $account;
  }
}