| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CookieComponentTest 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.5435
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Component', 'Controller');
|
|
|
20 |
App::uses('Controller', 'Controller');
|
|
|
21 |
App::uses('CookieComponent', 'Controller/Component');
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* CookieComponentTestController class
|
|
|
25 |
*
|
|
|
26 |
* @package Cake.Test.Case.Controller.Component
|
|
|
27 |
*/
|
|
|
28 |
class CookieComponentTestController extends Controller {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* components property
|
|
|
32 |
*
|
|
|
33 |
* @var array
|
|
|
34 |
*/
|
|
|
35 |
public $components = array('Cookie');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* beforeFilter method
|
|
|
39 |
*
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
public function beforeFilter() {
|
|
|
43 |
$this->Cookie->name = 'CakeTestCookie';
|
|
|
44 |
$this->Cookie->time = 10;
|
|
|
45 |
$this->Cookie->path = '/';
|
|
|
46 |
$this->Cookie->domain = '';
|
|
|
47 |
$this->Cookie->secure = false;
|
|
|
48 |
$this->Cookie->key = 'somerandomhaskey';
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* CookieComponentTest class
|
|
|
55 |
*
|
|
|
56 |
* @package Cake.Test.Case.Controller.Component
|
|
|
57 |
*/
|
|
|
58 |
class CookieComponentTest extends CakeTestCase {
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Controller property
|
|
|
62 |
*
|
|
|
63 |
* @var CookieComponentTestController
|
|
|
64 |
*/
|
|
|
65 |
public $Controller;
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* start
|
|
|
69 |
*
|
|
|
70 |
* @return void
|
|
|
71 |
*/
|
|
|
72 |
public function setUp() {
|
|
|
73 |
parent::setUp();
|
|
|
74 |
$_COOKIE = array();
|
|
|
75 |
$this->Controller = new CookieComponentTestController(new CakeRequest(), new CakeResponse());
|
|
|
76 |
$this->Controller->constructClasses();
|
|
|
77 |
$this->Cookie = $this->Controller->Cookie;
|
|
|
78 |
|
|
|
79 |
$this->Cookie->name = 'CakeTestCookie';
|
|
|
80 |
$this->Cookie->time = 10;
|
|
|
81 |
$this->Cookie->path = '/';
|
|
|
82 |
$this->Cookie->domain = '';
|
|
|
83 |
$this->Cookie->secure = false;
|
|
|
84 |
$this->Cookie->key = 'somerandomhaskey';
|
|
|
85 |
|
|
|
86 |
$this->Cookie->startup($this->Controller);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* end
|
|
|
91 |
*
|
|
|
92 |
* @return void
|
|
|
93 |
*/
|
|
|
94 |
public function tearDown() {
|
|
|
95 |
parent::tearDown();
|
|
|
96 |
$this->Cookie->destroy();
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* sets up some default cookie data.
|
|
|
101 |
*
|
|
|
102 |
* @return void
|
|
|
103 |
*/
|
|
|
104 |
protected function _setCookieData() {
|
|
|
105 |
$this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
|
|
|
106 |
$this->Cookie->write(array('Encrytped_multi_cookies.name' => 'CakePHP'));
|
|
|
107 |
$this->Cookie->write(array('Encrytped_multi_cookies.version' => '1.2.0.x'));
|
|
|
108 |
$this->Cookie->write(array('Encrytped_multi_cookies.tag' => 'CakePHP Rocks!'));
|
|
|
109 |
|
|
|
110 |
$this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
|
|
|
111 |
$this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
|
|
|
112 |
$this->Cookie->write(array('Plain_multi_cookies.version' => '1.2.0.x'), null, false);
|
|
|
113 |
$this->Cookie->write(array('Plain_multi_cookies.tag' => 'CakePHP Rocks!'), null, false);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* test that initialize sets settings from components array
|
|
|
118 |
*
|
|
|
119 |
* @return void
|
|
|
120 |
*/
|
|
|
121 |
public function testSettings() {
|
|
|
122 |
$settings = array(
|
|
|
123 |
'time' => '5 days',
|
|
|
124 |
'path' => '/'
|
|
|
125 |
);
|
|
|
126 |
$Cookie = new CookieComponent(new ComponentCollection(), $settings);
|
|
|
127 |
$this->assertEquals($Cookie->time, $settings['time']);
|
|
|
128 |
$this->assertEquals($Cookie->path, $settings['path']);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* testCookieName
|
|
|
133 |
*
|
|
|
134 |
* @return void
|
|
|
135 |
*/
|
|
|
136 |
public function testCookieName() {
|
|
|
137 |
$this->assertEquals('CakeTestCookie', $this->Cookie->name);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* testReadEncryptedCookieData
|
|
|
142 |
*
|
|
|
143 |
* @return void
|
|
|
144 |
*/
|
|
|
145 |
public function testReadEncryptedCookieData() {
|
|
|
146 |
$this->_setCookieData();
|
|
|
147 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
148 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
149 |
$this->assertEquals($expected, $data);
|
|
|
150 |
|
|
|
151 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
152 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
153 |
$this->assertEquals($expected, $data);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* testReadPlainCookieData
|
|
|
158 |
*
|
|
|
159 |
* @return void
|
|
|
160 |
*/
|
|
|
161 |
public function testReadPlainCookieData() {
|
|
|
162 |
$this->_setCookieData();
|
|
|
163 |
$data = $this->Cookie->read('Plain_array');
|
|
|
164 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
165 |
$this->assertEquals($expected, $data);
|
|
|
166 |
|
|
|
167 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
168 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
169 |
$this->assertEquals($expected, $data);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* test read() after switching the cookie name.
|
|
|
174 |
*
|
|
|
175 |
* @return void
|
|
|
176 |
*/
|
|
|
177 |
public function testReadWithNameSwitch() {
|
|
|
178 |
$_COOKIE = array(
|
|
|
179 |
'CakeTestCookie' => array(
|
|
|
180 |
'key' => 'value'
|
|
|
181 |
),
|
|
|
182 |
'OtherTestCookie' => array(
|
|
|
183 |
'key' => 'other value'
|
|
|
184 |
)
|
|
|
185 |
);
|
|
|
186 |
$this->assertEquals('value', $this->Cookie->read('key'));
|
|
|
187 |
|
|
|
188 |
$this->Cookie->name = 'OtherTestCookie';
|
|
|
189 |
$this->assertEquals('other value', $this->Cookie->read('key'));
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* test a simple write()
|
|
|
194 |
*
|
|
|
195 |
* @return void
|
|
|
196 |
*/
|
|
|
197 |
public function testWriteSimple() {
|
|
|
198 |
$this->Cookie->write('Testing', 'value');
|
|
|
199 |
$result = $this->Cookie->read('Testing');
|
|
|
200 |
|
|
|
201 |
$this->assertEquals('value', $result);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* test that two write() calls use the expiry.
|
|
|
206 |
*
|
|
|
207 |
* @return void
|
|
|
208 |
*/
|
|
|
209 |
public function testWriteMultipleShareExpiry() {
|
|
|
210 |
$this->Cookie->write('key1', 'value1', false);
|
|
|
211 |
$this->Cookie->write('key2', 'value2', false);
|
|
|
212 |
|
|
|
213 |
$name = $this->Cookie->name . '[key1]';
|
|
|
214 |
$result = $this->Controller->response->cookie($name);
|
|
|
215 |
$this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
|
|
|
216 |
|
|
|
217 |
$name = $this->Cookie->name . '[key2]';
|
|
|
218 |
$result = $this->Controller->response->cookie($name);
|
|
|
219 |
$this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* test write with distant future cookies
|
|
|
224 |
*
|
|
|
225 |
* @return void
|
|
|
226 |
*/
|
|
|
227 |
public function testWriteFarFuture() {
|
|
|
228 |
$this->Cookie->write('Testing', 'value', false, '+90 years');
|
|
|
229 |
$future = new DateTime('now');
|
|
|
230 |
$future->modify('+90 years');
|
|
|
231 |
|
|
|
232 |
$expected = array(
|
|
|
233 |
'name' => $this->Cookie->name . '[Testing]',
|
|
|
234 |
'value' => 'value',
|
|
|
235 |
'path' => '/',
|
|
|
236 |
'domain' => '',
|
|
|
237 |
'secure' => false,
|
|
|
238 |
'httpOnly' => false);
|
|
|
239 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
|
|
240 |
|
|
|
241 |
$this->assertEquals($future->format('U'), $result['expire'], '', 3);
|
|
|
242 |
unset($result['expire']);
|
|
|
243 |
|
|
|
244 |
$this->assertEquals($expected, $result);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
/**
|
|
|
248 |
* test write with httpOnly cookies
|
|
|
249 |
*
|
|
|
250 |
* @return void
|
|
|
251 |
*/
|
|
|
252 |
public function testWriteHttpOnly() {
|
|
|
253 |
$this->Cookie->httpOnly = true;
|
|
|
254 |
$this->Cookie->secure = false;
|
|
|
255 |
$this->Cookie->write('Testing', 'value', false);
|
|
|
256 |
$expected = array(
|
|
|
257 |
'name' => $this->Cookie->name . '[Testing]',
|
|
|
258 |
'value' => 'value',
|
|
|
259 |
'expire' => time() + 10,
|
|
|
260 |
'path' => '/',
|
|
|
261 |
'domain' => '',
|
|
|
262 |
'secure' => false,
|
|
|
263 |
'httpOnly' => true);
|
|
|
264 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
|
|
265 |
$this->assertEquals($expected, $result);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* test delete with httpOnly
|
|
|
270 |
*
|
|
|
271 |
* @return void
|
|
|
272 |
*/
|
|
|
273 |
public function testDeleteHttpOnly() {
|
|
|
274 |
$this->Cookie->httpOnly = true;
|
|
|
275 |
$this->Cookie->secure = false;
|
|
|
276 |
$this->Cookie->delete('Testing', false);
|
|
|
277 |
$expected = array(
|
|
|
278 |
'name' => $this->Cookie->name . '[Testing]',
|
|
|
279 |
'value' => '',
|
|
|
280 |
'expire' => time() - 42000,
|
|
|
281 |
'path' => '/',
|
|
|
282 |
'domain' => '',
|
|
|
283 |
'secure' => false,
|
|
|
284 |
'httpOnly' => true);
|
|
|
285 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
|
|
286 |
$this->assertEquals($expected, $result);
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* testWritePlainCookieArray
|
|
|
291 |
*
|
|
|
292 |
* @return void
|
|
|
293 |
*/
|
|
|
294 |
public function testWritePlainCookieArray() {
|
|
|
295 |
$this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
|
|
|
296 |
|
|
|
297 |
$this->assertEquals('CakePHP', $this->Cookie->read('name'));
|
|
|
298 |
$this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
|
|
|
299 |
$this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
|
|
|
300 |
|
|
|
301 |
$this->Cookie->delete('name');
|
|
|
302 |
$this->Cookie->delete('version');
|
|
|
303 |
$this->Cookie->delete('tag');
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* test writing values that are not scalars
|
|
|
308 |
*
|
|
|
309 |
* @return void
|
|
|
310 |
*/
|
|
|
311 |
public function testWriteArrayValues() {
|
|
|
312 |
$this->Cookie->secure = false;
|
|
|
313 |
$this->Cookie->write('Testing', array(1, 2, 3), false);
|
|
|
314 |
$expected = array(
|
|
|
315 |
'name' => $this->Cookie->name . '[Testing]',
|
|
|
316 |
'value' => '[1,2,3]',
|
|
|
317 |
'path' => '/',
|
|
|
318 |
'domain' => '',
|
|
|
319 |
'secure' => false,
|
|
|
320 |
'httpOnly' => false);
|
|
|
321 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
|
|
322 |
|
|
|
323 |
$this->assertWithinMargin($result['expire'], time() + 10, 1);
|
|
|
324 |
unset($result['expire']);
|
|
|
325 |
$this->assertEquals($expected, $result);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
/**
|
|
|
329 |
* Test that writing mixed arrays results in the correct data.
|
|
|
330 |
*
|
|
|
331 |
* @return void
|
|
|
332 |
*/
|
|
|
333 |
public function testWriteMixedArray() {
|
|
|
334 |
$this->Cookie->encrypt = false;
|
|
|
335 |
$this->Cookie->write('User', array('name' => 'mark'), false);
|
|
|
336 |
$this->Cookie->write('User.email', 'mark@example.com', false);
|
|
|
337 |
$expected = array(
|
|
|
338 |
'name' => $this->Cookie->name . '[User]',
|
|
|
339 |
'value' => '{"name":"mark","email":"mark@example.com"}',
|
|
|
340 |
'path' => '/',
|
|
|
341 |
'domain' => '',
|
|
|
342 |
'secure' => false,
|
|
|
343 |
'httpOnly' => false
|
|
|
344 |
);
|
|
|
345 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
|
|
|
346 |
unset($result['expire']);
|
|
|
347 |
|
|
|
348 |
$this->assertEquals($expected, $result);
|
|
|
349 |
|
|
|
350 |
$this->Cookie->write('User.email', 'mark@example.com', false);
|
|
|
351 |
$this->Cookie->write('User', array('name' => 'mark'), false);
|
|
|
352 |
$expected = array(
|
|
|
353 |
'name' => $this->Cookie->name . '[User]',
|
|
|
354 |
'value' => '{"name":"mark"}',
|
|
|
355 |
'path' => '/',
|
|
|
356 |
'domain' => '',
|
|
|
357 |
'secure' => false,
|
|
|
358 |
'httpOnly' => false
|
|
|
359 |
);
|
|
|
360 |
$result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
|
|
|
361 |
unset($result['expire']);
|
|
|
362 |
|
|
|
363 |
$this->assertEquals($expected, $result);
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
/**
|
|
|
367 |
* testReadingCookieValue
|
|
|
368 |
*
|
|
|
369 |
* @return void
|
|
|
370 |
*/
|
|
|
371 |
public function testReadingCookieValue() {
|
|
|
372 |
$this->_setCookieData();
|
|
|
373 |
$data = $this->Cookie->read();
|
|
|
374 |
$expected = array(
|
|
|
375 |
'Encrytped_array' => array(
|
|
|
376 |
'name' => 'CakePHP',
|
|
|
377 |
'version' => '1.2.0.x',
|
|
|
378 |
'tag' => 'CakePHP Rocks!'),
|
|
|
379 |
'Encrytped_multi_cookies' => array(
|
|
|
380 |
'name' => 'CakePHP',
|
|
|
381 |
'version' => '1.2.0.x',
|
|
|
382 |
'tag' => 'CakePHP Rocks!'),
|
|
|
383 |
'Plain_array' => array(
|
|
|
384 |
'name' => 'CakePHP',
|
|
|
385 |
'version' => '1.2.0.x',
|
|
|
386 |
'tag' => 'CakePHP Rocks!'),
|
|
|
387 |
'Plain_multi_cookies' => array(
|
|
|
388 |
'name' => 'CakePHP',
|
|
|
389 |
'version' => '1.2.0.x',
|
|
|
390 |
'tag' => 'CakePHP Rocks!'));
|
|
|
391 |
$this->assertEquals($expected, $data);
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
/**
|
|
|
395 |
* testDeleteCookieValue
|
|
|
396 |
*
|
|
|
397 |
* @return void
|
|
|
398 |
*/
|
|
|
399 |
public function testDeleteCookieValue() {
|
|
|
400 |
$this->_setCookieData();
|
|
|
401 |
$this->Cookie->delete('Encrytped_multi_cookies.name');
|
|
|
402 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
403 |
$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
404 |
$this->assertEquals($expected, $data);
|
|
|
405 |
|
|
|
406 |
$this->Cookie->delete('Encrytped_array');
|
|
|
407 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
408 |
$this->assertNull($data);
|
|
|
409 |
|
|
|
410 |
$this->Cookie->delete('Plain_multi_cookies.name');
|
|
|
411 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
412 |
$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
413 |
$this->assertEquals($expected, $data);
|
|
|
414 |
|
|
|
415 |
$this->Cookie->delete('Plain_array');
|
|
|
416 |
$data = $this->Cookie->read('Plain_array');
|
|
|
417 |
$this->assertNull($data);
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
/**
|
|
|
421 |
* testReadingCookieArray
|
|
|
422 |
*
|
|
|
423 |
* @return void
|
|
|
424 |
*/
|
|
|
425 |
public function testReadingCookieArray() {
|
|
|
426 |
$this->_setCookieData();
|
|
|
427 |
|
|
|
428 |
$data = $this->Cookie->read('Encrytped_array.name');
|
|
|
429 |
$expected = 'CakePHP';
|
|
|
430 |
$this->assertEquals($expected, $data);
|
|
|
431 |
|
|
|
432 |
$data = $this->Cookie->read('Encrytped_array.version');
|
|
|
433 |
$expected = '1.2.0.x';
|
|
|
434 |
$this->assertEquals($expected, $data);
|
|
|
435 |
|
|
|
436 |
$data = $this->Cookie->read('Encrytped_array.tag');
|
|
|
437 |
$expected = 'CakePHP Rocks!';
|
|
|
438 |
$this->assertEquals($expected, $data);
|
|
|
439 |
|
|
|
440 |
$data = $this->Cookie->read('Encrytped_multi_cookies.name');
|
|
|
441 |
$expected = 'CakePHP';
|
|
|
442 |
$this->assertEquals($expected, $data);
|
|
|
443 |
|
|
|
444 |
$data = $this->Cookie->read('Encrytped_multi_cookies.version');
|
|
|
445 |
$expected = '1.2.0.x';
|
|
|
446 |
$this->assertEquals($expected, $data);
|
|
|
447 |
|
|
|
448 |
$data = $this->Cookie->read('Encrytped_multi_cookies.tag');
|
|
|
449 |
$expected = 'CakePHP Rocks!';
|
|
|
450 |
$this->assertEquals($expected, $data);
|
|
|
451 |
|
|
|
452 |
$data = $this->Cookie->read('Plain_array.name');
|
|
|
453 |
$expected = 'CakePHP';
|
|
|
454 |
$this->assertEquals($expected, $data);
|
|
|
455 |
|
|
|
456 |
$data = $this->Cookie->read('Plain_array.version');
|
|
|
457 |
$expected = '1.2.0.x';
|
|
|
458 |
$this->assertEquals($expected, $data);
|
|
|
459 |
|
|
|
460 |
$data = $this->Cookie->read('Plain_array.tag');
|
|
|
461 |
$expected = 'CakePHP Rocks!';
|
|
|
462 |
$this->assertEquals($expected, $data);
|
|
|
463 |
|
|
|
464 |
$data = $this->Cookie->read('Plain_multi_cookies.name');
|
|
|
465 |
$expected = 'CakePHP';
|
|
|
466 |
$this->assertEquals($expected, $data);
|
|
|
467 |
|
|
|
468 |
$data = $this->Cookie->read('Plain_multi_cookies.version');
|
|
|
469 |
$expected = '1.2.0.x';
|
|
|
470 |
$this->assertEquals($expected, $data);
|
|
|
471 |
|
|
|
472 |
$data = $this->Cookie->read('Plain_multi_cookies.tag');
|
|
|
473 |
$expected = 'CakePHP Rocks!';
|
|
|
474 |
$this->assertEquals($expected, $data);
|
|
|
475 |
}
|
|
|
476 |
|
|
|
477 |
/**
|
|
|
478 |
* testReadingCookieDataOnStartup
|
|
|
479 |
*
|
|
|
480 |
* @return void
|
|
|
481 |
*/
|
|
|
482 |
public function testReadingCookieDataOnStartup() {
|
|
|
483 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
484 |
$this->assertNull($data);
|
|
|
485 |
|
|
|
486 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
487 |
$this->assertNull($data);
|
|
|
488 |
|
|
|
489 |
$data = $this->Cookie->read('Plain_array');
|
|
|
490 |
$this->assertNull($data);
|
|
|
491 |
|
|
|
492 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
493 |
$this->assertNull($data);
|
|
|
494 |
|
|
|
495 |
$_COOKIE['CakeTestCookie'] = array(
|
|
|
496 |
'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
|
|
|
497 |
'Encrytped_multi_cookies' => array(
|
|
|
498 |
'name' => $this->_encrypt('CakePHP'),
|
|
|
499 |
'version' => $this->_encrypt('1.2.0.x'),
|
|
|
500 |
'tag' => $this->_encrypt('CakePHP Rocks!')),
|
|
|
501 |
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
|
|
|
502 |
'Plain_multi_cookies' => array(
|
|
|
503 |
'name' => 'CakePHP',
|
|
|
504 |
'version' => '1.2.0.x',
|
|
|
505 |
'tag' => 'CakePHP Rocks!'));
|
|
|
506 |
|
|
|
507 |
$this->Cookie->startup(new CookieComponentTestController());
|
|
|
508 |
|
|
|
509 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
510 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
511 |
$this->assertEquals($expected, $data);
|
|
|
512 |
|
|
|
513 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
514 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
515 |
$this->assertEquals($expected, $data);
|
|
|
516 |
|
|
|
517 |
$data = $this->Cookie->read('Plain_array');
|
|
|
518 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
519 |
$this->assertEquals($expected, $data);
|
|
|
520 |
|
|
|
521 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
522 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
523 |
$this->assertEquals($expected, $data);
|
|
|
524 |
$this->Cookie->destroy();
|
|
|
525 |
unset($_COOKIE['CakeTestCookie']);
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
/**
|
|
|
529 |
* testReadingCookieDataWithoutStartup
|
|
|
530 |
*
|
|
|
531 |
* @return void
|
|
|
532 |
*/
|
|
|
533 |
public function testReadingCookieDataWithoutStartup() {
|
|
|
534 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
535 |
$expected = null;
|
|
|
536 |
$this->assertEquals($expected, $data);
|
|
|
537 |
|
|
|
538 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
539 |
$expected = null;
|
|
|
540 |
$this->assertEquals($expected, $data);
|
|
|
541 |
|
|
|
542 |
$data = $this->Cookie->read('Plain_array');
|
|
|
543 |
$expected = null;
|
|
|
544 |
$this->assertEquals($expected, $data);
|
|
|
545 |
|
|
|
546 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
547 |
$expected = null;
|
|
|
548 |
$this->assertEquals($expected, $data);
|
|
|
549 |
|
|
|
550 |
$_COOKIE['CakeTestCookie'] = array(
|
|
|
551 |
'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
|
|
|
552 |
'Encrytped_multi_cookies' => array(
|
|
|
553 |
'name' => $this->_encrypt('CakePHP'),
|
|
|
554 |
'version' => $this->_encrypt('1.2.0.x'),
|
|
|
555 |
'tag' => $this->_encrypt('CakePHP Rocks!')),
|
|
|
556 |
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
|
|
|
557 |
'Plain_multi_cookies' => array(
|
|
|
558 |
'name' => 'CakePHP',
|
|
|
559 |
'version' => '1.2.0.x',
|
|
|
560 |
'tag' => 'CakePHP Rocks!'));
|
|
|
561 |
|
|
|
562 |
$data = $this->Cookie->read('Encrytped_array');
|
|
|
563 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
564 |
$this->assertEquals($expected, $data);
|
|
|
565 |
|
|
|
566 |
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
|
|
567 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
568 |
$this->assertEquals($expected, $data);
|
|
|
569 |
|
|
|
570 |
$data = $this->Cookie->read('Plain_array');
|
|
|
571 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
572 |
$this->assertEquals($expected, $data);
|
|
|
573 |
|
|
|
574 |
$data = $this->Cookie->read('Plain_multi_cookies');
|
|
|
575 |
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
|
|
576 |
$this->assertEquals($expected, $data);
|
|
|
577 |
$this->Cookie->destroy();
|
|
|
578 |
unset($_COOKIE['CakeTestCookie']);
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
/**
|
|
|
582 |
* Test Reading legacy cookie values.
|
|
|
583 |
*
|
|
|
584 |
* @return void
|
|
|
585 |
*/
|
|
|
586 |
public function testReadLegacyCookieValue() {
|
|
|
587 |
$_COOKIE['CakeTestCookie'] = array(
|
|
|
588 |
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
|
|
|
589 |
);
|
|
|
590 |
$result = $this->Cookie->read('Legacy.value');
|
|
|
591 |
$expected = array(1, 2, 3);
|
|
|
592 |
$this->assertEquals($expected, $result);
|
|
|
593 |
}
|
|
|
594 |
|
|
|
595 |
/**
|
|
|
596 |
* Test reading empty values.
|
|
|
597 |
*/
|
|
|
598 |
public function testReadEmpty() {
|
|
|
599 |
$_COOKIE['CakeTestCookie'] = array(
|
|
|
600 |
'JSON' => '{"name":"value"}',
|
|
|
601 |
'Empty' => '',
|
|
|
602 |
'String' => '{"somewhat:"broken"}',
|
|
|
603 |
'Array' => '{}'
|
|
|
604 |
);
|
|
|
605 |
$this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
|
|
|
606 |
$this->assertEquals('value', $this->Cookie->read('JSON.name'));
|
|
|
607 |
$this->assertEquals('', $this->Cookie->read('Empty'));
|
|
|
608 |
$this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
|
|
|
609 |
$this->assertEquals(array(), $this->Cookie->read('Array'));
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
/**
|
|
|
613 |
* test that no error is issued for non array data.
|
|
|
614 |
*
|
|
|
615 |
* @return void
|
|
|
616 |
*/
|
|
|
617 |
public function testNoErrorOnNonArrayData() {
|
|
|
618 |
$this->Cookie->destroy();
|
|
|
619 |
$_COOKIE['CakeTestCookie'] = 'kaboom';
|
|
|
620 |
|
|
|
621 |
$this->assertNull($this->Cookie->read('value'));
|
|
|
622 |
}
|
|
|
623 |
|
|
|
624 |
/**
|
|
|
625 |
* testCheck method
|
|
|
626 |
*
|
|
|
627 |
* @return void
|
|
|
628 |
*/
|
|
|
629 |
public function testCheck() {
|
|
|
630 |
$this->Cookie->write('CookieComponentTestCase', 'value');
|
|
|
631 |
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
|
|
|
632 |
|
|
|
633 |
$this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
|
|
|
634 |
}
|
|
|
635 |
|
|
|
636 |
/**
|
|
|
637 |
* testCheckingSavedEmpty method
|
|
|
638 |
*
|
|
|
639 |
* @return void
|
|
|
640 |
*/
|
|
|
641 |
public function testCheckingSavedEmpty() {
|
|
|
642 |
$this->Cookie->write('CookieComponentTestCase', 0);
|
|
|
643 |
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
|
|
|
644 |
|
|
|
645 |
$this->Cookie->write('CookieComponentTestCase', '0');
|
|
|
646 |
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
|
|
|
647 |
|
|
|
648 |
$this->Cookie->write('CookieComponentTestCase', false);
|
|
|
649 |
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
|
|
|
650 |
|
|
|
651 |
$this->Cookie->write('CookieComponentTestCase', null);
|
|
|
652 |
$this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
|
|
|
653 |
}
|
|
|
654 |
|
|
|
655 |
/**
|
|
|
656 |
* testCheckKeyWithSpaces method
|
|
|
657 |
*
|
|
|
658 |
* @return void
|
|
|
659 |
*/
|
|
|
660 |
public function testCheckKeyWithSpaces() {
|
|
|
661 |
$this->Cookie->write('CookieComponent Test', "test");
|
|
|
662 |
$this->assertTrue($this->Cookie->check('CookieComponent Test'));
|
|
|
663 |
$this->Cookie->delete('CookieComponent Test');
|
|
|
664 |
|
|
|
665 |
$this->Cookie->write('CookieComponent Test.Test Case', "test");
|
|
|
666 |
$this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
/**
|
|
|
670 |
* testCheckEmpty
|
|
|
671 |
*
|
|
|
672 |
* @return void
|
|
|
673 |
*/
|
|
|
674 |
public function testCheckEmpty() {
|
|
|
675 |
$this->assertFalse($this->Cookie->check());
|
|
|
676 |
}
|
|
|
677 |
|
|
|
678 |
/**
|
|
|
679 |
* test that deleting a top level keys kills the child elements too.
|
|
|
680 |
*
|
|
|
681 |
* @return void
|
|
|
682 |
*/
|
|
|
683 |
public function testDeleteRemovesChildren() {
|
|
|
684 |
$_COOKIE['CakeTestCookie'] = array(
|
|
|
685 |
'User' => array('email' => 'example@example.com', 'name' => 'mark'),
|
|
|
686 |
'other' => 'value'
|
|
|
687 |
);
|
|
|
688 |
$this->assertEquals('mark', $this->Cookie->read('User.name'));
|
|
|
689 |
|
|
|
690 |
$this->Cookie->delete('User');
|
|
|
691 |
$this->assertNull($this->Cookie->read('User.email'));
|
|
|
692 |
$this->Cookie->destroy();
|
|
|
693 |
}
|
|
|
694 |
|
|
|
695 |
/**
|
|
|
696 |
* Test deleting recursively with keys that don't exist.
|
|
|
697 |
*
|
|
|
698 |
* @return void
|
|
|
699 |
*/
|
|
|
700 |
public function testDeleteChildrenNotExist() {
|
|
|
701 |
$this->assertNull($this->Cookie->delete('NotFound'));
|
|
|
702 |
$this->assertNull($this->Cookie->delete('Not.Found'));
|
|
|
703 |
}
|
|
|
704 |
|
|
|
705 |
/**
|
|
|
706 |
* Helper method for generating old style encoded cookie values.
|
|
|
707 |
*
|
|
|
708 |
* @return string.
|
|
|
709 |
*/
|
|
|
710 |
protected function _oldImplode(array $array) {
|
|
|
711 |
$string = '';
|
|
|
712 |
foreach ($array as $key => $value) {
|
|
|
713 |
$string .= ',' . $key . '|' . $value;
|
|
|
714 |
}
|
|
|
715 |
return substr($string, 1);
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
/**
|
|
|
719 |
* Implode method to keep keys are multidimensional arrays
|
|
|
720 |
*
|
|
|
721 |
* @param array $array Map of key and values
|
|
|
722 |
* @return string String in the form key1|value1,key2|value2
|
|
|
723 |
*/
|
|
|
724 |
protected function _implode(array $array) {
|
|
|
725 |
return json_encode($array);
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
/**
|
|
|
729 |
* encrypt method
|
|
|
730 |
*
|
|
|
731 |
* @param array|string $value
|
|
|
732 |
* @return string
|
|
|
733 |
*/
|
|
|
734 |
protected function _encrypt($value) {
|
|
|
735 |
if (is_array($value)) {
|
|
|
736 |
$value = $this->_implode($value);
|
|
|
737 |
}
|
|
|
738 |
return "Q2FrZQ==." . base64_encode(Security::cipher($value, $this->Cookie->key));
|
|
|
739 |
}
|
|
|
740 |
|
|
|
741 |
}
|