CookieComponentTest.php 21 KB

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