DatabaseSessionTest.php 4.5 KB

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