You are here

session_api.test in Session API 6

Same filename and directory in other branches
  1. 7 session_api.test

Session API SimpleTests.

File

session_api.test
View source
<?php

/**
 * @file
 * Session API SimpleTests.
 */

/**
 * Session API SimpleTest class
 */
class SessionApiTestCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Session API tests'),
      'description' => t('Tests various Session API functions'),
      'group' => t('Session API'),
    );
  }
  function setUp() {
    parent::setUp('session_api');
  }

  /**
   * Verify functions.
   */
  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);
  }

}

Classes

Namesort descending Description
SessionApiTestCase Session API SimpleTest class