CookieComponentTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 CakePHP(tm) v 1.2.0.5435
  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->name = 'CakeTestCookie';
  46. $this->Cookie->time = 10;
  47. $this->Cookie->path = '/';
  48. $this->Cookie->domain = '';
  49. $this->Cookie->secure = false;
  50. $this->Cookie->key = 'somerandomhaskeysomerandomhaskey';
  51. $event = new Event('Controller.startup', $this->Controller);
  52. $this->Cookie->startup($event);
  53. }
  54. /**
  55. * end
  56. *
  57. * @return void
  58. */
  59. public function tearDown() {
  60. parent::tearDown();
  61. $this->Cookie->destroy();
  62. }
  63. /**
  64. * sets up some default cookie data.
  65. *
  66. * @return void
  67. */
  68. protected function _setCookieData() {
  69. $this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
  70. $this->Cookie->write(array('Encrytped_multi_cookies.name' => 'CakePHP'));
  71. $this->Cookie->write(array('Encrytped_multi_cookies.version' => '1.2.0.x'));
  72. $this->Cookie->write(array('Encrytped_multi_cookies.tag' => 'CakePHP Rocks!'));
  73. $this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
  74. $this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
  75. $this->Cookie->write(array('Plain_multi_cookies.version' => '1.2.0.x'), null, false);
  76. $this->Cookie->write(array('Plain_multi_cookies.tag' => 'CakePHP Rocks!'), null, false);
  77. }
  78. /**
  79. * test that initialize sets settings from components array
  80. *
  81. * @return void
  82. */
  83. public function testSettings() {
  84. $settings = array(
  85. 'time' => '5 days',
  86. 'path' => '/'
  87. );
  88. $Cookie = new CookieComponent(new ComponentRegistry(), $settings);
  89. $this->assertEquals($Cookie->time, $settings['time']);
  90. $this->assertEquals($Cookie->path, $settings['path']);
  91. }
  92. /**
  93. * testCookieName
  94. *
  95. * @return void
  96. */
  97. public function testCookieName() {
  98. $this->assertEquals('CakeTestCookie', $this->Cookie->name);
  99. }
  100. /**
  101. * testReadEncryptedCookieData
  102. *
  103. * @return void
  104. */
  105. public function testReadEncryptedCookieData() {
  106. $this->_setCookieData();
  107. $data = $this->Cookie->read('Encrytped_array');
  108. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  109. $this->assertEquals($expected, $data);
  110. $data = $this->Cookie->read('Encrytped_multi_cookies');
  111. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  112. $this->assertEquals($expected, $data);
  113. }
  114. /**
  115. * testReadPlainCookieData
  116. *
  117. * @return void
  118. */
  119. public function testReadPlainCookieData() {
  120. $this->_setCookieData();
  121. $data = $this->Cookie->read('Plain_array');
  122. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  123. $this->assertEquals($expected, $data);
  124. $data = $this->Cookie->read('Plain_multi_cookies');
  125. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  126. $this->assertEquals($expected, $data);
  127. }
  128. /**
  129. * test read() after switching the cookie name.
  130. *
  131. * @return void
  132. */
  133. public function testReadWithNameSwitch() {
  134. $this->request->cookies = array(
  135. 'CakeTestCookie' => array(
  136. 'key' => 'value'
  137. ),
  138. 'OtherTestCookie' => array(
  139. 'key' => 'other value'
  140. )
  141. );
  142. $this->assertEquals('value', $this->Cookie->read('key'));
  143. $this->Cookie->name = 'OtherTestCookie';
  144. $this->assertEquals('other value', $this->Cookie->read('key'));
  145. }
  146. /**
  147. * test a simple write()
  148. *
  149. * @return void
  150. */
  151. public function testWriteSimple() {
  152. $this->Cookie->write('Testing', 'value');
  153. $result = $this->Cookie->read('Testing');
  154. $this->assertEquals('value', $result);
  155. }
  156. /**
  157. * test that two write() calls use the expiry.
  158. *
  159. * @return void
  160. */
  161. public function testWriteMultipleShareExpiry() {
  162. $this->Cookie->write('key1', 'value1', false);
  163. $this->Cookie->write('key2', 'value2', false);
  164. $name = $this->Cookie->name . '[key1]';
  165. $result = $this->Controller->response->cookie($name);
  166. $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
  167. $name = $this->Cookie->name . '[key2]';
  168. $result = $this->Controller->response->cookie($name);
  169. $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
  170. }
  171. /**
  172. * test write with distant future cookies
  173. *
  174. * @return void
  175. */
  176. public function testWriteFarFuture() {
  177. $this->Cookie->write('Testing', 'value', false, '+90 years');
  178. $future = new \DateTime('now');
  179. $future->modify('+90 years');
  180. $expected = array(
  181. 'name' => $this->Cookie->name . '[Testing]',
  182. 'value' => 'value',
  183. 'path' => '/',
  184. 'domain' => '',
  185. 'secure' => false,
  186. 'httpOnly' => false);
  187. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  188. $this->assertEquals($future->format('U'), $result['expire'], '', 3);
  189. unset($result['expire']);
  190. $this->assertEquals($expected, $result);
  191. }
  192. /**
  193. * test write with httpOnly cookies
  194. *
  195. * @return void
  196. */
  197. public function testWriteHttpOnly() {
  198. $this->Cookie->httpOnly = true;
  199. $this->Cookie->secure = false;
  200. $this->Cookie->write('Testing', 'value', false);
  201. $expected = array(
  202. 'name' => $this->Cookie->name . '[Testing]',
  203. 'value' => 'value',
  204. 'expire' => time() + 10,
  205. 'path' => '/',
  206. 'domain' => '',
  207. 'secure' => false,
  208. 'httpOnly' => true);
  209. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  210. $this->assertEquals($expected, $result);
  211. }
  212. /**
  213. * test delete with httpOnly
  214. *
  215. * @return void
  216. */
  217. public function testDeleteHttpOnly() {
  218. $this->Cookie->httpOnly = true;
  219. $this->Cookie->secure = false;
  220. $this->Cookie->delete('Testing', false);
  221. $expected = array(
  222. 'name' => $this->Cookie->name . '[Testing]',
  223. 'value' => '',
  224. 'expire' => time() - 42000,
  225. 'path' => '/',
  226. 'domain' => '',
  227. 'secure' => false,
  228. 'httpOnly' => true);
  229. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  230. $this->assertEquals($expected, $result);
  231. }
  232. /**
  233. * testWritePlainCookieArray
  234. *
  235. * @return void
  236. */
  237. public function testWritePlainCookieArray() {
  238. $this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
  239. $this->assertEquals('CakePHP', $this->Cookie->read('name'));
  240. $this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
  241. $this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
  242. $this->Cookie->delete('name');
  243. $this->Cookie->delete('version');
  244. $this->Cookie->delete('tag');
  245. }
  246. /**
  247. * test writing values that are not scalars
  248. *
  249. * @return void
  250. */
  251. public function testWriteArrayValues() {
  252. $this->Cookie->secure = false;
  253. $this->Cookie->write('Testing', array(1, 2, 3), false);
  254. $expected = array(
  255. 'name' => $this->Cookie->name . '[Testing]',
  256. 'value' => '[1,2,3]',
  257. 'path' => '/',
  258. 'domain' => '',
  259. 'secure' => false,
  260. 'httpOnly' => false);
  261. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  262. $this->assertWithinMargin($result['expire'], time() + 10, 1);
  263. unset($result['expire']);
  264. $this->assertEquals($expected, $result);
  265. }
  266. /**
  267. * testReadingCookieValue
  268. *
  269. * @return void
  270. */
  271. public function testReadingCookieValue() {
  272. $this->_setCookieData();
  273. $data = $this->Cookie->read();
  274. $expected = array(
  275. 'Encrytped_array' => array(
  276. 'name' => 'CakePHP',
  277. 'version' => '1.2.0.x',
  278. 'tag' => 'CakePHP Rocks!'),
  279. 'Encrytped_multi_cookies' => array(
  280. 'name' => 'CakePHP',
  281. 'version' => '1.2.0.x',
  282. 'tag' => 'CakePHP Rocks!'),
  283. 'Plain_array' => array(
  284. 'name' => 'CakePHP',
  285. 'version' => '1.2.0.x',
  286. 'tag' => 'CakePHP Rocks!'),
  287. 'Plain_multi_cookies' => array(
  288. 'name' => 'CakePHP',
  289. 'version' => '1.2.0.x',
  290. 'tag' => 'CakePHP Rocks!'));
  291. $this->assertEquals($expected, $data);
  292. }
  293. /**
  294. * testDeleteCookieValue
  295. *
  296. * @return void
  297. */
  298. public function testDeleteCookieValue() {
  299. $this->_setCookieData();
  300. $this->Cookie->delete('Encrytped_multi_cookies.name');
  301. $data = $this->Cookie->read('Encrytped_multi_cookies');
  302. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  303. $this->assertEquals($expected, $data);
  304. $this->Cookie->delete('Encrytped_array');
  305. $data = $this->Cookie->read('Encrytped_array');
  306. $this->assertNull($data);
  307. $this->Cookie->delete('Plain_multi_cookies.name');
  308. $data = $this->Cookie->read('Plain_multi_cookies');
  309. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  310. $this->assertEquals($expected, $data);
  311. $this->Cookie->delete('Plain_array');
  312. $data = $this->Cookie->read('Plain_array');
  313. $this->assertNull($data);
  314. }
  315. /**
  316. * testReadingCookieArray
  317. *
  318. * @return void
  319. */
  320. public function testReadingCookieArray() {
  321. $this->_setCookieData();
  322. $data = $this->Cookie->read('Encrytped_array.name');
  323. $expected = 'CakePHP';
  324. $this->assertEquals($expected, $data);
  325. $data = $this->Cookie->read('Encrytped_array.version');
  326. $expected = '1.2.0.x';
  327. $this->assertEquals($expected, $data);
  328. $data = $this->Cookie->read('Encrytped_array.tag');
  329. $expected = 'CakePHP Rocks!';
  330. $this->assertEquals($expected, $data);
  331. $data = $this->Cookie->read('Encrytped_multi_cookies.name');
  332. $expected = 'CakePHP';
  333. $this->assertEquals($expected, $data);
  334. $data = $this->Cookie->read('Encrytped_multi_cookies.version');
  335. $expected = '1.2.0.x';
  336. $this->assertEquals($expected, $data);
  337. $data = $this->Cookie->read('Encrytped_multi_cookies.tag');
  338. $expected = 'CakePHP Rocks!';
  339. $this->assertEquals($expected, $data);
  340. $data = $this->Cookie->read('Plain_array.name');
  341. $expected = 'CakePHP';
  342. $this->assertEquals($expected, $data);
  343. $data = $this->Cookie->read('Plain_array.version');
  344. $expected = '1.2.0.x';
  345. $this->assertEquals($expected, $data);
  346. $data = $this->Cookie->read('Plain_array.tag');
  347. $expected = 'CakePHP Rocks!';
  348. $this->assertEquals($expected, $data);
  349. $data = $this->Cookie->read('Plain_multi_cookies.name');
  350. $expected = 'CakePHP';
  351. $this->assertEquals($expected, $data);
  352. $data = $this->Cookie->read('Plain_multi_cookies.version');
  353. $expected = '1.2.0.x';
  354. $this->assertEquals($expected, $data);
  355. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  356. $expected = 'CakePHP Rocks!';
  357. $this->assertEquals($expected, $data);
  358. }
  359. /**
  360. * testReadingCookieDataOnStartup
  361. *
  362. * @return void
  363. */
  364. public function testReadingCookieDataOnStartup() {
  365. $data = $this->Cookie->read('Encrytped_array');
  366. $this->assertNull($data);
  367. $data = $this->Cookie->read('Encrytped_multi_cookies');
  368. $this->assertNull($data);
  369. $data = $this->Cookie->read('Plain_array');
  370. $this->assertNull($data);
  371. $data = $this->Cookie->read('Plain_multi_cookies');
  372. $this->assertNull($data);
  373. $this->request->cookies['CakeTestCookie'] = array(
  374. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  375. 'Encrytped_multi_cookies' => array(
  376. 'name' => $this->_encrypt('CakePHP'),
  377. 'version' => $this->_encrypt('1.2.0.x'),
  378. 'tag' => $this->_encrypt('CakePHP Rocks!')
  379. ),
  380. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  381. 'Plain_multi_cookies' => array(
  382. 'name' => 'CakePHP',
  383. 'version' => '1.2.0.x',
  384. 'tag' => 'CakePHP Rocks!'
  385. )
  386. );
  387. $data = $this->Cookie->read('Encrytped_array');
  388. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  389. $this->assertEquals($expected, $data);
  390. $data = $this->Cookie->read('Encrytped_multi_cookies');
  391. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  392. $this->assertEquals($expected, $data);
  393. $data = $this->Cookie->read('Plain_array');
  394. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  395. $this->assertEquals($expected, $data);
  396. $data = $this->Cookie->read('Plain_multi_cookies');
  397. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  398. $this->assertEquals($expected, $data);
  399. $this->Cookie->destroy();
  400. }
  401. /**
  402. * testReadingCookieDataWithoutStartup
  403. *
  404. * @return void
  405. */
  406. public function testReadingCookieDataWithoutStartup() {
  407. $data = $this->Cookie->read('Encrytped_array');
  408. $expected = null;
  409. $this->assertEquals($expected, $data);
  410. $data = $this->Cookie->read('Encrytped_multi_cookies');
  411. $expected = null;
  412. $this->assertEquals($expected, $data);
  413. $data = $this->Cookie->read('Plain_array');
  414. $expected = null;
  415. $this->assertEquals($expected, $data);
  416. $data = $this->Cookie->read('Plain_multi_cookies');
  417. $expected = null;
  418. $this->assertEquals($expected, $data);
  419. $this->request->cookies['CakeTestCookie'] = array(
  420. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  421. 'Encrytped_multi_cookies' => array(
  422. 'name' => $this->_encrypt('CakePHP'),
  423. 'version' => $this->_encrypt('1.2.0.x'),
  424. 'tag' => $this->_encrypt('CakePHP Rocks!')
  425. ),
  426. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  427. 'Plain_multi_cookies' => array(
  428. 'name' => 'CakePHP',
  429. 'version' => '1.2.0.x',
  430. 'tag' => 'CakePHP Rocks!'
  431. )
  432. );
  433. $data = $this->Cookie->read('Encrytped_array');
  434. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  435. $this->assertEquals($expected, $data);
  436. $data = $this->Cookie->read('Encrytped_multi_cookies');
  437. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  438. $this->assertEquals($expected, $data);
  439. $data = $this->Cookie->read('Plain_array');
  440. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  441. $this->assertEquals($expected, $data);
  442. $data = $this->Cookie->read('Plain_multi_cookies');
  443. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  444. $this->assertEquals($expected, $data);
  445. $this->Cookie->destroy();
  446. }
  447. /**
  448. * Test Reading legacy cookie values.
  449. *
  450. * @return void
  451. */
  452. public function testReadLegacyCookieValue() {
  453. $this->request->cookies['CakeTestCookie'] = array(
  454. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  455. );
  456. $result = $this->Cookie->read('Legacy.value');
  457. $expected = array(1, 2, 3);
  458. $this->assertEquals($expected, $result);
  459. }
  460. /**
  461. * Test reading empty values.
  462. */
  463. public function testReadEmpty() {
  464. $this->request->cookies['CakeTestCookie'] = array(
  465. 'JSON' => '{"name":"value"}',
  466. 'Empty' => '',
  467. 'String' => '{"somewhat:"broken"}'
  468. );
  469. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  470. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  471. $this->assertEquals('', $this->Cookie->read('Empty'));
  472. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  473. }
  474. /**
  475. * test that no error is issued for non array data.
  476. *
  477. * @return void
  478. */
  479. public function testNoErrorOnNonArrayData() {
  480. $this->request->cookies['CakeTestCookie'] = 'kaboom';
  481. $this->assertNull($this->Cookie->read('value'));
  482. }
  483. /**
  484. * testCheck method
  485. *
  486. * @return void
  487. */
  488. public function testCheck() {
  489. $this->Cookie->write('CookieComponentTestCase', 'value');
  490. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  491. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  492. }
  493. /**
  494. * testCheckingSavedEmpty method
  495. *
  496. * @return void
  497. */
  498. public function testCheckingSavedEmpty() {
  499. $this->Cookie->write('CookieComponentTestCase', 0);
  500. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  501. $this->Cookie->write('CookieComponentTestCase', '0');
  502. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  503. }
  504. /**
  505. * testCheckKeyWithSpaces method
  506. *
  507. * @return void
  508. */
  509. public function testCheckKeyWithSpaces() {
  510. $this->Cookie->write('CookieComponent Test', "test");
  511. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  512. $this->Cookie->delete('CookieComponent Test');
  513. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  514. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  515. }
  516. /**
  517. * testCheckEmpty
  518. *
  519. * @return void
  520. */
  521. public function testCheckEmpty() {
  522. $this->assertFalse($this->Cookie->check());
  523. }
  524. /**
  525. * test that deleting a top level keys kills the child elements too.
  526. *
  527. * @return void
  528. */
  529. public function testDeleteRemovesChildren() {
  530. $this->request->cookies['CakeTestCookie'] = array(
  531. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  532. 'other' => 'value'
  533. );
  534. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  535. $this->Cookie->delete('User');
  536. $this->assertNull($this->Cookie->read('User.email'));
  537. $this->Cookie->destroy();
  538. }
  539. /**
  540. * Test deleting recursively with keys that don't exist.
  541. *
  542. * @return void
  543. */
  544. public function testDeleteChildrenNotExist() {
  545. $this->assertNull($this->Cookie->delete('NotFound'));
  546. $this->assertNull($this->Cookie->delete('Not.Found'));
  547. }
  548. /**
  549. * Helper method for generating old style encoded cookie values.
  550. *
  551. * @return string.
  552. */
  553. protected function _oldImplode(array $array) {
  554. $string = '';
  555. foreach ($array as $key => $value) {
  556. $string .= ',' . $key . '|' . $value;
  557. }
  558. return substr($string, 1);
  559. }
  560. /**
  561. * Implode method to keep keys are multidimensional arrays
  562. *
  563. * @param array $array Map of key and values
  564. * @return string String in the form key1|value1,key2|value2
  565. */
  566. protected function _implode(array $array) {
  567. return json_encode($array);
  568. }
  569. /**
  570. * encrypt method
  571. *
  572. * @param array|string $value
  573. * @return string
  574. */
  575. protected function _encrypt($value) {
  576. if (is_array($value)) {
  577. $value = $this->_implode($value);
  578. }
  579. return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->key));
  580. }
  581. }