DatabaseSessionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * DatabaseSessionTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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. 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. )
  113. );
  114. $expires = $result['Session']['expires'];
  115. unset($result['Session']['expires']);
  116. $this->assertEquals($expected, $result);
  117. $expected = time() + (Configure::read('Session.timeout') * 60);
  118. $this->assertWithinMargin($expires, $expected, 1);
  119. }
  120. /**
  121. * testReadAndWriteWithDatabaseStorage method
  122. *
  123. * @return void
  124. */
  125. public function testWriteEmptySessionId() {
  126. $result = $this->storage->write('', 'This is a Test');
  127. $this->assertFalse($result);
  128. }
  129. /**
  130. * test read()
  131. *
  132. * @return void
  133. */
  134. public function testRead() {
  135. $this->storage->write('foo', 'Some value');
  136. $result = $this->storage->read('foo');
  137. $expected = 'Some value';
  138. $this->assertEquals($expected, $result);
  139. $result = $this->storage->read('made up value');
  140. $this->assertFalse($result);
  141. }
  142. /**
  143. * test blowing up the session.
  144. *
  145. * @return void
  146. */
  147. public function testDestroy() {
  148. $this->storage->write('foo', 'Some value');
  149. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  150. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  151. }
  152. /**
  153. * test the garbage collector
  154. *
  155. * @return void
  156. */
  157. public function testGc() {
  158. ClassRegistry::flush();
  159. Configure::write('Session.timeout', 0);
  160. $storage = new DatabaseSession();
  161. $storage->write('foo', 'Some value');
  162. sleep(1);
  163. $storage->gc();
  164. $this->assertFalse($storage->read('foo'));
  165. }
  166. }