統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Controller / Component / CookieComponentTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (21.584 KB)

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 write() encrypted data with falsey value
206
 *
207
 * @return void
208
 */
209
        public function testWriteWithFalseyValue() {
210
                $this->Cookie->type('aes');
211
                $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
212

    
213
                $this->Cookie->write('Testing');
214
                $result = $this->Cookie->read('Testing');
215
                $this->assertNull($result);
216

    
217
                $this->Cookie->write('Testing', '');
218
                $result = $this->Cookie->read('Testing');
219
                $this->assertEquals('', $result);
220

    
221
                $this->Cookie->write('Testing', false);
222
                $result = $this->Cookie->read('Testing');
223
                $this->assertFalse($result);
224

    
225
                $this->Cookie->write('Testing', 1);
226
                $result = $this->Cookie->read('Testing');
227
                $this->assertEquals(1, $result);
228

    
229
                $this->Cookie->write('Testing', '0');
230
                $result = $this->Cookie->read('Testing');
231
                $this->assertSame('0', $result);
232

    
233
                $this->Cookie->write('Testing', 0);
234
                $result = $this->Cookie->read('Testing');
235
                $this->assertSame(0, $result);
236
        }
237

    
238
/**
239
 * test that two write() calls use the expiry.
240
 *
241
 * @return void
242
 */
243
        public function testWriteMultipleShareExpiry() {
244
                $this->Cookie->write('key1', 'value1', false);
245
                $this->Cookie->write('key2', 'value2', false);
246

    
247
                $name = $this->Cookie->name . '[key1]';
248
                $result = $this->Controller->response->cookie($name);
249
                $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
250

    
251
                $name = $this->Cookie->name . '[key2]';
252
                $result = $this->Controller->response->cookie($name);
253
                $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
254
        }
255

    
256
/**
257
 * test write with distant future cookies
258
 *
259
 * @return void
260
 */
261
        public function testWriteFarFuture() {
262
                $this->Cookie->write('Testing', 'value', false, '+90 years');
263
                $future = new DateTime('now');
264
                $future->modify('+90 years');
265

    
266
                $expected = array(
267
                        'name' => $this->Cookie->name . '[Testing]',
268
                        'value' => 'value',
269
                        'path' => '/',
270
                        'domain' => '',
271
                        'secure' => false,
272
                        'httpOnly' => false);
273
                $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
274

    
275
                $this->assertEquals($future->format('U'), $result['expire'], '', 3);
276
                unset($result['expire']);
277

    
278
                $this->assertEquals($expected, $result);
279
        }
280

    
281
/**
282
 * test write with httpOnly cookies
283
 *
284
 * @return void
285
 */
286
        public function testWriteHttpOnly() {
287
                $this->Cookie->httpOnly = true;
288
                $this->Cookie->secure = false;
289
                $this->Cookie->write('Testing', 'value', false);
290
                $expected = array(
291
                        'name' => $this->Cookie->name . '[Testing]',
292
                        'value' => 'value',
293
                        'expire' => time() + 10,
294
                        'path' => '/',
295
                        'domain' => '',
296
                        'secure' => false,
297
                        'httpOnly' => true);
298
                $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
299
                $this->assertEquals($expected, $result);
300
        }
301

    
302
/**
303
 * test delete with httpOnly
304
 *
305
 * @return void
306
 */
307
        public function testDeleteHttpOnly() {
308
                $this->Cookie->httpOnly = true;
309
                $this->Cookie->secure = false;
310
                $this->Cookie->delete('Testing', false);
311
                $expected = array(
312
                        'name' => $this->Cookie->name . '[Testing]',
313
                        'value' => '',
314
                        'expire' => time() - 42000,
315
                        'path' => '/',
316
                        'domain' => '',
317
                        'secure' => false,
318
                        'httpOnly' => true);
319
                $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
320
                $this->assertEquals($expected, $result);
321
        }
