8000 bug #27659 [HttpKernel] Make AbstractTestSessionListener compatible w… · symfony/symfony@52b91bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 52b91bb

Browse files
bug #27659 [HttpKernel] Make AbstractTestSessionListener compatible with CookieClearingLogoutHandler (thewilkybarkid)
This PR was squashed before being merged into the 3.4 branch (closes #27659). Discussion ---------- [HttpKernel] Make AbstractTestSessionListener compatible with CookieClearingLogoutHandler | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | #26157 started to send a new cookie in `AbstractTestSessionListener`, but is incompatible with `CookieClearingLogoutHandler` as it overrides its `Set-Cookie` by setting a new cookie (breaking my test that checked to see that the cookie was removed after a log out). Commits ------- f54d969 [HttpKernel] Make AbstractTestSessionListener compatible with CookieClearingLogoutHandler
2 parents 0da18e0 + f54d969 commit 52b91bb

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function onKernelResponse(FilterResponseEvent $event)
7171

7272
if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
7373
$params = session_get_cookie_params();
74+
75+
foreach ($event->getResponse()->headers->getCookies() as $cookie) {
76+
if ($session->getName() === $cookie->getName() && $params['path'] === $cookie->getPath() && $params['domain'] == $cookie->getDomain()) {
77+
return;
78+
}
79+
}
80+
7481
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
7582
$this->sessionId = $session->getId();
7683
}

src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ public function testEmptySessionWithNewSessionIdDoesSendCookie()
106106
$this->assertNotEmpty($response->headers->getCookies());
107107
}
108108

109+
/**
110+
* @dataProvider anotherCookieProvider
111+
*/
112+
public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie($existing, array $expected)
113+
{
114+
$this->sessionHasBeenStarted();
115+
$this->sessionIsEmpty();
116+
$this->fixSessionId('456');
117+
118+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
119+
$request = Request::create('/', 'GET', array(), array('MOCKSESSID' => '123'));
120+
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
121+
$this->listener->onKernelRequest($event);
122+
123+
$response = new Response('', 200, array('Set-Cookie' => $existing));
124+
125+
$response = $this->filterResponse(new Request(), HttpKernelInterface::MASTER_REQUEST, $response);
126+
127+
$this->assertSame($expected, $response->headers->get('Set-Cookie', null, false));
128+
}
129+
130+
public function anotherCookieProvider()
131+
{
132+
return array(
133+
'same' => array('MOCKSESSID=789; path=/', array('MOCKSESSID=789; path=/')),
134+
'different domain' => array('MOCKSESSID=789; path=/; domain=example.com', array('MOCKSESSID=789; path=/; domain=example.com', 'MOCKSESSID=456; path=/')),
135+
'different path' => array('MOCKSESSID=789; path=/foo', array('MOCKSESSID=789; path=/foo', 'MOCKSESSID=456; path=/')),
136+
);
137+
}
138+
109139
public function testUnstartedSessionIsNotSave()
110140
{
111141
$this->sessionHasNotBeenStarted();
@@ -123,10 +153,10 @@ public function testDoesNotImplementServiceSubscriberInterface()
123153
$this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'));
124154
}
125155

126-
private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
156+
private function filterResponse(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, Response $response = null)
127157
{
128158
$request->setSession($this->session);
129-
$response = new Response();
159+
$response = $response ?: new Response();
130160
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
131161
$event = new FilterResponseEvent($kernel, $request, $type, $response);
132162

0 commit comments

Comments
 (0)
0