diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/UserHelpersTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/UserHelpersTest.php new file mode 100644 index 0000000000000000000000000000000000000000..269e00979d509660493046f6273850d0dc44ced1 --- /dev/null +++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/UserHelpersTest.php @@ -0,0 +1,63 @@ +<?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)); + } + +} diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 92c6ae5b6118a01da1c2392a468da946ee5783ec..1bf89497c8b8a8faff849f7120f420f8aee1452d 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -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; } }