CookieComponentTest.php 24 KB

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