322

    
323
/**
324
 * testWritePlainCookieArray
325
 *
326
 * @return void
327
 */
328
        public function testWritePlainCookieArray() {
329
                $this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
330

    
331
                $this->assertEquals('CakePHP', $this->Cookie->read('name'));
332
                $this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
333
                $this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
334

    
335
                $this->Cookie->delete('name');
336
                $this->Cookie->delete('version');
337
                $this->Cookie->delete('tag');
338
        }
339

    
340
/**
341
 * test writing values that are not scalars
342
 *
343
 * @return void
344
 */
345
        public function testWriteArrayValues() {
346
                $this->Cookie->secure = false;
347
                $this->Cookie->write('Testing', array(1, 2, 3), false);
348
                $expected = array(
349
                        'name' => $this->Cookie->name . '[Testing]',
350
                        'value' => '[1,2,3]',
351
                        'path' => '/',
352
                        'domain' => '',
353
                        'secure' => false,
354
                        'httpOnly' => false);
355
                $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
356

    
357
                $this->assertWithinMargin($result['expire'], time() + 10, 1);
358
                unset($result['expire']);
359
                $this->assertEquals($expected, $result);
360
        }
361

    
362
/**
363
 * Test that writing mixed arrays results in the correct data.
364
 *
365
 * @return void
366
 */
367
        public function testWriteMixedArray() {
368
                $this->Cookie->encrypt = false;
369
                $this->Cookie->write('User', array('name' => 'mark'), false);
370
                $this->Cookie->write('User.email', 'mark@example.com', false);
371
                $expected = array(
372
                        'name' => $this->Cookie->name . '[User]',
373
                        'value' => '{"name":"mark","email":"mark@example.com"}',
374
                        'path' => '/',
375
                        'domain' => '',
376
                        'secure' => false,
377
                        'httpOnly' => false
378
                );
379
                $result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
380
                unset($result['expire']);
381

    
382
                $this->assertEquals($expected, $result);
383

    
384
                $this->Cookie->write('User.email', 'mark@example.com', false);
385
                $this->Cookie->write('User', array('name' => 'mark'), false);
386
                $expected = array(
387
                        'name' => $this->Cookie->name . '[User]',
388
                        'value' => '{"name":"mark"}',
389
                        'path' => '/',
390
                        'domain' => '',
391
                        'secure' => false,
392
                        'httpOnly' => false
393
                );
394
                $result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
395
                unset($result['expire']);
396

    
397
                $this->assertEquals($expected, $result);
398
        }
399

    
400
/**
401
 * testReadingCookieValue
402
 *
403
 * @return void
404
 */
405
        public function testReadingCookieValue() {
406
                $this->_setCookieData();
407
                $data = $this->Cookie->read();
408
                $expected = array(
409
                        'Encrytped_array' => array(
410
                                'name' => 'CakePHP',
411
                                'version' => '1.2.0.x',
412
                                'tag' => 'CakePHP Rocks!'),
413
                        'Encrytped_multi_cookies' => array(
414
                                'name' => 'CakePHP',
415
                                'version' => '1.2.0.x',
416
                                'tag' => 'CakePHP Rocks!'),
417
                        'Plain_array' => array(
418
                                'name' => 'CakePHP',
419
                                'version' => '1.2.0.x',
420
                                'tag' => 'CakePHP Rocks!'),
421
                        'Plain_multi_cookies' => array(
422
                                'name' => 'CakePHP',
423
                                'version' => '1.2.0.x',
424
                                'tag' => 'CakePHP Rocks!'));
425
                $this->assertEquals($expected, $data);
426
        }
427

    
428
/**
429
 * testDeleteCookieValue
430
 *
431
 * @return void
432
 */
