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