|
| 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\Security\Http\Tests\EventListener; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Cookie; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpFoundation\Response; |
| 18 | +use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 19 | +use Symfony\Component\Security\Http\Event\LogoutEvent; |
| 20 | +use Symfony\Component\Security\Http\EventListener\CookieClearingLogoutListener; |
| 21 | + |
| 22 | +class CookieClearingLogoutListenerTest extends TestCase |
| 23 | +{ |
| 24 | + public function testLogout() |
| 25 | + { |
| 26 | + $response = new Response(); |
| 27 | + $event = new LogoutEvent(new Request(), null); |
| 28 | + $event->setResponse($response); |
| 29 | + |
| 30 | + $listener = new CookieClearingLogoutListener(['foo' => ['path' => '/foo', 'domain' => 'foo.foo', 'secure' => true, 'samesite' => Cookie::SAMESITE_STRICT], 'foo2' => ['path' => null, 'domain' => null]]); |
| 31 | + |
| 32 | + $cookies = $response->headers->getCookies(); |
| 33 | + $this->assertCount(0, $cookies); |
| 34 | + |
| 35 | + $listener->onLogout($event); |
| 36 | + |
| 37 | + $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY); |
| 38 | + $this->assertCount(2, $cookies); |
| 39 | + |
| 40 | + $cookie = $cookies['foo.foo']['/foo']['foo']; |
| 41 | + $this->assertEquals('foo', $cookie->getName()); |
| 42 | + $this->assertEquals('/foo', $cookie->getPath()); |
| 43 | + $this->assertEquals('foo.foo', $cookie->getDomain()); |
| 44 | + $this->assertEquals(Cookie::SAMESITE_STRICT, $cookie->getSameSite()); |
| 45 | + $this->assertTrue($cookie->isSecure()); |
| 46 | + $this->assertTrue($cookie->isCleared()); |
| 47 | + |
| 48 | + $cookie = $cookies['']['/']['foo2']; |
| 49 | + $this->assertStringStartsWith('foo2', $cookie->getName()); |
| 50 | + $this->assertEquals('/', $cookie->getPath()); |
| 51 | + $this->assertNull($cookie->getDomain()); |
| 52 | + $this->assertNull($cookie->getSameSite()); |
| 53 | + $this->assertFalse($cookie->isSecure()); |
| 54 | + $this->assertTrue($cookie->isCleared()); |
| 55 | + } |
| 56 | +} |
0 commit comments