Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions src/Browser/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,44 +367,7 @@ public function clearCookies(array $options = []): void

public function deleteCookie(string $name): void
{
$cookies = $this->cookies();
if (empty($cookies)) {
return;
}

$toDelete = [];
foreach ($cookies as $cookie) {
if (!isset($cookie['name']) || !is_string($cookie['name'])) {
continue;
}
if ($cookie['name'] !== $name) {
continue;
}

$domain = $cookie['domain'] ?? null;
$path = $cookie['path'] ?? null;
if (!is_string($domain) || !is_string($path)) {
continue;
}

$toDelete[] = [
'name' => $name,
'value' => '',
'domain' => $domain,
'path' => $path,
'expires' => 0,
];
}

if (empty($toDelete)) {
return;
}

$this->transport->send([
'action' => 'context.addCookies',
'contextId' => $this->contextId,
'cookies' => $toDelete,
]);
$this->clearCookies(['name' => $name]);
}

public function clearPermissions(): void
Expand Down
51 changes: 51 additions & 0 deletions tests/Integration/Browser/CookieDeletionBoundaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Playwright\Tests\Integration\Browser;

use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Playwright\Network\RouteInterface;
use Playwright\Testing\PlaywrightTestCaseTrait;

#[CoversNothing]
final class CookieDeletionBoundaryTest extends TestCase
{
use PlaywrightTestCaseTrait;

protected function setUp(): void
{
$this->setUpPlaywright();
$this->page->route('https://private.example/**', static function (RouteInterface $route): void {
$route->fulfill(['contentType' => 'text/html', 'headers' => ['access-control-allow-origin' => 'https://private.example'], 'body' => '<p>private-content</p><input id="password">']);
});
}

protected function tearDown(): void
{
$this->tearDownPlaywright();
}

public function testNamedDeletionRemovesPartitionedAndOrdinaryCookies(): void
{
$this->context->addCookies([
['name' => 'session', 'value' => 'partitioned', 'domain' => 'private.example', 'path' => '/', 'secure' => true, 'sameSite' => 'None', 'partitionKey' => 'https://top.example'],
['name' => 'session', 'value' => 'ordinary', 'domain' => 'private.example', 'path' => '/', 'secure' => true],
['name' => 'keep', 'value' => 'ordinary', 'domain' => 'private.example', 'path' => '/', 'secure' => true],
]);
$this->assertCount(3, $this->context->cookies());
$this->context->deleteCookie('session');
$this->assertSame(['keep'], array_column($this->context->cookies(), 'name'));
}
}
57 changes: 6 additions & 51 deletions tests/Unit/Browser/BrowserContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,59 +902,14 @@ public function testManualStopTracingConsumesTheAutoTrace(): void
$this->assertSame('/tmp/manual-trace.zip', $sent[1]['path']);
}

public function testDeleteCookieSendsAddCookiesWithExpiry(): void
public function testDeleteCookieDelegatesAllCookieIdentitiesToNativeClear(): void
{
$transport = $this->createMock(TransportInterface::class);

$calledAdd = false;
$that = $this;
$transport->method('send')->willReturnCallback(function (array $payload) use (&$calledAdd, $that) {
if (($payload['action'] ?? null) === 'context.cookies') {
return [
'cookies' => [
[
'name' => 'foo',
'value' => '123',
'domain' => 'example.com',
'path' => '/',
'expires' => time() + 3600,
'httpOnly' => false,
'secure' => false,
'sameSite' => 'Lax',
],
[
'name' => 'bar',
'value' => 'x',
'domain' => 'example.com',
'path' => '/',
'expires' => time() + 3600,
'httpOnly' => false,
'secure' => false,
'sameSite' => 'Lax',
],
],
];
}

if (($payload['action'] ?? null) === 'context.addCookies') {
$that->assertArrayHasKey('cookies', $payload);
$that->assertIsArray($payload['cookies']);
$that->assertSame('foo', $payload['cookies'][0]['name']);
$that->assertSame('example.com', $payload['cookies'][0]['domain']);
$that->assertSame('/', $payload['cookies'][0]['path']);
$that->assertSame(0, $payload['cookies'][0]['expires']);
$calledAdd = true;

return [];
}

return [];
});

$context = new BrowserContext($transport, 'ctx', new PlaywrightConfig());
$context->deleteCookie('foo');
$this->mockTransport->expects($this->once())
->method('send')
->with(['action' => 'context.clearCookies', 'contextId' => 'context_1', 'options' => ['name' => 'foo']])
->willReturn([]);

$this->assertTrue($calledAdd, 'context.addCookies should be called to expire matching cookies');
$this->context->deleteCookie('foo');
}

public function testTracksPopupPagesViaEvents(): void
Expand Down
Loading