433
        public function testDeleteCookieValue() {
434
                $this->_setCookieData();
435
                $this->Cookie->delete('Encrytped_multi_cookies.name');
436
                $data = $this->Cookie->read('Encrytped_multi_cookies');
437
                $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
438
                $this->assertEquals($expected, $data);
439

    
440
                $this->Cookie->delete('Encrytped_array');
441
                $data = $this->Cookie->read('Encrytped_array');
442
                $this->assertNull($data);
443

    
444
                $this->Cookie->delete('Plain_multi_cookies.name');
445
                $data = $this->Cookie->read('Plain_multi_cookies');
446
                $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
447
                $this->assertEquals($expected, $data);
448

    
449
                $this->Cookie->delete('Plain_array');
450
                $data = $this->Cookie->read('Plain_array');
451
                $this->assertNull($data);
452
        }
453

    
454
/**
455
 * testReadingCookieArray
456
 *
457
 * @return void
458
 */
459
        public function testReadingCookieArray() {
460
                $this->_setCookieData();
461

    
462
                $data = $this->Cookie->read('Encrytped_array.name');
463
                $expected = 'CakePHP';
464
                $this->assertEquals($expected, $data);
465

    
466
                $data = $this->Cookie->read('Encrytped_array.version');
467
                $expected = '1.2.0.x';
468
                $this->assertEquals($expected, $data);
469

    
470
                $data = $this->Cookie->read('Encrytped_array.tag');
471
                $expected = 'CakePHP Rocks!';
472
                $this->assertEquals($expected, $data);
473

    
474
                $data = $this->Cookie->read('Encrytped_multi_cookies.name');
475
                $expected = 'CakePHP';
476
                $this->assertEquals($expected, $data);
477

    
478
                $data = $this->Cookie->read('Encrytped_multi_cookies.version');
479
                $expected = '1.2.0.x';
480
                $this->assertEquals($expected, $data);
481

    
482
                $data = $this->Cookie->read('Encrytped_multi_cookies.tag');
483
                $expected = 'CakePHP Rocks!';
484
                $this->assertEquals($expected, $data);
485

    
486
                $data = $this->Cookie->read('Plain_array.name');
487
                $expected = 'CakePHP';
488
                $this->assertEquals($expected, $data);
489

    
490
                $data = $this->Cookie->read('Plain_array.version');
491
                $expected = '1.2.0.x';
492
                $this->assertEquals($expected, $data);
493

    
494
                $data = $this->Cookie->read('Plain_array.tag');
495
                $expected = 'CakePHP Rocks!';
496
                $this->assertEquals($expected, $data);
497

    
498
                $data = $this->Cookie->read('Plain_multi_cookies.name');
499
                $expected = 'CakePHP';
500
                $this->assertEquals($expected, $data);
501

    
502
                $data = $this->Cookie->read('Plain_multi_cookies.version');
503
                $expected = '1.2.0.x';
504
                $this->assertEquals($expected, $data);
505

    
506
                $data = $this->Cookie->read('Plain_multi_cookies.tag');
507
                $expected = 'CakePHP Rocks!';
508
                $this->assertEquals($expected, $data);
509
        }
510

    
511
/**
512
 * testReadingCookieDataOnStartup
513
 *
514
 * @return void
515
 */
516
        public function testReadingCookieDataOnStartup() {
517
                $data = $this->Cookie->read('Encrytped_array');
518
                $this->assertNull($data);
519

    
520
                $data = $this->Cookie->read('Encrytped_multi_cookies');
521
                $this->assertNull($data);
522

    
523
                $data = $this->Cookie->read('Plain_array');
524
                $this->assertNull($data);
525

    
526
                $data = $this->Cookie->read('Plain_multi_cookies');
527
                $this->assertNull($data);
528

    
529
                $_COOKIE['CakeTestCookie'] = array(
530
                                'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
531
                                'Encrytped_multi_cookies' => array(
532
                                                'name' => $this->_encrypt('CakePHP'),
533
                                                'version' => $this->_encrypt('1.2.0.x'),
534
                                                'tag' => $this->_encrypt('CakePHP Rocks!')),
535
                                'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
536
                                'Plain_multi_cookies' => array(
537
                                                'name' => 'CakePHP',
538
                                                'version' => '1.2.0.x',
539
                                                'tag' => 'CakePHP Rocks!'));
540

    
541
                $this->Cookie->startup(new CookieComponentTestController());
542

    
543
                $data = $this->Cookie->read('Encrytped_array');
544
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
545
                $this->assertEquals($expected, $data);
546

    
547
                $data = $this->Cookie->read('Encrytped_multi_cookies');
548
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
549
                $this->assertEquals($expected, $data);
550

    
551
                $data = $this->Cookie->read('Plain_array');
552
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
553
                $this->assertEquals($expected, $data);
554

    
555
                $data = $this->Cookie->read('Plain_multi_cookies');
556
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
557
                $this->assertEquals($expected, $data);
558
                $this->Cookie->destroy();
559
                unset($_COOKIE['CakeTestCookie']);
560
        }
