You are here

function SessionApiTestCase::testFunctions in Session API 7

Same name and namespace in other branches
  1. 5 tests/session_api.test \SessionApiTestCase::testFunctions()
  2. 6 session_api.test \SessionApiTestCase::testFunctions()

Verify functions work properly.

File

./session_api.test, line 51
Session API tests.

Class

SessionApiTestCase
Session API test class.

Code

function testFunctions() {
  module_load_include('module', 'session_api');

  // Capture existing values, which are restored at the end of this function.
  $old_cookie = $_COOKIE;

  // Test session_api_available() with cookies disabled.
  $_COOKIE = NULL;
  $this
    ->assertFalse(session_api_available(), t('Function session_api_available() correctly returns FALSE when cookies are disabled.'));
  $this
    ->assertFalse(session_api_get_sid(), t('Function session_api_get_sid() correctly returns FALSE when cookies are disabled.'));

  // Enable cookies.
  $_COOKIE = array(
    'session_api_test' => 'test',
  );

  // Check that the session_api_get_sid doesn't create a sid when the
  // caller don't want it to get created.
  unset($_COOKIE[session_api_get_cookie_name()]);
  $this
    ->assertEqual(session_api_get_sid(FALSE), -1, t("The session_api_get_sid returns a negative value when the caller don't want to create a new session if it doesn't exist"));

  // Store ID in the db.
  $rec = new stdClass();
  $rec->session_id = drupal_hash_base64(uniqid(mt_rand(), TRUE));
  drupal_write_record('session_api', $rec);
  $_COOKIE[session_api_get_cookie_name()] = $rec->session_id;
  $this
    ->assertEqual(session_api_get_sid(), $rec->sid, 'Function session_api_get_sid() correctly retrieves the session_api_id from the database.');

  // Initialize sessions.
  $this
    ->sessionReset();
  $this
    ->drupalGet('session-api-test');
  $sid = $this
    ->drupalGetHeader('X-Session-Api-Sid');
  $session_id = $this
    ->drupalGetHeader('X-Session-Api-Session-Id');
  $this
    ->assertTrue(!empty($session_id), t('Session API test module properly initialized session api.'));
  $stored = db_query("SELECT session_id FROM {session_api} WHERE sid = :sid", array(
    ':sid' => $sid,
  ))
    ->fetchField();
  $this
    ->assertEqual($stored, $session_id, t('session_api_get_sid() is properly storing the session_id.'));

  // Restore original cookie.
  $_COOKIE = $old_cookie;
}