DatabaseSessionTest.php 4.3 KB

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