CookieComponentTest.php 20 KB

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