Skip to content

Commit 7d6a390

Browse files
authored
Add cookie deletion (#19)
1 parent 059ab14 commit 7d6a390

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/Browser/BrowserContext.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ public function clearCookies(): void
195195
]);
196196
}
197197

198+
public function deleteCookie(string $name): void
199+
{
200+
$cookies = $this->cookies();
201+
if (empty($cookies)) {
202+
return;
203+
}
204+
205+
$toDelete = [];
206+
foreach ($cookies as $cookie) {
207+
if (!isset($cookie['name']) || !is_string($cookie['name'])) {
208+
continue;
209+
}
210+
if ($cookie['name'] !== $name) {
211+
continue;
212+
}
213+
214+
$domain = $cookie['domain'] ?? null;
215+
$path = $cookie['path'] ?? null;
216+
if (!is_string($domain) || !is_string($path)) {
217+
continue;
218+
}
219+
220+
$toDelete[] = [
221+
'name' => $name,
222+
'value' => '',
223+
'domain' => $domain,
224+
'path' => $path,
225+
'expires' => 0,
226+
];
227+
}
228+
229+
if (empty($toDelete)) {
230+
return;
231+
}
232+
233+
$this->transport->send([
234+
'action' => 'context.addCookies',
235+
'contextId' => $this->contextId,
236+
'cookies' => $toDelete,
237+
]);
238+
}
239+
198240
public function clearPermissions(): void
199241
{
200242
$this->transport->send([

src/Browser/BrowserContextInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function addInitScript(string $script): void;
2727

2828
public function clearCookies(): void;
2929

30+
/**
31+
* Delete all cookies with the given name across domain and path variants.
32+
*/
33+
public function deleteCookie(string $name): void;
34+
3035
public function clearPermissions(): void;
3136

3237
public function close(): void;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the playwright-php/playwright package.
7+
* For the full copyright and license information, please view
8+
* the LICENSE file that was distributed with this source code.
9+
*/
10+
11+
namespace PlaywrightPHP\Tests\Unit\Browser;
12+
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\TestCase;
15+
use PlaywrightPHP\Browser\BrowserContext;
16+
use PlaywrightPHP\Transport\TransportInterface;
17+
18+
#[CoversClass(BrowserContext::class)]
19+
final class BrowserContextDeleteCookieTest extends TestCase
20+
{
21+
public function testDeleteCookieSendsAddCookiesWithExpiry(): void
22+
{
23+
$transport = $this->createMock(TransportInterface::class);
24+
25+
$calledAdd = false;
26+
$that = $this;
27+
$transport->method('send')->willReturnCallback(function (array $payload) use (&$calledAdd, $that) {
28+
if (($payload['action'] ?? null) === 'context.cookies') {
29+
return [
30+
'cookies' => [
31+
[
32+
'name' => 'foo',
33+
'value' => '123',
34+
'domain' => 'example.com',
35+
'path' => '/',
36+
'expires' => time() + 3600,
37+
'httpOnly' => false,
38+
'secure' => false,
39+
'sameSite' => 'Lax',
40+
],
41+
[
42+
'name' => 'bar',
43+
'value' => 'x',
44+
'domain' => 'example.com',
45+
'path' => '/',
46+
'expires' => time() + 3600,
47+
'httpOnly' => false,
48+
'secure' => false,
49+
'sameSite' => 'Lax',
50+
],
51+
],
52+
];
53+
}
54+
55+
if (($payload['action'] ?? null) === 'context.addCookies') {
56+
$that->assertArrayHasKey('cookies', $payload);
57+
$that->assertIsArray($payload['cookies']);
58+
$that->assertSame('foo', $payload['cookies'][0]['name']);
59+
$that->assertSame('example.com', $payload['cookies'][0]['domain']);
60+
$that->assertSame('/', $payload['cookies'][0]['path']);
61+
$that->assertSame(0, $payload['cookies'][0]['expires']);
62+
$calledAdd = true;
63+
64+
return [];
65+
}
66+
67+
return [];
68+
});
69+
70+
$context = new BrowserContext($transport, 'ctx');
71+
$context->deleteCookie('foo');
72+
73+
$this->assertTrue($calledAdd, 'context.addCookies should be called to expire matching cookies');
74+
}
75+
}

0 commit comments

Comments
 (0)