You are here

protected function DrupalWebTestCase::drupalLogin in SimpleTest 7

Same name and namespace in other branches
  1. 6.2 drupal_web_test_case.php \DrupalWebTestCase::drupalLogin()
  2. 7.2 drupal_web_test_case.php \DrupalWebTestCase::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 login 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

$user: User object representing the user to login.

See also

drupalCreateUser()

19 calls to DrupalWebTestCase::drupalLogin()
ActionLoopTestCase::testActionLoop in tests/actions.test
Set up a loop with 10-50 recursions, and see if it aborts properly.
ActionsConfigurationTestCase::testActionConfiguration in tests/actions.test
Test the configuration of advanced actions through the administration interface.
BootstrapPageCacheTestCase::testConditionalRequests in tests/bootstrap.test
Test support for requests containing If-Modified-Since and If-None-Match headers.
BootstrapPageCacheTestCase::testPageCache in tests/bootstrap.test
Test cache headers.
FileSaveUploadTest::setUp in tests/file.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…

... See full list

File

./drupal_web_test_case.php, line 973

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

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

  // If a "log out" link appears on the page, it is almost certainly because
  // the login was successful.
  $pass = $this
    ->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array(
    '%name' => $user->name,
  )), t('User login'));
  if ($pass) {
    $this->loggedInUser = $user;
  }
}