CookieComponentTest.php 18 KB

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