| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* SessionComponentTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
|
|
|
13 |
* @link http://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 http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Controller', 'Controller');
|
|
|
20 |
App::uses('SessionComponent', 'Controller/Component');
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* SessionTestController class
|
|
|
24 |
*
|
|
|
25 |
* @package Cake.Test.Case.Controller.Component
|
|
|
26 |
*/
|
|
|
27 |
class 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 |
*/
|
|
|
52 |
class 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 |
*/
|
|
|
77 |
class 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 |
self::$_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', self::$_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 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
139 |
$Session->check('Test');
|
|
|
140 |
$this->assertTrue(isset($_SESSION));
|
|
|
141 |
|
|
|
142 |
$Object = new Object();
|
|
|
143 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
144 |
$expected = $Session->id();
|
|
|
145 |
|
|
|
146 |
$result = $Object->requestAction('/session_test/sessionId');
|
|
|
147 |
$this->assertEquals($expected, $result);
|
|
|
148 |
|
|
|
149 |
$result = $Object->requestAction('/orange_session_test/sessionId');
|
|
|
150 |
$this->assertEquals($expected, $result);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* testSessionValid method
|
|
|
155 |
*
|
|
|
156 |
* @return void
|
|
|
157 |
*/
|
|
|
158 |
public function testSessionValid() {
|
|
|
159 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
160 |
|
|
|
161 |
$this->assertTrue($Session->valid());
|
|
|
162 |
|
|
|
163 |
Configure::write('Session.checkAgent', true);
|
|
|
164 |
$Session->userAgent('rweerw');
|
|
|
165 |
$this->assertFalse($Session->valid());
|
|
|
166 |
|
|
|
167 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
168 |
$Session->time = $Session->read('Config.time') + 1;
|
|
|
169 |
$this->assertFalse($Session->valid());
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* testSessionError method
|
|
|
174 |
*
|
|
|
175 |
* @return void
|
|
|
176 |
*/
|
|
|
177 |
public function testSessionError() {
|
|
|
178 |
CakeSession::$lastError = null;
|
|
|
179 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
180 |
$this->assertFalse($Session->error());
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* testSessionReadWrite method
|
|
|
185 |
*
|
|
|
186 |
* @return void
|
|
|
187 |
*/
|
|
|
188 |
public function testSessionReadWrite() {
|
|
|
189 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
190 |
|
|
|
191 |
$this->assertNull($Session->read('Test'));
|
|
|
192 |
|
|
|
193 |
$this->assertTrue($Session->write('Test', 'some value'));
|
|
|
194 |
$this->assertEquals('some value', $Session->read('Test'));
|
|
|
195 |
$Session->delete('Test');
|
|
|
196 |
|
|
|
197 |
$this->assertTrue($Session->write('Test.key.path', 'some value'));
|
|
|
198 |
$this->assertEquals('some value', $Session->read('Test.key.path'));
|
|
|
199 |
$this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
|
|
|
200 |
$this->assertTrue($Session->write('Test.key.path2', 'another value'));
|
|
|
201 |
$this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
|
|
|
202 |
$Session->delete('Test');
|
|
|
203 |
|
|
|
204 |
$array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
|
|
|
205 |
$this->assertTrue($Session->write('Test', $array));
|
|
|
206 |
$this->assertEquals($Session->read('Test'), $array);
|
|
|
207 |
$Session->delete('Test');
|
|
|
208 |
|
|
|
209 |
$this->assertTrue($Session->write(array('Test'), 'some value'));
|
|
|
210 |
$this->assertTrue($Session->write(array('Test' => 'some value')));
|
|
|
211 |
$this->assertEquals('some value', $Session->read('Test'));
|
|
|
212 |
$Session->delete('Test');
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* testSessionDelete method
|
|
|
217 |
*
|
|
|
218 |
* @return void
|
|
|
219 |
*/
|
|
|
220 |
public function testSessionDelete() {
|
|
|
221 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
222 |
|
|
|
223 |
$this->assertFalse($Session->delete('Test'));
|
|
|
224 |
|
|
|
225 |
$Session->write('Test', 'some value');
|
|
|
226 |
$this->assertTrue($Session->delete('Test'));
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* testSessionCheck method
|
|
|
231 |
*
|
|
|
232 |
* @return void
|
|
|
233 |
*/
|
|
|
234 |
public function testSessionCheck() {
|
|
|
235 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
236 |
|
|
|
237 |
$this->assertFalse($Session->check('Test'));
|
|
|
238 |
|
|
|
239 |
$Session->write('Test', 'some value');
|
|
|
240 |
$this->assertTrue($Session->check('Test'));
|
|
|
241 |
$Session->delete('Test');
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* testSessionFlash method
|
|
|
246 |
*
|
|
|
247 |
* @return void
|
|
|
248 |
*/
|
|
|
249 |
public function testSessionFlash() {
|
|
|
250 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
251 |
|
|
|
252 |
$this->assertNull($Session->read('Message.flash'));
|
|
|
253 |
|
|
|
254 |
$Session->setFlash('This is a test message');
|
|
|
255 |
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
|
|
|
256 |
|
|
|
257 |
$Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
|
|
|
258 |
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
|
|
|
259 |
|
|
|
260 |
$Session->setFlash('This is a test message', 'default', array(), 'myFlash');
|
|
|
261 |
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
|
|
|
262 |
|
|
|
263 |
$Session->setFlash('This is a test message', 'non_existing_layout');
|
|
|
264 |
$this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
|
|
|
265 |
|
|
|
266 |
$Session->delete('Message');
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
/**
|
|
|
270 |
* testSessionId method
|
|
|
271 |
*
|
|
|
272 |
* @return void
|
|
|
273 |
*/
|
|
|
274 |
public function testSessionId() {
|
|
|
275 |
unset($_SESSION);
|
|
|
276 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
277 |
$Session->check('test');
|
|
|
278 |
$this->assertEquals(session_id(), $Session->id());
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* testSessionDestroy method
|
|
|
283 |
*
|
|
|
284 |
* @return void
|
|
|
285 |
*/
|
|
|
286 |
public function testSessionDestroy() {
|
|
|
287 |
$Session = new SessionComponent($this->ComponentCollection);
|
|
|
288 |
|
|
|
289 |
$Session->write('Test', 'some value');
|
|
|
290 |
$this->assertEquals('some value', $Session->read('Test'));
|
|
|
291 |
$Session->destroy('Test');
|
|
|
292 |
$this->assertNull($Session->read('Test'));
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
}
|