public function testOnKernelResponseWithoutSession() { $this->securityContext->setToken(new UsernamePasswordToken('test1', 'pass1', 'phpunit')); $request = new Request(); $event = new FilterResponseEvent($this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST, new Response()); $listener = new ContextListener($this->securityContext, array(), 'session'); $listener->onKernelResponse($event); $this->assertFalse($request->hasSession()); }
/** * Initiate session if not available then reads the SecurityContext from it. * * @param GetResponseEvent $event A GetResponseEvent instance */ public function handle(GetResponseEvent $event) { $request = $event->getRequest(); $application = $event->getKernel()->getApplication(); if (null !== $application && false === $request->hasSession()) { $container = $application->getContainer(); $event->getRequest()->setSession($container->get('bb_session')); } parent::handle($event); }
/** * Initiate session if not available then reads the SecurityContext from it. * * @param GetResponseEvent $event A GetResponseEvent instance */ public function handle(GetResponseEvent $event) { $request = $event->getRequest(); $application = $event->getKernel()->getApplication(); if (null !== $application && false === $request->hasSession()) { // Don't need to check if container has service with id `bb_session` cause we declared it as synthetic if (null === $application->getContainer()->get('bb_session')) { $application->getContainer()->set('bb_session', $application->getSession()); } $application->getContainer()->get('bb_session')->start(); $application->info("Session started"); $event->getRequest()->setSession($application->getContainer()->get('bb_session')); } parent::handle($event); }
protected function runSessionOnKernelResponse($newToken, $original = null) { $session = new Session(new MockArraySessionStorage()); if ($original !== null) { $session->set('_security_session', $original); } $tokenStorage = new TokenStorage(); $tokenStorage->setToken($newToken); $request = new Request(); $request->setSession($session); $request->cookies->set('MOCKSESSID', true); $event = new FilterResponseEvent($this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST, new Response()); $listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); return $session; }
/** * @dataProvider provideInvalidToken */ public function testInvalidTokenInSession($token) { $context = $this->getMock('Symfony\\Component\\Security\\Core\\SecurityContextInterface'); $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock(); $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request'); $session = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Session\\Session')->disableOriginalConstructor()->getMock(); $event->expects($this->any())->method('getRequest')->will($this->returnValue($request)); $request->expects($this->any())->method('hasPreviousSession')->will($this->returnValue(true)); $request->expects($this->any())->method('getSession')->will($this->returnValue($session)); $session->expects($this->any())->method('get')->with('_security_key123')->will($this->returnValue(serialize($token))); $context->expects($this->once())->method('setToken')->with(null); $listener = new ContextListener($context, array(), 'key123'); $listener->handle($event); }