SessionComponentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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('/:controller/:action');
  82. $Controller = new Controller();
  83. $Session = new SessionComponent($this->ComponentRegistry);
  84. $expected = $Session->id();
  85. $result = $Controller->requestAction('/session_test/session_id');
  86. $this->assertEquals($expected, $result);
  87. $result = $Controller->requestAction('/orange_session_test/session_id');
  88. $this->assertEquals($expected, $result);
  89. }
  90. /**
  91. * testSessionReadWrite method
  92. *
  93. * @return void
  94. */
  95. public function testSessionReadWrite() {
  96. $Session = new SessionComponent($this->ComponentRegistry);
  97. $this->assertNull($Session->read('Test'));
  98. $Session->write('Test', 'some value');
  99. $this->assertEquals('some value', $Session->read('Test'));
  100. $Session->delete('Test');
  101. $Session->write('Test.key.path', 'some value');
  102. $this->assertEquals('some value', $Session->read('Test.key.path'));
  103. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  104. $Session->write('Test.key.path2', 'another value');
  105. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  106. $Session->delete('Test');
  107. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  108. $Session->write('Test', $array);
  109. $this->assertEquals($Session->read('Test'), $array);
  110. $Session->delete('Test');
  111. $Session->write(array('Test'), 'some value');
  112. $Session->write(array('Test' => 'some value'));
  113. $this->assertEquals('some value', $Session->read('Test'));
  114. $Session->delete('Test');
  115. }
  116. /**
  117. * testSessionDelete method
  118. *
  119. * @return void
  120. */
  121. public function testSessionDelete() {
  122. $Session = new SessionComponent($this->ComponentRegistry);
  123. $Session->write('Test', 'some value');
  124. $Session->delete('Test');
  125. $this->assertNull($Session->read('Test'));
  126. }
  127. /**
  128. * testSessionCheck method
  129. *
  130. * @return void
  131. */
  132. public function testSessionCheck() {
  133. $Session = new SessionComponent($this->ComponentRegistry);
  134. $this->assertFalse($Session->check('Test'));
  135. $Session->write('Test', 'some value');
  136. $this->assertTrue($Session->check('Test'));
  137. $Session->delete('Test');
  138. }
  139. /**
  140. * testSessionFlash method
  141. *
  142. * @return void
  143. */
  144. public function testSessionFlash() {
  145. $Session = new SessionComponent($this->ComponentRegistry);
  146. $this->assertNull($Session->read('Message.flash'));
  147. $Session->setFlash('This is a test message');
  148. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  149. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  150. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  151. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  152. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  153. $Session->setFlash('This is a test message', 'non_existing_layout');
  154. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  155. $Session->delete('Message');
  156. }
  157. /**
  158. * testSessionId method
  159. *
  160. * @return void
  161. */
  162. public function testSessionId() {
  163. $Session = new SessionComponent($this->ComponentRegistry);
  164. $this->assertEquals(session_id(), $Session->id());
  165. }
  166. /**
  167. * testSessionDestroy method
  168. *
  169. * @return void
  170. */
  171. public function testSessionDestroy() {
  172. $Session = new SessionComponent($this->ComponentRegistry);
  173. $Session->write('Test', 'some value');
  174. $this->assertEquals('some value', $Session->read('Test'));
  175. $Session->destroy('Test');
  176. $this->assertNull($Session->read('Test'));
  177. }
  178. }