PageRenderTime 27ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

http://github.com/symfony/symfony
PHP | 366 lines | 245 code | 96 blank | 25 comment | 0 complexity | 2d5ee686dfd146206bc20e0c5dbfffb6 MD5 | raw file
  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. Cookie::create($name, null, 0, null, null, null, false, true);
  43. }
  44. /**
  45. * @dataProvider namesWithSpecialCharacters
  46. */
  47. public function testWithRawThrowsExceptionIfCookieNameContainsSpecialCharacters($name)
  48. {
  49. $this->expectException('InvalidArgumentException');
  50. Cookie::create($name)->withRaw();
  51. }
  52. /**
  53. * @dataProvider namesWithSpecialCharacters
  54. */
  55. public function testInstantiationSucceedNonRawCookieNameContainsSpecialCharacters($name)
  56. {
  57. $this->assertInstanceOf(Cookie::class, Cookie::create($name));
  58. }
  59. public function testInstantiationThrowsExceptionIfCookieNameIsEmpty()
  60. {
  61. $this->expectException('InvalidArgumentException');
  62. Cookie::create('');
  63. }
  64. public function testInvalidExpiration()
  65. {
  66. $this->expectException('InvalidArgumentException');
  67. Cookie::create('MyCookie', 'foo', 'bar');
  68. }
  69. public function testNegativeExpirationIsNotPossible()
  70. {
  71. $cookie = Cookie::create('foo', 'bar', -100);
  72. $this->assertSame(0, $cookie->getExpiresTime());
  73. $cookie = Cookie::create('foo', 'bar')->withExpires(-100);
  74. $this->assertSame(0, $cookie->getExpiresTime());
  75. }
  76. public function testGetValue()
  77. {
  78. $value = 'MyValue';
  79. $cookie = Cookie::create('MyCookie', $value);
  80. $this->assertSame($value, $cookie->getValue(), '->getValue() returns the proper value');
  81. }
  82. public function testGetPath()
  83. {
  84. $cookie = Cookie::create('foo', 'bar');
  85. $this->assertSame('/', $cookie->getPath(), '->getPath() returns / as the default path');
  86. }
  87. public function testGetExpiresTime()
  88. {
  89. $cookie = Cookie::create('foo', 'bar');
  90. $this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
  91. $cookie = Cookie::create('foo', 'bar', $expire = time() + 3600);
  92. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  93. $cookie = Cookie::create('foo')->withExpires($expire = time() + 3600);
  94. $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  95. }
  96. public function testGetExpiresTimeIsCastToInt()
  97. {
  98. $cookie = Cookie::create('foo', 'bar', 3600.9);
  99. $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
  100. $cookie = Cookie::create('foo')->withExpires(3600.6);
  101. $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
  102. }
  103. public function testConstructorWithDateTime()
  104. {
  105. $expire = new \DateTime();
  106. $cookie = Cookie::create('foo', 'bar', $expire);
  107. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  108. $cookie = Cookie::create('foo')->withExpires($expire);
  109. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  110. }
  111. public function testConstructorWithDateTimeImmutable()
  112. {
  113. $expire = new \DateTimeImmutable();
  114. $cookie = Cookie::create('foo', 'bar', $expire);
  115. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  116. $cookie = Cookie::create('foo')->withValue('bar')->withExpires($expire);
  117. $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
  118. }
  119. public function testGetExpiresTimeWithStringValue()
  120. {
  121. $value = '+1 day';
  122. $cookie = Cookie::create('foo', 'bar', $value);
  123. $expire = strtotime($value);
  124. $this->assertEqualsWithDelta($expire, $cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');
  125. $cookie = Cookie::create('foo')->withValue('bar')->withExpires($value);
  126. $this->assertEqualsWithDelta($expire, $cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');
  127. }
  128. public function testGetDomain()
  129. {
  130. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com');
  131. $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  132. $cookie = Cookie::create('foo')->withDomain('.mybardomain.com');
  133. $this->assertEquals('.mybardomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
  134. }
  135. public function testIsSecure()
  136. {
  137. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com', true);
  138. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  139. $cookie = Cookie::create('foo')->withSecure(true);
  140. $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
  141. }
  142. public function testIsHttpOnly()
  143. {
  144. $cookie = Cookie::create('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
  145. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  146. $cookie = Cookie::create('foo')->withHttpOnly(true);
  147. $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
  148. }
  149. public function testCookieIsNotCleared()
  150. {
  151. $cookie = Cookie::create('foo', 'bar', time() + 3600 * 24);
  152. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  153. $cookie = Cookie::create('foo')->withExpires(time() + 3600 * 24);
  154. $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
  155. }
  156. public function testCookieIsCleared()
  157. {
  158. $cookie = Cookie::create('foo', 'bar', time() - 20);
  159. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  160. $cookie = Cookie::create('foo')->withExpires(time() - 20);
  161. $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
  162. $cookie = Cookie::create('foo', 'bar');
  163. $this->assertFalse($cookie->isCleared());
  164. $cookie = Cookie::create('foo', 'bar');
  165. $this->assertFalse($cookie->isCleared());
  166. $cookie = Cookie::create('foo', 'bar', -1);
  167. $this->assertFalse($cookie->isCleared());
  168. $cookie = Cookie::create('foo')->withExpires(-1);
  169. $this->assertFalse($cookie->isCleared());
  170. }
  171. public function testToString()
  172. {
  173. $expected = 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly';
  174. $cookie = Cookie::create('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, false, null);
  175. $this->assertEquals($expected, (string) $cookie, '->__toString() returns string representation of the cookie');
  176. $cookie = Cookie::create('foo')
  177. ->withValue('bar')
  178. ->withExpires(strtotime('Fri, 20-May-2011 15:25:52 GMT'))
  179. ->withDomain('.myfoodomain.com')
  180. ->withSecure(true)
  181. ->withSameSite(null);
  182. $this->assertEquals($expected, (string) $cookie, '->__toString() returns string representation of the cookie');
  183. $expected = 'foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly';
  184. $cookie = Cookie::create('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, false, null);
  185. $this->assertEquals($expected, (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
  186. $cookie = Cookie::create('foo')
  187. ->withValue('bar with white spaces')
  188. ->withExpires(strtotime('Fri, 20-May-2011 15:25:52 GMT'))
  189. ->withDomain('.myfoodomain.com')
  190. ->withSecure(true)
  191. ->withSameSite(null);
  192. $this->assertEquals($expected, (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
  193. $expected = 'foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly';
  194. $cookie = Cookie::create('foo', null, 1, '/admin/', '.myfoodomain.com', false, true, false, null);
  195. $this->assertEquals($expected, (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
  196. $cookie = Cookie::create('foo')
  197. ->withExpires(1)
  198. ->withPath('/admin/')
  199. ->withDomain('.myfoodomain.com')
  200. ->withSameSite(null);
  201. $this->assertEquals($expected, (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
  202. $expected = 'foo=bar; path=/; httponly; samesite=lax';
  203. $cookie = Cookie::create('foo', 'bar');
  204. $this->assertEquals($expected, (string) $cookie);
  205. $cookie = Cookie::create('foo')->withValue('bar');
  206. $this->assertEquals($expected, (string) $cookie);
  207. }
  208. public function testRawCookie()
  209. {
  210. $cookie = Cookie::create('foo', 'b a r', 0, '/', null, false, false, false, null);
  211. $this->assertFalse($cookie->isRaw());
  212. $this->assertEquals('foo=b%20a%20r; path=/', (string) $cookie);
  213. $cookie = Cookie::create('test')->withValue('t e s t')->withHttpOnly(false)->withSameSite(null);
  214. $this->assertFalse($cookie->isRaw());
  215. $this->assertEquals('test=t%20e%20s%20t; path=/', (string) $cookie);
  216. $cookie = Cookie::create('foo', 'b+a+r', 0, '/', null, false, false, true, null);
  217. $this->assertTrue($cookie->isRaw());
  218. $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
  219. $cookie = Cookie::create('foo')
  220. ->withValue('t+e+s+t')
  221. ->withHttpOnly(false)
  222. ->withRaw(true)
  223. ->withSameSite(null);
  224. $this->assertTrue($cookie->isRaw());
  225. $this->assertEquals('foo=t+e+s+t; path=/', (string) $cookie);
  226. }
  227. public function testGetMaxAge()
  228. {
  229. $cookie = Cookie::create('foo', 'bar');
  230. $this->assertEquals(0, $cookie->getMaxAge());
  231. $cookie = Cookie::create('foo', 'bar', $expire = time() + 100);
  232. $this->assertEquals($expire - time(), $cookie->getMaxAge());
  233. $cookie = Cookie::create('foo', 'bar', $expire = time() - 100);
  234. $this->assertEquals(0, $cookie->getMaxAge());
  235. }
  236. public function testFromString()
  237. {
  238. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  239. $this->assertEquals(Cookie::create('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, true, null), $cookie);
  240. $cookie = Cookie::fromString('foo=bar', true);
  241. $this->assertEquals(Cookie::create('foo', 'bar', 0, '/', null, false, false, false, null), $cookie);
  242. $cookie = Cookie::fromString('foo', true);
  243. $this->assertEquals(Cookie::create('foo', null, 0, '/', null, false, false, false, null), $cookie);
  244. }
  245. public function testFromStringWithHttpOnly()
  246. {
  247. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
  248. $this->assertTrue($cookie->isHttpOnly());
  249. $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
  250. $this->assertFalse($cookie->isHttpOnly());
  251. }
  252. public function testSameSiteAttribute()
  253. {
  254. $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, 'Lax');
  255. $this->assertEquals('lax', $cookie->getSameSite());
  256. $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, '');
  257. $this->assertNull($cookie->getSameSite());
  258. $cookie = Cookie::create('foo')->withSameSite('Lax');
  259. $this->assertEquals('lax', $cookie->getSameSite());
  260. }
  261. public function testSetSecureDefault()
  262. {
  263. $cookie = Cookie::create('foo', 'bar');
  264. $this->assertFalse($cookie->isSecure());
  265. $cookie->setSecureDefault(true);
  266. $this->assertTrue($cookie->isSecure());
  267. $cookie->setSecureDefault(false);
  268. $this->assertFalse($cookie->isSecure());
  269. }
  270. }