CookieComponentTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\CookieComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Event\Event;
  20. use Cake\Network\Request;
  21. use Cake\Network\Response;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Security;
  24. /**
  25. * CookieComponentTest class
  26. */
  27. class CookieComponentTest extends TestCase {
  28. /**
  29. * start
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $controller = $this->getMock(
  36. 'Cake\Controller\Controller',
  37. array('redirect'),
  38. array(new Request(), new Response())
  39. );
  40. $controller->components = array('Cookie');
  41. $controller->constructClasses();
  42. $this->Controller = $controller;
  43. $this->Cookie = $controller->Cookie;
  44. $this->request = $controller->request;
  45. $this->Cookie->config([
  46. 'expires' => '+10 seconds',
  47. 'path' => '/',
  48. 'domain' => '',
  49. 'secure' => false,
  50. 'key' => 'somerandomhaskeysomerandomhaskey',
  51. 'encryption' => false,
  52. ]);
  53. }
  54. /**
  55. * Test setting config per key.
  56. *
  57. * @return void
  58. */
  59. public function testConfigKey() {
  60. $this->Cookie->configKey('User', 'expires', '+3 days');
  61. $result = $this->Cookie->configKey('User');
  62. $expected = [
  63. 'expires' => '+3 days',
  64. 'path' => '/',
  65. 'domain' => '',
  66. 'key' => 'somerandomhaskeysomerandomhaskey',
  67. 'secure' => false,
  68. 'httpOnly' => false,
  69. 'encryption' => false,
  70. ];
  71. $this->assertEquals($expected, $result);
  72. }
  73. /**
  74. * Test setting config per key.
  75. *
  76. * @return void
  77. */
  78. public function testConfigKeyArray() {
  79. $this->Cookie->configKey('User', [
  80. 'expires' => '+3 days',
  81. 'path' => '/shop'
  82. ]);
  83. $result = $this->Cookie->configKey('User');
  84. $expected = [
  85. 'expires' => '+3 days',
  86. 'path' => '/shop',
  87. 'domain' => '',
  88. 'key' => 'somerandomhaskeysomerandomhaskey',
  89. 'secure' => false,
  90. 'httpOnly' => false,
  91. 'encryption' => false,
  92. ];
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * sets up some default cookie data.
  97. *
  98. * @return void
  99. */
  100. protected function _setCookieData() {
  101. $this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
  102. $this->Cookie->write(array('Encrypted_multi_cookies.name' => 'CakePHP'));
  103. $this->Cookie->write(array('Encrypted_multi_cookies.version' => '1.2.0.x'));
  104. $this->Cookie->write(array('Encrypted_multi_cookies.tag' => 'CakePHP Rocks!'));
  105. $this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
  106. $this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
  107. $this->Cookie->write(array('Plain_multi_cookies.version' => '1.2.0.x'), null, false);
  108. $this->Cookie->write(array('Plain_multi_cookies.tag' => 'CakePHP Rocks!'), null, false);
  109. }
  110. /**
  111. * test that initialize sets settings from components array
  112. *
  113. * @return void
  114. */
  115. public function testSettings() {
  116. $settings = array(
  117. 'time' => '5 days',
  118. 'path' => '/'
  119. );
  120. $Cookie = new CookieComponent(new ComponentRegistry(), $settings);
  121. $this->assertEquals($Cookie->config('time'), $settings['time']);
  122. $this->assertEquals($Cookie->config('path'), $settings['path']);
  123. }
  124. /**
  125. * testReadEncryptedCookieData
  126. *
  127. * @return void
  128. */
  129. public function testReadEncryptedCookieData() {
  130. $this->_setCookieData();
  131. $data = $this->Cookie->read('Encrytped_array');
  132. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  133. $this->assertEquals($expected, $data);
  134. $data = $this->Cookie->read('Encrypted_multi_cookies');
  135. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  136. $this->assertEquals($expected, $data);
  137. }
  138. /**
  139. * testReadPlainCookieData
  140. *
  141. * @return void
  142. */
  143. public function testReadPlainCookieData() {
  144. $this->_setCookieData();
  145. $data = $this->Cookie->read('Plain_array');
  146. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  147. $this->assertEquals($expected, $data);
  148. $data = $this->Cookie->read('Plain_multi_cookies');
  149. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  150. $this->assertEquals($expected, $data);
  151. }
  152. /**
  153. * test read() after switching the cookie name.
  154. *
  155. * @return void
  156. */
  157. public function testReadMultipleNames() {
  158. $this->request->cookies = array(
  159. 'CakeCookie' => array(
  160. 'key' => 'value'
  161. ),
  162. 'OtherCookie' => array(
  163. 'key' => 'other value'
  164. )
  165. );
  166. $this->assertEquals('value', $this->Cookie->read('CakeCookie.key'));
  167. $this->assertEquals(['key' => 'value'], $this->Cookie->read('CakeCookie'));
  168. $this->assertEquals('other value', $this->Cookie->read('OtherCookie.key'));
  169. }
  170. /**
  171. * test a simple write()
  172. *
  173. * @return void
  174. */
  175. public function testWriteSimple() {
  176. $this->Cookie->write('Testing', 'value');
  177. $result = $this->Cookie->read('Testing');
  178. $this->assertEquals('value', $result);
  179. }
  180. /**
  181. * test write() encrypted data with falsey value
  182. *
  183. * @return void
  184. */
  185. public function testWriteWithFalseyValue() {
  186. $this->Cookie->encryption('aes');
  187. $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
  188. $this->Cookie->write('Testing');
  189. $result = $this->Cookie->read('Testing');
  190. $this->assertNull($result);
  191. $this->Cookie->write('Testing', '');
  192. $result = $this->Cookie->read('Testing');
  193. $this->assertEquals('', $result);
  194. $this->Cookie->write('Testing', false);
  195. $result = $this->Cookie->read('Testing');
  196. $this->assertFalse($result);
  197. $this->Cookie->write('Testing', 1);
  198. $result = $this->Cookie->read('Testing');
  199. $this->assertEquals(1, $result);
  200. $this->Cookie->write('Testing', '0');
  201. $result = $this->Cookie->read('Testing');
  202. $this->assertSame('0', $result);
  203. $this->Cookie->write('Testing', 0);
  204. $result = $this->Cookie->read('Testing');
  205. $this->assertSame(0, $result);
  206. }
  207. /**
  208. * test write with distant future cookies
  209. *
  210. * @return void
  211. */
  212. public function testWriteFarFuture() {
  213. $this->Cookie->configKey('Testing', 'expires', '+90 years');
  214. $this->Cookie->write('Testing', 'value');
  215. $future = new \DateTime('now');
  216. $future->modify('+90 years');
  217. $expected = array(
  218. 'name' => 'Testing',
  219. 'value' => 'value',
  220. 'path' => '/',
  221. 'domain' => '',
  222. 'secure' => false,
  223. 'httpOnly' => false);
  224. $result = $this->Controller->response->cookie('Testing');
  225. $this->assertEquals($future->format('U'), $result['expire'], '', 3);
  226. unset($result['expire']);
  227. $this->assertEquals($expected, $result);
  228. }
  229. /**
  230. * test write with httpOnly cookies
  231. *
  232. * @return void
  233. */
  234. public function testWriteHttpOnly() {
  235. $this->Cookie->config([
  236. 'httpOnly' => true,
  237. 'secure' => false
  238. ]);
  239. $this->Cookie->write('Testing', 'value', false);
  240. $expected = array(
  241. 'name' => 'Testing',
  242. 'value' => 'value',
  243. 'expire' => time() + 10,
  244. 'path' => '/',
  245. 'domain' => '',
  246. 'secure' => false,
  247. 'httpOnly' => true);
  248. $result = $this->Controller->response->cookie('Testing');
  249. $this->assertEquals($expected, $result);
  250. }
  251. /**
  252. * Test writing multiple nested keys when some are encrypted.
  253. *
  254. * @return void
  255. */
  256. public function testWriteMulitMixedEncryption() {
  257. $this->Cookie->configKey('Open', 'encryption', false);
  258. $this->Cookie->configKey('Closed', 'encryption', 'aes');
  259. $this->Cookie->write([
  260. 'Closed.key' => 'secret',
  261. 'Open.key' => 'not secret',
  262. ]);
  263. $expected = array(
  264. 'name' => 'Open',
  265. 'value' => '{"key":"not secret"}',
  266. 'path' => '/',
  267. 'domain' => '',
  268. 'secure' => false,
  269. 'httpOnly' => false
  270. );
  271. $result = $this->Controller->response->cookie('Open');
  272. unset($result['expire']);
  273. $this->assertEquals($expected, $result);
  274. $result = $this->Controller->response->cookie('Closed');
  275. $this->assertContains('Q2FrZQ==.', $result['value']);
  276. }
  277. /**
  278. * test delete with httpOnly
  279. *
  280. * @return void
  281. */
  282. public function testDeleteHttpOnly() {
  283. $this->Cookie->config([
  284. 'httpOnly' => true,
  285. 'secure' => false
  286. ]);
  287. $this->Cookie->delete('Testing');
  288. $expected = array(
  289. 'name' => 'Testing',
  290. 'value' => '',
  291. 'expire' => time() - 42000,
  292. 'path' => '/',
  293. 'domain' => '',
  294. 'secure' => false,
  295. 'httpOnly' => true);
  296. $result = $this->Controller->response->cookie('Testing');
  297. $this->assertEquals($expected, $result);
  298. }
  299. /**
  300. * test writing values that are not scalars
  301. *
  302. * @return void
  303. */
  304. public function testWriteArrayValues() {
  305. $this->Cookie->write('Testing', array(1, 2, 3));
  306. $expected = array(
  307. 'name' => 'Testing',
  308. 'value' => '[1,2,3]',
  309. 'path' => '/',
  310. 'domain' => '',
  311. 'secure' => false,
  312. 'httpOnly' => false
  313. );
  314. $result = $this->Controller->response->cookie('Testing');
  315. $this->assertWithinMargin($result['expire'], time() + 10, 1);
  316. unset($result['expire']);
  317. $this->assertEquals($expected, $result);
  318. }
  319. /**
  320. * Test that writing mixed arrays results in the correct data.
  321. *
  322. * @return void
  323. */
  324. public function testWriteMixedArray() {
  325. $this->Cookie->write('User', array('name' => 'mark'), false);
  326. $this->Cookie->write('User.email', 'mark@example.com', false);
  327. $expected = array(
  328. 'name' => 'User',
  329. 'value' => '{"name":"mark","email":"mark@example.com"}',
  330. 'path' => '/',
  331. 'domain' => '',
  332. 'secure' => false,
  333. 'httpOnly' => false
  334. );
  335. $result = $this->Controller->response->cookie('User');
  336. unset($result['expire']);
  337. $this->assertEquals($expected, $result);
  338. $this->Cookie->write('User.email', 'mark@example.com', false);
  339. $this->Cookie->write('User', array('name' => 'mark'), false);
  340. $expected = array(
  341. 'name' => 'User',
  342. 'value' => '{"name":"mark"}',
  343. 'path' => '/',
  344. 'domain' => '',
  345. 'secure' => false,
  346. 'httpOnly' => false
  347. );
  348. $result = $this->Controller->response->cookie('User');
  349. unset($result['expire']);
  350. $this->assertEquals($expected, $result);
  351. }
  352. /**
  353. * test reading all values at once.
  354. *
  355. * @return void
  356. */
  357. public function testReadingAllValues() {
  358. $this->_setCookieData();
  359. $data = $this->Cookie->read();
  360. $expected = array(
  361. 'Encrytped_array' => array(
  362. 'name' => 'CakePHP',
  363. 'version' => '1.2.0.x',
  364. 'tag' => 'CakePHP Rocks!'),
  365. 'Encrypted_multi_cookies' => array(
  366. 'name' => 'CakePHP',
  367. 'version' => '1.2.0.x',
  368. 'tag' => 'CakePHP Rocks!'),
  369. 'Plain_array' => array(
  370. 'name' => 'CakePHP',
  371. 'version' => '1.2.0.x',
  372. 'tag' => 'CakePHP Rocks!'),
  373. 'Plain_multi_cookies' => array(
  374. 'name' => 'CakePHP',
  375. 'version' => '1.2.0.x',
  376. 'tag' => 'CakePHP Rocks!'));
  377. $this->assertEquals($expected, $data);
  378. }
  379. /**
  380. * testDeleteCookieValue
  381. *
  382. * @return void
  383. */
  384. public function testDeleteCookieValue() {
  385. $this->_setCookieData();
  386. $this->Cookie->delete('Encrypted_multi_cookies.name');
  387. $data = $this->Cookie->read('Encrypted_multi_cookies');
  388. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  389. $this->assertEquals($expected, $data);
  390. $this->Cookie->delete('Encrytped_array');
  391. $data = $this->Cookie->read('Encrytped_array');
  392. $this->assertNull($data);
  393. $this->Cookie->delete('Plain_multi_cookies.name');
  394. $data = $this->Cookie->read('Plain_multi_cookies');
  395. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  396. $this->assertEquals($expected, $data);
  397. $this->Cookie->delete('Plain_array');
  398. $data = $this->Cookie->read('Plain_array');
  399. $this->assertNull($data);
  400. }
  401. /**
  402. * testReadingCookieArray
  403. *
  404. * @return void
  405. */
  406. public function testReadingCookieArray() {
  407. $this->_setCookieData();
  408. $data = $this->Cookie->read('Encrytped_array.name');
  409. $expected = 'CakePHP';
  410. $this->assertEquals($expected, $data);
  411. $data = $this->Cookie->read('Encrytped_array.version');
  412. $expected = '1.2.0.x';
  413. $this->assertEquals($expected, $data);
  414. $data = $this->Cookie->read('Encrytped_array.tag');
  415. $expected = 'CakePHP Rocks!';
  416. $this->assertEquals($expected, $data);
  417. $data = $this->Cookie->read('Encrypted_multi_cookies.name');
  418. $expected = 'CakePHP';
  419. $this->assertEquals($expected, $data);
  420. $data = $this->Cookie->read('Encrypted_multi_cookies.version');
  421. $expected = '1.2.0.x';
  422. $this->assertEquals($expected, $data);
  423. $data = $this->Cookie->read('Encrypted_multi_cookies.tag');
  424. $expected = 'CakePHP Rocks!';
  425. $this->assertEquals($expected, $data);
  426. $data = $this->Cookie->read('Plain_array.name');
  427. $expected = 'CakePHP';
  428. $this->assertEquals($expected, $data);
  429. $data = $this->Cookie->read('Plain_array.version');
  430. $expected = '1.2.0.x';
  431. $this->assertEquals($expected, $data);
  432. $data = $this->Cookie->read('Plain_array.tag');
  433. $expected = 'CakePHP Rocks!';
  434. $this->assertEquals($expected, $data);
  435. $data = $this->Cookie->read('Plain_multi_cookies.name');
  436. $expected = 'CakePHP';
  437. $this->assertEquals($expected, $data);
  438. $data = $this->Cookie->read('Plain_multi_cookies.version');
  439. $expected = '1.2.0.x';
  440. $this->assertEquals($expected, $data);
  441. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  442. $expected = 'CakePHP Rocks!';
  443. $this->assertEquals($expected, $data);
  444. }
  445. /**
  446. * testReadingCookieDataOnStartup
  447. *
  448. * @return void
  449. */
  450. public function testReadingDataFromRequest() {
  451. $this->Cookie->configKey('Encrypted_array', 'encryption', 'aes');
  452. $this->Cookie->configKey('Encrypted_multi_cookies', 'encryption', 'aes');
  453. $data = $this->Cookie->read('Encrypted_array');
  454. $this->assertNull($data);
  455. $data = $this->Cookie->read('Encrypted_multi_cookies');
  456. $this->assertNull($data);
  457. $data = $this->Cookie->read('Plain_array');
  458. $this->assertNull($data);
  459. $data = $this->Cookie->read('Plain_multi_cookies');
  460. $this->assertNull($data);
  461. $this->request->cookies = array(
  462. 'Encrypted_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  463. 'Encrypted_multi_cookies' => array(
  464. 'name' => $this->_encrypt('CakePHP'),
  465. 'version' => $this->_encrypt('1.2.0.x'),
  466. 'tag' => $this->_encrypt('CakePHP Rocks!')
  467. ),
  468. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  469. 'Plain_multi_cookies' => array(
  470. 'name' => 'CakePHP',
  471. 'version' => '1.2.0.x',
  472. 'tag' => 'CakePHP Rocks!'
  473. )
  474. );
  475. $data = $this->Cookie->read('Encrypted_array');
  476. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  477. $this->assertEquals($expected, $data);
  478. $data = $this->Cookie->read('Encrypted_multi_cookies');
  479. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  480. $this->assertEquals($expected, $data);
  481. $data = $this->Cookie->read('Plain_array');
  482. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  483. $this->assertEquals($expected, $data);
  484. $data = $this->Cookie->read('Plain_multi_cookies');
  485. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  486. $this->assertEquals($expected, $data);
  487. }
  488. /**
  489. * Test Reading legacy cookie values.
  490. *
  491. * @return void
  492. */
  493. public function testReadLegacyCookieValue() {
  494. $this->request->cookies = array(
  495. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  496. );
  497. $result = $this->Cookie->read('Legacy.value');
  498. $expected = array(1, 2, 3);
  499. $this->assertEquals($expected, $result);
  500. }
  501. /**
  502. * Test reading empty values.
  503. *
  504. * @return void
  505. */
  506. public function testReadEmpty() {
  507. $this->request->cookies = array(
  508. 'JSON' => '{"name":"value"}',
  509. 'Empty' => '',
  510. 'String' => '{"somewhat:"broken"}',
  511. 'Array' => '{}'
  512. );
  513. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  514. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  515. $this->assertEquals('', $this->Cookie->read('Empty'));
  516. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  517. $this->assertEquals(array(), $this->Cookie->read('Array'));
  518. }
  519. /**
  520. * testCheck method
  521. *
  522. * @return void
  523. */
  524. public function testCheck() {
  525. $this->Cookie->write('CookieComponentTestCase', 'value');
  526. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  527. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  528. }
  529. /**
  530. * testCheckingSavedEmpty method
  531. *
  532. * @return void
  533. */
  534. public function testCheckingSavedEmpty() {
  535. $this->Cookie->write('CookieComponentTestCase', 0);
  536. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  537. $this->Cookie->write('CookieComponentTestCase', '0');
  538. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  539. }
  540. /**
  541. * testCheckKeyWithSpaces method
  542. *
  543. * @return void
  544. */
  545. public function testCheckKeyWithSpaces() {
  546. $this->Cookie->write('CookieComponent Test', "test");
  547. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  548. $this->Cookie->delete('CookieComponent Test');
  549. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  550. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  551. }
  552. /**
  553. * testCheckEmpty
  554. *
  555. * @return void
  556. */
  557. public function testCheckEmpty() {
  558. $this->assertFalse($this->Cookie->check());
  559. }
  560. /**
  561. * test that deleting a top level keys kills the child elements too.
  562. *
  563. * @return void
  564. */
  565. public function testDeleteRemovesChildren() {
  566. $this->request->cookies = array(
  567. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  568. 'other' => 'value'
  569. );
  570. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  571. $this->Cookie->delete('User');
  572. $this->assertNull($this->Cookie->read('User.email'));
  573. $this->assertNull($this->Cookie->read('User.name'));
  574. $result = $this->Controller->response->cookie('User');
  575. $this->assertEquals('', $result['value']);
  576. $this->assertLessThan(time(), $result['expire']);
  577. }
  578. /**
  579. * Test deleting recursively with keys that don't exist.
  580. *
  581. * @return void
  582. */
  583. public function testDeleteChildrenNotExist() {
  584. $this->assertNull($this->Cookie->delete('NotFound'));
  585. $this->assertNull($this->Cookie->delete('Not.Found'));
  586. }
  587. /**
  588. * Helper method for generating old style encoded cookie values.
  589. *
  590. * @param array $array
  591. * @return string
  592. */
  593. protected function _oldImplode(array $array) {
  594. $string = '';
  595. foreach ($array as $key => $value) {
  596. $string .= ',' . $key . '|' . $value;
  597. }
  598. return substr($string, 1);
  599. }
  600. /**
  601. * Implode method to keep keys are multidimensional arrays
  602. *
  603. * @param array $array Map of key and values
  604. * @return string String in the form key1|value1,key2|value2
  605. */
  606. protected function _implode(array $array) {
  607. return json_encode($array);
  608. }
  609. /**
  610. * encrypt method
  611. *
  612. * @param array|string $value
  613. * @return string
  614. */
  615. protected function _encrypt($value) {
  616. if (is_array($value)) {
  617. $value = $this->_implode($value);
  618. }
  619. return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->config('key')));
  620. }
  621. }