3117e5ce47326e7664f5d6d5c047d398127b8ffc
[yaffs-website] / vendor / symfony / browser-kit / Tests / CookieJarTest.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 use Symfony\Component\BrowserKit\CookieJar;
17 use Symfony\Component\BrowserKit\Response;
18
19 class CookieJarTest extends TestCase
20 {
21     public function testSetGet()
22     {
23         $cookieJar = new CookieJar();
24         $cookieJar->set($cookie = new Cookie('foo', 'bar'));
25
26         $this->assertEquals($cookie, $cookieJar->get('foo'), '->set() sets a cookie');
27
28         $this->assertNull($cookieJar->get('foobar'), '->get() returns null if the cookie does not exist');
29
30         $cookieJar->set($cookie = new Cookie('foo', 'bar', time() - 86400));
31         $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
32     }
33
34     public function testExpire()
35     {
36         $cookieJar = new CookieJar();
37         $cookieJar->set($cookie = new Cookie('foo', 'bar'));
38         $cookieJar->expire('foo');
39         $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
40     }
41
42     public function testAll()
43     {
44         $cookieJar = new CookieJar();
45         $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
46         $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
47
48         $this->assertEquals(array($cookie1, $cookie2), $cookieJar->all(), '->all() returns all cookies in the jar');
49     }
50
51     public function testClear()
52     {
53         $cookieJar = new CookieJar();
54         $cookieJar->set($cookie1 = new Cookie('foo', 'bar'));
55         $cookieJar->set($cookie2 = new Cookie('bar', 'foo'));
56
57         $cookieJar->clear();
58
59         $this->assertEquals(array(), $cookieJar->all(), '->clear() expires all cookies');
60     }
61
62     public function testUpdateFromResponse()
63     {
64         $response = new Response('', 200, array('Set-Cookie' => 'foo=foo'));
65
66         $cookieJar = new CookieJar();
67         $cookieJar->updateFromResponse($response);
68
69         $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromResponse() updates cookies from a Response objects');
70     }
71
72     public function testUpdateFromSetCookie()
73     {
74         $setCookies = array('foo=foo');
75
76         $cookieJar = new CookieJar();
77         $cookieJar->set(new Cookie('bar', 'bar'));
78         $cookieJar->updateFromSetCookie($setCookies);
79
80         $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('foo'));
81         $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $cookieJar->get('bar'));
82         $this->assertEquals('foo', $cookieJar->get('foo')->getValue(), '->updateFromSetCookie() updates cookies from a Set-Cookie header');
83         $this->assertEquals('bar', $cookieJar->get('bar')->getValue(), '->updateFromSetCookie() keeps existing cookies');
84     }
85
86     public function testUpdateFromEmptySetCookie()
87     {
88         $cookieJar = new CookieJar();
89         $cookieJar->updateFromSetCookie(array(''));
90         $this->assertEquals(array(), $cookieJar->all());
91     }
92
93     public function testUpdateFromSetCookieWithMultipleCookies()
94     {
95         $timestamp = time() + 3600;
96         $date = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
97         $setCookies = array(sprintf('foo=foo; expires=%s; domain=.symfony.com; path=/, bar=bar; domain=.blog.symfony.com, PHPSESSID=id; expires=%1$s', $date));
98
99         $cookieJar = new CookieJar();
100         $cookieJar->updateFromSetCookie($setCookies);
101
102         $fooCookie = $cookieJar->get('foo', '/', '.symfony.com');
103         $barCookie = $cookieJar->get('bar', '/', '.blog.symfony.com');
104         $phpCookie = $cookieJar->get('PHPSESSID');
105
106         $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $fooCookie);
107         $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $barCookie);
108         $this->assertInstanceOf('Symfony\Component\BrowserKit\Cookie', $phpCookie);
109         $this->assertEquals('foo', $fooCookie->getValue());
110         $this->assertEquals('bar', $barCookie->getValue());
111         $this->assertEquals('id', $phpCookie->getValue());
112         $this->assertEquals($timestamp, $fooCookie->getExpiresTime());
113         $this->assertNull($barCookie->getExpiresTime());
114         $this->assertEquals($timestamp, $phpCookie->getExpiresTime());
115     }
116
117     /**
118      * @dataProvider provideAllValuesValues
119      */
120     public function testAllValues($uri, $values)
121     {
122         $cookieJar = new CookieJar();
123         $cookieJar->set($cookie1 = new Cookie('foo_nothing', 'foo'));
124         $cookieJar->set($cookie2 = new Cookie('foo_expired', 'foo', time() - 86400));
125         $cookieJar->set($cookie3 = new Cookie('foo_path', 'foo', null, '/foo'));
126         $cookieJar->set($cookie4 = new Cookie('foo_domain', 'foo', null, '/', '.example.com'));
127         $cookieJar->set($cookie4 = new Cookie('foo_strict_domain', 'foo', null, '/', '.www4.example.com'));
128         $cookieJar->set($cookie5 = new Cookie('foo_secure', 'foo', null, '/', '', true));
129
130         $this->assertEquals($values, array_keys($cookieJar->allValues($uri)), '->allValues() returns the cookie for a given URI');
131     }
132
133     public function provideAllValuesValues()
134     {
135         return array(
136             array('http://www.example.com', array('foo_nothing', 'foo_domain')),
137             array('http://www.example.com/', array('foo_nothing', 'foo_domain')),
138             array('http://foo.example.com/', array('foo_nothing', 'foo_domain')),
139             array('http://foo.example1.com/', array('foo_nothing')),
140             array('https://foo.example.com/', array('foo_nothing', 'foo_secure', 'foo_domain')),
141             array('http://www.example.com/foo/bar', array('foo_nothing', 'foo_path', 'foo_domain')),
142             array('http://www4.example.com/', array('foo_nothing', 'foo_domain', 'foo_strict_domain')),
143         );
144     }
145
146     public function testEncodedValues()
147     {
148         $cookieJar = new CookieJar();
149         $cookieJar->set($cookie = new Cookie('foo', 'bar%3Dbaz', null, '/', '', false, true, true));
150
151         $this->assertEquals(array('foo' => 'bar=baz'), $cookieJar->allValues('/'));
152         $this->assertEquals(array('foo' => 'bar%3Dbaz'), $cookieJar->allRawValues('/'));
153     }
154
155     public function testCookieExpireWithSameNameButDifferentPaths()
156     {
157         $cookieJar = new CookieJar();
158         $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
159         $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
160         $cookieJar->expire('foo', '/foo');
161
162         $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
163         $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
164         $this->assertEquals(array(), $cookieJar->allValues('http://example.com/foo'));
165         $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
166     }
167
168     public function testCookieExpireWithNullPaths()
169     {
170         $cookieJar = new CookieJar();
171         $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/'));
172         $cookieJar->expire('foo', null);
173
174         $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
175         $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
176     }
177
178     public function testCookieExpireWithDomain()
179     {
180         $cookieJar = new CookieJar();
181         $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo', 'http://example2.com/'));
182         $cookieJar->expire('foo', '/foo', 'http://example2.com/');
183
184         $this->assertNull($cookieJar->get('foo'), '->get() returns null if the cookie is expired');
185         $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example2.com/')));
186     }
187
188     public function testCookieWithSameNameButDifferentPaths()
189     {
190         $cookieJar = new CookieJar();
191         $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/foo'));
192         $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/bar'));
193
194         $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
195         $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://example.com/foo'));
196         $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://example.com/bar'));
197     }
198
199     public function testCookieWithSameNameButDifferentDomains()
200     {
201         $cookieJar = new CookieJar();
202         $cookieJar->set($cookie1 = new Cookie('foo', 'bar1', null, '/', 'foo.example.com'));
203         $cookieJar->set($cookie2 = new Cookie('foo', 'bar2', null, '/', 'bar.example.com'));
204
205         $this->assertEquals(array(), array_keys($cookieJar->allValues('http://example.com/')));
206         $this->assertEquals(array('foo' => 'bar1'), $cookieJar->allValues('http://foo.example.com/'));
207         $this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://bar.example.com/'));
208     }
209
210     public function testCookieGetWithSubdomain()
211     {
212         $cookieJar = new CookieJar();
213         $cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/', '.example.com'));
214         $cookieJar->set($cookie2 = new Cookie('foo1', 'bar', null, '/', 'test.example.com'));
215
216         $this->assertEquals($cookie1, $cookieJar->get('foo', '/', 'foo.example.com'));
217         $this->assertEquals($cookie1, $cookieJar->get('foo', '/', 'example.com'));
218         $this->assertEquals($cookie2, $cookieJar->get('foo1', '/', 'test.example.com'));
219     }
220
221     public function testCookieGetWithWrongSubdomain()
222     {
223         $cookieJar = new CookieJar();
224         $cookieJar->set($cookie1 = new Cookie('foo1', 'bar', null, '/', 'test.example.com'));
225
226         $this->assertNull($cookieJar->get('foo1', '/', 'foo.example.com'));
227     }
228
229     public function testCookieGetWithSubdirectory()
230     {
231         $cookieJar = new CookieJar();
232         $cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/test', '.example.com'));
233         $cookieJar->set($cookie2 = new Cookie('foo1', 'bar1', null, '/', '.example.com'));
234
235         $this->assertNull($cookieJar->get('foo', '/', '.example.com'));
236         $this->assertNull($cookieJar->get('foo', '/bar', '.example.com'));
237         $this->assertEquals($cookie1, $cookieJar->get('foo', '/test', 'example.com'));
238         $this->assertEquals($cookie2, $cookieJar->get('foo1', '/', 'example.com'));
239         $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar', 'example.com'));
240
241         $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar'));
242     }
243
244     public function testCookieWithWildcardDomain()
245     {
246         $cookieJar = new CookieJar();
247         $cookieJar->set(new Cookie('foo', 'bar', null, '/', '.example.com'));
248
249         $this->assertEquals(array('foo' => 'bar'), $cookieJar->allValues('http://www.example.com'));
250         $this->assertEmpty($cookieJar->allValues('http://wwwexample.com'));
251     }
252 }