CookieComponentTest.php 24 KB

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