DatabaseSessionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * DatabaseSessionTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Network\Session;
  18. use Cake\Core\Configure;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\Network\Session;
  21. use Cake\Network\Session\DatabaseSession;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * Database session test.
  27. */
  28. class DatabaseSessionTest extends TestCase
  29. {
  30. /**
  31. * fixtures
  32. *
  33. * @var string
  34. */
  35. public $fixtures = ['core.sessions'];
  36. /**
  37. * setUp
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. Configure::write('App.namespace', 'TestApp');
  45. $this->storage = new DatabaseSession();
  46. }
  47. /**
  48. * tearDown
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. unset($this->storage);
  55. TableRegistry::clear();
  56. parent::tearDown();
  57. }
  58. /**
  59. * test that constructor sets the right things up.
  60. *
  61. * @return void
  62. */
  63. public function testConstructionSettings()
  64. {
  65. TableRegistry::clear();
  66. new DatabaseSession();
  67. $session = TableRegistry::get('Sessions');
  68. $this->assertInstanceOf('Cake\ORM\Table', $session);
  69. $this->assertEquals('Sessions', $session->alias());
  70. $this->assertEquals(ConnectionManager::get('test'), $session->connection());
  71. $this->assertEquals('sessions', $session->table());
  72. }
  73. /**
  74. * test opening the session
  75. *
  76. * @return void
  77. */
  78. public function testOpen()
  79. {
  80. $this->assertTrue($this->storage->open(null, null));
  81. }
  82. /**
  83. * test write()
  84. *
  85. * @return void
  86. */
  87. public function testWrite()
  88. {
  89. $result = $this->storage->write('foo', 'Some value');
  90. $this->assertTrue($result);
  91. $expires = TableRegistry::get('Sessions')->get('foo')->expires;
  92. $expected = time() + ini_get('session.gc_maxlifetime');
  93. $this->assertWithinRange($expected, $expires, 1);
  94. }
  95. /**
  96. * testReadAndWriteWithDatabaseStorage method
  97. *
  98. * @return void
  99. */
  100. public function testWriteEmptySessionId()
  101. {
  102. $result = $this->storage->write('', 'This is a Test');
  103. $this->assertFalse($result);
  104. }
  105. /**
  106. * test read()
  107. *
  108. * @return void
  109. */
  110. public function testRead()
  111. {
  112. $this->storage->write('foo', 'Some value');
  113. $result = $this->storage->read('foo');
  114. $expected = 'Some value';
  115. $this->assertEquals($expected, $result);
  116. $result = $this->storage->read('made up value');
  117. $this->assertSame('', $result);
  118. }
  119. /**
  120. * test blowing up the session.
  121. *
  122. * @return void
  123. */
  124. public function testDestroy()
  125. {
  126. $this->storage->write('foo', 'Some value');
  127. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  128. $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
  129. $this->assertTrue($this->storage->destroy('foo'), 'Destroy should always return true');
  130. }
  131. /**
  132. * test the garbage collector
  133. *
  134. * @return void
  135. */
  136. public function testGc()
  137. {
  138. TableRegistry::clear();
  139. ini_set('session.gc_maxlifetime', 0);
  140. $storage = new DatabaseSession();
  141. $storage->write('foo', 'Some value');
  142. sleep(1);
  143. $storage->gc(0);
  144. $this->assertSame('', $storage->read('foo'));
  145. }
  146. /**
  147. * Tests serializing an entity
  148. *
  149. * @return void
  150. */
  151. public function testSerializeEntity()
  152. {
  153. $entity = new Entity();
  154. $entity->value = 'something';
  155. $result = $this->storage->write('key', serialize($entity));
  156. $data = TableRegistry::get('Sessions')->get('key')->data;
  157. $this->assertEquals(serialize($entity), stream_get_contents($data));
  158. }
  159. }