CookieComponentTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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\I18n\Time;
  21. use Cake\Network\Request;
  22. use Cake\Network\Response;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. /**
  26. * CookieComponentTest class
  27. */
  28. class CookieComponentTest extends TestCase {
  29. /**
  30. * start
  31. *
  32. * @return void
  33. */
  34. public function setUp() {
  35. parent::setUp();
  36. $controller = $this->getMock(
  37. 'Cake\Controller\Controller',
  38. array('redirect'),
  39. array(new Request(), new Response())
  40. );
  41. $controller->loadComponent('Cookie');
  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. * Test read when an invalid cipher is configured.
  126. *
  127. * @expectedException \RuntimeException
  128. * @expectedExceptionMessage Invalid encryption cipher. Must be one of aes, rijndael.
  129. * @return void
  130. */
  131. public function testReadInvalidCipher() {
  132. $this->request->cookies = [
  133. 'Test' => $this->_encrypt('value'),
  134. ];
  135. $this->Cookie->config('encryption', 'derp');
  136. $this->Cookie->read('Test');
  137. }
  138. /**
  139. * testReadEncryptedCookieData
  140. *
  141. * @return void
  142. */
  143. public function testReadEncryptedCookieData() {
  144. $this->_setCookieData();
  145. $data = $this->Cookie->read('Encrytped_array');
  146. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  147. $this->assertEquals($expected, $data);
  148. $data = $this->Cookie->read('Encrypted_multi_cookies');
  149. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  150. $this->assertEquals($expected, $data);
  151. }
  152. /**
  153. * testReadPlainCookieData
  154. *
  155. * @return void
  156. */
  157. public function testReadPlainCookieData() {
  158. $this->_setCookieData();
  159. $data = $this->Cookie->read('Plain_array');
  160. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  161. $this->assertEquals($expected, $data);
  162. $data = $this->Cookie->read('Plain_multi_cookies');
  163. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  164. $this->assertEquals($expected, $data);
  165. }
  166. /**
  167. * test read() after switching the cookie name.
  168. *
  169. * @return void
  170. */
  171. public function testReadMultipleNames() {
  172. $this->request->cookies = array(
  173. 'CakeCookie' => array(
  174. 'key' => 'value'
  175. ),
  176. 'OtherCookie' => array(
  177. 'key' => 'other value'
  178. )
  179. );
  180. $this->assertEquals('value', $this->Cookie->read('CakeCookie.key'));
  181. $this->assertEquals(['key' => 'value'], $this->Cookie->read('CakeCookie'));
  182. $this->assertEquals('other value', $this->Cookie->read('OtherCookie.key'));
  183. }
  184. /**
  185. * test a simple write()
  186. *
  187. * @return void
  188. */
  189. public function testWriteSimple() {
  190. $this->Cookie->write('Testing', 'value');
  191. $result = $this->Cookie->read('Testing');
  192. $this->assertEquals('value', $result);
  193. }
  194. /**
  195. * Test write when an invalid cipher is configured.
  196. *
  197. * @expectedException \RuntimeException
  198. * @expectedExceptionMessage Invalid encryption cipher. Must be one of aes, rijndael.
  199. * @return void
  200. */
  201. public function testWriteInvalidCipher() {
  202. $this->Cookie->config('encryption', 'derp');
  203. $this->Cookie->write('Test', 'nope');
  204. }
  205. /**
  206. * Test writes don't omit request data from being read.
  207. *
  208. * @return void
  209. */
  210. public function testWriteThanRead() {
  211. $this->request->cookies = [
  212. 'User' => ['name' => 'mark']
  213. ];
  214. $this->Cookie->write('Testing', 'value');
  215. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  216. }
  217. /**
  218. * test write() encrypted data with falsey value
  219. *
  220. * @return void
  221. */
  222. public function testWriteWithFalseyValue() {
  223. $this->Cookie->config([
  224. 'encryption' => 'aes',
  225. 'key' => 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^',
  226. ]);
  227. $this->Cookie->write('Testing');
  228. $result = $this->Cookie->read('Testing');
  229. $this->assertNull($result);
  230. $this->Cookie->write('Testing', '');
  231. $result = $this->Cookie->read('Testing');
  232. $this->assertEquals('', $result);
  233. $this->Cookie->write('Testing', false);
  234. $result = $this->Cookie->read('Testing');
  235. $this->assertFalse($result);
  236. $this->Cookie->write('Testing', 1);
  237. $result = $this->Cookie->read('Testing');
  238. $this->assertEquals(1, $result);
  239. $this->Cookie->write('Testing', '0');
  240. $result = $this->Cookie->read('Testing');
  241. $this->assertSame('0', $result);
  242. $this->Cookie->write('Testing', 0);
  243. $result = $this->Cookie->read('Testing');
  244. $this->assertSame(0, $result);
  245. }
  246. /**
  247. * test write with distant future cookies
  248. *
  249. * @return void
  250. */
  251. public function testWriteFarFuture() {
  252. $this->Cookie->configKey('Testing', 'expires', '+90 years');
  253. $this->Cookie->write('Testing', 'value');
  254. $future = new Time('now');
  255. $future->modify('+90 years');
  256. $expected = array(
  257. 'name' => 'Testing',
  258. 'value' => 'value',
  259. 'path' => '/',
  260. 'domain' => '',
  261. 'secure' => false,
  262. 'httpOnly' => false);
  263. $result = $this->Controller->response->cookie('Testing');
  264. $this->assertEquals($future->format('U'), $result['expire'], '', 3);
  265. unset($result['expire']);
  266. $this->assertEquals($expected, $result);
  267. }
  268. /**
  269. * test write with httpOnly cookies
  270. *
  271. * @return void
  272. */
  273. public function testWriteHttpOnly() {
  274. $this->Cookie->config([
  275. 'httpOnly' => true,
  276. 'secure' => false
  277. ]);
  278. $this->Cookie->write('Testing', 'value', false);
  279. $expected = array(
  280. 'name' => 'Testing',
  281. 'value' => 'value',
  282. 'expire' => (new Time('+10 seconds'))->format('U'),
  283. 'path' => '/',
  284. 'domain' => '',
  285. 'secure' => false,
  286. 'httpOnly' => true);
  287. $result = $this->Controller->response->cookie('Testing');
  288. $this->assertEquals($expected, $result);
  289. }
  290. /**
  291. * Test writing multiple nested keys when some are encrypted.
  292. *
  293. * @return void
  294. */
  295. public function testWriteMulitMixedEncryption() {
  296. $this->Cookie->configKey('Open', 'encryption', false);
  297. $this->Cookie->configKey('Closed', 'encryption', 'aes');
  298. $this->Cookie->write([
  299. 'Closed.key' => 'secret',
  300. 'Open.key' => 'not secret',
  301. ]);
  302. $expected = array(
  303. 'name' => 'Open',
  304. 'value' => '{"key":"not secret"}',
  305. 'path' => '/',
  306. 'domain' => '',
  307. 'secure' => false,
  308. 'httpOnly' => false
  309. );
  310. $result = $this->Controller->response->cookie('Open');
  311. unset($result['expire']);
  312. $this->assertEquals($expected, $result);
  313. $result = $this->Controller->response->cookie('Closed');
  314. $this->assertContains('Q2FrZQ==.', $result['value']);
  315. }
  316. /**
  317. * test delete with httpOnly
  318. *
  319. * @return void
  320. */
  321. public function testDeleteHttpOnly() {
  322. $this->Cookie->config([
  323. 'httpOnly' => true,
  324. 'secure' => false
  325. ]);
  326. $this->Cookie->delete('Testing');
  327. $expected = array(
  328. 'name' => 'Testing',
  329. 'value' => '',
  330. 'expire' => (new Time('now'))->format('U') - 42000,
  331. 'path' => '/',
  332. 'domain' => '',
  333. 'secure' => false,
  334. 'httpOnly' => true);
  335. $result = $this->Controller->response->cookie('Testing');
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * test writing values that are not scalars
  340. *
  341. * @return void
  342. */
  343. public function testWriteArrayValues() {
  344. $this->Cookie->write('Testing', array(1, 2, 3));
  345. $expected = array(
  346. 'name' => 'Testing',
  347. 'value' => '[1,2,3]',
  348. 'path' => '/',
  349. 'domain' => '',
  350. 'secure' => false,
  351. 'httpOnly' => false
  352. );
  353. $result = $this->Controller->response->cookie('Testing');
  354. $time = new Time('now');
  355. $this->assertWithinRange($time->format('U') + 10, $result['expire'], 1);
  356. unset($result['expire']);
  357. $this->assertEquals($expected, $result);
  358. }
  359. /**
  360. * Test that writing mixed arrays results in the correct data.
  361. *
  362. * @return void
  363. */
  364. public function testWriteMixedArray() {
  365. $this->Cookie->write('User', array('name' => 'mark'), false);
  366. $this->Cookie->write('User.email', 'mark@example.com', false);
  367. $expected = array(
  368. 'name' => 'User',
  369. 'value' => '{"name":"mark","email":"mark@example.com"}',
  370. 'path' => '/',
  371. 'domain' => '',
  372. 'secure' => false,
  373. 'httpOnly' => false
  374. );
  375. $result = $this->Controller->response->cookie('User');
  376. unset($result['expire']);
  377. $this->assertEquals($expected, $result);
  378. $this->Cookie->write('User.email', 'mark@example.com', false);
  379. $this->Cookie->write('User', array('name' => 'mark'), false);
  380. $expected = array(
  381. 'name' => 'User',
  382. 'value' => '{"name":"mark"}',
  383. 'path' => '/',
  384. 'domain' => '',
  385. 'secure' => false,
  386. 'httpOnly' => false
  387. );
  388. $result = $this->Controller->response->cookie('User');
  389. unset($result['expire']);
  390. $this->assertEquals($expected, $result);
  391. }
  392. /**
  393. * testDeleteCookieValue
  394. *
  395. * @return void
  396. */
  397. public function testDeleteCookieValue() {
  398. $this->_setCookieData();
  399. $this->Cookie->delete('Encrypted_multi_cookies.name');
  400. $data = $this->Cookie->read('Encrypted_multi_cookies');
  401. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  402. $this->assertEquals($expected, $data);
  403. $this->Cookie->delete('Encrytped_array');
  404. $data = $this->Cookie->read('Encrytped_array');
  405. $this->assertNull($data);
  406. $this->Cookie->delete('Plain_multi_cookies.name');
  407. $data = $this->Cookie->read('Plain_multi_cookies');
  408. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  409. $this->assertEquals($expected, $data);
  410. $this->Cookie->delete('Plain_array');
  411. $data = $this->Cookie->read('Plain_array');
  412. $this->assertNull($data);
  413. }
  414. /**
  415. * testReadingCookieArray
  416. *
  417. * @return void
  418. */
  419. public function testReadingCookieArray() {
  420. $this->_setCookieData();
  421. $data = $this->Cookie->read('Encrytped_array.name');
  422. $expected = 'CakePHP';
  423. $this->assertEquals($expected, $data);
  424. $data = $this->Cookie->read('Encrytped_array.version');
  425. $expected = '1.2.0.x';
  426. $this->assertEquals($expected, $data);
  427. $data = $this->Cookie->read('Encrytped_array.tag');
  428. $expected = 'CakePHP Rocks!';
  429. $this->assertEquals($expected, $data);
  430. $data = $this->Cookie->read('Encrypted_multi_cookies.name');
  431. $expected = 'CakePHP';
  432. $this->assertEquals($expected, $data);
  433. $data = $this->Cookie->read('Encrypted_multi_cookies.version');
  434. $expected = '1.2.0.x';
  435. $this->assertEquals($expected, $data);
  436. $data = $this->Cookie->read('Encrypted_multi_cookies.tag');
  437. $expected = 'CakePHP Rocks!';
  438. $this->assertEquals($expected, $data);
  439. $data = $this->Cookie->read('Plain_array.name');
  440. $expected = 'CakePHP';
  441. $this->assertEquals($expected, $data);
  442. $data = $this->Cookie->read('Plain_array.version');
  443. $expected = '1.2.0.x';
  444. $this->assertEquals($expected, $data);
  445. $data = $this->Cookie->read('Plain_array.tag');
  446. $expected = 'CakePHP Rocks!';
  447. $this->assertEquals($expected, $data);
  448. $data = $this->Cookie->read('Plain_multi_cookies.name');
  449. $expected = 'CakePHP';
  450. $this->assertEquals($expected, $data);
  451. $data = $this->Cookie->read('Plain_multi_cookies.version');
  452. $expected = '1.2.0.x';
  453. $this->assertEquals($expected, $data);
  454. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  455. $expected = 'CakePHP Rocks!';
  456. $this->assertEquals($expected, $data);
  457. }
  458. /**
  459. * testReadingCookieDataOnStartup
  460. *
  461. * @return void
  462. */
  463. public function testReadingDataFromRequest() {
  464. $this->Cookie->configKey('Encrypted_array', 'encryption', 'aes');
  465. $this->Cookie->configKey('Encrypted_multi_cookies', 'encryption', 'aes');
  466. $data = $this->Cookie->read('Encrypted_array');
  467. $this->assertNull($data);
  468. $data = $this->Cookie->read('Encrypted_multi_cookies');
  469. $this->assertNull($data);
  470. $data = $this->Cookie->read('Plain_array');
  471. $this->assertNull($data);
  472. $data = $this->Cookie->read('Plain_multi_cookies');
  473. $this->assertNull($data);
  474. $this->request->cookies = array(
  475. 'Encrypted_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  476. 'Encrypted_multi_cookies' => array(
  477. 'name' => $this->_encrypt('CakePHP'),
  478. 'version' => $this->_encrypt('1.2.0.x'),
  479. 'tag' => $this->_encrypt('CakePHP Rocks!')
  480. ),
  481. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  482. 'Plain_multi_cookies' => array(
  483. 'name' => 'CakePHP',
  484. 'version' => '1.2.0.x',
  485. 'tag' => 'CakePHP Rocks!'
  486. )
  487. );
  488. $data = $this->Cookie->read('Encrypted_array');
  489. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  490. $this->assertEquals($expected, $data);
  491. $data = $this->Cookie->read('Encrypted_multi_cookies');
  492. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  493. $this->assertEquals($expected, $data);
  494. $data = $this->Cookie->read('Plain_array');
  495. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  496. $this->assertEquals($expected, $data);
  497. $data = $this->Cookie->read('Plain_multi_cookies');
  498. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  499. $this->assertEquals($expected, $data);
  500. }
  501. /**
  502. * Test Reading legacy cookie values.
  503. *
  504. * @return void
  505. */
  506. public function testReadLegacyCookieValue() {
  507. $this->request->cookies = array(
  508. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  509. );
  510. $result = $this->Cookie->read('Legacy.value');
  511. $expected = array(1, 2, 3);
  512. $this->assertEquals($expected, $result);
  513. }
  514. /**
  515. * Test reading empty values.
  516. *
  517. * @return void
  518. */
  519. public function testReadEmpty() {
  520. $this->request->cookies = array(
  521. 'JSON' => '{"name":"value"}',
  522. 'Empty' => '',
  523. 'String' => '{"somewhat:"broken"}',
  524. 'Array' => '{}'
  525. );
  526. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  527. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  528. $this->assertEquals('', $this->Cookie->read('Empty'));
  529. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  530. $this->assertEquals(array(), $this->Cookie->read('Array'));
  531. }
  532. /**
  533. * testCheck method
  534. *
  535. * @return void
  536. */
  537. public function testCheck() {
  538. $this->Cookie->write('CookieComponentTestCase', 'value');
  539. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  540. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  541. }
  542. /**
  543. * testCheckingSavedEmpty method
  544. *
  545. * @return void
  546. */
  547. public function testCheckingSavedEmpty() {
  548. $this->Cookie->write('CookieComponentTestCase', 0);
  549. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  550. $this->Cookie->write('CookieComponentTestCase', '0');
  551. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  552. }
  553. /**
  554. * testCheckKeyWithSpaces method
  555. *
  556. * @return void
  557. */
  558. public function testCheckKeyWithSpaces() {
  559. $this->Cookie->write('CookieComponent Test', "test");
  560. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  561. $this->Cookie->delete('CookieComponent Test');
  562. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  563. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  564. }
  565. /**
  566. * testCheckEmpty
  567. *
  568. * @return void
  569. */
  570. public function testCheckEmpty() {
  571. $this->assertFalse($this->Cookie->check());
  572. }
  573. /**
  574. * test that deleting a top level keys kills the child elements too.
  575. *
  576. * @return void
  577. */
  578. public function testDeleteRemovesChildren() {
  579. $this->request->cookies = array(
  580. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  581. 'other' => 'value'
  582. );
  583. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  584. $this->Cookie->delete('User');
  585. $this->assertNull($this->Cookie->read('User.email'));
  586. $this->assertNull($this->Cookie->read('User.name'));
  587. $result = $this->Controller->response->cookie('User');
  588. $this->assertEquals('', $result['value']);
  589. $this->assertLessThan(time(), $result['expire']);
  590. }
  591. /**
  592. * Test deleting recursively with keys that don't exist.
  593. *
  594. * @return void
  595. */
  596. public function testDeleteChildrenNotExist() {
  597. $this->assertNull($this->Cookie->delete('NotFound'));
  598. $this->assertNull($this->Cookie->delete('Not.Found'));
  599. }
  600. /**
  601. * Helper method for generating old style encoded cookie values.
  602. *
  603. * @param array $array
  604. * @return string
  605. */
  606. protected function _oldImplode(array $array) {
  607. $string = '';
  608. foreach ($array as $key => $value) {
  609. $string .= ',' . $key . '|' . $value;
  610. }
  611. return substr($string, 1);
  612. }
  613. /**
  614. * Implode method to keep keys are multidimensional arrays
  615. *
  616. * @param array $array Map of key and values
  617. * @return string String in the form key1|value1,key2|value2
  618. */
  619. protected function _implode(array $array) {
  620. return json_encode($array);
  621. }
  622. /**
  623. * encrypt method
  624. *
  625. * @param array|string $value
  626. * @return string
  627. */
  628. protected function _encrypt($value) {
  629. if (is_array($value)) {
  630. $value = $this->_implode($value);
  631. }
  632. return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->config('key')));
  633. }
  634. }