DatabaseSessionTest.php 4.0 KB

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