PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/tests/cases/libs/controller/components/session.test.php

http://skygames.googlecode.com/
PHP | 330 lines | 176 code | 39 blank | 115 comment | 0 complexity | 4226d4fb880354cb1b77347974e9d50b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: session.test.php 7805 2008-10-30 17:30:26Z AD7six $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake.tests
  20. * @subpackage cake.tests.cases.libs.controller.components
  21. * @since CakePHP(tm) v 1.2.0.5436
  22. * @version $Revision: 7805 $
  23. * @modifiedby $LastChangedBy: AD7six $
  24. * @lastmodified $Date: 2008-10-30 12:30:26 -0500 (Thu, 30 Oct 2008) $
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', array('Controller', 'Object'));
  28. App::import('Component', 'Session');
  29. /**
  30. * SessionTestController class
  31. *
  32. * @package cake.tests
  33. * @subpackage cake.tests.cases.libs.controller.components
  34. */
  35. class SessionTestController extends Controller {
  36. var $uses = array();
  37. function session_id() {
  38. return $this->Session->id();
  39. }
  40. }
  41. /**
  42. * OrangeSessionTestController class
  43. *
  44. * @package cake.tests
  45. * @subpackage cake.tests.cases.libs.controller.components
  46. */
  47. class OrangeSessionTestController extends Controller {
  48. var $uses = array();
  49. function session_id() {
  50. return $this->Session->id();
  51. }
  52. }
  53. /**
  54. * Short description for class.
  55. *
  56. * @package cake.tests
  57. * @subpackage cake.tests.cases.libs.controller.components
  58. */
  59. class SessionComponentTest extends CakeTestCase {
  60. /**
  61. * testSessionAutoStart method
  62. *
  63. * @access public
  64. * @return void
  65. */
  66. function testSessionAutoStart() {
  67. Configure::write('Session.start', false);
  68. $Session =& new SessionComponent();
  69. $this->assertFalse($Session->__active);
  70. $this->assertFalse($Session->__started);
  71. $Session->startup(new SessionTestController());
  72. Configure::write('Session.start', true);
  73. $Session =& new SessionComponent();
  74. $this->assertTrue($Session->__active);
  75. $this->assertFalse($Session->__started);
  76. $Session->startup(new SessionTestController());
  77. $this->assertTrue(isset($_SESSION));
  78. $Object = new Object();
  79. $Session =& new SessionComponent();
  80. $Session->start();
  81. $expected = $Session->id();
  82. $result = $Object->requestAction('/session_test/session_id');
  83. $this->assertEqual($result, $expected);
  84. $result = $Object->requestAction('/orange_session_test/session_id');
  85. $this->assertEqual($result, $expected);
  86. }
  87. /**
  88. * testSessionInitialize method
  89. *
  90. * @access public
  91. * @return void
  92. */
  93. function testSessionInitialize() {
  94. $Session =& new SessionComponent();
  95. $this->assertEqual($Session->__bare, 0);
  96. $Session->initialize(new SessionTestController());
  97. $this->assertEqual($Session->__bare, 0);
  98. $sessionController =& new SessionTestController();
  99. $sessionController->params['bare'] = 1;
  100. $Session->initialize($sessionController);
  101. $this->assertEqual($Session->__bare, 1);
  102. }
  103. /**
  104. * testSessionActivate method
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. function testSessionActivate() {
  110. $Session =& new SessionComponent();
  111. $this->assertTrue($Session->__active);
  112. $this->assertNull($Session->activate());
  113. $this->assertTrue($Session->__active);
  114. Configure::write('Session.start', false);
  115. $Session =& new SessionComponent();
  116. $this->assertFalse($Session->__active);
  117. $this->assertNull($Session->activate());
  118. $this->assertTrue($Session->__active);
  119. Configure::write('Session.start', true);
  120. $Session->destroy();
  121. }
  122. /**
  123. * testSessionValid method
  124. *
  125. * @access public
  126. * @return void
  127. */
  128. function testSessionValid() {
  129. $Session =& new SessionComponent();
  130. $this->assertTrue($Session->valid());
  131. $Session->_userAgent = 'rweerw';
  132. $this->assertFalse($Session->valid());
  133. Configure::write('Session.start', false);
  134. $Session =& new SessionComponent();
  135. $this->assertFalse($Session->__active);
  136. $this->assertFalse($Session->valid());
  137. Configure::write('Session.start', true);
  138. $Session =& new SessionComponent();
  139. $Session->time = $Session->read('Config.time') + 1;
  140. $this->assertFalse($Session->valid());
  141. Configure::write('Session.checkAgent', false);
  142. $Session =& new SessionComponent();
  143. $Session->time = $Session->read('Config.time') + 1;
  144. $this->assertFalse($Session->valid());
  145. Configure::write('Session.checkAgent', true);
  146. }
  147. /**
  148. * testSessionError method
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. function testSessionError() {
  154. $Session =& new SessionComponent();
  155. $this->assertFalse($Session->error());
  156. Configure::write('Session.start', false);
  157. $Session =& new SessionComponent();
  158. $this->assertFalse($Session->__active);
  159. $this->assertFalse($Session->error());
  160. Configure::write('Session.start', true);
  161. }
  162. /**
  163. * testSessionReadWrite method
  164. *
  165. * @access public
  166. * @return void
  167. */
  168. function testSessionReadWrite() {
  169. $Session =& new SessionComponent();
  170. $this->assertFalse($Session->read('Test'));
  171. $this->assertTrue($Session->write('Test', 'some value'));
  172. $this->assertEqual($Session->read('Test'), 'some value');
  173. $this->assertFalse($Session->write('Test.key', 'some value'));
  174. $Session->del('Test');
  175. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  176. $this->assertEqual($Session->read('Test.key.path'), 'some value');
  177. $this->assertEqual($Session->read('Test.key'), array('path' => 'some value'));
  178. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  179. $this->assertEqual($Session->read('Test.key'), array('path' => 'some value', 'path2' => 'another value'));
  180. $Session->del('Test');
  181. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  182. $this->assertTrue($Session->write('Test', $array));
  183. $this->assertEqual($Session->read('Test'), $array);
  184. $Session->del('Test');
  185. $this->assertFalse($Session->write(array('Test'), 'some value'));
  186. $this->assertTrue($Session->write(array('Test' => 'some value')));
  187. $this->assertEqual($Session->read('Test'), 'some value');
  188. $Session->del('Test');
  189. Configure::write('Session.start', false);
  190. $Session =& new SessionComponent();
  191. $this->assertFalse($Session->write('Test', 'some value'));
  192. $Session->write('Test', 'some value');
  193. $this->assertFalse($Session->read('Test'));
  194. Configure::write('Session.start', true);
  195. }
  196. /**
  197. * testSessionDel method
  198. *
  199. * @access public
  200. * @return void
  201. */
  202. function testSessionDel() {
  203. $Session =& new SessionComponent();
  204. $this->assertFalse($Session->del('Test'));
  205. $Session->write('Test', 'some value');
  206. $this->assertTrue($Session->del('Test'));
  207. Configure::write('Session.start', false);
  208. $Session =& new SessionComponent();
  209. $Session->write('Test', 'some value');
  210. $this->assertFalse($Session->del('Test'));
  211. Configure::write('Session.start', true);
  212. }
  213. /**
  214. * testSessionDelete method
  215. *
  216. * @access public
  217. * @return void
  218. */
  219. function testSessionDelete() {
  220. $Session =& new SessionComponent();
  221. $this->assertFalse($Session->delete('Test'));
  222. $Session->write('Test', 'some value');
  223. $this->assertTrue($Session->delete('Test'));
  224. Configure::write('Session.start', false);
  225. $Session =& new SessionComponent();
  226. $Session->write('Test', 'some value');
  227. $this->assertFalse($Session->delete('Test'));
  228. Configure::write('Session.start', true);
  229. }
  230. /**
  231. * testSessionCheck method
  232. *
  233. * @access public
  234. * @return void
  235. */
  236. function testSessionCheck() {
  237. $Session =& new SessionComponent();
  238. $this->assertFalse($Session->check('Test'));
  239. $Session->write('Test', 'some value');
  240. $this->assertTrue($Session->check('Test'));
  241. $Session->delete('Test');
  242. Configure::write('Session.start', false);
  243. $Session =& new SessionComponent();
  244. $Session->write('Test', 'some value');
  245. $this->assertFalse($Session->check('Test'));
  246. Configure::write('Session.start', true);
  247. }
  248. /**
  249. * testSessionFlash method
  250. *
  251. * @access public
  252. * @return void
  253. */
  254. function testSessionFlash() {
  255. $Session =& new SessionComponent();
  256. $this->assertNull($Session->read('Message.flash'));
  257. $Session->setFlash('This is a test message');
  258. $this->assertEqual($Session->read('Message.flash'), array('message' => 'This is a test message', 'layout' => 'default', 'params' => array()));
  259. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  260. $this->assertEqual($Session->read('Message.flash'), array('message' => 'This is a test message', 'layout' => 'test', 'params' => array('name' => 'Joel Moss')));
  261. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  262. $this->assertEqual($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'layout' => 'default', 'params' => array()));
  263. $Session->setFlash('This is a test message', 'non_existing_layout');
  264. $this->assertEqual($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'layout' => 'default', 'params' => array()));
  265. }
  266. /**
  267. * testSessionId method
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. function testSessionId() {
  273. unset($_SESSION);
  274. $Session =& new SessionComponent();
  275. $this->assertNull($Session->id());
  276. }
  277. /**
  278. * testSessionDestroy method
  279. *
  280. * @access public
  281. * @return void
  282. */
  283. function testSessionDestroy() {
  284. $Session =& new SessionComponent();
  285. $Session->write('Test', 'some value');
  286. $this->assertEqual($Session->read('Test'), 'some value');
  287. $Session->destroy('Test');
  288. $this->assertNull($Session->read('Test'));
  289. }
  290. }
  291. ?>