You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CookieTest.php 8.5 KiB

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