DatabaseSessionTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * DatabaseSessionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Model.Datasource.Session
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('CakeSession', 'Model/Datasource');
  21. App::uses('DatabaseSession', 'Model/Datasource/Session');
  22. class_exists('CakeSession');
  23. class SessionTestModel extends Model {
  24. public $name = 'SessionTestModel';
  25. public $useTable = 'sessions';
  26. }
  27. /**
  28. * Database session test.
  29. *
  30. * @package Cake.Test.Case.Model.Datasource.Session
  31. */
  32. class DatabaseSessionTest extends CakeTestCase {
  33. protected static $_sessionBackup;
  34. /**
  35. * fixtures
  36. *
  37. * @var string
  38. */
  39. public $fixtures = array('core.session');
  40. /**
  41. * test case startup
  42. *
  43. * @return void
  44. */
  45. public static function setupBeforeClass() {
  46. self::$_sessionBackup = Configure::read('Session');
  47. Configure::write('Session.handler', array(
  48. 'model' => 'SessionTestModel',
  49. ));
  50. Configure::write('Session.timeout', 100);
  51. }
  52. /**
  53. * cleanup after test case.
  54. *
  55. * @return void
  56. */
  57. public static function teardownAfterClass() {
  58. Configure::write('Session', self::$_sessionBackup);
  59. }
  60. /**
  61. * setup
  62. *
  63. * @return void
  64. */
  65. public function setup() {
  66. $this->storage = new DatabaseSession();
  67. }
  68. /**
  69. * teardown
  70. *
  71. * @return void
  72. */
  73. public function teardown() {
  74. unset($this->storage);
  75. ClassRegistry::flush();
  76. }
  77. /**
  78. * test that constructor sets the right things up.
  79. *
  80. * @return void
  81. */
  82. public function testConstructionSettings() {
  83. ClassRegistry::flush();
  84. $storage = new DatabaseSession();
  85. $session = ClassRegistry::getObject('session');
  86. $this->assertInstanceOf('SessionTestModel', $session);
  87. $this->assertEquals('Session', $session->alias);
  88. $this->assertEquals('test', $session->useDbConfig);
  89. $this->assertEquals('sessions', $session->useTable);
  90. }
  91. /**
  92. * test opening the session
  93. *
  94. * @return void
  95. */
  96. public function testOpen() {
  97. $this->assertTrue($this->storage->open());
  98. }
  99. /**
  100. * test write()
  101. *
  102. * @return void
  103. */
  104. public function testWrite() {
  105. $result = $this->storage->write('foo', 'Some value');
  106. $expected = array(
  107. 'Session' => array(
  108. 'id' => 'foo',
  109. 'data' => 'Some value',
  110. 'expires' => time() + (Configure::read('Session.timeout') * 60)
  111. )
  112. );
  113. $this->assertEquals($expected, $result);
  114. }
  115. /**
  116. * testReadAndWriteWithDatabaseStorage method
  117. *
  118. * @return void
  119. */
  120. public function testWriteEmptySessionId() {
  121. $result = $this->storage->write('', 'This is a Test');
  122. $this->assertFalse($result);
  123. }
  124. /**
  125. * test read()
  126. *
  127. * @return void
  128. */
  129. public function testRead() {
  130. $this->storage->write('foo', 'Some value');
  131. $result = $this->storage->read('foo');
  132. $expected = 'Some value';
  133. $this->assertEquals($expected, $result);
  134. $result = $this->storage->read('made up value');
  135. $this->assertFalse($result);
  136. }
  137. /**
  138. * test blowing up the session.
  139. *
  140. * @return void
  141. */
  142. public function testDestroy() {
  143. $this->storage->write('foo', 'Some value');
  144. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  145. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  146. }
  147. /**
  148. * test the garbage collector
  149. *
  150. * @return void
  151. */
  152. public function testGc() {
  153. ClassRegistry::flush();
  154. Configure::write('Session.timeout', 0);
  155. $storage = new DatabaseSession();
  156. $storage->write('foo', 'Some value');
  157. sleep(1);
  158. $storage->gc();
  159. $this->assertFalse($storage->read('foo'));
  160. }
  161. }