DatabaseSessionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-2012, 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-2012, 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. parent::setUp();
  67. $this->storage = new DatabaseSession();
  68. }
  69. /**
  70. * tearDown
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. unset($this->storage);
  76. ClassRegistry::flush();
  77. parent::tearDown();
  78. }
  79. /**
  80. * test that constructor sets the right things up.
  81. *
  82. * @return void
  83. */
  84. public function testConstructionSettings() {
  85. ClassRegistry::flush();
  86. $storage = new DatabaseSession();
  87. $session = ClassRegistry::getObject('session');
  88. $this->assertInstanceOf('SessionTestModel', $session);
  89. $this->assertEquals('Session', $session->alias);
  90. $this->assertEquals('test', $session->useDbConfig);
  91. $this->assertEquals('sessions', $session->useTable);
  92. }
  93. /**
  94. * test opening the session
  95. *
  96. * @return void
  97. */
  98. public function testOpen() {
  99. $this->assertTrue($this->storage->open());
  100. }
  101. /**
  102. * test write()
  103. *
  104. * @return void
  105. */
  106. public function testWrite() {
  107. $result = $this->storage->write('foo', 'Some value');
  108. $expected = array(
  109. 'Session' => array(
  110. 'id' => 'foo',
  111. 'data' => 'Some value',
  112. 'expires' => time() + (Configure::read('Session.timeout') * 60)
  113. )
  114. );
  115. $this->assertEquals($expected, $result);
  116. }
  117. /**
  118. * testReadAndWriteWithDatabaseStorage method
  119. *
  120. * @return void
  121. */
  122. public function testWriteEmptySessionId() {
  123. $result = $this->storage->write('', 'This is a Test');
  124. $this->assertFalse($result);
  125. }
  126. /**
  127. * test read()
  128. *
  129. * @return void
  130. */
  131. public function testRead() {
  132. $this->storage->write('foo', 'Some value');
  133. $result = $this->storage->read('foo');
  134. $expected = 'Some value';
  135. $this->assertEquals($expected, $result);
  136. $result = $this->storage->read('made up value');
  137. $this->assertFalse($result);
  138. }
  139. /**
  140. * test blowing up the session.
  141. *
  142. * @return void
  143. */
  144. public function testDestroy() {
  145. $this->storage->write('foo', 'Some value');
  146. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  147. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  148. }
  149. /**
  150. * test the garbage collector
  151. *
  152. * @return void
  153. */
  154. public function testGc() {
  155. ClassRegistry::flush();
  156. Configure::write('Session.timeout', 0);
  157. $storage = new DatabaseSession();
  158. $storage->write('foo', 'Some value');
  159. sleep(1);
  160. $storage->gc();
  161. $this->assertFalse($storage->read('foo'));
  162. }
  163. }