CookieTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. */
  40. public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
  41. {
  42. $this->expectException('InvalidArgumentException');
  43. new Cookie($name);
  44. }
  45. public function testInvalidExpiration()
  46. {
  47. $this->expectException('InvalidArgumentException');
  48. new Cookie('MyCookie', 'foo', 'bar');
  49. }
  50. public function testNegativeExpirationIsNotPossible()
  51. {
  52. $cookie = new Cookie('foo', 'bar', -100);
  53. $this->assertSame(0, $cookie->getExpiresTime());
  54. }
  55. public function testGetValue()
  56. {
  57. $value = 'MyValue';
  58. $cookie = new Cookie('MyCookie', $value);
  59. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  60. }
  61. public function testGetPath()
  62. {
  63. $cookie = new Cookie('foo', 'bar');
  64. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  65. }
  66. public function testGetExpiresTime()
  67. {
  68. $cookie = new Cookie('foo', 'bar');
  69. $this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
  70. $cookie = new Cookie('foo', 'bar', $expire = time() + 3600);
  71. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  72. }
  73. public function testGetExpiresTimeIsCastToInt()
  74. {
  75. $cookie = new Cookie('foo', 'bar', 3600.9);
  76. $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
  77. }
  78. public function testConstructorWithDateTime()
  79. {
  80. $expire = new \DateTime();
  81. $cookie = new Cookie('foo', 'bar', $expire);
  82. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  83. }
  84. /**
  85. * @requires PHP 5.5
  86. */
  87. public function testConstructorWithDateTimeImmutable()
  88. {
  89. $expire = new \DateTimeImmutable();
  90. $cookie = new Cookie('foo', 'bar', $expire);
  91. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  92. }
  93. public function testGetExpiresTimeWithStringValue()
  94. {
  95. $value = '+1 day';
  96. $cookie = new Cookie('foo', 'bar', $value);
  97. $expire = strtotime($value);
  98. $this->assertEqualsWithDelta($expire, $cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');
  99. }
  100. public function testGetDomain()
  101. {
  102. $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com');
  103. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  104. }
  105. public function testIsSecure()
  106. {
  107. $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', true);
  108. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  109. }
  110. public function testIsHttpOnly()
  111. {
  112. $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
  113. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  114. }
  115. public function testCookieIsNotCleared()
  116. {
  117. $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
  118. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  119. }
  120. public function testCookieIsCleared()
  121. {
  122. $cookie = new Cookie('foo', 'bar', time() - 20);
  123. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  124. $cookie = new Cookie('foo', 'bar');
  125. $this->assertFalse($cookie->isCleared());
  126. $cookie = new Cookie('foo', 'bar', 0);
  127. $this->assertFalse($cookie->isCleared());
  128. $cookie = new Cookie('foo', 'bar', -1);
  129. $this->assertFalse($cookie->isCleared());
  130. }
  131. public function testToString()
  132. {
  133. $cookie = new Cookie('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  134. $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');
  135. $cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
  136. $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)');
  137. $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
  138. $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');
  139. $cookie = new Cookie('foo', 'bar', 0, '/', '');
  140. $this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
  141. }
  142. public function testRawCookie()
  143. {
  144. $cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
  145. $this->assertFalse($cookie->isRaw());
  146. $this->assertEquals('foo=b%20a%20r; path=/', (string) $cookie);
  147. $cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
  148. $this->assertTrue($cookie->isRaw());
  149. $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
  150. }
  151. public function testGetMaxAge()
  152. {
  153. $cookie = new Cookie('foo', 'bar');
  154. $this->assertEquals(0, $cookie->getMaxAge());
  155. $cookie = new Cookie('foo', 'bar', $expire = time() + 100);
  156. $this->assertEquals($expire - time(), $cookie->getMaxAge());
  157. $cookie = new Cookie('foo', 'bar', $expire = time() - 100);
  158. $this->assertEquals(0, $cookie->getMaxAge());
  159. }
  160. public function testFromString()
  161. {
  162. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  163. $this->assertEquals(new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, true), $cookie);
  164. $cookie = Cookie::fromString('foo=bar', true);
  165. $this->assertEquals(new Cookie('foo', 'bar', 0, '/', null, false, false), $cookie);
  166. }
  167. public function testFromStringWithHttpOnly()
  168. {
  169. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  170. $this->assertTrue($cookie->isHttpOnly());
  171. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
  172. $this->assertFalse($cookie->isHttpOnly());
  173. }
  174. public function testSameSiteAttributeIsCaseInsensitive()
  175. {
  176. $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, 'Lax');
  177. $this->assertEquals('lax', $cookie->getSameSite());
  178. }
  179. }