CookieComponentTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. * @package Cake.Test.Case.Controller.Component
  15. * @since CakePHP(tm) v 1.2.0.5435
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Component', 'Controller');
  19. App::uses('Controller', 'Controller');
  20. App::uses('CookieComponent', 'Controller/Component');
  21. /**
  22. * CookieComponentTestController class
  23. *
  24. * @package Cake.Test.Case.Controller.Component
  25. */
  26. class CookieComponentTestController extends Controller {
  27. /**
  28. * components property
  29. *
  30. * @var array
  31. */
  32. public $components = array('Cookie');
  33. /**
  34. * beforeFilter method
  35. *
  36. * @return void
  37. */
  38. public function beforeFilter() {
  39. $this->Cookie->name = 'CakeTestCookie';
  40. $this->Cookie->time = 10;
  41. $this->Cookie->path = '/';
  42. $this->Cookie->domain = '';
  43. $this->Cookie->secure = false;
  44. $this->Cookie->key = 'somerandomhaskey';
  45. }
  46. }
  47. /**
  48. * CookieComponentTest class
  49. *
  50. * @package Cake.Test.Case.Controller.Component
  51. */
  52. class CookieComponentTest extends CakeTestCase {
  53. /**
  54. * Controller property
  55. *
  56. * @var CookieComponentTestController
  57. */
  58. public $Controller;
  59. /**
  60. * start
  61. *
  62. * @return void
  63. */
  64. public function setUp() {
  65. parent::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. parent::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() encrypted data with falsey value
  182. *
  183. * @return void
  184. */
  185. public function testWriteWithFalseyValue() {
  186. $this->Cookie->type('aes');
  187. $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
  188. $this->Cookie->write('Testing');
  189. $result = $this->Cookie->read('Testing');
  190. $this->assertNull($result);
  191. $this->Cookie->write('Testing', '');
  192. $result = $this->Cookie->read('Testing');
  193. $this->assertEquals('', $result);
  194. $this->Cookie->write('Testing', false);
  195. $result = $this->Cookie->read('Testing');
  196. $this->assertFalse($result);
  197. $this->Cookie->write('Testing', 1);
  198. $result = $this->Cookie->read('Testing');
  199. $this->assertEquals(1, $result);
  200. $this->Cookie->write('Testing', '0');
  201. $result = $this->Cookie->read('Testing');
  202. $this->assertSame('0', $result);
  203. $this->Cookie->write('Testing', 0);
  204. $result = $this->Cookie->read('Testing');
  205. $this->assertSame(0, $result);
  206. }
  207. /**
  208. * test that two write() calls use the expiry.
  209. *
  210. * @return void
  211. */
  212. public function testWriteMultipleShareExpiry() {
  213. $this->Cookie->write('key1', 'value1', false);
  214. $this->Cookie->write('key2', 'value2', false);
  215. $name = $this->Cookie->name . '[key1]';
  216. $result = $this->Controller->response->cookie($name);
  217. $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
  218. $name = $this->Cookie->name . '[key2]';
  219. $result = $this->Controller->response->cookie($name);
  220. $this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
  221. }
  222. /**
  223. * test write with distant future cookies
  224. *
  225. * @return void
  226. */
  227. public function testWriteFarFuture() {
  228. $this->Cookie->write('Testing', 'value', false, '+90 years');
  229. $future = new DateTime('now');
  230. $future->modify('+90 years');
  231. $expected = array(
  232. 'name' => $this->Cookie->name . '[Testing]',
  233. 'value' => 'value',
  234. 'path' => '/',
  235. 'domain' => '',
  236. 'secure' => false,
  237. 'httpOnly' => false);
  238. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  239. $this->assertEquals($future->format('U'), $result['expire'], '', 3);
  240. unset($result['expire']);
  241. $this->assertEquals($expected, $result);
  242. }
  243. /**
  244. * test write with httpOnly cookies
  245. *
  246. * @return void
  247. */
  248. public function testWriteHttpOnly() {
  249. $this->Cookie->httpOnly = true;
  250. $this->Cookie->secure = false;
  251. $this->Cookie->write('Testing', 'value', false);
  252. $expected = array(
  253. 'name' => $this->Cookie->name . '[Testing]',
  254. 'value' => 'value',
  255. 'expire' => time() + 10,
  256. 'path' => '/',
  257. 'domain' => '',
  258. 'secure' => false,
  259. 'httpOnly' => true);
  260. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  261. $this->assertEquals($expected, $result);
  262. }
  263. /**
  264. * test delete with httpOnly
  265. *
  266. * @return void
  267. */
  268. public function testDeleteHttpOnly() {
  269. $this->Cookie->httpOnly = true;
  270. $this->Cookie->secure = false;
  271. $this->Cookie->delete('Testing', false);
  272. $expected = array(
  273. 'name' => $this->Cookie->name . '[Testing]',
  274. 'value' => '',
  275. 'expire' => time() - 42000,
  276. 'path' => '/',
  277. 'domain' => '',
  278. 'secure' => false,
  279. 'httpOnly' => true);
  280. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  281. $this->assertEquals($expected, $result);
  282. }
  283. /**
  284. * testWritePlainCookieArray
  285. *
  286. * @return void
  287. */
  288. public function testWritePlainCookieArray() {
  289. $this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
  290. $this->assertEquals('CakePHP', $this->Cookie->read('name'));
  291. $this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
  292. $this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
  293. $this->Cookie->delete('name');
  294. $this->Cookie->delete('version');
  295. $this->Cookie->delete('tag');
  296. }
  297. /**
  298. * test writing values that are not scalars
  299. *
  300. * @return void
  301. */
  302. public function testWriteArrayValues() {
  303. $this->Cookie->secure = false;
  304. $this->Cookie->write('Testing', array(1, 2, 3), false);
  305. $expected = array(
  306. 'name' => $this->Cookie->name . '[Testing]',
  307. 'value' => '[1,2,3]',
  308. 'path' => '/',
  309. 'domain' => '',
  310. 'secure' => false,
  311. 'httpOnly' => false);
  312. $result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
  313. $this->assertWithinMargin($result['expire'], time() + 10, 1);
  314. unset($result['expire']);
  315. $this->assertEquals($expected, $result);
  316. }
  317. /**
  318. * Test that writing mixed arrays results in the correct data.
  319. *
  320. * @return void
  321. */
  322. public function testWriteMixedArray() {
  323. $this->Cookie->encrypt = false;
  324. $this->Cookie->write('User', array('name' => 'mark'), false);
  325. $this->Cookie->write('User.email', 'mark@example.com', false);
  326. $expected = array(
  327. 'name' => $this->Cookie->name . '[User]',
  328. 'value' => '{"name":"mark","email":"mark@example.com"}',
  329. 'path' => '/',
  330. 'domain' => '',
  331. 'secure' => false,
  332. 'httpOnly' => false
  333. );
  334. $result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
  335. unset($result['expire']);
  336. $this->assertEquals($expected, $result);
  337. $this->Cookie->write('User.email', 'mark@example.com', false);
  338. $this->Cookie->write('User', array('name' => 'mark'), false);
  339. $expected = array(
  340. 'name' => $this->Cookie->name . '[User]',
  341. 'value' => '{"name":"mark"}',
  342. 'path' => '/',
  343. 'domain' => '',
  344. 'secure' => false,
  345. 'httpOnly' => false
  346. );
  347. $result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
  348. unset($result['expire']);
  349. $this->assertEquals($expected, $result);
  350. }
  351. /**
  352. * testReadingCookieValue
  353. *
  354. * @return void
  355. */
  356. public function testReadingCookieValue() {
  357. $this->_setCookieData();
  358. $data = $this->Cookie->read();
  359. $expected = array(
  360. 'Encrytped_array' => array(
  361. 'name' => 'CakePHP',
  362. 'version' => '1.2.0.x',
  363. 'tag' => 'CakePHP Rocks!'),
  364. 'Encrytped_multi_cookies' => array(
  365. 'name' => 'CakePHP',
  366. 'version' => '1.2.0.x',
  367. 'tag' => 'CakePHP Rocks!'),
  368. 'Plain_array' => array(
  369. 'name' => 'CakePHP',
  370. 'version' => '1.2.0.x',
  371. 'tag' => 'CakePHP Rocks!'),
  372. 'Plain_multi_cookies' => array(
  373. 'name' => 'CakePHP',
  374. 'version' => '1.2.0.x',
  375. 'tag' => 'CakePHP Rocks!'));
  376. $this->assertEquals($expected, $data);
  377. }
  378. /**
  379. * testDeleteCookieValue
  380. *
  381. * @return void
  382. */
  383. public function testDeleteCookieValue() {
  384. $this->_setCookieData();
  385. $this->Cookie->delete('Encrytped_multi_cookies.name');
  386. $data = $this->Cookie->read('Encrytped_multi_cookies');
  387. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  388. $this->assertEquals($expected, $data);
  389. $this->Cookie->delete('Encrytped_array');
  390. $data = $this->Cookie->read('Encrytped_array');
  391. $this->assertNull($data);
  392. $this->Cookie->delete('Plain_multi_cookies.name');
  393. $data = $this->Cookie->read('Plain_multi_cookies');
  394. $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  395. $this->assertEquals($expected, $data);
  396. $this->Cookie->delete('Plain_array');
  397. $data = $this->Cookie->read('Plain_array');
  398. $this->assertNull($data);
  399. }
  400. /**
  401. * testReadingCookieArray
  402. *
  403. * @return void
  404. */
  405. public function testReadingCookieArray() {
  406. $this->_setCookieData();
  407. $data = $this->Cookie->read('Encrytped_array.name');
  408. $expected = 'CakePHP';
  409. $this->assertEquals($expected, $data);
  410. $data = $this->Cookie->read('Encrytped_array.version');
  411. $expected = '1.2.0.x';
  412. $this->assertEquals($expected, $data);
  413. $data = $this->Cookie->read('Encrytped_array.tag');
  414. $expected = 'CakePHP Rocks!';
  415. $this->assertEquals($expected, $data);
  416. $data = $this->Cookie->read('Encrytped_multi_cookies.name');
  417. $expected = 'CakePHP';
  418. $this->assertEquals($expected, $data);
  419. $data = $this->Cookie->read('Encrytped_multi_cookies.version');
  420. $expected = '1.2.0.x';
  421. $this->assertEquals($expected, $data);
  422. $data = $this->Cookie->read('Encrytped_multi_cookies.tag');
  423. $expected = 'CakePHP Rocks!';
  424. $this->assertEquals($expected, $data);
  425. $data = $this->Cookie->read('Plain_array.name');
  426. $expected = 'CakePHP';
  427. $this->assertEquals($expected, $data);
  428. $data = $this->Cookie->read('Plain_array.version');
  429. $expected = '1.2.0.x';
  430. $this->assertEquals($expected, $data);
  431. $data = $this->Cookie->read('Plain_array.tag');
  432. $expected = 'CakePHP Rocks!';
  433. $this->assertEquals($expected, $data);
  434. $data = $this->Cookie->read('Plain_multi_cookies.name');
  435. $expected = 'CakePHP';
  436. $this->assertEquals($expected, $data);
  437. $data = $this->Cookie->read('Plain_multi_cookies.version');
  438. $expected = '1.2.0.x';
  439. $this->assertEquals($expected, $data);
  440. $data = $this->Cookie->read('Plain_multi_cookies.tag');
  441. $expected = 'CakePHP Rocks!';
  442. $this->assertEquals($expected, $data);
  443. }
  444. /**
  445. * testReadingCookieDataOnStartup
  446. *
  447. * @return void
  448. */
  449. public function testReadingCookieDataOnStartup() {
  450. $data = $this->Cookie->read('Encrytped_array');
  451. $this->assertNull($data);
  452. $data = $this->Cookie->read('Encrytped_multi_cookies');
  453. $this->assertNull($data);
  454. $data = $this->Cookie->read('Plain_array');
  455. $this->assertNull($data);
  456. $data = $this->Cookie->read('Plain_multi_cookies');
  457. $this->assertNull($data);
  458. $_COOKIE['CakeTestCookie'] = array(
  459. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  460. 'Encrytped_multi_cookies' => array(
  461. 'name' => $this->_encrypt('CakePHP'),
  462. 'version' => $this->_encrypt('1.2.0.x'),
  463. 'tag' => $this->_encrypt('CakePHP Rocks!')),
  464. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  465. 'Plain_multi_cookies' => array(
  466. 'name' => 'CakePHP',
  467. 'version' => '1.2.0.x',
  468. 'tag' => 'CakePHP Rocks!'));
  469. $this->Cookie->startup(new CookieComponentTestController());
  470. $data = $this->Cookie->read('Encrytped_array');
  471. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  472. $this->assertEquals($expected, $data);
  473. $data = $this->Cookie->read('Encrytped_multi_cookies');
  474. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  475. $this->assertEquals($expected, $data);
  476. $data = $this->Cookie->read('Plain_array');
  477. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  478. $this->assertEquals($expected, $data);
  479. $data = $this->Cookie->read('Plain_multi_cookies');
  480. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  481. $this->assertEquals($expected, $data);
  482. $this->Cookie->destroy();
  483. unset($_COOKIE['CakeTestCookie']);
  484. }
  485. /**
  486. * testReadingCookieDataWithoutStartup
  487. *
  488. * @return void
  489. */
  490. public function testReadingCookieDataWithoutStartup() {
  491. $data = $this->Cookie->read('Encrytped_array');
  492. $expected = null;
  493. $this->assertEquals($expected, $data);
  494. $data = $this->Cookie->read('Encrytped_multi_cookies');
  495. $expected = null;
  496. $this->assertEquals($expected, $data);
  497. $data = $this->Cookie->read('Plain_array');
  498. $expected = null;
  499. $this->assertEquals($expected, $data);
  500. $data = $this->Cookie->read('Plain_multi_cookies');
  501. $expected = null;
  502. $this->assertEquals($expected, $data);
  503. $_COOKIE['CakeTestCookie'] = array(
  504. 'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
  505. 'Encrytped_multi_cookies' => array(
  506. 'name' => $this->_encrypt('CakePHP'),
  507. 'version' => $this->_encrypt('1.2.0.x'),
  508. 'tag' => $this->_encrypt('CakePHP Rocks!')),
  509. 'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
  510. 'Plain_multi_cookies' => array(
  511. 'name' => 'CakePHP',
  512. 'version' => '1.2.0.x',
  513. 'tag' => 'CakePHP Rocks!'));
  514. $data = $this->Cookie->read('Encrytped_array');
  515. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  516. $this->assertEquals($expected, $data);
  517. $data = $this->Cookie->read('Encrytped_multi_cookies');
  518. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  519. $this->assertEquals($expected, $data);
  520. $data = $this->Cookie->read('Plain_array');
  521. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  522. $this->assertEquals($expected, $data);
  523. $data = $this->Cookie->read('Plain_multi_cookies');
  524. $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
  525. $this->assertEquals($expected, $data);
  526. $this->Cookie->destroy();
  527. unset($_COOKIE['CakeTestCookie']);
  528. }
  529. /**
  530. * Test Reading legacy cookie values.
  531. *
  532. * @return void
  533. */
  534. public function testReadLegacyCookieValue() {
  535. $_COOKIE['CakeTestCookie'] = array(
  536. 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
  537. );
  538. $result = $this->Cookie->read('Legacy.value');
  539. $expected = array(1, 2, 3);
  540. $this->assertEquals($expected, $result);
  541. }
  542. /**
  543. * Test reading empty values.
  544. *
  545. * @return void
  546. */
  547. public function testReadEmpty() {
  548. $_COOKIE['CakeTestCookie'] = array(
  549. 'JSON' => '{"name":"value"}',
  550. 'Empty' => '',
  551. 'String' => '{"somewhat:"broken"}',
  552. 'Array' => '{}'
  553. );
  554. $this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
  555. $this->assertEquals('value', $this->Cookie->read('JSON.name'));
  556. $this->assertEquals('', $this->Cookie->read('Empty'));
  557. $this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
  558. $this->assertEquals(array(), $this->Cookie->read('Array'));
  559. }
  560. /**
  561. * test that no error is issued for non array data.
  562. *
  563. * @return void
  564. */
  565. public function testNoErrorOnNonArrayData() {
  566. $this->Cookie->destroy();
  567. $_COOKIE['CakeTestCookie'] = 'kaboom';
  568. $this->assertNull($this->Cookie->read('value'));
  569. }
  570. /**
  571. * testCheck method
  572. *
  573. * @return void
  574. */
  575. public function testCheck() {
  576. $this->Cookie->write('CookieComponentTestCase', 'value');
  577. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  578. $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  579. }
  580. /**
  581. * testCheckingSavedEmpty method
  582. *
  583. * @return void
  584. */
  585. public function testCheckingSavedEmpty() {
  586. $this->Cookie->write('CookieComponentTestCase', 0);
  587. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  588. $this->Cookie->write('CookieComponentTestCase', '0');
  589. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  590. $this->Cookie->write('CookieComponentTestCase', false);
  591. $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  592. $this->Cookie->write('CookieComponentTestCase', null);
  593. $this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
  594. }
  595. /**
  596. * testCheckKeyWithSpaces method
  597. *
  598. * @return void
  599. */
  600. public function testCheckKeyWithSpaces() {
  601. $this->Cookie->write('CookieComponent Test', "test");
  602. $this->assertTrue($this->Cookie->check('CookieComponent Test'));
  603. $this->Cookie->delete('CookieComponent Test');
  604. $this->Cookie->write('CookieComponent Test.Test Case', "test");
  605. $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  606. }
  607. /**
  608. * testCheckEmpty
  609. *
  610. * @return void
  611. */
  612. public function testCheckEmpty() {
  613. $this->assertFalse($this->Cookie->check());
  614. }
  615. /**
  616. * test that deleting a top level keys kills the child elements too.
  617. *
  618. * @return void
  619. */
  620. public function testDeleteRemovesChildren() {
  621. $_COOKIE['CakeTestCookie'] = array(
  622. 'User' => array('email' => 'example@example.com', 'name' => 'mark'),
  623. 'other' => 'value'
  624. );
  625. $this->assertEquals('mark', $this->Cookie->read('User.name'));
  626. $this->Cookie->delete('User');
  627. $this->assertNull($this->Cookie->read('User.email'));
  628. $this->Cookie->destroy();
  629. }
  630. /**
  631. * Test deleting recursively with keys that don't exist.
  632. *
  633. * @return void
  634. */
  635. public function testDeleteChildrenNotExist() {
  636. $this->assertNull($this->Cookie->delete('NotFound'));
  637. $this->assertNull($this->Cookie->delete('Not.Found'));
  638. }
  639. /**
  640. * Helper method for generating old style encoded cookie values.
  641. *
  642. * @return string.
  643. */
  644. protected function _oldImplode(array $array) {
  645. $string = '';
  646. foreach ($array as $key => $value) {
  647. $string .= ',' . $key . '|' . $value;
  648. }
  649. return substr($string, 1);
  650. }
  651. /**
  652. * Implode method to keep keys are multidimensional arrays
  653. *
  654. * @param array $array Map of key and values
  655. * @return string String in the form key1|value1,key2|value2
  656. */
  657. protected function _implode(array $array) {
  658. return json_encode($array);
  659. }
  660. /**
  661. * encrypt method
  662. *
  663. * @param array|string $value
  664. * @return string
  665. */
  666. protected function _encrypt($value) {
  667. if (is_array($value)) {
  668. $value = $this->_implode($value);
  669. }
  670. return "Q2FrZQ==." . base64_encode(Security::cipher($value, $this->Cookie->key));
  671. }
  672. }