CookieTest.php 18 KB

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