CookieTest.php 16 KB

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