Open Journal Systems  3.3.0
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 
13 
14 use PHPUnit\Framework\TestCase;
16 
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 
47  {
48  new Cookie($name);
49  }
50 
54  public function testInvalidExpiration()
55  {
56  new Cookie('MyCookie', 'foo', 'bar');
57  }
58 
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');
84 
85  $this->assertEquals(0, $cookie->getExpiresTime(), '->getExpiresTime() returns the default expire date');
86 
87  $cookie = new Cookie('foo', 'bar', $expire = time() + 3600);
88 
89  $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
90  }
91 
93  {
94  $cookie = new Cookie('foo', 'bar', 3600.9);
95 
96  $this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
97  }
98 
99  public function testConstructorWithDateTime()
100  {
101  $expire = new \DateTime();
102  $cookie = new Cookie('foo', 'bar', $expire);
103 
104  $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
105  }
106 
111  {
112  $expire = new \DateTimeImmutable();
113  $cookie = new Cookie('foo', 'bar', $expire);
114 
115  $this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
116  }
117 
119  {
120  $value = '+1 day';
121  $cookie = new Cookie('foo', 'bar', $value);
122  $expire = strtotime($value);
123 
124  $this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date', 1);
125  }
126 
127  public function testGetDomain()
128  {
129  $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com');
130 
131  $this->assertEquals('.myfoodomain.com', $cookie->getDomain(), '->getDomain() returns the domain name on which the cookie is valid');
132  }
133 
134  public function testIsSecure()
135  {
136  $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', true);
137 
138  $this->assertTrue($cookie->isSecure(), '->isSecure() returns whether the cookie is transmitted over HTTPS');
139  }
140 
141  public function testIsHttpOnly()
142  {
143  $cookie = new Cookie('foo', 'bar', 0, '/', '.myfoodomain.com', false, true);
144 
145  $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP');
146  }
147 
148  public function testCookieIsNotCleared()
149  {
150  $cookie = new Cookie('foo', 'bar', time() + 3600 * 24);
151 
152  $this->assertFalse($cookie->isCleared(), '->isCleared() returns false if the cookie did not expire yet');
153  }
154 
155  public function testCookieIsCleared()
156  {
157  $cookie = new Cookie('foo', 'bar', time() - 20);
158 
159  $this->assertTrue($cookie->isCleared(), '->isCleared() returns true if the cookie has expired');
160  }
161 
162  public function testToString()
163  {
164  $cookie = new Cookie('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
165  $this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
166 
167  $cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
168  $this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
169 
170  $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
171  $this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; max-age='.($expire - time()).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
172 
173  $cookie = new Cookie('foo', 'bar', 0, '/', '');
174  $this->assertEquals('foo=bar; path=/; httponly', (string) $cookie);
175  }
176 
177  public function testRawCookie()
178  {
179  $cookie = new Cookie('foo', 'b a r', 0, '/', null, false, false);
180  $this->assertFalse($cookie->isRaw());
181  $this->assertEquals('foo=b%20a%20r; path=/', (string) $cookie);
182 
183  $cookie = new Cookie('foo', 'b+a+r', 0, '/', null, false, false, true);
184  $this->assertTrue($cookie->isRaw());
185  $this->assertEquals('foo=b+a+r; path=/', (string) $cookie);
186  }
187 
188  public function testGetMaxAge()
189  {
190  $cookie = new Cookie('foo', 'bar');
191  $this->assertEquals(0, $cookie->getMaxAge());
192 
193  $cookie = new Cookie('foo', 'bar', $expire = time() + 100);
194  $this->assertEquals($expire - time(), $cookie->getMaxAge());
195 
196  $cookie = new Cookie('foo', 'bar', $expire = time() - 100);
197  $this->assertEquals($expire - time(), $cookie->getMaxAge());
198  }
199 
200  public function testFromString()
201  {
202  $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
203  $this->assertEquals(new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true, true, true), $cookie);
204 
205  $cookie = Cookie::fromString('foo=bar', true);
206  $this->assertEquals(new Cookie('foo', 'bar', 0, '/', null, false, false), $cookie);
207  }
208 
209  public function testFromStringWithHttpOnly()
210  {
211  $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly');
212  $this->assertTrue($cookie->isHttpOnly());
213 
214  $cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure');
215  $this->assertFalse($cookie->isHttpOnly());
216  }
217 
219  {
220  $cookie = new Cookie('foo', 'bar', 0, '/', null, false, true, false, 'Lax');
221  $this->assertEquals('lax', $cookie->getSameSite());
222  }
223 }
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetMaxAge
testGetMaxAge()
Definition: symfony/http-foundation/Tests/CookieTest.php:188
Symfony\Component\HttpFoundation\Tests\CookieTest\testIsHttpOnly
testIsHttpOnly()
Definition: symfony/http-foundation/Tests/CookieTest.php:141
Symfony\Component\HttpFoundation\Tests\CookieTest\testToString
testToString()
Definition: symfony/http-foundation/Tests/CookieTest.php:162
Symfony\Component\HttpFoundation\Tests\CookieTest\testCookieIsCleared
testCookieIsCleared()
Definition: symfony/http-foundation/Tests/CookieTest.php:155
Symfony\Component\HttpFoundation\Tests\CookieTest\testSameSiteAttributeIsCaseInsensitive
testSameSiteAttributeIsCaseInsensitive()
Definition: symfony/http-foundation/Tests/CookieTest.php:218
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetPath
testGetPath()
Definition: symfony/http-foundation/Tests/CookieTest.php:74
Symfony\Component\HttpFoundation\Tests\CookieTest\invalidNames
invalidNames()
Definition: symfony/http-foundation/Tests/CookieTest.php:27
Symfony\Component\HttpFoundation\Tests\CookieTest\testFromString
testFromString()
Definition: symfony/http-foundation/Tests/CookieTest.php:200
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetDomain
testGetDomain()
Definition: symfony/http-foundation/Tests/CookieTest.php:127
Symfony\Component\HttpFoundation\Tests\CookieTest\testConstructorWithDateTimeImmutable
testConstructorWithDateTimeImmutable()
Definition: symfony/http-foundation/Tests/CookieTest.php:110
Symfony\Component\HttpFoundation\Tests\CookieTest\testFromStringWithHttpOnly
testFromStringWithHttpOnly()
Definition: symfony/http-foundation/Tests/CookieTest.php:209
Symfony\Component\HttpFoundation\Tests\CookieTest\testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters
testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
Definition: symfony/http-foundation/Tests/CookieTest.php:46
Symfony\Component\HttpFoundation\Tests\CookieTest\testCookieIsNotCleared
testCookieIsNotCleared()
Definition: symfony/http-foundation/Tests/CookieTest.php:148
Symfony\Component\HttpFoundation\Tests\CookieTest\testIsSecure
testIsSecure()
Definition: symfony/http-foundation/Tests/CookieTest.php:134
Symfony\Component\HttpFoundation\Tests\CookieTest\testNegativeExpirationIsNotPossible
testNegativeExpirationIsNotPossible()
Definition: symfony/http-foundation/Tests/CookieTest.php:59
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetExpiresTimeWithStringValue
testGetExpiresTimeWithStringValue()
Definition: symfony/http-foundation/Tests/CookieTest.php:118
Symfony\Component\HttpFoundation\Cookie\fromString
static fromString($cookie, $decode=false)
Definition: lib/vendor/symfony/http-foundation/Cookie.php:42
Symfony\Component\HttpFoundation\Tests\CookieTest\testConstructorWithDateTime
testConstructorWithDateTime()
Definition: symfony/http-foundation/Tests/CookieTest.php:99
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetValue
testGetValue()
Definition: symfony/http-foundation/Tests/CookieTest.php:66
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\Tests
Definition: AcceptHeaderItemTest.php:12
Symfony\Component\HttpFoundation\Tests\CookieTest\testRawCookie
testRawCookie()
Definition: symfony/http-foundation/Tests/CookieTest.php:177
Symfony\Component\HttpFoundation\Tests\CookieTest\testInvalidExpiration
testInvalidExpiration()
Definition: symfony/http-foundation/Tests/CookieTest.php:54
Symfony\Component\HttpFoundation\Tests\CookieTest
Definition: symfony/http-foundation/Tests/CookieTest.php:25
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetExpiresTimeIsCastToInt
testGetExpiresTimeIsCastToInt()
Definition: symfony/http-foundation/Tests/CookieTest.php:92
Symfony\Component\HttpFoundation\Tests\CookieTest\testGetExpiresTime
testGetExpiresTime()
Definition: symfony/http-foundation/Tests/CookieTest.php:81