1<?php
2/**
3 * SessionComponentTest file
4 *
5 * CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
6 * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
13 * @link          https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14 * @package       Cake.Test.Case.Controller.Component
15 * @since         CakePHP(tm) v 1.2.0.5436
16 * @license       https://opensource.org/licenses/mit-license.php MIT License
17 */
18
19App::uses('Controller', 'Controller');
20App::uses('SessionComponent', 'Controller/Component');
21
22/**
23 * SessionTestController class
24 *
25 * @package       Cake.Test.Case.Controller.Component
26 */
27class SessionTestController extends Controller {
28
29/**
30 * uses property
31 *
32 * @var array
33 */
34	public $uses = array();
35
36/**
37 * sessionId method
38 *
39 * @return string
40 */
41	public function sessionId() {
42		return $this->Session->id();
43	}
44
45}
46
47/**
48 * OrangeSessionTestController class
49 *
50 * @package       Cake.Test.Case.Controller.Component
51 */
52class OrangeSessionTestController extends Controller {
53
54/**
55 * uses property
56 *
57 * @var array
58 */
59	public $uses = array();
60
61/**
62 * sessionId method
63 *
64 * @return string
65 */
66	public function sessionId() {
67		return $this->Session->id();
68	}
69
70}
71
72/**
73 * SessionComponentTest class
74 *
75 * @package       Cake.Test.Case.Controller.Component
76 */
77class SessionComponentTest extends CakeTestCase {
78
79	protected static $_sessionBackup;
80
81/**
82 * fixtures
83 *
84 * @var string
85 */
86	public $fixtures = array('core.session');
87
88/**
89 * test case startup
90 *
91 * @return void
92 */
93	public static function setupBeforeClass() {
94		static::$_sessionBackup = Configure::read('Session');
95		Configure::write('Session', array(
96			'defaults' => 'php',
97			'timeout' => 100,
98			'cookie' => 'test'
99		));
100	}
101
102/**
103 * cleanup after test case.
104 *
105 * @return void
106 */
107	public static function teardownAfterClass() {
108		Configure::write('Session', static::$_sessionBackup);
109	}
110
111/**
112 * setUp method
113 *
114 * @return void
115 */
116	public function setUp() {
117		parent::setUp();
118		$_SESSION = null;
119		$this->ComponentCollection = new ComponentCollection();
120	}
121
122/**
123 * tearDown method
124 *
125 * @return void
126 */
127	public function tearDown() {
128		parent::tearDown();
129		CakeSession::destroy();
130	}
131
132/**
133 * ensure that session ids don't change when request action is called.
134 *
135 * @return void
136 */
137	public function testSessionIdConsistentAcrossRequestAction() {
138		$Object = new CakeObject();
139		$Session = new SessionComponent($this->ComponentCollection);
140		$expected = $Session->id();
141
142		$result = $Object->requestAction('/session_test/sessionId');
143		$this->assertEquals($expected, $result);
144
145		$result = $Object->requestAction('/orange_session_test/sessionId');
146		$this->assertEquals($expected, $result);
147	}
148
149/**
150 * testSessionValid method
151 *
152 * @return void
153 */
154	public function testSessionValid() {
155		$Session = new SessionComponent($this->ComponentCollection);
156
157		$this->assertTrue($Session->valid());
158
159		Configure::write('Session.checkAgent', true);
160		$Session->userAgent('rweerw');
161		$this->assertFalse($Session->valid());
162
163		$Session = new SessionComponent($this->ComponentCollection);
164		$Session->time = $Session->read('Config.time') + 1;
165		$this->assertFalse($Session->valid());
166	}
167
168/**
169 * testSessionError method
170 *
171 * @return void
172 */
173	public function testSessionError() {
174		CakeSession::$lastError = null;
175		$Session = new SessionComponent($this->ComponentCollection);
176		$this->assertFalse($Session->error());
177	}
178
179/**
180 * testSessionReadWrite method
181 *
182 * @return void
183 */
184	public function testSessionReadWrite() {
185		$Session = new SessionComponent($this->ComponentCollection);
186
187		$this->assertNull($Session->read('Test'));
188
189		$this->assertTrue($Session->write('Test', 'some value'));
190		$this->assertEquals('some value', $Session->read('Test'));
191		$Session->delete('Test');
192
193		$this->assertTrue($Session->write('Test.key.path', 'some value'));
194		$this->assertEquals('some value', $Session->read('Test.key.path'));
195		$this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
196		$this->assertTrue($Session->write('Test.key.path2', 'another value'));
197		$this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
198		$Session->delete('Test');
199
200		$array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
201		$this->assertTrue($Session->write('Test', $array));
202		$this->assertEquals($Session->read('Test'), $array);
203		$Session->delete('Test');
204
205		$this->assertTrue($Session->write(array('Test' => 'some value')));
206		$this->assertEquals('some value', $Session->read('Test'));
207		$Session->delete('Test');
208	}
209
210/**
211 * testSessionDelete method
212 *
213 * @return void
214 */
215	public function testSessionDelete() {
216		$Session = new SessionComponent($this->ComponentCollection);
217
218		$this->assertFalse($Session->delete('Test'));
219
220		$Session->write('Test', 'some value');
221		$this->assertTrue($Session->delete('Test'));
222	}
223
224/**
225 * testSessionCheck method
226 *
227 * @return void
228 */
229	public function testSessionCheck() {
230		$Session = new SessionComponent($this->ComponentCollection);
231
232		$this->assertFalse($Session->check('Test'));
233
234		$Session->write('Test', 'some value');
235		$this->assertTrue($Session->check('Test'));
236		$Session->delete('Test');
237	}
238
239/**
240 * testSessionFlash method
241 *
242 * @return void
243 */
244	public function testSessionFlash() {
245		$Session = new SessionComponent($this->ComponentCollection);
246
247		$this->assertNull($Session->read('Message.flash'));
248
249		$Session->setFlash('This is a test message');
250		$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash.0'));
251
252		$Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
253		$this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash.1'));
254
255		$Session->setFlash('This is a test message', 'default', array(), 'myFlash');
256		$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash.0'));
257
258		$Session->delete('Message');
259	}
260
261/**
262 * testSessionId method
263 *
264 * @return void
265 */
266	public function testSessionId() {
267		unset($_SESSION);
268		$Session = new SessionComponent($this->ComponentCollection);
269		CakeSession::start();
270		$this->assertEquals(session_id(), $Session->id());
271	}
272
273/**
274 * testSessionDestroy method
275 *
276 * @return void
277 */
278	public function testSessionDestroy() {
279		$Session = new SessionComponent($this->ComponentCollection);
280
281		$Session->write('Test', 'some value');
282		$this->assertEquals('some value', $Session->read('Test'));
283		$Session->destroy('Test');
284		$this->assertNull($Session->read('Test'));
285	}
286
287}
288