DatabaseSessionTest.php 4.3 KB

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