forked from minkphp/driver-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieTest.php
212 lines (176 loc) · 9.01 KB
/
CookieTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
namespace Behat\Mink\Tests\Driver\Basic;
use Behat\Mink\Tests\Driver\TestCase;
/**
* @phpstan-type TCookieRemovalMode 'session_reset'|'cookie_delete'
*/
final class CookieTest extends TestCase
{
/**
* test cookie decoding.
*
* @group issue140
*/
public function testIssue140(): void
{
$this->getSession()->visit($this->pathTo('/issue140.php'));
$this->getSession()->getPage()->fillField('cookie_value', 'some:value;');
$this->getSession()->getPage()->pressButton('Set cookie');
$this->getSession()->visit($this->pathTo('/issue140.php?show_value'));
$this->assertEquals('some:value;', $this->getSession()->getCookie('tc'));
$this->assertEquals('some:value;', $this->getSession()->getPage()->getText());
}
public function testCookie(): void
{
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $this->getSession()->getPage()->getText());
$this->assertNull($this->getSession()->getCookie('srvr_cookie'));
$this->getSession()->setCookie('srvr_cookie', 'client cookie set');
$this->getSession()->reload();
$this->assertStringContainsString('Previous cookie: client cookie set', $this->getSession()->getPage()->getText());
$this->assertEquals('client cookie set', $this->getSession()->getCookie('srvr_cookie'));
$this->getSession()->setCookie('srvr_cookie', null);
$this->getSession()->reload();
$this->assertStringContainsString('Previous cookie: NO', $this->getSession()->getPage()->getText());
$this->getSession()->visit($this->pathTo('/cookie_page1.php'));
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: srv_var_is_set', $this->getSession()->getPage()->getText());
$this->getSession()->setCookie('srvr_cookie', null);
$this->getSession()->reload();
$this->assertStringContainsString('Previous cookie: NO', $this->getSession()->getPage()->getText());
}
public function testCookieWithSemicolon(): void
{
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->getSession()->setCookie('srvr_cookie', 'foo;bar;baz');
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertEquals('foo;bar;baz', $this->getSession()->getCookie('srvr_cookie'));
$this->assertStringContainsString('Previous cookie: foo;bar;baz', $this->getSession()->getPage()->getText());
}
/**
* @dataProvider cookieWithPathsDataProvider
*/
public function testCookieWithPaths(string $cookieRemovalMode): void
{
// start clean
$session = $this->getSession();
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
// cookie from root path is accessible in sub-folder
$session->visit($this->pathTo('/cookie_page1.php'));
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: srv_var_is_set', $session->getPage()->getText());
// cookie from sub-folder overrides cookie from root path
$session->visit($this->pathTo('/sub-folder/cookie_page1.php'));
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: srv_var_is_set_sub_folder', $session->getPage()->getText());
if ($cookieRemovalMode === 'session_reset') {
$session->reset();
} elseif ($cookieRemovalMode === 'cookie_delete') {
$session->setCookie('srvr_cookie', null);
}
// cookie is removed from all paths
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
}
/**
* @phpstan-return iterable<array{TCookieRemovalMode}>
*/
public static function cookieWithPathsDataProvider(): iterable
{
return [
['session_reset'],
['cookie_delete'],
];
}
/**
* @phpstan-param TCookieRemovalMode $cookieRemovalMode
* @dataProvider cookieWithPathsDataProvider
*/
public function testCookieInSubPath(string $cookieRemovalMode): void
{
// Start clean.
// The cookie is set when viewing the page.
$session = $this->getSession();
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
$session->visit($this->pathTo('/sub-folder/cookie_page4.php'));
// On the next load, the cookie has been set.
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: srv_var_is_set', $session->getPage()->getText());
if ($cookieRemovalMode === 'session_reset') {
$session->reset();
} elseif ($cookieRemovalMode === 'cookie_delete') {
$session->setCookie('srvr_cookie', null);
}
// Cookie is removed>
$session->visit($this->pathTo('/sub-folder/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
}
public function testReset(): void
{
$this->getSession()->visit($this->pathTo('/cookie_page1.php'));
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: srv_var_is_set', $this->getSession()->getPage()->getText());
$this->getSession()->reset();
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $this->getSession()->getPage()->getText());
$this->getSession()->setCookie('srvr_cookie', 'test_cookie');
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: test_cookie', $this->getSession()->getPage()->getText());
$this->getSession()->reset();
$this->getSession()->visit($this->pathTo('/cookie_page2.php'));
$this->assertStringContainsString('Previous cookie: NO', $this->getSession()->getPage()->getText());
$this->getSession()->setCookie('client_cookie1', 'some_val');
$this->getSession()->setCookie('client_cookie2', '123');
$this->getSession()->visit($this->pathTo('/session_test.php'));
$this->getSession()->visit($this->pathTo('/cookie_page1.php'));
$this->getSession()->visit($this->pathTo('/print_cookies.php'));
$this->assertStringContainsString(
'client_cookie1 = `some_val`',
$this->getSession()->getPage()->getText()
);
$this->assertStringContainsString(
'client_cookie2 = `123`',
$this->getSession()->getPage()->getText()
);
$this->assertStringContainsString(
'_SESS = ',
$this->getSession()->getPage()->getText()
);
$this->assertStringContainsString(
' srvr_cookie = `srv_var_is_set`',
$this->getSession()->getPage()->getText()
);
$this->getSession()->reset();
$this->getSession()->visit($this->pathTo('/print_cookies.php'));
$this->assertStringContainsString('array()', $this->getSession()->getPage()->getText());
}
public function testHttpOnlyCookieIsDeleted(): void
{
$this->getSession()->restart();
$this->getSession()->visit($this->pathTo('/cookie_page3.php'));
$this->assertEquals('Has Cookie: false', $this->findById('cookie-status')->getText());
$this->getSession()->reload();
$this->assertEquals('Has Cookie: true', $this->findById('cookie-status')->getText());
$this->getSession()->restart();
$this->getSession()->visit($this->pathTo('/cookie_page3.php'));
$this->assertEquals('Has Cookie: false', $this->findById('cookie-status')->getText());
}
public function testSessionPersistsBetweenRequests(): void
{
$this->getSession()->visit($this->pathTo('/session_test.php'));
$webAssert = $this->getAssertSession();
$node = $webAssert->elementExists('css', '#session-id');
$sessionId = $node->getText();
$this->getSession()->visit($this->pathTo('/session_test.php'));
$node = $webAssert->elementExists('css', '#session-id');
$this->assertEquals($sessionId, $node->getText());
$this->getSession()->visit($this->pathTo('/session_test.php?login'));
$node = $webAssert->elementExists('css', '#session-id');
$this->assertNotEquals($sessionId, $newSessionId = $node->getText());
$this->getSession()->visit($this->pathTo('/session_test.php'));
$node = $webAssert->elementExists('css', '#session-id');
$this->assertEquals($newSessionId, $node->getText());
}
}