- <?php
- * @file
- * Provides SimpleTests for core session handling functionality.
- */
-
- class SessionTestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('session_test');
-
-
- config_set('system.core', 'cache', '0');
- }
-
-
- * Tests for backdrop_save_session() and backdrop_session_regenerate().
- */
- function testSessionSaveRegenerate() {
- $this->assertFalse(backdrop_save_session(), 'backdrop_save_session() correctly returns FALSE (inside of testing framework) when initially called with no arguments.', 'Session');
- $this->assertFalse(backdrop_save_session(FALSE), 'backdrop_save_session() correctly returns FALSE when called with FALSE.', 'Session');
- $this->assertFalse(backdrop_save_session(), 'backdrop_save_session() correctly returns FALSE when saving has been disabled.', 'Session');
- $this->assertTrue(backdrop_save_session(TRUE), 'backdrop_save_session() correctly returns TRUE when called with TRUE.', 'Session');
- $this->assertTrue(backdrop_save_session(), 'backdrop_save_session() correctly returns TRUE when saving has been enabled.', 'Session');
-
-
- $user = $this->backdropCreateUser(array('access content'));
-
-
- $this->sessionReset($user->uid);
-
-
- $this->backdropLogin($user);
- $this->assertTrue(preg_match('/HttpOnly/i', $this->backdropGetHeader('Set-Cookie', TRUE)), 'Session cookie is set as HttpOnly.');
- $this->backdropLogout();
-
-
-
- $user->name = 'session_test_user';
- $user->save();
- $this->backdropGet('session-test/id');
- $matches = array();
- preg_match('/\s*session_id:(.*)\n/', $this->backdropGetContent(), $matches);
- $this->assertTrue(!empty($matches[1]) , 'Found session ID before logging in.');
- $original_session = $matches[1];
-
-
-
- $edit = array(
- 'name' => $user->name,
- 'pass' => $user->pass_raw
- );
- $this->backdropPost('user/login', $edit, t('Log in'));
- $this->backdropGet('user');
- $pass = $this->assertText($user->name, format_string('Found name: %name', array('%name' => $user->name)), 'User login');
-
- $this->backdropGet('session-test/id');
- $matches = array();
- preg_match('/\s*session_id:(.*)\n/', $this->backdropGetContent(), $matches);
- $this->assertTrue(!empty($matches[1]) , 'Found session ID after logging in.');
- $this->assertTrue($matches[1] != $original_session, 'Session ID changed after login.');
- }
-
-
- * Test data persistence via the session_test module callbacks.
- */
- function testDataPersistence() {
- $user = $this->backdropCreateUser(array('access content'));
-
- $this->sessionReset($user->uid);
-
- $this->backdropLogin($user);
-
- $value_1 = $this->randomName();
- $this->backdropGet('session-test/set/' . $value_1);
- $this->assertText($value_1, 'The session value was stored.', 'Session');
- $this->backdropGet('session-test/get');
- $this->assertText($value_1, 'Session correctly returned the stored data for an authenticated user.', 'Session');
-
-
-
- $value_2 = $this->randomName();
- $this->backdropGet('session-test/no-set/' . $value_2);
- $this->assertText($value_2, 'The session value was correctly passed to session-test/no-set.', 'Session');
- $this->backdropGet('session-test/get');
- $this->assertText($value_1, 'Session data is not saved for backdrop_save_session(FALSE).', 'Session');
-
-
- $this->sessionReset();
- $this->sessionReset($user->uid);
- $this->assertText($value_1, 'Session data persists through browser close.', 'Session');
-
-
- $this->backdropLogout();
- $this->sessionReset();
- $this->backdropGet('session-test/get');
- $this->assertNoText($value_1, "After logout, previous user's session data is not available.", 'Session');
-
-
- $value_3 = $this->randomName();
- $this->backdropGet('session-test/set/' . $value_3);
- $this->assertText($value_3, 'Session data stored for anonymous user.', 'Session');
- $this->backdropGet('session-test/get');
- $this->assertText($value_3, 'Session correctly returned the stored data for an anonymous user.', 'Session');
-
-
- $value_4 = $this->randomName();
- $this->backdropGet('session-test/no-set/' . $value_4);
- $this->assertText($value_4, 'The session value was correctly passed to session-test/no-set.', 'Session');
- $this->backdropGet('session-test/get');
- $this->assertText($value_3, 'Session data is not saved for backdrop_save_session(FALSE).', 'Session');
-
-
- $this->backdropLogin($user);
- $this->sessionReset($user->uid);
- $this->backdropGet('session-test/get');
- $this->assertNoText($value_1, 'Session has persisted for an authenticated user after logging out and then back in.', 'Session');
-
-
- $user2 = $this->backdropCreateUser(array('access content'));
- $this->sessionReset($user2->uid);
- $this->backdropLogin($user2);
- }
-
-
- * Test that empty anonymous sessions are destroyed.
- */
- function testEmptyAnonymousSession() {
-
- $this->backdropGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
-
-
- config_set('system.core', 'cache', 1);
- $this->backdropGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'MISS', 'Page was not cached.');
-
-
- $this->backdropGet('session-test/set-message');
- $this->assertSessionCookie(TRUE);
- $this->assertTrue($this->backdropGetHeader('Set-Cookie'), 'New session was started.');
-
-
-
- $this->backdropGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(FALSE);
- $this->assertFalse($this->backdropGetHeader('X-Backdrop-Cache'), 'Caching was bypassed.');
- $this->assertText(t('This is a dummy message.'), 'Message was displayed.');
- $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->backdropGetHeader('Set-Cookie')), 'Session cookie was deleted.');
-
-
- $this->backdropGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertNoText(t('This is a dummy message.'), 'Message was not cached.');
- $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
- $this->assertFalse($this->backdropGetHeader('Set-Cookie'), 'New session was not started.');
-
-
- $this->backdropGet('session-test/set-message-but-do-not-save');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
-
-
- $this->backdropGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertNoText(t('This is a dummy message.'), 'The message was not saved.');
- }
-
-
- * Test that sessions are only saved when necessary.
- */
- function testSessionWrite() {
- $user = $this->backdropCreateUser(array('access content'));
- $this->backdropLogin($user);
-
- $sql = 'SELECT u.access, s.timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE u.uid = :uid';
- $times1 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
-
-
-
-
-
- sleep(1);
- $this->backdropGet('session-test/set/foo');
- $times2 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times2->access, $times1->access, 'Users table was not updated.');
- $this->assertNotEqual($times2->timestamp, $times1->timestamp, 'Sessions table was updated.');
-
-
- sleep(1);
- $this->backdropGet('session-test/set/foo');
- $times3 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times3->access, $times1->access, 'Users table was not updated.');
- $this->assertEqual($times3->timestamp, $times2->timestamp, 'Sessions table was not updated.');
-
-
- sleep(1);
- $this->backdropGet('');
- $times4 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times4->access, $times3->access, 'Users table was not updated.');
- $this->assertEqual($times4->timestamp, $times3->timestamp, 'Sessions table was not updated.');
-
-
- tempstore_set('simpletest', 'settings', array(
- 'session_write_interval' => 0,
- ), REQUEST_TIME + 3600);
- $this->backdropGet('');
- $times5 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertNotEqual($times5->access, $times4->access, 'Users table was updated.');
- $this->assertNotEqual($times5->timestamp, $times4->timestamp, 'Sessions table was updated.');
- }
-
-
- * Test that empty session IDs are not allowed.
- */
- function testEmptySessionID() {
- $user = $this->backdropCreateUser(array('access content'));
- $this->backdropLogin($user);
- $this->backdropGet('session-test/is-logged-in');
- $this->assertResponse(200, 'User is logged in.');
-
-
-
- db_query("UPDATE {sessions} SET sid = '' WHERE uid = :uid", array(':uid' => $user->uid));
-
-
-
- $this->curlClose();
- $this->additionalCurlOptions[CURLOPT_COOKIE] = rawurlencode($this->session_name) . '=;';
- $this->backdropGet('session-test/id-from-cookie');
- $this->assertRaw("session_id:\n", 'Session ID is blank as sent from cookie header.');
-
- $this->backdropGet('session-test/is-logged-in');
- $this->assertResponse(403, 'An empty session ID is not allowed.');
- }
-
-
- * Reset the cookie file so that it refers to the specified user.
- *
- * @param $uid User id to set as the active session.
- */
- function sessionReset($uid = 0) {
-
- $this->curlClose();
- $this->loggedInUser = FALSE;
-
-
- $this->cookieFile = file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath() . '/cookie.' . $uid . '.txt';
- $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
- $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
- $this->backdropGet('session-test/get');
- $this->assertResponse(200, 'Session test module is correctly enabled.', 'Session');
- }
-
-
- * Assert whether the SimpleTest browser sent a session cookie.
- */
- function assertSessionCookie($sent) {
- if ($sent) {
- $this->assertNotNull($this->session_id, 'Session cookie was sent.');
- }
- else {
- $this->assertNull($this->session_id, 'Session cookie was not sent.');
- }
- }
-
-
- * Assert whether $_SESSION is empty at the beginning of the request.
- */
- function assertSessionEmpty($empty) {
- if ($empty) {
- $empty = $this->backdropGetHeader('X-Session-Empty');
- $this->assertTrue($empty === FALSE || $empty === '1', 'Session was empty.');
- }
- else {
- $this->assertIdentical($this->backdropGetHeader('X-Session-Empty'), '0', 'Session was not empty.');
- }
- }
- }
-
- * Ensure that when running under HTTPS two session cookies are generated.
- */
- class SessionHttpsTestCase extends BackdropWebTestCase {
- public function setUp() {
- parent::setUp('session_test');
- }
-
- protected function testHttpsSession() {
- global $is_https;
-
- if ($is_https) {
- $secure_session_name = session_name();
- $insecure_session_name = substr(session_name(), 1);
- }
- else {
- $secure_session_name = 'S' . session_name();
- $insecure_session_name = session_name();
- }
-
- $user = $this->backdropCreateUser(array('access administration pages'));
-
-
-
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user/login');
- $edit = array('name' => $user->name, 'pass' => $user->pass_raw);
- $this->backdropPost(NULL, $edit, t('Log in'));
-
-
- $this->curlClose();
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user/login');
- $this->backdropPost(NULL, $edit, t('Log in'));
-
-
- $this->assertTrue($this->cookies[$secure_session_name]['secure'], 'The secure cookie has the secure attribute');
-
- $this->assertFalse(isset($this->cookies[$insecure_session_name]));
- $ssid = $this->cookies[$secure_session_name]['value'];
- $this->assertSessionIds($ssid, $ssid, 'Session has a non-empty SID and a correct secure SID.');
- $cookie = $secure_session_name . '=' . $ssid;
-
-
- $this->curlClose();
- $this->backdropGet($this->httpsUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertText(t('Configuration'));
- $this->assertResponse(200);
-
-
- $this->curlClose();
- $this->backdropGet($this->httpUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertNoText(t('Configuration'));
- $this->assertResponse(403);
-
-
- $this->curlClose();
- $cookie = $insecure_session_name . '=';
- $this->backdropGet($this->httpUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertResponse(403);
-
-
-
-
- $this->curlClose();
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpUrl('user/login');
- $edit = array('name' => $user->name, 'pass' => $user->pass_raw);
- $this->backdropPost(NULL, $edit, t('Log in'));
- $this->backdropGet($this->httpUrl('admin/config'));
- $this->assertResponse(200);
- $sid = $this->cookies[$insecure_session_name]['value'];
- $this->assertSessionIds($sid, '', 'Session has the correct SID and an empty secure SID.');
-
-
- $this->curlClose();
- $cookie = $secure_session_name . '=';
- $this->backdropGet($this->httpsUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertResponse(403);
-
-
- $this->cookies = array();
-
- if ($is_https) {
-
- return;
- }
-
-
- state_set('session_test_https', TRUE);
-
- $this->curlClose();
-
- $session_data = $this->randomName();
- $this->backdropGet('session-test/set/' . $session_data);
-
- $this->assertFalse(isset($this->cookies[$secure_session_name]), 'The secure cookie is not sent on insecure pages.');
-
- $this->assertFalse($this->cookies[$insecure_session_name]['secure'], 'The insecure cookie does not have the secure attribute');
-
-
-
- $anonymous_cookie = $insecure_session_name . '=' . $this->cookies[$insecure_session_name]['value'];
-
-
- $this->backdropGet('user/password');
- $form = $this->xpath('//form[@id="user-pass"]');
- $this->assertNotEqual(substr($form[0]['action'], 0, 6), 'https:', 'Password request form action is not secure');
- $form[0]['action'] = $this->httpsUrl('user/login');
-
-
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $this->assertEqual(substr($form[0]['action'], 0, 6), 'https:', 'Login form action is secure');
- $form[0]['action'] = $this->httpsUrl('user/login');
-
- $edit = array(
- 'name' => $user->name,
- 'pass' => $user->pass_raw,
- );
- $this->backdropPost(NULL, $edit, t('Log in'));
-
- $this->assertTrue($this->cookies[$secure_session_name]['secure'], 'The secure cookie has the secure attribute');
-
- $this->assertFalse($this->cookies[$insecure_session_name]['secure'], 'The insecure cookie does not have the secure attribute');
-
- $sid = $this->cookies[$insecure_session_name]['value'];
- $ssid = $this->cookies[$secure_session_name]['value'];
- $this->assertSessionIds($sid, $ssid, 'Session has both secure and insecure SIDs');
- $cookies = array(
- $insecure_session_name . '=' . $sid,
- $secure_session_name . '=' . $ssid,
- );
-
-
-
- $this->backdropGet('session-test/get');
- $this->assertText($session_data, 'Session correctly returned the stored data set by the anonymous session.');
-
- foreach ($cookies as $cookie_key => $cookie) {
- foreach (array('admin/config', $this->httpsUrl('admin/config')) as $url_key => $url) {
- $this->curlClose();
-
- $this->backdropGet($url, array(), array('Cookie: ' . $cookie));
- if ($cookie_key == $url_key) {
- $this->assertText(t('Configuration'));
- $this->assertResponse(200);
- }
- else {
- $this->assertNoText(t('Configuration'));
- $this->assertResponse(403);
- }
- }
- }
-
-
-
- $this->cookies = array();
- $this->backdropGet('session-test/get', array('Cookie: ' . $anonymous_cookie));
- $this->assertNoText($session_data, 'Initial anonymous session is inactive after login.');
-
-
- $this->cookies = array();
-
-
- $this->backdropGet($this->httpsUrl('session-test/set/1'));
-
-
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user/login');
- $this->backdropPost(NULL, $edit, t('Log in'));
-
-
- $this->backdropGet("user/{$user->uid}/edit");
- $this->assertResponse(200);
- }
-
-
- * Tests that empty session IDs do not cause unrelated sessions to load.
- */
- public function testEmptySessionId() {
- global $is_https;
-
- if ($is_https) {
- $secure_session_name = session_name();
- }
- else {
- $secure_session_name = 'S' . session_name();
- }
-
- session_test_boot();
-
- $admin_user = $this->backdropCreateUser(array('access administration pages'));
- $standard_user = $this->backdropCreateUser(array('access content'));
-
-
-
-
- $edit = array(
- 'name' => $admin_user->name,
- 'pass' => $admin_user->pass_raw
- );
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpUrl('user/login');
- $this->backdropPost(NULL, $edit, t('Log in'));
-
- $this->curlClose();
-
-
- $edit = array(
- 'name' => $standard_user->name,
- 'pass' => $standard_user->pass_raw
- );
- $this->backdropGet('user/login');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user/login');
- $this->backdropPost(NULL, $edit, t('Log in'));
-
-
- curl_setopt($this->curlHandle, CURLOPT_COOKIE, "$secure_session_name=");
- $this->backdropGet($this->httpsUrl('user/login'));
- $this->assertNoText($admin_user->name, 'User is not logged in as admin');
- $this->assertNoText($standard_user->name, "The user's own name is not displayed because the invalid session cookie has logged them out.");
- }
-
-
- * Test that there exists a session with two specific session IDs.
- *
- * @param $sid
- * The insecure session ID to search for.
- * @param $ssid
- * The secure session ID to search for.
- * @param $assertion_text
- * The text to display when we perform the assertion.
- *
- * @return
- * The result of assertTrue() that there's a session in the system that
- * has the given insecure and secure session IDs.
- */
- protected function assertSessionIds($sid, $ssid, $assertion_text) {
- $args = array(
- ':sid' => $sid,
- ':ssid' => $ssid,
- );
- return $this->assertTrue(db_query('SELECT timestamp FROM {sessions} WHERE sid = :sid AND ssid = :ssid', $args)->fetchField(), $assertion_text);
- }
-
-
- * Builds a URL for submitting a mock HTTPS request to HTTP test environments.
- *
- * @param $url
- * A Backdrop path such as 'user/login'.
- *
- * @return
- * An absolute URL.
- */
- protected function httpsUrl($url) {
- global $base_url;
- return $base_url . '/core/modules/simpletest/tests/https.php?q=' . $url;
- }
-
-
- * Builds a URL for submitting a mock HTTP request to HTTPS test environments.
- *
- * @param $url
- * A Backdrop path such as 'user/login'.
- *
- * @return
- * An absolute URL.
- */
- protected function httpUrl($url) {
- global $base_url;
- return $base_url . '/core/modules/simpletest/tests/http.php?q=' . $url;
- }
- }