Skip to content
Snippets Groups Projects
Commit 4a75f92b authored by Dries Buytaert's avatar Dries Buytaert
Browse files

Issue #1894692 by sun: allow to identify whether a user is logged using an API

parent 431801a5
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Contains \Drupal\simpletest\Tests\UserLoginTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests User related helper methods of WebTestBase.
*/
class UserHelpersTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User helper methods',
'description' => 'Tests User related helper methods of WebTestBase.',
'group' => 'SimpleTest',
);
}
/**
* Tests WebTestBase::drupalUserIsLoggedIn().
*/
function testDrupalUserIsLoggedIn() {
$first_user = $this->drupalCreateUser();
$second_user = $this->drupalCreateUser();
// After logging in, the first user should be logged in, the second not.
$this->drupalLogin($first_user);
$this->assertTrue($this->drupalUserIsLoggedIn($first_user));
$this->assertFalse($this->drupalUserIsLoggedIn($second_user));
// Verify that logged in state is retained across pages.
$this->drupalGet('');
$this->assertTrue($this->drupalUserIsLoggedIn($first_user));
$this->assertFalse($this->drupalUserIsLoggedIn($second_user));
// After logging out, both users should be logged out.
$this->drupalLogout();
$this->assertFalse($this->drupalUserIsLoggedIn($first_user));
$this->assertFalse($this->drupalUserIsLoggedIn($second_user));
// After logging back in, the second user should still be logged out.
$this->drupalLogin($first_user);
$this->assertTrue($this->drupalUserIsLoggedIn($first_user));
$this->assertFalse($this->drupalUserIsLoggedIn($second_user));
// After logging in the second user, the first one should be logged out.
$this->drupalLogin($second_user);
$this->assertTrue($this->drupalUserIsLoggedIn($second_user));
$this->assertFalse($this->drupalUserIsLoggedIn($first_user));
// After logging out, both should be logged out.
$this->drupalLogout();
$this->assertFalse($this->drupalUserIsLoggedIn($first_user));
$this->assertFalse($this->drupalUserIsLoggedIn($second_user));
}
}
......@@ -632,15 +632,30 @@ protected function drupalLogin($user) {
);
$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'));
// @see WebTestBase::drupalUserIsLoggedIn()
if (isset($this->session_id)) {
$user->session_id = $this->session_id;
}
$pass = $this->assert($this->drupalUserIsLoggedIn($user), format_string('User %name successfully logged in.', array('%name' => $user->name)), 'User login');
if ($pass) {
$this->loggedInUser = $user;
}
}
/**
* Returns whether a given user account is logged in.
*
* @param \Drupal\user\User $account
* The user account object to check.
*/
protected function drupalUserIsLoggedIn($account) {
if (!isset($account->session_id)) {
return FALSE;
}
// @see _drupal_session_read()
return (bool) db_query("SELECT sid FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(':sid' => $account->session_id))->fetchField();
}
/**
* Generate a token for the currently logged in user.
*/
......@@ -662,6 +677,8 @@ protected function drupalLogout() {
$pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout'));
if ($pass) {
// @see WebTestBase::drupalUserIsLoggedIn()
unset($this->loggedInUser->session_id);
$this->loggedInUser = FALSE;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment