CookieTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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; path=/', $result);
  70. $date = Chronos::createFromFormat('m/d/Y h:i: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=Wed, 01-Dec-2027 12:00:00 GMT; path=/; 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 default path in cookies
  195. *
  196. * @return void
  197. */
  198. public function testDefaultPath()
  199. {
  200. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  201. $this->assertContains('path=/', $cookie->toHeaderValue());
  202. }
  203. /**
  204. * Test setting httponly in cookies
  205. *
  206. * @return void
  207. */
  208. public function testWithHttpOnlyInvalidConstructor()
  209. {
  210. $this->expectException(\InvalidArgumentException::class);
  211. $this->expectExceptionMessage('The provided arg must be of type `bool` but `string` given');
  212. new Cookie('cakephp', 'cakephp-rocks', null, '', '', false, 'invalid');
  213. }
  214. /**
  215. * Test setting httponly in cookies
  216. *
  217. * @return void
  218. */
  219. public function testWithHttpOnlyInvalid()
  220. {
  221. $this->expectException(\InvalidArgumentException::class);
  222. $this->expectExceptionMessage('The provided arg must be of type `bool` but `string` given');
  223. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  224. $cookie->withHttpOnly('no');
  225. }
  226. /**
  227. * Test setting httponly in cookies
  228. *
  229. * @return void
  230. */
  231. public function testWithHttpOnly()
  232. {
  233. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  234. $new = $cookie->withHttpOnly(true);
  235. $this->assertNotSame($new, $cookie, 'Should clone');
  236. $this->assertFalse($cookie->isHttpOnly());
  237. $this->assertTrue($new->isHttpOnly());
  238. }
  239. /**
  240. * Test setting secure in cookies
  241. *
  242. * @return void
  243. */
  244. public function testWithSecureInvalidConstructor()
  245. {
  246. $this->expectException(\InvalidArgumentException::class);
  247. $this->expectExceptionMessage('The provided arg must be of type `bool` but `string` given');
  248. new Cookie('cakephp', 'cakephp-rocks', null, '', '', 'invalid');
  249. }
  250. /**
  251. * Test setting secure in cookies
  252. *
  253. * @return void
  254. */
  255. public function testWithSecureInvalid()
  256. {
  257. $this->expectException(\InvalidArgumentException::class);
  258. $this->expectExceptionMessage('The provided arg must be of type `bool` but `string` given');
  259. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  260. $cookie->withSecure('no');
  261. }
  262. /**
  263. * Test setting secure in cookies
  264. *
  265. * @return void
  266. */
  267. public function testWithSecure()
  268. {
  269. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  270. $new = $cookie->withSecure(true);
  271. $this->assertNotSame($new, $cookie, 'Should clone');
  272. $this->assertFalse($cookie->isSecure());
  273. $this->assertTrue($new->isSecure());
  274. }
  275. /**
  276. * Test the never expiry method
  277. *
  278. * @return void
  279. */
  280. public function testWithNeverExpire()
  281. {
  282. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  283. $new = $cookie->withNeverExpire();
  284. $this->assertNotSame($new, $cookie, 'Should clone');
  285. $this->assertContains('01-Jan-2038', $new->toHeaderValue());
  286. }
  287. /**
  288. * Test the expired method
  289. *
  290. * @return void
  291. */
  292. public function testWithExpired()
  293. {
  294. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  295. $new = $cookie->withExpired();
  296. $this->assertNotSame($new, $cookie, 'Should clone');
  297. $this->assertNotContains('expiry', $cookie->toHeaderValue());
  298. $this->assertContains('01-Jan-1970', $new->toHeaderValue());
  299. }
  300. /**
  301. * Test the withExpiry method
  302. *
  303. * @return void
  304. */
  305. public function testWithExpiry()
  306. {
  307. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  308. $new = $cookie->withExpiry(Chronos::createFromDate(2022, 6, 15));
  309. $this->assertNotSame($new, $cookie, 'Should clone');
  310. $this->assertNotContains('expires', $cookie->toHeaderValue());
  311. $this->assertContains('expires=Wed, 15-Jun-2022', $new->toHeaderValue());
  312. }
  313. /**
  314. * Test the withExpiry method changes timezone
  315. *
  316. * @return void
  317. */
  318. public function testWithExpiryChangesTimezone()
  319. {
  320. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  321. $date = Chronos::createFromDate(2022, 6, 15);
  322. $date = $date->setTimezone('America/New_York');
  323. $new = $cookie->withExpiry($date);
  324. $this->assertNotSame($new, $cookie, 'Should clone');
  325. $this->assertNotContains('expires', $cookie->toHeaderValue());
  326. $this->assertContains('expires=Wed, 15-Jun-2022', $new->toHeaderValue());
  327. $this->assertContains('GMT', $new->toHeaderValue());
  328. $this->assertSame($date->format('U'), $new->getExpiresTimestamp());
  329. }
  330. /**
  331. * Test the isExpired method
  332. *
  333. * @return void
  334. */
  335. public function testIsExpired()
  336. {
  337. $date = Chronos::now();
  338. $cookie = new Cookie('cakephp', 'yay');
  339. $this->assertFalse($cookie->isExpired($date));
  340. $cookie = new Cookie('cakephp', 'yay', $date);
  341. $this->assertFalse($cookie->isExpired($date), 'same time, not expired');
  342. $date = $date->modify('+10 seconds');
  343. $this->assertTrue($cookie->isExpired($date), 'future now');
  344. $date = $date->modify('-1 minute');
  345. $this->assertFalse($cookie->isExpired($date), 'expires later');
  346. }
  347. /**
  348. * Test the withName method
  349. *
  350. * @return void
  351. */
  352. public function testWithName()
  353. {
  354. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  355. $new = $cookie->withName('user');
  356. $this->assertNotSame($new, $cookie, 'Should clone');
  357. $this->assertNotSame('user', $cookie->getName());
  358. $this->assertSame('user', $new->getName());
  359. }
  360. /**
  361. * Test the withAddedValue method
  362. *
  363. * @return void
  364. */
  365. public function testWithAddedValue()
  366. {
  367. $cookie = new Cookie('cakephp', '{"type":"mvc", "icing": true}');
  368. $new = $cookie->withAddedValue('type', 'mvc')
  369. ->withAddedValue('user.name', 'mark');
  370. $this->assertNotSame($new, $cookie, 'Should clone');
  371. $this->assertNull($cookie->read('user.name'));
  372. $this->assertSame('mvc', $new->read('type'));
  373. $this->assertSame('mark', $new->read('user.name'));
  374. }
  375. /**
  376. * Test the withoutAddedValue method
  377. *
  378. * @return void
  379. */
  380. public function testWithoutAddedValue()
  381. {
  382. $cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
  383. $new = $cookie->withoutAddedValue('type', 'mvc')
  384. ->withoutAddedValue('user.name');
  385. $this->assertNotSame($new, $cookie, 'Should clone');
  386. $this->assertNotNull($cookie->read('type'));
  387. $this->assertNull($new->read('type'));
  388. $this->assertNull($new->read('user.name'));
  389. }
  390. /**
  391. * Test check() with serialized source data.
  392. *
  393. * @return void
  394. */
  395. public function testCheckStringSourceData()
  396. {
  397. $cookie = new Cookie('cakephp', '{"type":"mvc", "user": {"name":"mark"}}');
  398. $this->assertTrue($cookie->check('type'));
  399. $this->assertTrue($cookie->check('user.name'));
  400. $this->assertFalse($cookie->check('nope'));
  401. $this->assertFalse($cookie->check('user.nope'));
  402. }
  403. /**
  404. * Test check() with array source data.
  405. *
  406. * @return void
  407. */
  408. public function testCheckArraySourceData()
  409. {
  410. $data = [
  411. 'type' => 'mvc',
  412. 'user' => ['name' => 'mark']
  413. ];
  414. $cookie = new Cookie('cakephp', $data);
  415. $this->assertTrue($cookie->check('type'));
  416. $this->assertTrue($cookie->check('user.name'));
  417. $this->assertFalse($cookie->check('nope'));
  418. $this->assertFalse($cookie->check('user.nope'));
  419. }
  420. /**
  421. * test read() and set on different types
  422. *
  423. * @return void
  424. */
  425. public function testReadExpandsOnDemand()
  426. {
  427. $data = [
  428. 'username' => 'florian',
  429. 'profile' => [
  430. 'profession' => 'developer'
  431. ]
  432. ];
  433. $cookie = new Cookie('cakephp', json_encode($data));
  434. $this->assertFalse($cookie->isExpanded());
  435. $this->assertEquals('developer', $cookie->read('profile.profession'));
  436. $this->assertTrue($cookie->isExpanded(), 'Cookies expand when read.');
  437. $cookie = $cookie->withValue(json_encode($data));
  438. $this->assertTrue($cookie->check('profile.profession'), 'Cookies expand when read.');
  439. $this->assertTrue($cookie->isExpanded());
  440. $cookie = $cookie->withValue(json_encode($data))
  441. ->withAddedValue('face', 'punch');
  442. $this->assertTrue($cookie->isExpanded());
  443. $this->assertSame('punch', $cookie->read('face'));
  444. }
  445. /**
  446. * test read() on structured data.
  447. *
  448. * @return void
  449. */
  450. public function testReadComplexData()
  451. {
  452. $data = [
  453. 'username' => 'florian',
  454. 'profile' => [
  455. 'profession' => 'developer'
  456. ]
  457. ];
  458. $cookie = new Cookie('cakephp', $data);
  459. $result = $cookie->getValue();
  460. $this->assertEquals($data, $result);
  461. $result = $cookie->read('foo');
  462. $this->assertNull($result);
  463. $result = $cookie->read();
  464. $this->assertEquals($data, $result);
  465. $result = $cookie->read('profile.profession');
  466. $this->assertEquals('developer', $result);
  467. }
  468. /**
  469. * Test reading complex data serialized in 1.x and early 2.x
  470. *
  471. * @return void
  472. */
  473. public function testReadLegacyComplexData()
  474. {
  475. $data = 'key|value,key2|value2';
  476. $cookie = new Cookie('cakephp', $data);
  477. $this->assertEquals('value', $cookie->read('key'));
  478. $this->assertNull($cookie->read('nope'));
  479. }
  480. /**
  481. * Test that toHeaderValue() collapses data.
  482. *
  483. * @return void
  484. */
  485. public function testToHeaderValueCollapsesComplexData()
  486. {
  487. $data = [
  488. 'username' => 'florian',
  489. 'profile' => [
  490. 'profession' => 'developer'
  491. ]
  492. ];
  493. $cookie = new Cookie('cakephp', $data);
  494. $this->assertEquals('developer', $cookie->read('profile.profession'));
  495. $expected = '{"username":"florian","profile":{"profession":"developer"}}';
  496. $this->assertContains(urlencode($expected), $cookie->toHeaderValue());
  497. }
  498. /**
  499. * Tests getting the id
  500. *
  501. * @return void
  502. */
  503. public function testGetId()
  504. {
  505. $cookie = new Cookie('cakephp', 'cakephp-rocks');
  506. $this->assertEquals('cakephp;;/', $cookie->getId());
  507. $cookie = new Cookie('CAKEPHP', 'cakephp-rocks');
  508. $this->assertEquals('CAKEPHP;;/', $cookie->getId());
  509. $cookie = new Cookie('test', 'val', null, '/path', 'example.com');
  510. $this->assertEquals('test;example.com;/path', $cookie->getId());
  511. }
  512. }