SessionComponentTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\SessionComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Network\Request;
  21. use Cake\Network\Session;
  22. use Cake\Routing\DispatcherFactory;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * SessionComponentTest class
  27. *
  28. */
  29. class SessionComponentTest extends TestCase {
  30. protected static $_sessionBackup;
  31. /**
  32. * fixtures
  33. *
  34. * @var string
  35. */
  36. public $fixtures = array('core.session');
  37. /**
  38. * test case startup
  39. *
  40. * @return void
  41. */
  42. public static function setupBeforeClass() {
  43. DispatcherFactory::add('Routing');
  44. DispatcherFactory::add('ControllerFactory');
  45. }
  46. /**
  47. * cleanup after test case.
  48. *
  49. * @return void
  50. */
  51. public static function teardownAfterClass() {
  52. DispatcherFactory::clear();
  53. }
  54. /**
  55. * setUp method
  56. *
  57. * @return void
  58. */
  59. public function setUp() {
  60. parent::setUp();
  61. $_SESSION = [];
  62. Configure::write('App.namespace', 'TestApp');
  63. $controller = new Controller(new Request(['session' => new Session()]));
  64. $this->ComponentRegistry = new ComponentRegistry($controller);
  65. }
  66. /**
  67. * tearDown method
  68. *
  69. * @return void
  70. */
  71. public function tearDown() {
  72. parent::tearDown();
  73. }
  74. /**
  75. * ensure that session ids don't change when request action is called.
  76. *
  77. * @return void
  78. */
  79. public function testSessionIdConsistentAcrossRequestAction() {
  80. Configure::write('App.namespace', 'TestApp');
  81. Router::connect('/session_test/:action', ['controller' => 'SessionTest']);
  82. Router::connect('/orange_session_test/:action', ['controller' => 'OrangeSessionTest']);
  83. $Controller = new Controller();
  84. $Session = new SessionComponent($this->ComponentRegistry);
  85. $expected = $Session->id();
  86. $result = $Controller->requestAction('/session_test/session_id');
  87. $this->assertEquals($expected, $result);
  88. $result = $Controller->requestAction('/orange_session_test/session_id');
  89. $this->assertEquals($expected, $result);
  90. }
  91. /**
  92. * testSessionReadWrite method
  93. *
  94. * @return void
  95. */
  96. public function testSessionReadWrite() {
  97. $Session = new SessionComponent($this->ComponentRegistry);
  98. $this->assertNull($Session->read('Test'));
  99. $Session->write('Test', 'some value');
  100. $this->assertEquals('some value', $Session->read('Test'));
  101. $Session->delete('Test');
  102. $Session->write('Test.key.path', 'some value');
  103. $this->assertEquals('some value', $Session->read('Test.key.path'));
  104. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  105. $Session->write('Test.key.path2', 'another value');
  106. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  107. $Session->delete('Test');
  108. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  109. $Session->write('Test', $array);
  110. $this->assertEquals($Session->read('Test'), $array);
  111. $Session->delete('Test');
  112. $Session->write(array('Test'), 'some value');
  113. $Session->write(array('Test' => 'some value'));
  114. $this->assertEquals('some value', $Session->read('Test'));
  115. $Session->delete('Test');
  116. }
  117. /**
  118. * testSessionDelete method
  119. *
  120. * @return void
  121. */
  122. public function testSessionDelete() {
  123. $Session = new SessionComponent($this->ComponentRegistry);
  124. $Session->write('Test', 'some value');
  125. $Session->delete('Test');
  126. $this->assertNull($Session->read('Test'));
  127. }
  128. /**
  129. * testSessionCheck method
  130. *
  131. * @return void
  132. */
  133. public function testSessionCheck() {
  134. $Session = new SessionComponent($this->ComponentRegistry);
  135. $this->assertFalse($Session->check('Test'));
  136. $Session->write('Test', 'some value');
  137. $this->assertTrue($Session->check('Test'));
  138. $Session->delete('Test');
  139. }
  140. /**
  141. * testSessionFlash method
  142. *
  143. * @return void
  144. */
  145. public function testSessionFlash() {
  146. $Session = new SessionComponent($this->ComponentRegistry);
  147. $this->assertNull($Session->read('Flash.flash'));
  148. $Session->setFlash('This is a test message');
  149. $this->assertEquals(array(
  150. 'message' => 'This is a test message',
  151. 'element' => null,
  152. 'params' => array(),
  153. 'key' => 'flash'
  154. ), $Session->read('Flash.flash'));
  155. }
  156. /**
  157. * testSessionId method
  158. *
  159. * @return void
  160. */
  161. public function testSessionId() {
  162. $Session = new SessionComponent($this->ComponentRegistry);
  163. $this->assertEquals(session_id(), $Session->id());
  164. }
  165. /**
  166. * testSessionDestroy method
  167. *
  168. * @return void
  169. */
  170. public function testSessionDestroy() {
  171. $Session = new SessionComponent($this->ComponentRegistry);
  172. $Session->write('Test', 'some value');
  173. $this->assertEquals('some value', $Session->read('Test'));
  174. $Session->destroy('Test');
  175. $this->assertNull($Session->read('Test'));
  176. }
  177. }