diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index 32cf409..b4a1cd1 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -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 diff --git a/tests/Integration/Browser/CookieDeletionBoundaryTest.php b/tests/Integration/Browser/CookieDeletionBoundaryTest.php new file mode 100644 index 0000000..d5de05a --- /dev/null +++ b/tests/Integration/Browser/CookieDeletionBoundaryTest.php @@ -0,0 +1,51 @@ +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' => '
private-content
']); + }); + } + + 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')); + } +} diff --git a/tests/Unit/Browser/BrowserContextTest.php b/tests/Unit/Browser/BrowserContextTest.php index ade5d00..eb7d9cb 100644 --- a/tests/Unit/Browser/BrowserContextTest.php +++ b/tests/Unit/Browser/BrowserContextTest.php @@ -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