CookieComponentTest.php 21 KB

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