DatabaseSessionTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * DatabaseSessionTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * Database session test.
  28. *
  29. */
  30. class DatabaseSessionTest extends TestCase
  31. {
  32. /**
  33. * fixtures
  34. *
  35. * @var string
  36. */
  37. public $fixtures = ['core.sessions'];
  38. /**
  39. * setUp
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. Configure::write('App.namespace', 'TestApp');
  47. $this->storage = new DatabaseSession();
  48. }
  49. /**
  50. * tearDown
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. unset($this->storage);
  57. TableRegistry::clear();
  58. parent::tearDown();
  59. }
  60. /**
  61. * test that constructor sets the right things up.
  62. *
  63. * @return void
  64. */
  65. public function testConstructionSettings()
  66. {
  67. TableRegistry::clear();
  68. new DatabaseSession();
  69. $session = TableRegistry::get('Sessions');
  70. $this->assertInstanceOf('Cake\ORM\Table', $session);
  71. $this->assertEquals('Sessions', $session->alias());
  72. $this->assertEquals(ConnectionManager::get('test'), $session->connection());
  73. $this->assertEquals('sessions', $session->table());
  74. }
  75. /**
  76. * test opening the session
  77. *
  78. * @return void
  79. */
  80. public function testOpen()
  81. {
  82. $this->assertTrue($this->storage->open(null, null));
  83. }
  84. /**
  85. * test write()
  86. *
  87. * @return void
  88. */
  89. public function testWrite()
  90. {
  91. $result = $this->storage->write('foo', 'Some value');
  92. $this->assertTrue($result);
  93. $expires = TableRegistry::get('Sessions')->get('foo')->expires;
  94. $expected = time() + ini_get('session.gc_maxlifetime');
  95. $this->assertWithinRange($expected, $expires, 1);
  96. }
  97. /**
  98. * testReadAndWriteWithDatabaseStorage method
  99. *
  100. * @return void
  101. */
  102. public function testWriteEmptySessionId()
  103. {
  104. $result = $this->storage->write('', 'This is a Test');
  105. $this->assertFalse($result);
  106. }
  107. /**
  108. * test read()
  109. *
  110. * @return void
  111. */
  112. public function testRead()
  113. {
  114. $this->storage->write('foo', 'Some value');
  115. $result = $this->storage->read('foo');
  116. $expected = 'Some value';
  117. $this->assertEquals($expected, $result);
  118. $result = $this->storage->read('made up value');
  119. $this->assertFalse($result);
  120. }
  121. /**
  122. * test blowing up the session.
  123. *
  124. * @return void
  125. */
  126. public function testDestroy()
  127. {
  128. $this->storage->write('foo', 'Some value');
  129. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  130. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  131. }
  132. /**
  133. * test the garbage collector
  134. *
  135. * @return void
  136. */
  137. public function testGc()
  138. {
  139. TableRegistry::clear();
  140. ini_set('session.gc_maxlifetime', 0);
  141. $storage = new DatabaseSession();
  142. $storage->write('foo', 'Some value');
  143. sleep(1);
  144. $storage->gc(0);
  145. $this->assertFalse($storage->read('foo'));
  146. }
  147. /**
  148. * Tests serializing an entity
  149. *
  150. * @return void
  151. */
  152. public function testSerializeEntity()
  153. {
  154. $entity = new Entity();
  155. $entity->value = 'something';
  156. $result = $this->storage->write('key', serialize($entity));
  157. $data = TableRegistry::get('Sessions')->get('key')->data;
  158. $this->assertEquals(serialize($entity), stream_get_contents($data));
  159. }
  160. }