DatabaseSessionTest.php 4.3 KB

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