561

    
562
/**
563
 * testReadingCookieDataWithoutStartup
564
 *
565
 * @return void
566
 */
567
        public function testReadingCookieDataWithoutStartup() {
568
                $data = $this->Cookie->read('Encrytped_array');
569
                $expected = null;
570
                $this->assertEquals($expected, $data);
571

    
572
                $data = $this->Cookie->read('Encrytped_multi_cookies');
573
                $expected = null;
574
                $this->assertEquals($expected, $data);
575

    
576
                $data = $this->Cookie->read('Plain_array');
577
                $expected = null;
578
                $this->assertEquals($expected, $data);
579

    
580
                $data = $this->Cookie->read('Plain_multi_cookies');
581
                $expected = null;
582
                $this->assertEquals($expected, $data);
583

    
584
                $_COOKIE['CakeTestCookie'] = array(
585
                                'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
586
                                'Encrytped_multi_cookies' => array(
587
                                                'name' => $this->_encrypt('CakePHP'),
588
                                                'version' => $this->_encrypt('1.2.0.x'),
589
                                                'tag' => $this->_encrypt('CakePHP Rocks!')),
590
                                'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
591
                                'Plain_multi_cookies' => array(
592
                                                'name' => 'CakePHP',
593
                                                'version' => '1.2.0.x',
594
                                                'tag' => 'CakePHP Rocks!'));
595

    
596
                $data = $this->Cookie->read('Encrytped_array');
597
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
598
                $this->assertEquals($expected, $data);
599

    
600
                $data = $this->Cookie->read('Encrytped_multi_cookies');
601
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
602
                $this->assertEquals($expected, $data);
603

    
604
                $data = $this->Cookie->read('Plain_array');
605
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
606
                $this->assertEquals($expected, $data);
607

    
608
                $data = $this->Cookie->read('Plain_multi_cookies');
609
                $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
610
                $this->assertEquals($expected, $data);
611
                $this->Cookie->destroy();
612
                unset($_COOKIE['CakeTestCookie']);
613
        }
614

    
615
/**
616
 * Test Reading legacy cookie values.
617
 *
618
 * @return void
619
 */
620
        public function testReadLegacyCookieValue() {
621
                $_COOKIE['CakeTestCookie'] = array(
622
                        'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
623
                );
624
                $result = $this->Cookie->read('Legacy.value');
625
                $expected = array(1, 2, 3);
626
                $this->assertEquals($expected, $result);
627
        }
628

    
629
/**
630
 * Test reading empty values.
631
 *
632
 * @return void
633
 */
634
        public function testReadEmpty() {
635
                $_COOKIE['CakeTestCookie'] = array(
636
                        'JSON' => '{"name":"value"}',
637
                        'Empty' => '',
638
                        'String' => '{"somewhat:"broken"}',
639
                        'Array' => '{}'
640
                );
641
                $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
642
                $this->assertEquals('value', $this->Cookie->read('JSON.name'));
643
                $this->assertEquals('', $this->Cookie->read('Empty'));
644
                $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
645
                $this->assertEquals(array(), $this->Cookie->read('Array'));
646
        }
