SessionComponentTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\Session;
  21. use Cake\Routing\DispatcherFactory;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * SessionComponentTest class
  26. *
  27. */
  28. class SessionComponentTest extends TestCase {
  29. protected static $_sessionBackup;
  30. /**
  31. * fixtures
  32. *
  33. * @var string
  34. */
  35. public $fixtures = array('core.session');
  36. /**
  37. * test case startup
  38. *
  39. * @return void
  40. */
  41. public static function setupBeforeClass() {
  42. static::$_sessionBackup = Configure::read('Session');
  43. Configure::write('Session', array(
  44. 'defaults' => 'php',
  45. 'timeout' => 100,
  46. 'cookie' => 'test'
  47. ));
  48. DispatcherFactory::add('Routing');
  49. DispatcherFactory::add('ControllerFactory');
  50. }
  51. /**
  52. * cleanup after test case.
  53. *
  54. * @return void
  55. */
  56. public static function teardownAfterClass() {
  57. Configure::write('Session', static::$_sessionBackup);
  58. DispatcherFactory::clear();
  59. }
  60. /**
  61. * setUp method
  62. *
  63. * @return void
  64. */
  65. public function setUp() {
  66. parent::setUp();
  67. $_SESSION = null;
  68. Configure::write('App.namespace', 'TestApp');
  69. $this->ComponentRegistry = new ComponentRegistry();
  70. }
  71. /**
  72. * tearDown method
  73. *
  74. * @return void
  75. */
  76. public function tearDown() {
  77. parent::tearDown();
  78. Session::destroy();
  79. }
  80. /**
  81. * ensure that session ids don't change when request action is called.
  82. *
  83. * @return void
  84. */
  85. public function testSessionIdConsistentAcrossRequestAction() {
  86. Configure::write('App.namespace', 'TestApp');
  87. Router::connect('/:controller/:action');
  88. $Controller = new Controller();
  89. $Session = new SessionComponent($this->ComponentRegistry);
  90. $expected = $Session->id();
  91. $result = $Controller->requestAction('/session_test/session_id');
  92. $this->assertEquals($expected, $result);
  93. $result = $Controller->requestAction('/orange_session_test/session_id');
  94. $this->assertEquals($expected, $result);
  95. }
  96. /**
  97. * testSessionValid method
  98. *
  99. * @return void
  100. */
  101. public function testSessionValid() {
  102. $Session = new SessionComponent($this->ComponentRegistry);
  103. $this->assertTrue($Session->valid());
  104. Configure::write('Session.checkAgent', true);
  105. $Session->userAgent('rweerw');
  106. $this->assertFalse($Session->valid());
  107. $Session = new SessionComponent($this->ComponentRegistry);
  108. $Session->time = $Session->read('Config.time') + 1;
  109. $this->assertFalse($Session->valid());
  110. }
  111. /**
  112. * testSessionError method
  113. *
  114. * @return void
  115. */
  116. public function testSessionError() {
  117. Session::$lastError = null;
  118. $Session = new SessionComponent($this->ComponentRegistry);
  119. $this->assertFalse($Session->error());
  120. }
  121. /**
  122. * testSessionReadWrite method
  123. *
  124. * @return void
  125. */
  126. public function testSessionReadWrite() {
  127. $Session = new SessionComponent($this->ComponentRegistry);
  128. $this->assertNull($Session->read('Test'));
  129. $this->assertTrue($Session->write('Test', 'some value'));
  130. $this->assertEquals('some value', $Session->read('Test'));
  131. $Session->delete('Test');
  132. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  133. $this->assertEquals('some value', $Session->read('Test.key.path'));
  134. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  135. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  136. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  137. $Session->delete('Test');
  138. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  139. $this->assertTrue($Session->write('Test', $array));
  140. $this->assertEquals($Session->read('Test'), $array);
  141. $Session->delete('Test');
  142. $this->assertTrue($Session->write(array('Test'), 'some value'));
  143. $this->assertTrue($Session->write(array('Test' => 'some value')));
  144. $this->assertEquals('some value', $Session->read('Test'));
  145. $Session->delete('Test');
  146. }
  147. /**
  148. * testSessionDelete method
  149. *
  150. * @return void
  151. */
  152. public function testSessionDelete() {
  153. $Session = new SessionComponent($this->ComponentRegistry);
  154. $this->assertFalse($Session->delete('Test'));
  155. $Session->write('Test', 'some value');
  156. $this->assertTrue($Session->delete('Test'));
  157. }
  158. /**
  159. * testSessionCheck method
  160. *
  161. * @return void
  162. */
  163. public function testSessionCheck() {
  164. $Session = new SessionComponent($this->ComponentRegistry);
  165. $this->assertFalse($Session->check('Test'));
  166. $Session->write('Test', 'some value');
  167. $this->assertTrue($Session->check('Test'));
  168. $Session->delete('Test');
  169. }
  170. /**
  171. * testSessionFlash method
  172. *
  173. * @return void
  174. */
  175. public function testSessionFlash() {
  176. $Session = new SessionComponent($this->ComponentRegistry);
  177. $this->assertNull($Session->read('Message.flash'));
  178. $Session->setFlash('This is a test message');
  179. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  180. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  181. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  182. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  183. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  184. $Session->setFlash('This is a test message', 'non_existing_layout');
  185. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  186. $Session->delete('Message');
  187. }
  188. /**
  189. * testSessionId method
  190. *
  191. * @return void
  192. */
  193. public function testSessionId() {
  194. unset($_SESSION);
  195. $Session = new SessionComponent($this->ComponentRegistry);
  196. Session::start();
  197. $this->assertEquals(session_id(), $Session->id());
  198. }
  199. /**
  200. * testSessionDestroy method
  201. *
  202. * @return void
  203. */
  204. public function testSessionDestroy() {
  205. $Session = new SessionComponent($this->ComponentRegistry);
  206. $Session->write('Test', 'some value');
  207. $this->assertEquals('some value', $Session->read('Test'));
  208. $Session->destroy('Test');
  209. $this->assertNull($Session->read('Test'));
  210. }
  211. }