/** * Test the forcedLogin redirect. * * @covers ::forceLogin * @covers ::__construct */ public function testForceLogin() { $request = $this->getMock('\\Symfony\\Component\\HttpFoundation\\Request'); $query = $this->getMock('\\Symfony\\Component\\HttpFoundation\\ParameterBag'); $request->query = $query; $this->requestStack->expects($this->once())->method('getCurrentRequest')->will($this->returnValue($request)); $parameters = array('returnto' => 'node/1', 'foo' => 'bar'); $query->expects($this->once())->method('all')->will($this->returnValue($parameters)); $this->casHelper->expects($this->once())->method('getServerLoginUrl')->with($this->equalTo($parameters))->will($this->returnValue('https://example.com')); $cacheableMetadata = new CacheableMetadata(); $cacheableMetadata->addCacheTags(array('config:cas.settings')); $expected_response = new TrustedRedirectResponse('https://example.com', 302); $expected_response->addCacheableDependency($cacheableMetadata); $force_login_controller = new ForceLoginController($this->casHelper, $this->requestStack); $response = $force_login_controller->forceLogin(); $this->assertEquals($expected_response, $response); }
/** * Prior to set the response it check if we can redirect. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The event object. * @param \Drupal\Core\Url $url * The Url where we want to redirect. */ protected function setResponse(GetResponseEvent $event, Url $url) { $request = $event->getRequest(); $this->context->fromRequest($request); parse_str($request->getQueryString(), $query); $url->setOption('query', $query); $url->setAbsolute(TRUE); // We can only check access for routed URLs. if (!$url->isRouted() || $this->checker->canRedirect($request, $url->getRouteName())) { // Add the 'rendered' cache tag, so that we can invalidate all responses // when settings are changed. $response = new TrustedRedirectResponse($url->toString(), 301); $response->addCacheableDependency(CacheableMetadata::createFromRenderArray([])->addCacheTags(['rendered'])); $event->setResponse($response); } }