CookieComponentTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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->assertWithinMargin($result['expire'], $time->format('U') + 10, 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. * test reading all values at once.
  395. *
  396. * @return void
  397. */
  398. public function testReadingAllValues() {
  399. $this->_setCookieData();
  400. $data = $this->Cookie->read();
  401. $expected = array(
  402. 'Encrytped_array' => array(
  403. 'name' => 'CakePHP',
  404. 'version' => '1.2.0.x',
  405. 'tag' => 'CakePHP Rocks!'),
  406. 'Encrypted_multi_cookies' => array(
  407. 'name' => 'CakePHP',
  408. 'version' => '1.2.0.x',
  409. 'tag' => 'CakePHP Rocks!'),
  410. 'Plain_array' => array(
  411. 'name' => 'CakePHP',
  412. 'version' => '1.2.0.x',
  413. 'tag' => 'CakePHP Rocks!'),
  414. 'Plain_multi_cookies' => array(
  415. 'name' => 'CakePHP',
  416. 'version' => '1.2.0.x',
  417. 'tag' => 'CakePHP Rocks!'));
  418. $this->assertEquals($expected, $data);
  419. }
  420. /**
  421. * testDeleteCookieValue
  422. *
  423. * @return void
  424. */
  425. public function testDeleteCookieValue() {
  426. $this->_setCookieData();
  427. $this->Cookie->delete('Encrypted_multi_cookies.name');
  428. $data = $this->Cookie->read('Encrypted_multi_cookies');
  429. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  430. $this->assertEquals($expected, $data);
  431. $this->Cookie->delete('Encrytped_array');
  432. $data = $this->Cookie->read('Encrytped_array');
  433. $this->assertNull($data);
  434. $this->Cookie->delete('Plain_multi_cookies.name');
  435. $data = $this->Cookie->read('Plain_multi_cookies');
  436. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  437. $this->assertEquals($expected, $data);
  438. $this->Cookie->delete('Plain_array');
  439. $data = $this->Cookie->read('Plain_array');
  440. $this->assertNull($data);
  441. }
  442. /**
  443. * testReadingCookieArray
  444. *
  445. * @return void
  446. */
  447. public function testReadingCookieArray() {
  448. $this->_setCookieData();
  449. $data = $this->Cookie->read('Encrytped_array.name');
  450. $expected = 'CakePHP';
  451. $this->assertEquals($expected, $data);
  452. $data = $this->Cookie->read('Encrytped_array.version');
  453. $expected = '1.2.0.x';
  454. $this->assertEquals($expected, $data);
  455. $data = $this->Cookie->read('Encrytped_array.tag');
  456. $expected = 'CakePHP Rocks!';
  457. $this->assertEquals($expected, $data);
  458. $data = $this->Cookie->read('Encrypted_multi_cookies.name');
  459. $expected = 'CakePHP';
  460. $this->assertEquals($expected, $data);
  461. $data = $this->Cookie->read('Encrypted_multi_cookies.version');
  462. $expected = '1.2.0.x';
  463. $this->assertEquals($expected, $data);
  464. $data = $this->Cookie->read('Encrypted_multi_cookies.tag');
  465. $expected = 'CakePHP Rocks!';
  466. $this->assertEquals($expected, $data);
  467. $data = $this->Cookie->read('Plain_array.name');
  468. $expected = 'CakePHP';
  469. $this->assertEquals($expected, $data);
  470. $data = $this->Cookie->read('Plain_array.version');
  471. $expected = '1.2.0.x';
  472. $this->assertEquals($expected, $data);
  473. $data = $this->Cookie->read('Plain_array.tag');
  474. $expected = 'CakePHP Rocks!';
  475. $this->assertEquals($expected, $data);
  476. $data = $this->Cookie->read('Plain_multi_cookies.name');
  477. $expected = 'CakePHP';
  478. $this->assertEquals($expected, $data);
  479. $data = $this->Cookie->read('Plain_multi_cookies.version');
  480. $expected = '1.2.0.x';
  481. $this->assertEquals($expected, $data);
  482. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  483. $expected = 'CakePHP Rocks!';
  484. $this->assertEquals($expected, $data);
  485. }
  486. /**
  487. * testReadingCookieDataOnStartup
  488. *
  489. * @return void
  490. */
  491. public function testReadingDataFromRequest() {
  492. $this->Cookie->configKey('Encrypted_array', 'encryption', 'aes');
  493. $this->Cookie->configKey('Encrypted_multi_cookies', 'encryption', 'aes');
  494. $data = $this->Cookie->read('Encrypted_array');
  495. $this->assertNull($data);
  496. $data = $this->Cookie->read('Encrypted_multi_cookies');
  497. $this->assertNull($data);
  498. $data = $this->Cookie->read('Plain_array');
  499. $this->assertNull($data);
  500. $data = $this->Cookie->read('Plain_multi_cookies');
  501. $this->assertNull($data);
  502. $this->request->cookies = array(
  503. 'Encrypted_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  504. 'Encrypted_multi_cookies' => array(
  505. 'name' => $this->_encrypt('CakePHP'),
  506. 'version' => $this->_encrypt('1.2.0.x'),
  507. 'tag' => $this->_encrypt('CakePHP Rocks!')
  508. ),
  509. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  510. 'Plain_multi_cookies' => array(
  511. 'name' => 'CakePHP',
  512. 'version' => '1.2.0.x',
  513. 'tag' => 'CakePHP Rocks!'
  514. )
  515. );
  516. $data = $this->Cookie->read('Encrypted_array');
  517. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  518. $this->assertEquals($expected, $data);
  519. $data = $this->Cookie->read('Encrypted_multi_cookies');
  520. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  521. $this->assertEquals($expected, $data);
  522. $data = $this->Cookie->read('Plain_array');
  523. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  524. $this->assertEquals($expected, $data);
  525. $data = $this->Cookie->read('Plain_multi_cookies');
  526. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  527. $this->assertEquals($expected, $data);
  528. }
  529. /**
  530. * Test Reading legacy cookie values.
  531. *
  532. * @return void
  533. */
  534. public function testReadLegacyCookieValue() {
  535. $this->request->cookies = array(
  536. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  537. );
  538. $result = $this->Cookie->read('Legacy.value');
  539. $expected = array(1, 2, 3);
  540. $this->assertEquals($expected, $result);
  541. }
  542. /**
  543. * Test reading empty values.
  544. *
  545. * @return void
  546. */
  547. public function testReadEmpty() {
  548. $this->request->cookies = array(
  549. 'JSON' => '{"name":"value"}',
  550. 'Empty' => '',
  551. 'String' => '{"somewhat:"broken"}',
  552. 'Array' => '{}'
  553. );
  554. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  555. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  556. $this->assertEquals('', $this->Cookie->read('Empty'));
  557. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  558. $this->assertEquals(array(), $this->Cookie->read('Array'));
  559. }
  560. /**
  561. * testCheck method
  562. *
  563. * @return void
  564. */
  565. public function testCheck() {
  566. $this->Cookie->write('CookieComponentTestCase', 'value');
  567. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  568. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  569. }
  570. /**
  571. * testCheckingSavedEmpty method
  572. *
  573. * @return void
  574. */
  575. public function testCheckingSavedEmpty() {
  576. $this->Cookie->write('CookieComponentTestCase', 0);
  577. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  578. $this->Cookie->write('CookieComponentTestCase', '0');
  579. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  580. }
  581. /**
  582. * testCheckKeyWithSpaces method
  583. *
  584. * @return void
  585. */
  586. public function testCheckKeyWithSpaces() {
  587. $this->Cookie->write('CookieComponent Test', "test");
  588. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  589. $this->Cookie->delete('CookieComponent Test');
  590. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  591. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  592. }
  593. /**
  594. * testCheckEmpty
  595. *
  596. * @return void
  597. */
  598. public function testCheckEmpty() {
  599. $this->assertFalse($this->Cookie->check());
  600. }
  601. /**
  602. * test that deleting a top level keys kills the child elements too.
  603. *
  604. * @return void
  605. */
  606. public function testDeleteRemovesChildren() {
  607. $this->request->cookies = array(
  608. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  609. 'other' => 'value'
  610. );
  611. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  612. $this->Cookie->delete('User');
  613. $this->assertNull($this->Cookie->read('User.email'));
  614. $this->assertNull($this->Cookie->read('User.name'));
  615. $result = $this->Controller->response->cookie('User');
  616. $this->assertEquals('', $result['value']);
  617. $this->assertLessThan(time(), $result['expire']);
  618. }
  619. /**
  620. * Test deleting recursively with keys that don't exist.
  621. *
  622. * @return void
  623. */
  624. public function testDeleteChildrenNotExist() {
  625. $this->assertNull($this->Cookie->delete('NotFound'));
  626. $this->assertNull($this->Cookie->delete('Not.Found'));
  627. }
  628. /**
  629. * Helper method for generating old style encoded cookie values.
  630. *
  631. * @param array $array
  632. * @return string
  633. */
  634. protected function _oldImplode(array $array) {
  635. $string = '';
  636. foreach ($array as $key => $value) {
  637. $string .= ',' . $key . '|' . $value;
  638. }
  639. return substr($string, 1);
  640. }
  641. /**
  642. * Implode method to keep keys are multidimensional arrays
  643. *
  644. * @param array $array Map of key and values
  645. * @return string String in the form key1|value1,key2|value2
  646. */
  647. protected function _implode(array $array) {
  648. return json_encode($array);
  649. }
  650. /**
  651. * encrypt method
  652. *
  653. * @param array|string $value
  654. * @return string
  655. */
  656. protected function _encrypt($value) {
  657. if (is_array($value)) {
  658. $value = $this->_implode($value);
  659. }
  660. return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->config('key')));
  661. }
  662. }