DatabaseSessionTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * DatabaseSessionTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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\Table;
  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. $expected = [
  92. 'id' => 'foo',
  93. 'data' => 'Some value',
  94. ];
  95. $expires = $result['expires'];
  96. unset($result['expires']);
  97. $this->assertEquals($expected, $result);
  98. $expected = time() + ini_get('session.gc_maxlifetime');
  99. $this->assertWithinRange($expected, $expires, 1);
  100. }
  101. /**
  102. * testReadAndWriteWithDatabaseStorage method
  103. *
  104. * @return void
  105. */
  106. public function testWriteEmptySessionId()
  107. {
  108. $result = $this->storage->write('', 'This is a Test');
  109. $this->assertFalse($result);
  110. }
  111. /**
  112. * test read()
  113. *
  114. * @return void
  115. */
  116. public function testRead()
  117. {
  118. $this->storage->write('foo', 'Some value');
  119. $result = $this->storage->read('foo');
  120. $expected = 'Some value';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->storage->read('made up value');
  123. $this->assertFalse($result);
  124. }
  125. /**
  126. * test blowing up the session.
  127. *
  128. * @return void
  129. */
  130. public function testDestroy()
  131. {
  132. $this->storage->write('foo', 'Some value');
  133. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  134. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  135. }
  136. /**
  137. * test the garbage collector
  138. *
  139. * @return void
  140. */
  141. public function testGc()
  142. {
  143. TableRegistry::clear();
  144. ini_set('session.gc_maxlifetime', 0);
  145. $storage = new DatabaseSession();
  146. $storage->write('foo', 'Some value');
  147. sleep(1);
  148. $storage->gc(0);
  149. $this->assertFalse($storage->read('foo'));
  150. }
  151. }