CookieComponentTest.php 20 KB

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