From 95ec4eb5e2ab7b1c9a82be7e249e3dc718c1b033 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Fri, 24 Feb 2012 05:14:10 -0800 Subject: [PATCH] [WebProfilerBundle] fixed session assumption --- .../EventListener/WebDebugToolbarListener.php | 2 +- .../WebDebugToolbarListenerTest.php | 61 +++++++++++-------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index b9418e6b2a4d..c49cab8995f1 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -72,7 +72,7 @@ public function onKernelResponse(FilterResponseEvent $event) if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) { $session = $request->getSession(); - if ($session->getFlashBag() instanceof AutoExpireFlashBag) { + if ($session && $session->getFlashBag() instanceof AutoExpireFlashBag) { // keep current flashes for one more request if using AutoExpireFlashBag $session->getFlashBag()->setAll($session->getFlashBag()->peekAll()); } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php index ce75b593dc6d..c9e4a59a8b2f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php @@ -51,19 +51,20 @@ public function getInjectToolbarTests() ); } - public function testRedirectionIsIntercepted() + /** + * @dataProvider provideRedirects + */ + public function testRedirectionIsIntercepted($statusCode, $hasSession) { - foreach (array(301, 302) as $statusCode) { - $response = new Response('Some content', $statusCode); - $response->headers->set('X-Debug-Token', 'xxxxxxxx'); - $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response); + $response = new Response('Some content', $statusCode); + $response->headers->set('X-Debug-Token', 'xxxxxxxx'); + $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response); - $listener = new WebDebugToolbarListener($this->getTemplatingMock('Redirection'), true); - $listener->onKernelResponse($event); + $listener = new WebDebugToolbarListener($this->getTemplatingMock('Redirection'), true); + $listener->onKernelResponse($event); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals('Redirection', $response->getContent()); - } + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('Redirection', $response->getContent()); } public function testToolbarIsInjected() @@ -81,19 +82,28 @@ public function testToolbarIsInjected() /** * @depends testToolbarIsInjected + * @dataProvider provideRedirects */ - public function testToolbarIsNotInjectedOnRedirection() + public function testToolbarIsNotInjectedOnRedirection($statusCode, $hasSession) { - foreach (array(301, 302) as $statusCode) { - $response = new Response('', $statusCode); - $response->headers->set('X-Debug-Token', 'xxxxxxxx'); - $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response); + $response = new Response('', $statusCode); + $response->headers->set('X-Debug-Token', 'xxxxxxxx'); + $event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response); - $listener = new WebDebugToolbarListener($this->getTemplatingMock()); - $listener->onKernelResponse($event); + $listener = new WebDebugToolbarListener($this->getTemplatingMock()); + $listener->onKernelResponse($event); - $this->assertEquals('', $response->getContent()); - } + $this->assertEquals('', $response->getContent()); + } + + public function provideRedirects() + { + return array( + array(301, true), + array(302, true), + array(301, false), + array(302, false), + ); } /** @@ -175,9 +185,8 @@ public function testToolbarIsNotInjectedOnNonHtmlRequests() $this->assertEquals('', $response->getContent()); } - protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html') + protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true) { - $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false); $request = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array('getSession', 'isXmlHttpRequest', 'getRequestFormat'), @@ -189,9 +198,13 @@ protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'h $request->expects($this->any()) ->method('getRequestFormat') ->will($this->returnValue($requestFormat)); - $request->expects($this->any()) - ->method('getSession') - ->will($this->returnValue($session)); + + if ($hasSession) { + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session', array(), array(), '', false); + $request->expects($this->any()) + ->method('getSession') + ->will($this->returnValue($session)); + } return $request; }