DatabaseSessionTest.php 4.3 KB

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