session_api.test

Session API tests.

Classes

Namesort descending Description
SessionApiTestCase Session API test class.

File

session_api.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Session API tests.
  5. */
  6. /**
  7. * Session API test class.
  8. */
  9. class SessionApiTestCase extends DrupalWebTestCase {
  10. /**
  11. * Implement getInfo().
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Session API tests',
  16. 'description' => 'Tests various Session API functions',
  17. 'group' => 'Session API',
  18. );
  19. }
  20. /**
  21. * Implement setUp().
  22. */
  23. function setUp() {
  24. parent::setUp('session_api', 'session_api_test');
  25. }
  26. /**
  27. * Reset the cookie file so that it refers to the specified user.
  28. *
  29. * @param $uid User id to set as the active session.
  30. */
  31. function sessionReset($uid = 0) {
  32. // Close the internal browser.
  33. $this->curlClose();
  34. $this->loggedInUser = FALSE;
  35. // Change cookie file for user.
  36. $this->cookieFile = drupal_realpath('public://cookie.' . $uid . '.txt');
  37. $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
  38. $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
  39. $this->drupalGet('session-api-test');
  40. $this->assertResponse(200, t('Session API test module is correctly enabled.'), t('Session API'));
  41. }
  42. /**
  43. * Verify functions work properly.
  44. */
  45. function testFunctions() {
  46. module_load_include('module', 'session_api');
  47. // Capture existing values, which are restored at the end of this function.
  48. $old_cookie = $_COOKIE;
  49. // Test session_api_available() with cookies disabled.
  50. $_COOKIE = NULL;
  51. $this->assertFalse(session_api_available(), t('Function session_api_available() correctly returns FALSE when cookies are disabled.'));
  52. $this->assertFalse(session_api_get_sid(), t('Function session_api_get_sid() correctly returns FALSE when cookies are disabled.'));
  53. // Enable cookies.
  54. $_COOKIE = array('session_api_test' => 'test');
  55. // Check that the session_api_get_sid doesn't create a sid when the
  56. // caller don't want it to get created.
  57. unset($_COOKIE['session_api_session']);
  58. $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"));
  59. // Store ID in the db.
  60. $rec = new stdClass();
  61. $rec->session_id = drupal_hash_base64(uniqid(mt_rand(), TRUE));
  62. drupal_write_record('session_api', $rec);
  63. $_COOKIE['session_api_session'] = $rec->session_id;
  64. $this->assertEqual(session_api_get_sid(), $rec->sid, 'Function session_api_get_sid() correctly retrieves the session_api_id from the database.');
  65. // Initialize sessions.
  66. $this->sessionReset();
  67. $this->drupalGet('session-api-test');
  68. $sid = $this->drupalGetHeader('X-Session-Api-Sid');
  69. $session_id = $this->drupalGetHeader('X-Session-Api-Session-Id');
  70. $this->assertTrue(!empty($session_id), t('Session API test module properly initialized session api.'));
  71. $stored = db_query("SELECT session_id FROM {session_api} WHERE sid = :sid", array(':sid' => $sid))->fetchField();
  72. $this->assertEqual($stored, $session_id, t('session_api_get_sid() is properly storing the session_id.'));
  73. // Restore original cookie.
  74. $_COOKIE = $old_cookie;
  75. }
  76. }