CookieComponentTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 writing with a custom encryption key using ConfigKey
  333. *
  334. * @return void
  335. */
  336. public function testWriteConfigKeyWithCustomEncryptionKey()
  337. {
  338. $name = 'sampleCookieTest';
  339. $value = 'some data';
  340. $encryption = 'aes';
  341. $prefix = "Q2FrZQ==.";
  342. $key = 'justanotherencryptionkeyjustanotherencryptionkey';
  343. $this->Cookie->configKey($name, compact('key', 'encryption'));
  344. $this->Cookie->write($name, $value);
  345. $cookie = $this->Controller->response->cookie($name);
  346. $this->assertEquals($value, Security::decrypt(base64_decode(substr($cookie['value'], strlen($prefix))), $key));
  347. }
  348. /**
  349. * Test reading with a custom encryption key using ConfigKey
  350. *
  351. * @return void
  352. */
  353. public function testReadConfigKeyWithCustomEncryptionKey()
  354. {
  355. $name = 'sampleCookieTest';
  356. $value = 'some data';
  357. $encryption = 'aes';
  358. $key = 'justanotherencryptionkeyjustanotherencryptionkey';
  359. $this->Cookie->configKey($name, compact('key', 'encryption'));
  360. $this->Cookie->write($name, $value);
  361. $this->assertEquals('some data', $this->Cookie->read($name));
  362. }
  363. /**
  364. * test delete with httpOnly
  365. *
  366. * @return void
  367. */
  368. public function testDeleteHttpOnly()
  369. {
  370. $this->Cookie->config([
  371. 'httpOnly' => true,
  372. 'secure' => false
  373. ]);
  374. $this->Cookie->delete('Testing');
  375. $expected = [
  376. 'name' => 'Testing',
  377. 'value' => '',
  378. 'expire' => (new Time('now'))->format('U') - 42000,
  379. 'path' => '/',
  380. 'domain' => '',
  381. 'secure' => false,
  382. 'httpOnly' => true];
  383. $result = $this->Controller->response->cookie('Testing');
  384. $this->assertEquals($expected, $result);
  385. }
  386. /**
  387. * test writing values that are not scalars
  388. *
  389. * @return void
  390. */
  391. public function testWriteArrayValues()
  392. {
  393. $this->Cookie->write('Testing', [1, 2, 3]);
  394. $expected = [
  395. 'name' => 'Testing',
  396. 'value' => '[1,2,3]',
  397. 'path' => '/',
  398. 'domain' => '',
  399. 'secure' => false,
  400. 'httpOnly' => false
  401. ];
  402. $result = $this->Controller->response->cookie('Testing');
  403. $time = new Time('now');
  404. $this->assertWithinRange($time->format('U') + 10, $result['expire'], 1);
  405. unset($result['expire']);
  406. $this->assertEquals($expected, $result);
  407. }
  408. /**
  409. * Test that writing mixed arrays results in the correct data.
  410. *
  411. * @return void
  412. */
  413. public function testWriteMixedArray()
  414. {
  415. $this->Cookie->write('User', ['name' => 'mark'], false);
  416. $this->Cookie->write('User.email', 'mark@example.com', false);
  417. $expected = [
  418. 'name' => 'User',
  419. 'value' => '{"name":"mark","email":"mark@example.com"}',
  420. 'path' => '/',
  421. 'domain' => '',
  422. 'secure' => false,
  423. 'httpOnly' => false
  424. ];
  425. $result = $this->Controller->response->cookie('User');
  426. unset($result['expire']);
  427. $this->assertEquals($expected, $result);
  428. $this->Cookie->write('User.email', 'mark@example.com', false);
  429. $this->Cookie->write('User', ['name' => 'mark'], false);
  430. $expected = [
  431. 'name' => 'User',
  432. 'value' => '{"name":"mark"}',
  433. 'path' => '/',
  434. 'domain' => '',
  435. 'secure' => false,
  436. 'httpOnly' => false
  437. ];
  438. $result = $this->Controller->response->cookie('User');
  439. unset($result['expire']);
  440. $this->assertEquals($expected, $result);
  441. }
  442. /**
  443. * testDeleteCookieValue
  444. *
  445. * @return void
  446. */
  447. public function testDeleteCookieValue()
  448. {
  449. $this->_setCookieData();
  450. $this->Cookie->delete('Encrypted_multi_cookies.name');
  451. $data = $this->Cookie->read('Encrypted_multi_cookies');
  452. $expected = ['version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  453. $this->assertEquals($expected, $data);
  454. $this->Cookie->delete('Encrytped_array');
  455. $data = $this->Cookie->read('Encrytped_array');
  456. $this->assertNull($data);
  457. $this->Cookie->delete('Plain_multi_cookies.name');
  458. $data = $this->Cookie->read('Plain_multi_cookies');
  459. $expected = ['version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  460. $this->assertEquals($expected, $data);
  461. $this->Cookie->delete('Plain_array');
  462. $data = $this->Cookie->read('Plain_array');
  463. $this->assertNull($data);
  464. }
  465. /**
  466. * testReadingCookieArray
  467. *
  468. * @return void
  469. */
  470. public function testReadingCookieArray()
  471. {
  472. $this->_setCookieData();
  473. $data = $this->Cookie->read('Encrytped_array.name');
  474. $expected = 'CakePHP';
  475. $this->assertEquals($expected, $data);
  476. $data = $this->Cookie->read('Encrytped_array.version');
  477. $expected = '1.2.0.x';
  478. $this->assertEquals($expected, $data);
  479. $data = $this->Cookie->read('Encrytped_array.tag');
  480. $expected = 'CakePHP Rocks!';
  481. $this->assertEquals($expected, $data);
  482. $data = $this->Cookie->read('Encrypted_multi_cookies.name');
  483. $expected = 'CakePHP';
  484. $this->assertEquals($expected, $data);
  485. $data = $this->Cookie->read('Encrypted_multi_cookies.version');
  486. $expected = '1.2.0.x';
  487. $this->assertEquals($expected, $data);
  488. $data = $this->Cookie->read('Encrypted_multi_cookies.tag');
  489. $expected = 'CakePHP Rocks!';
  490. $this->assertEquals($expected, $data);
  491. $data = $this->Cookie->read('Plain_array.name');
  492. $expected = 'CakePHP';
  493. $this->assertEquals($expected, $data);
  494. $data = $this->Cookie->read('Plain_array.version');
  495. $expected = '1.2.0.x';
  496. $this->assertEquals($expected, $data);
  497. $data = $this->Cookie->read('Plain_array.tag');
  498. $expected = 'CakePHP Rocks!';
  499. $this->assertEquals($expected, $data);
  500. $data = $this->Cookie->read('Plain_multi_cookies.name');
  501. $expected = 'CakePHP';
  502. $this->assertEquals($expected, $data);
  503. $data = $this->Cookie->read('Plain_multi_cookies.version');
  504. $expected = '1.2.0.x';
  505. $this->assertEquals($expected, $data);
  506. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  507. $expected = 'CakePHP Rocks!';
  508. $this->assertEquals($expected, $data);
  509. }
  510. /**
  511. * testReadingCookieDataOnStartup
  512. *
  513. * @return void
  514. */
  515. public function testReadingDataFromRequest()
  516. {
  517. $this->Cookie->configKey('Encrypted_array', 'encryption', 'aes');
  518. $this->Cookie->configKey('Encrypted_multi_cookies', 'encryption', 'aes');
  519. $data = $this->Cookie->read('Encrypted_array');
  520. $this->assertNull($data);
  521. $data = $this->Cookie->read('Encrypted_multi_cookies');
  522. $this->assertNull($data);
  523. $data = $this->Cookie->read('Plain_array');
  524. $this->assertNull($data);
  525. $data = $this->Cookie->read('Plain_multi_cookies');
  526. $this->assertNull($data);
  527. $this->request->cookies = [
  528. 'Encrypted_array' => $this->_encrypt(['name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!']),
  529. 'Encrypted_multi_cookies' => [
  530. 'name' => $this->_encrypt('CakePHP'),
  531. 'version' => $this->_encrypt('1.2.0.x'),
  532. 'tag' => $this->_encrypt('CakePHP Rocks!')
  533. ],
  534. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  535. 'Plain_multi_cookies' => [
  536. 'name' => 'CakePHP',
  537. 'version' => '1.2.0.x',
  538. 'tag' => 'CakePHP Rocks!'
  539. ]
  540. ];
  541. $data = $this->Cookie->read('Encrypted_array');
  542. $expected = ['name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  543. $this->assertEquals($expected, $data);
  544. $data = $this->Cookie->read('Encrypted_multi_cookies');
  545. $expected = ['name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  546. $this->assertEquals($expected, $data);
  547. $data = $this->Cookie->read('Plain_array');
  548. $expected = ['name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  549. $this->assertEquals($expected, $data);
  550. $data = $this->Cookie->read('Plain_multi_cookies');
  551. $expected = ['name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'];
  552. $this->assertEquals($expected, $data);
  553. }
  554. /**
  555. * Test Reading legacy cookie values.
  556. *
  557. * @return void
  558. */
  559. public function testReadLegacyCookieValue()
  560. {
  561. $this->request->cookies = [
  562. 'Legacy' => ['value' => $this->_oldImplode([1, 2, 3])]
  563. ];
  564. $result = $this->Cookie->read('Legacy.value');
  565. $expected = [1, 2, 3];
  566. $this->assertEquals($expected, $result);
  567. }
  568. /**
  569. * Test reading empty values.
  570. *
  571. * @return void
  572. */
  573. public function testReadEmpty()
  574. {
  575. $this->request->cookies = [
  576. 'JSON' => '{"name":"value"}',
  577. 'Empty' => '',
  578. 'String' => '{"somewhat:"broken"}',
  579. 'Array' => '{}'
  580. ];
  581. $this->assertEquals(['name' => 'value'], $this->Cookie->read('JSON'));
  582. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  583. $this->assertEquals('', $this->Cookie->read('Empty'));
  584. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  585. $this->assertEquals([], $this->Cookie->read('Array'));
  586. }
  587. /**
  588. * testCheck method
  589. *
  590. * @return void
  591. */
  592. public function testCheck()
  593. {
  594. $this->Cookie->write('CookieComponentTestCase', 'value');
  595. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  596. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  597. }
  598. /**
  599. * testCheckingSavedEmpty method
  600. *
  601. * @return void
  602. */
  603. public function testCheckingSavedEmpty()
  604. {
  605. $this->Cookie->write('CookieComponentTestCase', 0);
  606. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  607. $this->Cookie->write('CookieComponentTestCase', '0');
  608. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  609. }
  610. /**
  611. * testCheckKeyWithSpaces method
  612. *
  613. * @return void
  614. */
  615. public function testCheckKeyWithSpaces()
  616. {
  617. $this->Cookie->write('CookieComponent Test', "test");
  618. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  619. $this->Cookie->delete('CookieComponent Test');
  620. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  621. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  622. }
  623. /**
  624. * testCheckEmpty
  625. *
  626. * @return void
  627. */
  628. public function testCheckEmpty()
  629. {
  630. $this->assertFalse($this->Cookie->check());
  631. }
  632. /**
  633. * test that deleting a top level keys kills the child elements too.
  634. *
  635. * @return void
  636. */
  637. public function testDeleteRemovesChildren()
  638. {
  639. $this->request->cookies = [
  640. 'User' => ['email' => 'example@example.com', 'name' => 'mark'],
  641. 'other' => 'value'
  642. ];
  643. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  644. $this->Cookie->delete('User');
  645. $this->assertNull($this->Cookie->read('User.email'));
  646. $this->assertNull($this->Cookie->read('User.name'));
  647. $result = $this->Controller->response->cookie('User');
  648. $this->assertEquals('', $result['value']);
  649. $this->assertLessThan(time(), $result['expire']);
  650. }
  651. /**
  652. * Test deleting recursively with keys that don't exist.
  653. *
  654. * @return void
  655. */
  656. public function testDeleteChildrenNotExist()
  657. {
  658. $this->assertNull($this->Cookie->delete('NotFound'));
  659. $this->assertNull($this->Cookie->delete('Not.Found'));
  660. }
  661. /**
  662. * Helper method for generating old style encoded cookie values.
  663. *
  664. * @param array $array
  665. * @return string
  666. */
  667. protected function _oldImplode(array $array)
  668. {
  669. $string = '';
  670. foreach ($array as $key => $value) {
  671. $string .= ',' . $key . '|' . $value;
  672. }
  673. return substr($string, 1);
  674. }
  675. /**
  676. * Implode method to keep keys are multidimensional arrays
  677. *
  678. * @param array $array Map of key and values
  679. * @return string String in the form key1|value1,key2|value2
  680. */
  681. protected function _implode(array $array)
  682. {
  683. return json_encode($array);
  684. }
  685. /**
  686. * encrypt method
  687. *
  688. * @param array|string $value
  689. * @return string
  690. */
  691. protected function _encrypt($value)
  692. {
  693. if (is_array($value)) {
  694. $value = $this->_implode($value);
  695. }
  696. return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->config('key')));
  697. }
  698. }