647

    
648
/**
649
 * test that no error is issued for non array data.
650
 *
651
 * @return void
652
 */
653
        public function testNoErrorOnNonArrayData() {
654
                $this->Cookie->destroy();
655
                $_COOKIE['CakeTestCookie'] = 'kaboom';
656

    
657
                $this->assertNull($this->Cookie->read('value'));
658
        }
659

    
660
/**
661
 * testCheck method
662
 *
663
 * @return void
664
 */
665
        public function testCheck() {
666
                $this->Cookie->write('CookieComponentTestCase', 'value');
667
                $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
668

    
669
                $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
670
        }
671

    
672
/**
673
 * testCheckingSavedEmpty method
674
 *
675
 * @return void
676
 */
677
        public function testCheckingSavedEmpty() {
678
                $this->Cookie->write('CookieComponentTestCase', 0);
679
                $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
680

    
681
                $this->Cookie->write('CookieComponentTestCase', '0');
682
                $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
683

    
684
                $this->Cookie->write('CookieComponentTestCase', false);
685
                $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
686

    
687
                $this->Cookie->write('CookieComponentTestCase', null);
688
                $this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
689
        }
690

    
691
/**
692
 * testCheckKeyWithSpaces method
693
 *
694
 * @return void
695
 */
696
        public function testCheckKeyWithSpaces() {
697
                $this->Cookie->write('CookieComponent Test', "test");
698
                $this->assertTrue($this->Cookie->check('CookieComponent Test'));
699
                $this->Cookie->delete('CookieComponent Test');
700

    
701
                $this->Cookie->write('CookieComponent Test.Test Case', "test");
702
                $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
703
        }
704

    
705
/**
706
 * testCheckEmpty
707
 *
708
 * @return void
709
 */
710
        public function testCheckEmpty() {
711
                $this->assertFalse($this->Cookie->check());
712
        }
713

    
714
/**
715
 * test that deleting a top level keys kills the child elements too.
716
 *
717
 * @return void
718
 */
719
        public function testDeleteRemovesChildren() {
720
                $_COOKIE['CakeTestCookie'] = array(
721
                        'User' => array('email' => 'example@example.com', 'name' => 'mark'),
722
                        'other' => 'value'
723
                );
724
                $this->assertEquals('mark', $this->Cookie->read('User.name'));
725

    
726
                $this->Cookie->delete('User');
727
                $this->assertNull($this->Cookie->read('User.email'));
728
                $this->Cookie->destroy();
729
        }
730

    
731
/**
732
 * Test deleting recursively with keys that don't exist.
733
 *
734
 * @return void
735
 */
736
        public function testDeleteChildrenNotExist() {
737
                $this->assertNull($this->Cookie->delete('NotFound'));
738
                $this->assertNull($this->Cookie->delete('Not.Found'));
739
        }
740

    
741
/**
742
 * Helper method for generating old style encoded cookie values.
743
 *
744
 * @return string.
745
 */
746
        protected function _oldImplode(array $array) {
747
                $string = '';
748
                foreach ($array as $key => $value) {
749
                        $string .= ',' . $key . '|' . $value;
750
                }
751
                return substr($string, 1);
752
        }
753

    
754
/**
755
 * Implode method to keep keys are multidimensional arrays
756
 *
757
 * @param array $array Map of key and values
758
 * @return string String in the form key1|value1,key2|value2
759
 */
760
        protected function _implode(array $array) {
761
                return json_encode($array);
762
        }
763

    
764
/**
765
 * encrypt method
766
 *
767
 * @param array|string $value
768
 * @return string
769
 */
770
        protected function _encrypt($value) {
771
                if (is_array($value)) {
772
                        $value = $this->_implode($value);
773
                }
774
                return "Q2FrZQ==." . base64_encode(Security::cipher($value, $this->Cookie->key));
775
        }
776

    
777
}