DatabaseSessionTest.php 4.3 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. */
  29. class DatabaseSessionTest extends TestCase
  30. {
  31. /**
  32. * fixtures
  33. *
  34. * @var string
  35. */
  36. public $fixtures = ['core.sessions'];
  37. /**
  38. * setUp
  39. *
  40. * @return void
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. Configure::write('App.namespace', 'TestApp');
  46. $this->storage = new DatabaseSession();
  47. }
  48. /**
  49. * tearDown
  50. *
  51. * @return void
  52. */
  53. public function tearDown()
  54. {
  55. unset($this->storage);
  56. TableRegistry::clear();
  57. parent::tearDown();
  58. }
  59. /**
  60. * test that constructor sets the right things up.
  61. *
  62. * @return void
  63. */
  64. public function testConstructionSettings()
  65. {
  66. TableRegistry::clear();
  67. new DatabaseSession();
  68. $session = TableRegistry::get('Sessions');
  69. $this->assertInstanceOf('Cake\ORM\Table', $session);
  70. $this->assertEquals('Sessions', $session->alias());
  71. $this->assertEquals(ConnectionManager::get('test'), $session->connection());
  72. $this->assertEquals('sessions', $session->table());
  73. }
  74. /**
  75. * test opening the session
  76. *
  77. * @return void
  78. */
  79. public function testOpen()
  80. {
  81. $this->assertTrue($this->storage->open(null, null));
  82. }
  83. /**
  84. * test write()
  85. *
  86. * @return void
  87. */
  88. public function testWrite()
  89. {
  90. $result = $this->storage->write('foo', 'Some value');
  91. $this->assertTrue($result);
  92. $expires = TableRegistry::get('Sessions')->get('foo')->expires;
  93. $expected = time() + ini_get('session.gc_maxlifetime');
  94. $this->assertWithinRange($expected, $expires, 1);
  95. }
  96. /**
  97. * testReadAndWriteWithDatabaseStorage method
  98. *
  99. * @return void
  100. */
  101. public function testWriteEmptySessionId()
  102. {
  103. $result = $this->storage->write('', 'This is a Test');
  104. $this->assertFalse($result);
  105. }
  106. /**
  107. * test read()
  108. *
  109. * @return void
  110. */
  111. public function testRead()
  112. {
  113. $this->storage->write('foo', 'Some value');
  114. $result = $this->storage->read('foo');
  115. $expected = 'Some value';
  116. $this->assertEquals($expected, $result);
  117. $result = $this->storage->read('made up value');
  118. $this->assertSame('', $result);
  119. }
  120. /**
  121. * test blowing up the session.
  122. *
  123. * @return void
  124. */
  125. public function testDestroy()
  126. {
  127. $this->storage->write('foo', 'Some value');
  128. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  129. $this->assertSame('', $this->storage->read('foo'), 'Value still present.');
  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. }