onLogout example

use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Component\Security\Http\EventListener\CsrfTokenClearingLogoutListener;

class CsrfTokenClearingLogoutListenerTest extends TestCase
{
    public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
    {
        try {
            (new CsrfTokenClearingLogoutListener(
                new SessionTokenStorage(new RequestStack())
            ))->onLogout(new LogoutEvent(new Request(), null));
        } catch (SessionNotFoundException) {
            $this->fail('clear() must not be called if the request is not associated with a session instance');
        }

        $this->addToAssertionCount(1);
    }
}

    public function testLogout(array $clearSiteDataConfig, string $expectedHeader)
    {
        $response = new Response();
        $event = new LogoutEvent(new Request(), null);
        $event->setResponse($response);

        $listener = new ClearSiteDataLogoutListener($clearSiteDataConfig);

        $headerCountBefore = $response->headers->count();

        $listener->onLogout($event);

        $this->assertEquals(++$headerCountBefore$response->headers->count());

        $this->assertNotNull($response->headers->get('Clear-Site-Data'));
        $this->assertEquals($expectedHeader$response->headers->get('Clear-Site-Data'));
    }

    public static function provideClearSiteDataConfig(): iterable
    {
        yield [['*'], '"*"'];
        yield [['cache', 'cookies', 'storage', 'executionContexts'], '"cache", "cookies", "storage", "executionContexts"'];
    }
public function testLogout()
    {
        $response = new Response();
        $event = new LogoutEvent(new Request(), null);
        $event->setResponse($response);

        $listener = new CookieClearingLogoutListener(['foo' => ['path' => '/foo', 'domain' => 'foo.foo', 'secure' => true, 'samesite' => Cookie::SAMESITE_STRICT], 'foo2' => ['path' => null, 'domain' => null]]);

        $cookies = $response->headers->getCookies();
        $this->assertCount(0, $cookies);

        $listener->onLogout($event);

        $cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
        $this->assertCount(2, $cookies);

        $cookie = $cookies['foo.foo']['/foo']['foo'];
        $this->assertEquals('foo', $cookie->getName());
        $this->assertEquals('/foo', $cookie->getPath());
        $this->assertEquals('foo.foo', $cookie->getDomain());
        $this->assertEquals(Cookie::SAMESITE_STRICT, $cookie->getSameSite());
        $this->assertTrue($cookie->isSecure());
        $this->assertTrue($cookie->isCleared());

        
use Symfony\Component\Security\Http\EventListener\SessionLogoutListener;

class SessionLogoutListenerTest extends TestCase
{
    public function testOnLogoutIfHasNoSession()
    {
        $request = $this->createMock(Request::class);
        $request->method('hasSession')->willReturn(false);
        $request->expects($this->never())->method('getSession');

        $sessionLogoutListener = new SessionLogoutListener();
        $sessionLogoutListener->onLogout(new LogoutEvent($request, null));
    }

    public function testOnLogoutIfHasSession()
    {
        $session = $this->createMock(Session::class);
        $session->expects($this->once())->method('invalidate');

        $request = $this->createMock(Request::class);
        $request->method('getSession')->willReturn($session);
        $request->method('hasSession')->willReturn(true);

        
Home | Imprint | This part of the site doesn't use cookies.