CookieComponentTest.php 23 KB

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