DatabaseSessionTest.php 4.2 KB

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