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