CookieTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. * @link http://cakephp.org CakePHP(tm) Project
  10. * @since 3.5.0
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Test\TestCase\Http\Cookie;
  14. use Cake\Chronos\Chronos;
  15. use Cake\Http\Cookie\Cookie;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * HTTP cookies test.
  19. */
  20. class CookieTest extends TestCase
  21. {
  22. /**
  23. * Encryption key used in the tests
  24. *
  25. * @var string
  26. */
  27. protected $encryptionKey = 'someverysecretkeythatisatleast32charslong';
  28. /**
  29. * Test invalid cookie name
  30. *
  31. * @expectedException \InvalidArgumentException
  32. * @expectedExceptionMessage The cookie name `no, this wont, work` contains invalid characters.
  33. */
  34. public function testValidateNameInvalidChars()
  35. {
  36. new Cookie('no, this wont, work', '');
  37. }
  38. /**
  39. * Test empty cookie name
  40. *
  41. * @expectedException \InvalidArgumentException
  42. * @expectedExceptionMessage The cookie name cannot be empty.
  43. * @return void
  44. */
  45. public function testValidateNameEmptyName()
  46. {
  47. new Cookie('', '');
  48. }
  49. /**
  50. * Test decrypting the cookie
  51. *
  52. * @return void
  53. */
  54. public function testDecrypt()
  55. {
  56. $value = 'cakephp-rocks-and-is-awesome';
  57. $cookie = new Cookie('cakephp', $value);
  58. $cookie->encrypt($this->encryptionKey);
  59. $this->assertTextStartsWith('Q2FrZQ==.', $cookie->getValue());
  60. $cookie->decrypt($this->encryptionKey);
  61. $this->assertSame($value, $cookie->getValue());
  62. }
  63. /**
  64. * Testing encrypting the cookie
  65. *
  66. * @return void
  67. */
  68. public function testEncrypt()
  69. {
  70. $value = 'cakephp-rocks-and-is-awesome';
  71. $cookie = new Cookie('cakephp', $value);
  72. $cookie->encrypt($this->encryptionKey);
  73. $this->assertNotEquals($value, $cookie->getValue());
  74. $this->assertNotEmpty($cookie->getValue());
  75. }
  76. /**
  77. * Tests the header value
  78. *
  79. * @return void
  80. */
  81. public function testToHeaderValue()
  82. {
  83. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  84. $result = $cookie->toHeaderValue();
  85. $this->assertEquals('cakephp=cakephp-rocks', $result);
  86. $date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2027 12:00:00');
  87. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  88. $cookie = $cookie->withDomain('cakephp.org')
  89. ->withExpiry($date)
  90. ->withHttpOnly(true)
  91. ->withSecure(true);
  92. $result = $cookie->toHeaderValue();
  93. $expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; domain=cakephp.org; secure; httponly';
  94. $this->assertEquals($expected, $result);
  95. }
  96. /**
  97. * Test getting the value from the cookie
  98. *
  99. * @return void
  100. */
  101. public function testGetValue()
  102. {
  103. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  104. $result = $cookie->getValue();
  105. $this->assertEquals('cakephp-rocks', $result);
  106. $cookie = new Cookie('cakephp', '');
  107. $result = $cookie->getValue();
  108. $this->assertEquals('', $result);
  109. }
  110. /**
  111. * Test setting values in cookies
  112. *
  113. * @return void
  114. */
  115. public function testWithValue()
  116. {
  117. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  118. $new = $cookie->withValue('new');
  119. $this->assertNotSame($new, $cookie, 'Should make a clone');
  120. $this->assertSame('cakephp-rocks', $cookie->getValue(), 'old instance not modified');
  121. $this->assertSame('new', $new->getValue());
  122. }
  123. /**
  124. * Test setting domain in cookies
  125. *
  126. * @return void
  127. * @expectedException \InvalidArgumentException
  128. * @expectedExceptionMessage The provided arg must be of type `string` but `integer` given
  129. */
  130. public function testWithDomainInvalidConstructor()
  131. {
  132. new Cookie('cakephp', 'rocks', null, '', 1234);
  133. }
  134. /**
  135. * Test setting domain in cookies
  136. *
  137. * @return void
  138. * @expectedException \InvalidArgumentException
  139. * @expectedExceptionMessage The provided arg must be of type `string` but `array` given
  140. */
  141. public function testWithDomainInvalid()
  142. {
  143. $cookie = new Cookie('cakephp', 'rocks');
  144. $cookie->withDomain(['oops']);
  145. }
  146. /**
  147. * Test setting domain in cookies
  148. *
  149. * @return void
  150. */
  151. public function testWithDomain()
  152. {
  153. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  154. $new = $cookie->withDomain('example.com');
  155. $this->assertNotSame($new, $cookie, 'Should make a clone');
  156. $this->assertNotContains('example.com', $cookie->toHeaderValue(), 'old instance not modified');
  157. $this->assertContains('domain=example.com', $new->toHeaderValue());
  158. }
  159. /**
  160. * Test setting path in cookies
  161. *
  162. * @return void
  163. * @expectedException \InvalidArgumentException
  164. * @expectedExceptionMessage The provided arg must be of type `string` but `array` given
  165. */
  166. public function testWithPathInvalid()
  167. {
  168. $cookie = new Cookie('cakephp', 'rocks');
  169. $cookie->withPath(['oops']);
  170. }
  171. /**
  172. * Test setting path in cookies
  173. *
  174. * @return void
  175. * @expectedException \InvalidArgumentException
  176. * @expectedExceptionMessage The provided arg must be of type `string` but `integer` given
  177. */
  178. public function testWithPathInvalidConstructor()
  179. {
  180. new Cookie('cakephp', 'rocks', null, 123);
  181. }
  182. /**
  183. * Test setting path in cookies
  184. *
  185. * @return void
  186. */
  187. public function testWithPath()
  188. {
  189. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  190. $new = $cookie->withPath('/api');
  191. $this->assertNotSame($new, $cookie, 'Should make a clone');
  192. $this->assertNotContains('path=/api', $cookie->toHeaderValue(), 'old instance not modified');
  193. $this->assertContains('path=/api', $new->toHeaderValue());
  194. }
  195. /**
  196. * Test setting httponly in cookies
  197. *
  198. * @return void
  199. * @expectedException \InvalidArgumentException
  200. * @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
  201. */
  202. public function testWithHttpOnlyInvalidConstructor()
  203. {
  204. new Cookie('cakephp', 'cakephp-rocks', null, '', '', false, 'invalid');
  205. }
  206. /**
  207. * Test setting httponly in cookies
  208. *
  209. * @return void
  210. * @expectedException \InvalidArgumentException
  211. * @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
  212. */
  213. public function testWithHttpOnlyInvalid()
  214. {
  215. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  216. $cookie->withHttpOnly('no');
  217. }
  218. /**
  219. * Test setting httponly in cookies
  220. *
  221. * @return void
  222. */
  223. public function testWithHttpOnly()
  224. {
  225. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  226. $new = $cookie->withHttpOnly(true);
  227. $this->assertNotSame($new, $cookie, 'Should clone');
  228. $this->assertFalse($cookie->isHttpOnly());
  229. $this->assertTrue($new->isHttpOnly());
  230. }
  231. /**
  232. * Test setting secure in cookies
  233. *
  234. * @return void
  235. * @expectedException \InvalidArgumentException
  236. * @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
  237. */
  238. public function testWithSecureInvalidConstructor()
  239. {
  240. new Cookie('cakephp', 'cakephp-rocks', null, '', '', 'invalid');
  241. }
  242. /**
  243. * Test setting secure in cookies
  244. *
  245. * @return void
  246. * @expectedException \InvalidArgumentException
  247. * @expectedExceptionMessage The provided arg must be of type `bool` but `string` given
  248. */
  249. public function testWithSecureInvalid()
  250. {
  251. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  252. $cookie->withSecure('no');
  253. }
  254. /**
  255. * Test setting secure in cookies
  256. *
  257. * @return void
  258. */
  259. public function testWithSecure()
  260. {
  261. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  262. $new = $cookie->withSecure(true);
  263. $this->assertNotSame($new, $cookie, 'Should clone');
  264. $this->assertFalse($cookie->isSecure());
  265. $this->assertTrue($new->isSecure());
  266. }
  267. /**
  268. * Test the never expiry method
  269. *
  270. * @return void
  271. */
  272. public function testWithNeverExpire()
  273. {
  274. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  275. $new = $cookie->withNeverExpire();
  276. $this->assertNotSame($new, $cookie, 'Should clone');
  277. $this->assertContains('01-Jan-2038', $new->toHeaderValue());
  278. }
  279. /**
  280. * Test the expired method
  281. *
  282. * @return void
  283. */
  284. public function testWithExpired()
  285. {
  286. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  287. $new = $cookie->withExpired();
  288. $this->assertNotSame($new, $cookie, 'Should clone');
  289. $this->assertNotContains('expiry', $cookie->toHeaderValue());
  290. $now = Chronos::parse('-1 year');
  291. $this->assertContains($now->format('Y'), $new->toHeaderValue());
  292. }
  293. /**
  294. * Test the withExpiry method
  295. *
  296. * @return void
  297. */
  298. public function testWithExpiry()
  299. {
  300. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  301. $new = $cookie->withExpiry(Chronos::createFromDate(2022, 6, 15));
  302. $this->assertNotSame($new, $cookie, 'Should clone');
  303. $this->assertNotContains('expires', $cookie->toHeaderValue());
  304. $this->assertContains('expires=Wed, 15-Jun-2022', $new->toHeaderValue());
  305. }
  306. /**
  307. * Test the withName method
  308. *
  309. * @return void
  310. */
  311. public function testWithName()
  312. {
  313. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  314. $new = $cookie->withName('user');
  315. $this->assertNotSame($new, $cookie, 'Should clone');
  316. $this->assertNotSame('user', $cookie->getName());
  317. $this->assertSame('user', $new->getName());
  318. }
  319. /**
  320. * Test the withAddedValue method
  321. *
  322. * @return void
  323. */
  324. public function testWithAddedValue()
  325. {
  326. $cookie = new Cookie('cakephp', '{"type":"mvc", "icing": true}');
  327. $new = $cookie->withAddedValue('type', 'mvc')
  328. ->withAddedValue('user.name', 'mark');
  329. $this->assertNotSame($new, $cookie, 'Should clone');
  330. $this->assertNull($cookie->read('user.name'));
  331. $this->assertSame('mvc', $new->read('type'));
  332. $this->assertSame('mark', $new->read('user.name'));
  333. }
  334. /**
  335. * Test the withoutAddedValue method
  336. *
  337. * @return void
  338. */
  339. public function testWithoutAddedValue()
  340. {
  341. $cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
  342. $new = $cookie->withoutAddedValue('type', 'mvc')
  343. ->withoutAddedValue('user.name');
  344. $this->assertNotSame($new, $cookie, 'Should clone');
  345. $this->assertNotNull($cookie->read('type'));
  346. $this->assertNull($new->read('type'));
  347. $this->assertNull($new->read('user.name'));
  348. }
  349. /**
  350. * Test check() with serialized source data.
  351. *
  352. * @return void
  353. */
  354. public function testCheckStringSourceData()
  355. {
  356. $cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
  357. $this->assertTrue($cookie->check('type'));
  358. $this->assertTrue($cookie->check('user.name'));
  359. $this->assertFalse($cookie->check('nope'));
  360. $this->assertFalse($cookie->check('user.nope'));
  361. }
  362. /**
  363. * Test check() with array source data.
  364. *
  365. * @return void
  366. */
  367. public function testCheckArraySourceData()
  368. {
  369. $data = [
  370. 'type' => 'mvc',
  371. 'user' => ['name' => 'mark']
  372. ];
  373. $cookie = new Cookie('cakephp', $data);
  374. $this->assertTrue($cookie->check('type'));
  375. $this->assertTrue($cookie->check('user.name'));
  376. $this->assertFalse($cookie->check('nope'));
  377. $this->assertFalse($cookie->check('user.nope'));
  378. }
  379. /**
  380. * test read() and set on different types
  381. *
  382. * @return void
  383. */
  384. public function testReadExpandsOnDemand()
  385. {
  386. $data = [
  387. 'username' => 'florian',
  388. 'profile' => [
  389. 'profession' => 'developer'
  390. ]
  391. ];
  392. $cookie = new Cookie('cakephp', json_encode($data));
  393. $this->assertFalse($cookie->isExpanded());
  394. $this->assertEquals('developer', $cookie->read('profile.profession'));
  395. $this->assertTrue($cookie->isExpanded(), 'Cookies expand when read.');
  396. $cookie = $cookie->withValue(json_encode($data));
  397. $this->assertTrue($cookie->check('profile.profession'), 'Cookies expand when read.');
  398. $this->assertTrue($cookie->isExpanded());
  399. $cookie = $cookie->withValue(json_encode($data))
  400. ->withAddedValue('face', 'punch');
  401. $this->assertTrue($cookie->isExpanded());
  402. $this->assertSame('punch', $cookie->read('face'));
  403. }
  404. /**
  405. * test read() on structured data.
  406. *
  407. * @return void
  408. */
  409. public function testReadComplexData()
  410. {
  411. $data = [
  412. 'username' => 'florian',
  413. 'profile' => [
  414. 'profession' => 'developer'
  415. ]
  416. ];
  417. $cookie = new Cookie('cakephp', $data);
  418. $result = $cookie->getValue();
  419. $this->assertEquals($data, $result);
  420. $result = $cookie->read('foo');
  421. $this->assertNull($result);
  422. $result = $cookie->read();
  423. $this->assertEquals($data, $result);
  424. $result = $cookie->read('profile.profession');
  425. $this->assertEquals('developer', $result);
  426. }
  427. /**
  428. * Test reading complex data serialized in 1.x and early 2.x
  429. *
  430. * @return void
  431. */
  432. public function testReadLegacyComplexData()
  433. {
  434. $data = 'key|value,key2|value2';
  435. $cookie = new Cookie('cakephp', $data);
  436. $this->assertEquals('value', $cookie->read('key'));
  437. $this->assertNull($cookie->read('nope'));
  438. }
  439. /**
  440. * Test that toHeaderValue() collapses data.
  441. *
  442. * @return void
  443. */
  444. public function testToHeaderValueCollapsesComplexData()
  445. {
  446. $data = [
  447. 'username' => 'florian',
  448. 'profile' => [
  449. 'profession' => 'developer'
  450. ]
  451. ];
  452. $cookie = new Cookie('cakephp', $data);
  453. $this->assertEquals('developer', $cookie->read('profile.profession'));
  454. $expected = '{"username":"florian","profile":{"profession":"developer"}}';
  455. $this->assertContains(urlencode($expected), $cookie->toHeaderValue());
  456. }
  457. /**
  458. * Tests getting the id
  459. *
  460. * @return void
  461. */
  462. public function testGetId()
  463. {
  464. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  465. $this->assertEquals('cakephp;;', $cookie->getId());
  466. $cookie = new Cookie('test', 'val', null, '/path', 'example.com');
  467. $this->assertEquals('test;example.com;/path', $cookie->getId());
  468. }
  469. }