You are here

function SessionApiTestCase::testFunctions in Session API 6

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

Verify functions.

File

./session_api.test, line 28
Session API SimpleTests.

Class

SessionApiTestCase
Session API SimpleTest 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;

  // session_api_available();
  $_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 = $old_cookie;

  // 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_session']);
  $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();
  $data = uniqid(mt_rand(), true);
  $hash = base64_encode(hash('sha256', $data, TRUE));
  $hash = strtr($hash, array(
    '+' => '-',
    '/' => '_',
    '=' => '',
  ));
  $rec->session_id = $hash;
  drupal_write_record('session_api', $rec);
  $_COOKIE['session_api_session'] = $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.' . $sid);

  // Restore pre-testing values.
  $_COOKIE = $old_cookie;
  db_query("DELETE FROM {session_api} WHERE session_id = %d", $rec->session_id);
}