CookieComponentTest.php 19 KB

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