DatabaseSessionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @var array
  30. */
  31. protected array $fixtures = ['core.Sessions'];
  32. /**
  33. * @var \Cake\Http\Session\DatabaseSession
  34. */
  35. protected $storage;
  36. /**
  37. * setUp
  38. */
  39. public function setUp(): void
  40. {
  41. parent::setUp();
  42. static::setAppNamespace();
  43. $this->storage = new DatabaseSession();
  44. }
  45. /**
  46. * tearDown
  47. */
  48. public function tearDown(): void
  49. {
  50. unset($this->storage);
  51. parent::tearDown();
  52. }
  53. /**
  54. * test that constructor sets the right things up.
  55. */
  56. public function testConstructionSettings(): void
  57. {
  58. $this->getTableLocator()->clear();
  59. new DatabaseSession();
  60. $session = $this->getTableLocator()->get('Sessions');
  61. $this->assertInstanceOf('Cake\ORM\Table', $session);
  62. $this->assertSame('Sessions', $session->getAlias());
  63. $this->assertEquals(ConnectionManager::get('test'), $session->getConnection());
  64. $this->assertSame('sessions', $session->getTable());
  65. }
  66. /**
  67. * test opening the session
  68. */
  69. public function testOpen(): void
  70. {
  71. $this->assertTrue($this->storage->open('', ''));
  72. }
  73. /**
  74. * test write()
  75. */
  76. public function testWrite(): void
  77. {
  78. $result = $this->storage->write('foo', 'Some value');
  79. $this->assertTrue($result);
  80. $expires = $this->getTableLocator()->get('Sessions')->get('foo')->expires;
  81. $expected = time() + ini_get('session.gc_maxlifetime');
  82. $this->assertWithinRange($expected, $expires, 1);
  83. }
  84. /**
  85. * testReadAndWriteWithDatabaseStorage method
  86. */
  87. public function testWriteEmptySessionId(): void
  88. {
  89. $result = $this->storage->write('', 'This is a Test');
  90. $this->assertFalse($result);
  91. }
  92. /**
  93. * test read()
  94. */
  95. public function testRead(): void
  96. {
  97. $this->storage->write('foo', 'Some value');
  98. $result = $this->storage->read('foo');
  99. $expected = 'Some value';
  100. $this->assertSame($expected, $result);
  101. $result = $this->storage->read('made up value');
  102. $this->assertSame('', $result);
  103. }
  104. /**
  105. * test blowing up the session.
  106. */
  107. public function testDestroy(): void
  108. {
  109. $this->assertTrue($this->storage->write('foo', 'Some value'));
  110. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  111. $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
  112. $this->assertTrue($this->storage->destroy('foo'), 'Destroy should always return true');
  113. }
  114. /**
  115. * test the garbage collector
  116. */
  117. public function testGc(): void
  118. {
  119. $this->getTableLocator()->clear();
  120. $storage = new DatabaseSession();
  121. $storage->setTimeout(0);
  122. $storage->write('foo', 'Some value');
  123. sleep(1);
  124. $storage->gc(0);
  125. $this->assertSame('', $storage->read('foo'));
  126. }
  127. /**
  128. * Tests serializing an entity
  129. */
  130. public function testSerializeEntity(): void
  131. {
  132. $entity = new Entity();
  133. $entity->value = 'something';
  134. $this->storage->write('key', serialize($entity));
  135. $data = $this->getTableLocator()->get('Sessions')->get('key')->data;
  136. $this->assertSame(serialize($entity), stream_get_contents($data));
  137. }
  138. }