CookieComponentTest.php 22 KB

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