DatabaseSessionTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * fixtures
  32. *
  33. * @var string
  34. */
  35. public $fixtures = ['core.session'];
  36. /**
  37. * setUp
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. Configure::write('App.namespace', 'TestApp');
  44. $this->storage = new DatabaseSession();
  45. }
  46. /**
  47. * tearDown
  48. *
  49. * @return void
  50. */
  51. public function tearDown() {
  52. unset($this->storage);
  53. TableRegistry::clear();
  54. parent::tearDown();
  55. }
  56. /**
  57. * test that constructor sets the right things up.
  58. *
  59. * @return void
  60. */
  61. public function testConstructionSettings() {
  62. TableRegistry::clear();
  63. new DatabaseSession();
  64. $session = TableRegistry::get('Sessions');
  65. $this->assertInstanceOf('Cake\ORM\Table', $session);
  66. $this->assertEquals('Sessions', $session->alias());
  67. $this->assertEquals(ConnectionManager::get('test'), $session->connection());
  68. $this->assertEquals('sessions', $session->table());
  69. }
  70. /**
  71. * test opening the session
  72. *
  73. * @return void
  74. */
  75. public function testOpen() {
  76. $this->assertTrue($this->storage->open(null, null));
  77. }
  78. /**
  79. * test write()
  80. *
  81. * @return void
  82. */
  83. public function testWrite() {
  84. $result = $this->storage->write('foo', 'Some value');
  85. $expected = [
  86. 'id' => 'foo',
  87. 'data' => 'Some value',
  88. ];
  89. $expires = $result['expires'];
  90. unset($result['expires']);
  91. $this->assertEquals($expected, $result);
  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. $result = $this->storage->write('', 'This is a Test');
  102. $this->assertFalse($result);
  103. }
  104. /**
  105. * test read()
  106. *
  107. * @return void
  108. */
  109. public function testRead() {
  110. $this->storage->write('foo', 'Some value');
  111. $result = $this->storage->read('foo');
  112. $expected = 'Some value';
  113. $this->assertEquals($expected, $result);
  114. $result = $this->storage->read('made up value');
  115. $this->assertFalse($result);
  116. }
  117. /**
  118. * test blowing up the session.
  119. *
  120. * @return void
  121. */
  122. public function testDestroy() {
  123. $this->storage->write('foo', 'Some value');
  124. $this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
  125. $this->assertFalse($this->storage->read('foo'), 'Value still present.');
  126. }
  127. /**
  128. * test the garbage collector
  129. *
  130. * @return void
  131. */
  132. public function testGc() {
  133. TableRegistry::clear();
  134. ini_set('session.gc_maxlifetime', 0);
  135. $storage = new DatabaseSession();
  136. $storage->write('foo', 'Some value');
  137. sleep(1);
  138. $storage->gc(0);
  139. $this->assertFalse($storage->read('foo'));
  140. }
  141. }