CookieTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. /**
  14. * CookieTest.
  15. *
  16. * @author John Kary <john@johnkary.net>
  17. * @author Hugo Hamon <hugo.hamon@sensio.com>
  18. *
  19. * @group time-sensitive
  20. */
  21. class CookieTest extends TestCase
  22. {
  23. public function invalidNames()
  24. {
  25. return [
  26. [''],
  27. [',MyName'],
  28. [';MyName'],
  29. [' MyName'],
  30. ["\tMyName"],
  31. ["\rMyName"],
  32. ["\nMyName"],
  33. ["\013MyName"],
  34. ["\014MyName"],
  35. ];
  36. }
  37. /**
  38. * @dataProvider invalidNames
  39. * @expectedException \InvalidArgumentException
  40. */
  41. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  42. {
  43. Cookie::create($name);
  44. }
  45. /**
  46. * @expectedException \InvalidArgumentException
  47. */
  48. public function testInvalidExpiration()
  49. {
  50. Cookie::create('MyCookie', 'foo', 'bar');
  51. }
  52. public function testNegativeExpirationIsNotPossible()
  53. {
  54. $cookie = Cookie::create('foo', 'bar', -100);
  55. $this->assertSame(0, $cookie->getExpiresTime());
  56. }
  57. public function testGetValue()
  58. {
  59. $value = 'MyValue';
  60. $cookie = Cookie::create('MyCookie', $value);
  61. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  62. }
  63. public function testGetPath()
  64. {
  65. $cookie = Cookie::create('foo', 'bar');
  66. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  67. }
  68. public function testGetExpiresTime()
  69. {
  70. $cookie = Cookie::create('foo', 'bar');
  71. $this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
  72. $cookie = Cookie::create('foo', 'bar', $expire = time() + 3600);
  73. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  74. }
  75. public function testGetExpiresTimeIsCastToInt()
  76. {
  77. $cookie = Cookie::create('foo', 'bar', 3600.9);
  78. $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
  79. }
  80. public function testConstructorWithDateTime()
  81. {
  82. $expire = new \DateTime();
  83. $cookie = Cookie::create('foo', 'bar', $expire);
  84. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  85. }
  86. public function testConstructorWithDateTimeImmutable()
  87. {
  88. $expire = new \DateTimeImmutable();
  89. $cookie = Cookie::create('foo', 'bar', $expire);
  90. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  91. }
  92. public function testGetExpiresTimeWithStringValue()
  93. {
  94. $value = '+1 day';
  95. $cookie = Cookie::create('foo', 'bar', $value);
  96. $expire = strtotime($value);
  97. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
  98. }
  99. public function testGetDomain()
  100. {
  101. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com');
  102. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  103. }
  104. public function testIsSecure()
  105. {
  106. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com', true);
  107. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  108. }
  109. public function testIsHttpOnly()
  110. {
  111. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
  112. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  113. }
  114. public function testCookieIsNotCleared()
  115. {
  116. $cookie = Cookie::create('foo', 'bar', time() + 3600 * 24);
  117. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  118. }
  119. public function testCookieIsCleared()
  120. {
  121. $cookie = Cookie::create('foo', 'bar', time() - 20);
  122. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  123. $cookie = Cookie::create('foo', 'bar');
  124. $this->assertFalse($cookie->isCleared());
  125. $cookie = Cookie::create('foo', 'bar');
  126. $this->assertFalse($cookie->isCleared());
  127. $cookie = Cookie::create('foo', 'bar', -1);
  128. $this->assertFalse($cookie->isCleared());
  129. }
  130. public function testToString()
  131. {
  132. $cookie = Cookie::create('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, false, null);
  133. $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
  134. $cookie = Cookie::create('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, false, null);
  135. $this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
  136. $cookie = Cookie::create('foo', null, 1, '/admin/', '.myfoodomain.com', false, true, false, null);
  137. $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
  138. $cookie = Cookie::create('foo', 'bar');
  139. $this->assertEquals('foo=bar; path=/; httponly; samesite=lax', (string) $cookie);
  140. }
  141. public function testRawCookie()
  142. {
  143. $cookie = Cookie::create('foo', 'b a r', 0, '/', null, false, false, false, null);
  144. $this->assertFalse($cookie->isRaw());
  145. $this->assertEquals('foo=b%20a%20r; path=/', (string) $cookie);
  146. $cookie = Cookie::create('foo', 'b+a+r', 0, '/', null, false, false, true, null);
  147. $this->assertTrue($cookie->isRaw());
  148. $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
  149. }
  150. public function testGetMaxAge()
  151. {
  152. $cookie = Cookie::create('foo', 'bar');
  153. $this->assertEquals(0, $cookie->getMaxAge());
  154. $cookie = Cookie::create('foo', 'bar', $expire = time() + 100);
  155. $this->assertEquals($expire - time(), $cookie->getMaxAge());
  156. $cookie = Cookie::create('foo', 'bar', $expire = time() - 100);
  157. $this->assertEquals(0, $cookie->getMaxAge());
  158. }
  159. public function testFromString()
  160. {
  161. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  162. $this->assertEquals(Cookie::create('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, true, null), $cookie);
  163. $cookie = Cookie::fromString('foo=bar', true);
  164. $this->assertEquals(Cookie::create('foo', 'bar', 0, '/', null, false, false, false, null), $cookie);
  165. $cookie = Cookie::fromString('foo', true);
  166. $this->assertEquals(Cookie::create('foo', null, 0, '/', null, false, false, false, null), $cookie);
  167. }
  168. public function testFromStringWithHttpOnly()
  169. {
  170. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  171. $this->assertTrue($cookie->isHttpOnly());
  172. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
  173. $this->assertFalse($cookie->isHttpOnly());
  174. }
  175. public function testSameSiteAttribute()
  176. {
  177. $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, 'Lax');
  178. $this->assertEquals('lax', $cookie->getSameSite());
  179. $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, '');
  180. $this->assertNull($cookie->getSameSite());
  181. }
  182. public function testSetSecureDefault()
  183. {
  184. $cookie = Cookie::create('foo', 'bar');
  185. $this->assertFalse($cookie->isSecure());
  186. $cookie->setSecureDefault(true);
  187. $this->assertTrue($cookie->isSecure());
  188. $cookie->setSecureDefault(false);
  189. $this->assertFalse($cookie->isSecure());
  190. }
  191. }