CookieComponentTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. /**
  3. * CookieComponentTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Controller.Component
  17. * @since CakePHP(tm) v 1.2.0.5435
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Component', 'Controller');
  21. App::uses('Controller', 'Controller');
  22. App::uses('CookieComponent', 'Controller/Component');
  23. /**
  24. * CookieComponentTestController class
  25. *
  26. * @package Cake.Test.Case.Controller.Component
  27. */
  28. class CookieComponentTestController extends Controller {
  29. /**
  30. * components property
  31. *
  32. * @var array
  33. */
  34. public $components = array('Cookie');
  35. /**
  36. * beforeFilter method
  37. *
  38. * @return void
  39. */
  40. public function beforeFilter() {
  41. $this->Cookie->name = 'CakeTestCookie';
  42. $this->Cookie->time = 10;
  43. $this->Cookie->path = '/';
  44. $this->Cookie->domain = '';
  45. $this->Cookie->secure = false;
  46. $this->Cookie->key = 'somerandomhaskey';
  47. }
  48. }
  49. /**
  50. * CookieComponentTest class
  51. *
  52. * @package Cake.Test.Case.Controller.Component
  53. */
  54. class CookieComponentTest extends CakeTestCase {
  55. /**
  56. * Controller property
  57. *
  58. * @var CookieComponentTestController
  59. */
  60. public $Controller;
  61. /**
  62. * start
  63. *
  64. * @return void
  65. */
  66. public function setUp() {
  67. $_COOKIE = array();
  68. $this->Controller = new CookieComponentTestController(new CakeRequest(), new CakeResponse());
  69. $this->Controller->constructClasses();
  70. $this->Cookie = $this->Controller->Cookie;
  71. $this->Cookie->name = 'CakeTestCookie';
  72. $this->Cookie->time = 10;
  73. $this->Cookie->path = '/';
  74. $this->Cookie->domain = '';
  75. $this->Cookie->secure = false;
  76. $this->Cookie->key = 'somerandomhaskey';
  77. $this->Cookie->startup($this->Controller);
  78. }
  79. /**
  80. * end
  81. *
  82. * @return void
  83. */
  84. public function tearDown() {
  85. $this->Cookie->destroy();
  86. }
  87. /**
  88. * sets up some default cookie data.
  89. *
  90. * @return void
  91. */
  92. protected function _setCookieData() {
  93. $this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
  94. $this->Cookie->write(array('Encrytped_multi_cookies.name' => 'CakePHP'));
  95. $this->Cookie->write(array('Encrytped_multi_cookies.version' => '1.2.0.x'));
  96. $this->Cookie->write(array('Encrytped_multi_cookies.tag' => 'CakePHP Rocks!'));
  97. $this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
  98. $this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
  99. $this->Cookie->write(array('Plain_multi_cookies.version' => '1.2.0.x'), null, false);
  100. $this->Cookie->write(array('Plain_multi_cookies.tag' => 'CakePHP Rocks!'), null, false);
  101. }
  102. /**
  103. * test that initialize sets settings from components array
  104. *
  105. * @return void
  106. */
  107. public function testSettings() {
  108. $settings = array(
  109. 'time' => '5 days',
  110. 'path' => '/'
  111. );
  112. $Cookie = new CookieComponent(new ComponentCollection(), $settings);
  113. $this->assertEquals($Cookie->time, $settings['time']);
  114. $this->assertEquals($Cookie->path, $settings['path']);
  115. }
  116. /**
  117. * testCookieName
  118. *
  119. * @return void
  120. */
  121. public function testCookieName() {
  122. $this->assertEquals('CakeTestCookie', $this->Cookie->name);
  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 testReadWithNameSwitch() {
  158. $_COOKIE = array(
  159. 'CakeTestCookie' => array(
  160. 'key' => 'value'
  161. ),
  162. 'OtherTestCookie' => array(
  163. 'key' => 'other value'
  164. )
  165. );
  166. $this->assertEquals('value', $this->Cookie->read('key'));
  167. $this->Cookie->name = 'OtherTestCookie';
  168. $this->assertEquals('other value', $this->Cookie->read('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 with httpOnly cookies
  182. *
  183. * @return void
  184. */
  185. public function testWriteHttpOnly() {
  186. $this->Cookie->httpOnly = true;
  187. $this->Cookie->secure = false;
  188. $this->Cookie->write('Testing', 'value', false);
  189. $expected = array(
  190. 'name' => $this->Cookie->name . '[Testing]',
  191. 'value' => 'value',
  192. 'expire' => time() + 10,
  193. 'path' => '/',
  194. 'domain' => '',
  195. 'secure' => false,
  196. 'httpOnly' => true);
  197. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  198. $this->assertEquals($expected, $result);
  199. }
  200. /**
  201. * test delete with httpOnly
  202. *
  203. * @return void
  204. */
  205. public function testDeleteHttpOnly() {
  206. $this->Cookie->httpOnly = true;
  207. $this->Cookie->secure = false;
  208. $this->Cookie->delete('Testing', false);
  209. $expected = array(
  210. 'name' => $this->Cookie->name . '[Testing]',
  211. 'value' => '',
  212. 'expire' => time() - 42000,
  213. 'path' => '/',
  214. 'domain' => '',
  215. 'secure' => false,
  216. 'httpOnly' => true);
  217. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  218. $this->assertEquals($expected, $result);
  219. }
  220. /**
  221. * testWritePlainCookieArray
  222. *
  223. * @return void
  224. */
  225. public function testWritePlainCookieArray() {
  226. $this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
  227. $this->assertEquals('CakePHP', $this->Cookie->read('name'));
  228. $this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
  229. $this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
  230. $this->Cookie->delete('name');
  231. $this->Cookie->delete('version');
  232. $this->Cookie->delete('tag');
  233. }
  234. /**
  235. * test writing values that are not scalars
  236. *
  237. * @return void
  238. */
  239. public function testWriteArrayValues() {
  240. $this->Cookie->secure = false;
  241. $this->Cookie->write('Testing', array(1, 2, 3), false);
  242. $expected = array(
  243. 'name' => $this->Cookie->name . '[Testing]',
  244. 'value' => '[1,2,3]',
  245. 'path' => '/',
  246. 'domain' => '',
  247. 'secure' => false,
  248. 'httpOnly' => false);
  249. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  250. $this->assertWithinMargin($result['expire'], time() + 10, 1);
  251. unset($result['expire']);
  252. $this->assertEquals($expected, $result);
  253. }
  254. /**
  255. * testReadingCookieValue
  256. *
  257. * @return void
  258. */
  259. public function testReadingCookieValue() {
  260. $this->_setCookieData();
  261. $data = $this->Cookie->read();
  262. $expected = array(
  263. 'Encrytped_array' => array(
  264. 'name' => 'CakePHP',
  265. 'version' => '1.2.0.x',
  266. 'tag' => 'CakePHP Rocks!'),
  267. 'Encrytped_multi_cookies' => array(
  268. 'name' => 'CakePHP',
  269. 'version' => '1.2.0.x',
  270. 'tag' => 'CakePHP Rocks!'),
  271. 'Plain_array' => array(
  272. 'name' => 'CakePHP',
  273. 'version' => '1.2.0.x',
  274. 'tag' => 'CakePHP Rocks!'),
  275. 'Plain_multi_cookies' => array(
  276. 'name' => 'CakePHP',
  277. 'version' => '1.2.0.x',
  278. 'tag' => 'CakePHP Rocks!'));
  279. $this->assertEquals($expected, $data);
  280. }
  281. /**
  282. * testDeleteCookieValue
  283. *
  284. * @return void
  285. */
  286. public function testDeleteCookieValue() {
  287. $this->_setCookieData();
  288. $this->Cookie->delete('Encrytped_multi_cookies.name');
  289. $data = $this->Cookie->read('Encrytped_multi_cookies');
  290. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  291. $this->assertEquals($expected, $data);
  292. $this->Cookie->delete('Encrytped_array');
  293. $data = $this->Cookie->read('Encrytped_array');
  294. $this->assertNull($data);
  295. $this->Cookie->delete('Plain_multi_cookies.name');
  296. $data = $this->Cookie->read('Plain_multi_cookies');
  297. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  298. $this->assertEquals($expected, $data);
  299. $this->Cookie->delete('Plain_array');
  300. $data = $this->Cookie->read('Plain_array');
  301. $this->assertNull($data);
  302. }
  303. /**
  304. * testReadingCookieArray
  305. *
  306. * @return void
  307. */
  308. public function testReadingCookieArray() {
  309. $this->_setCookieData();
  310. $data = $this->Cookie->read('Encrytped_array.name');
  311. $expected = 'CakePHP';
  312. $this->assertEquals($expected, $data);
  313. $data = $this->Cookie->read('Encrytped_array.version');
  314. $expected = '1.2.0.x';
  315. $this->assertEquals($expected, $data);
  316. $data = $this->Cookie->read('Encrytped_array.tag');
  317. $expected = 'CakePHP Rocks!';
  318. $this->assertEquals($expected, $data);
  319. $data = $this->Cookie->read('Encrytped_multi_cookies.name');
  320. $expected = 'CakePHP';
  321. $this->assertEquals($expected, $data);
  322. $data = $this->Cookie->read('Encrytped_multi_cookies.version');
  323. $expected = '1.2.0.x';
  324. $this->assertEquals($expected, $data);
  325. $data = $this->Cookie->read('Encrytped_multi_cookies.tag');
  326. $expected = 'CakePHP Rocks!';
  327. $this->assertEquals($expected, $data);
  328. $data = $this->Cookie->read('Plain_array.name');
  329. $expected = 'CakePHP';
  330. $this->assertEquals($expected, $data);
  331. $data = $this->Cookie->read('Plain_array.version');
  332. $expected = '1.2.0.x';
  333. $this->assertEquals($expected, $data);
  334. $data = $this->Cookie->read('Plain_array.tag');
  335. $expected = 'CakePHP Rocks!';
  336. $this->assertEquals($expected, $data);
  337. $data = $this->Cookie->read('Plain_multi_cookies.name');
  338. $expected = 'CakePHP';
  339. $this->assertEquals($expected, $data);
  340. $data = $this->Cookie->read('Plain_multi_cookies.version');
  341. $expected = '1.2.0.x';
  342. $this->assertEquals($expected, $data);
  343. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  344. $expected = 'CakePHP Rocks!';
  345. $this->assertEquals($expected, $data);
  346. }
  347. /**
  348. * testReadingCookieDataOnStartup
  349. *
  350. * @return void
  351. */
  352. public function testReadingCookieDataOnStartup() {
  353. $data = $this->Cookie->read('Encrytped_array');
  354. $this->assertNull($data);
  355. $data = $this->Cookie->read('Encrytped_multi_cookies');
  356. $this->assertNull($data);
  357. $data = $this->Cookie->read('Plain_array');
  358. $this->assertNull($data);
  359. $data = $this->Cookie->read('Plain_multi_cookies');
  360. $this->assertNull($data);
  361. $_COOKIE['CakeTestCookie'] = array(
  362. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  363. 'Encrytped_multi_cookies' => array(
  364. 'name' => $this->_encrypt('CakePHP'),
  365. 'version' => $this->_encrypt('1.2.0.x'),
  366. 'tag' => $this->_encrypt('CakePHP Rocks!')),
  367. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  368. 'Plain_multi_cookies' => array(
  369. 'name' => 'CakePHP',
  370. 'version' => '1.2.0.x',
  371. 'tag' => 'CakePHP Rocks!'));
  372. $this->Cookie->startup(new CookieComponentTestController());
  373. $data = $this->Cookie->read('Encrytped_array');
  374. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  375. $this->assertEquals($expected, $data);
  376. $data = $this->Cookie->read('Encrytped_multi_cookies');
  377. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  378. $this->assertEquals($expected, $data);
  379. $data = $this->Cookie->read('Plain_array');
  380. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  381. $this->assertEquals($expected, $data);
  382. $data = $this->Cookie->read('Plain_multi_cookies');
  383. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  384. $this->assertEquals($expected, $data);
  385. $this->Cookie->destroy();
  386. unset($_COOKIE['CakeTestCookie']);
  387. }
  388. /**
  389. * testReadingCookieDataWithoutStartup
  390. *
  391. * @return void
  392. */
  393. public function testReadingCookieDataWithoutStartup() {
  394. $data = $this->Cookie->read('Encrytped_array');
  395. $expected = null;
  396. $this->assertEquals($expected, $data);
  397. $data = $this->Cookie->read('Encrytped_multi_cookies');
  398. $expected = null;
  399. $this->assertEquals($expected, $data);
  400. $data = $this->Cookie->read('Plain_array');
  401. $expected = null;
  402. $this->assertEquals($expected, $data);
  403. $data = $this->Cookie->read('Plain_multi_cookies');
  404. $expected = null;
  405. $this->assertEquals($expected, $data);
  406. $_COOKIE['CakeTestCookie'] = array(
  407. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  408. 'Encrytped_multi_cookies' => array(
  409. 'name' => $this->_encrypt('CakePHP'),
  410. 'version' => $this->_encrypt('1.2.0.x'),
  411. 'tag' => $this->_encrypt('CakePHP Rocks!')),
  412. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  413. 'Plain_multi_cookies' => array(
  414. 'name' => 'CakePHP',
  415. 'version' => '1.2.0.x',
  416. 'tag' => 'CakePHP Rocks!'));
  417. $data = $this->Cookie->read('Encrytped_array');
  418. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  419. $this->assertEquals($expected, $data);
  420. $data = $this->Cookie->read('Encrytped_multi_cookies');
  421. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  422. $this->assertEquals($expected, $data);
  423. $data = $this->Cookie->read('Plain_array');
  424. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  425. $this->assertEquals($expected, $data);
  426. $data = $this->Cookie->read('Plain_multi_cookies');
  427. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  428. $this->assertEquals($expected, $data);
  429. $this->Cookie->destroy();
  430. unset($_COOKIE['CakeTestCookie']);
  431. }
  432. /**
  433. * Test Reading legacy cookie values.
  434. *
  435. * @return void
  436. */
  437. public function testReadLegacyCookieValue() {
  438. $_COOKIE['CakeTestCookie'] = array(
  439. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  440. );
  441. $result = $this->Cookie->read('Legacy.value');
  442. $expected = array(1, 2, 3);
  443. $this->assertEquals($expected, $result);
  444. }
  445. /**
  446. * Test reading empty values.
  447. */
  448. public function testReadEmpty() {
  449. $_COOKIE['CakeTestCookie'] = array(
  450. 'JSON' => '{"name":"value"}',
  451. 'Empty' => '',
  452. 'String' => '{"somewhat:"broken"}'
  453. );
  454. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  455. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  456. $this->assertEquals('', $this->Cookie->read('Empty'));
  457. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  458. }
  459. /**
  460. * test that no error is issued for non array data.
  461. *
  462. * @return void
  463. */
  464. public function testNoErrorOnNonArrayData() {
  465. $this->Cookie->destroy();
  466. $_COOKIE['CakeTestCookie'] = 'kaboom';
  467. $this->assertNull($this->Cookie->read('value'));
  468. }
  469. /**
  470. * testCheck method
  471. *
  472. * @return void
  473. */
  474. public function testCheck() {
  475. $this->Cookie->write('CookieComponentTestCase', 'value');
  476. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  477. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  478. }
  479. /**
  480. * testCheckingSavedEmpty method
  481. *
  482. * @return void
  483. */
  484. public function testCheckingSavedEmpty() {
  485. $this->Cookie->write('CookieComponentTestCase', 0);
  486. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  487. $this->Cookie->write('CookieComponentTestCase', '0');
  488. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  489. $this->Cookie->write('CookieComponentTestCase', false);
  490. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  491. $this->Cookie->write('CookieComponentTestCase', null);
  492. $this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
  493. }
  494. /**
  495. * testCheckKeyWithSpaces method
  496. *
  497. * @return void
  498. */
  499. public function testCheckKeyWithSpaces() {
  500. $this->Cookie->write('CookieComponent Test', "test");
  501. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  502. $this->Cookie->delete('CookieComponent Test');
  503. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  504. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  505. }
  506. /**
  507. * testCheckEmpty
  508. *
  509. * @return void
  510. */
  511. public function testCheckEmpty() {
  512. $this->assertFalse($this->Cookie->check());
  513. }
  514. /**
  515. * test that deleting a top level keys kills the child elements too.
  516. *
  517. * @return void
  518. */
  519. public function testDeleteRemovesChildren() {
  520. $_COOKIE['CakeTestCookie'] = array(
  521. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  522. 'other' => 'value'
  523. );
  524. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  525. $this->Cookie->delete('User');
  526. $this->assertNull($this->Cookie->read('User.email'));
  527. $this->Cookie->destroy();
  528. }
  529. /**
  530. * Test deleting recursively with keys that don't exist.
  531. *
  532. * @return void
  533. */
  534. public function testDeleteChildrenNotExist() {
  535. $this->assertNull($this->Cookie->delete('NotFound'));
  536. $this->assertNull($this->Cookie->delete('Not.Found'));
  537. }
  538. /**
  539. * Helper method for generating old style encoded cookie values.
  540. *
  541. * @return string.
  542. */
  543. protected function _oldImplode(array $array) {
  544. $string = '';
  545. foreach ($array as $key => $value) {
  546. $string .= ',' . $key . '|' . $value;
  547. }
  548. return substr($string, 1);
  549. }
  550. /**
  551. * Implode method to keep keys are multidimensional arrays
  552. *
  553. * @param array $array Map of key and values
  554. * @return string String in the form key1|value1,key2|value2
  555. */
  556. protected function _implode(array $array) {
  557. return json_encode($array);
  558. }
  559. /**
  560. * encrypt method
  561. *
  562. * @param array|string $value
  563. * @return string
  564. */
  565. protected function _encrypt($value) {
  566. if (is_array($value)) {
  567. $value = $this->_implode($value);
  568. }
  569. return "Q2FrZQ==." . base64_encode(Security::cipher($value, $this->Cookie->key));
  570. }
  571. }