2f5a08d104143cf3007ef33f4a8d94ed145610b4
[yaffs-website] / vendor / symfony / browser-kit / 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\BrowserKit\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\BrowserKit\Cookie;
16
17 class CookieTest extends TestCase
18 {
19     public function testToString()
20     {
21         $cookie = new Cookie('foo', 'bar', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
22         $this->assertEquals('foo=bar; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie');
23
24         $cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true);
25         $this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20 May 2011 15:25:52 GMT; domain=.myfoodomain.com; path=/; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)');
26
27         $cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com');
28         $this->assertEquals('foo=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=.myfoodomain.com; path=/admin/; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL');
29
30         $cookie = new Cookie('foo', 'bar', 0, '/', '');
31         $this->assertEquals('foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; httponly', (string) $cookie);
32     }
33
34     /**
35      * @dataProvider getTestsForToFromString
36      */
37     public function testToFromString($cookie, $url = null)
38     {
39         $this->assertEquals($cookie, (string) Cookie::fromString($cookie, $url));
40     }
41
42     public function getTestsForToFromString()
43     {
44         return array(
45             array('foo=bar; path=/'),
46             array('foo=bar; path=/foo'),
47             array('foo=bar; domain=google.com; path=/'),
48             array('foo=bar; domain=example.com; path=/; secure', 'https://example.com/'),
49             array('foo=bar; path=/; httponly'),
50             array('foo=bar; domain=google.com; path=/foo; secure; httponly', 'https://google.com/'),
51             array('foo=bar=baz; path=/'),
52             array('foo=bar%3Dbaz; path=/'),
53         );
54     }
55
56     public function testFromStringIgnoreSecureFlag()
57     {
58         $this->assertFalse(Cookie::fromString('foo=bar; secure')->isSecure());
59         $this->assertFalse(Cookie::fromString('foo=bar; secure', 'http://example.com/')->isSecure());
60     }
61
62     /**
63      * @dataProvider getExpireCookieStrings
64      */
65     public function testFromStringAcceptsSeveralExpiresDateFormats($cookie)
66     {
67         $this->assertEquals(1596185377, Cookie::fromString($cookie)->getExpiresTime());
68     }
69
70     public function getExpireCookieStrings()
71     {
72         return array(
73             array('foo=bar; expires=Fri, 31-Jul-2020 08:49:37 GMT'),
74             array('foo=bar; expires=Fri, 31 Jul 2020 08:49:37 GMT'),
75             array('foo=bar; expires=Fri, 31-07-2020 08:49:37 GMT'),
76             array('foo=bar; expires=Fri, 31-07-20 08:49:37 GMT'),
77             array('foo=bar; expires=Friday, 31-Jul-20 08:49:37 GMT'),
78             array('foo=bar; expires=Fri Jul 31 08:49:37 2020'),
79             array('foo=bar; expires=\'Fri Jul 31 08:49:37 2020\''),
80             array('foo=bar; expires=Friday July 31st 2020, 08:49:37 GMT'),
81         );
82     }
83
84     public function testFromStringWithCapitalization()
85     {
86         $this->assertEquals('Foo=Bar; path=/', (string) Cookie::fromString('Foo=Bar'));
87         $this->assertEquals('foo=bar; expires=Fri, 31 Dec 2010 23:59:59 GMT; path=/', (string) Cookie::fromString('foo=bar; Expires=Fri, 31 Dec 2010 23:59:59 GMT'));
88         $this->assertEquals('foo=bar; domain=www.example.org; path=/; httponly', (string) Cookie::fromString('foo=bar; DOMAIN=www.example.org; HttpOnly'));
89     }
90
91     public function testFromStringWithUrl()
92     {
93         $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com/'));
94         $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com'));
95         $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar', 'http://www.example.com?foo'));
96         $this->assertEquals('foo=bar; domain=www.example.com; path=/foo', (string) Cookie::fromString('foo=bar', 'http://www.example.com/foo/bar'));
97         $this->assertEquals('foo=bar; domain=www.example.com; path=/', (string) Cookie::fromString('foo=bar; path=/', 'http://www.example.com/foo/bar'));
98         $this->assertEquals('foo=bar; domain=www.myotherexample.com; path=/', (string) Cookie::fromString('foo=bar; domain=www.myotherexample.com', 'http://www.example.com/'));
99     }
100
101     public function testFromStringThrowsAnExceptionIfCookieIsNotValid()
102     {
103         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
104         Cookie::fromString('foo');
105     }
106
107     public function testFromStringIgnoresInvalidExpiresDate()
108     {
109         $cookie = Cookie::fromString('foo=bar; expires=Flursday July 31st 2020, 08:49:37 GMT');
110
111         $this->assertFalse($cookie->isExpired());
112     }
113
114     public function testFromStringThrowsAnExceptionIfUrlIsNotValid()
115     {
116         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
117         Cookie::fromString('foo=bar', 'foobar');
118     }
119
120     public function testGetName()
121     {
122         $cookie = new Cookie('foo', 'bar');
123         $this->assertEquals('foo', $cookie->getName(), '->getName() returns the cookie name');
124     }
125
126     public function testGetValue()
127     {
128         $cookie = new Cookie('foo', 'bar');
129         $this->assertEquals('bar', $cookie->getValue(), '->getValue() returns the cookie value');
130
131         $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
132         $this->assertEquals('bar=baz', $cookie->getValue(), '->getValue() returns the urldecoded cookie value');
133     }
134
135     public function testGetRawValue()
136     {
137         $cookie = new Cookie('foo', 'bar=baz'); // decoded value
138         $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the urlencoded cookie value');
139         $cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true); // raw value
140         $this->assertEquals('bar%3Dbaz', $cookie->getRawValue(), '->getRawValue() returns the non-urldecoded cookie value');
141     }
142
143     public function testGetPath()
144     {
145         $cookie = new Cookie('foo', 'bar', 0);
146         $this->assertEquals('/', $cookie->getPath(), '->getPath() returns / is no path is defined');
147
148         $cookie = new Cookie('foo', 'bar', 0, '/foo');
149         $this->assertEquals('/foo', $cookie->getPath(), '->getPath() returns the cookie path');
150     }
151
152     public function testGetDomain()
153     {
154         $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com');
155         $this->assertEquals('foo.com', $cookie->getDomain(), '->getDomain() returns the cookie domain');
156     }
157
158     public function testIsSecure()
159     {
160         $cookie = new Cookie('foo', 'bar');
161         $this->assertFalse($cookie->isSecure(), '->isSecure() returns false if not defined');
162
163         $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', true);
164         $this->assertTrue($cookie->isSecure(), '->isSecure() returns the cookie secure flag');
165     }
166
167     public function testIsHttponly()
168     {
169         $cookie = new Cookie('foo', 'bar');
170         $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns false if not defined');
171
172         $cookie = new Cookie('foo', 'bar', 0, '/', 'foo.com', false, true);
173         $this->assertTrue($cookie->isHttpOnly(), '->isHttpOnly() returns the cookie httponly flag');
174     }
175
176     public function testGetExpiresTime()
177     {
178         $cookie = new Cookie('foo', 'bar');
179         $this->assertNull($cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
180
181         $cookie = new Cookie('foo', 'bar', $time = time() - 86400);
182         $this->assertEquals($time, $cookie->getExpiresTime(), '->getExpiresTime() returns the expires time');
183     }
184
185     public function testIsExpired()
186     {
187         $cookie = new Cookie('foo', 'bar');
188         $this->assertFalse($cookie->isExpired(), '->isExpired() returns false when the cookie never expires (null as expires time)');
189
190         $cookie = new Cookie('foo', 'bar', time() - 86400);
191         $this->assertTrue($cookie->isExpired(), '->isExpired() returns true when the cookie is expired');
192
193         $cookie = new Cookie('foo', 'bar', 0);
194         $this->assertFalse($cookie->isExpired());
195     }
196
197     /**
198      * @expectedException        \UnexpectedValueException
199      * @expectedExceptionMessage The cookie expiration time "string" is not valid.
200      */
201     public function testConstructException()
202     {
203         $cookie = new Cookie('foo', 'bar', 'string');
204     }
205 }