/**
  * Tests the can redirect check.
  */
 public function testCanRedirect()
 {
     $config = array('redirect.settings' => array('ignore_admin_path' => FALSE, 'access_check' => TRUE));
     $state = $this->getMockBuilder('Drupal\\Core\\State\\StateInterface')->getMock();
     $state->expects($this->any())->method('get')->with('system.maintenance_mode')->will($this->returnValue(FALSE));
     $access = $this->getMockBuilder('Drupal\\Core\\Access\\AccessManager')->disableOriginalConstructor()->getMock();
     $account = $this->getMockBuilder('Drupal\\Core\\Session\\AccountInterface')->getMock();
     $route_provider = $this->getMockBuilder('Drupal\\Core\\Routing\\RouteProviderInterface')->getMock();
     $route = new Route('/example');
     $route_provider->expects($this->any())->method('getRouteByName')->willReturn($route);
     $access->expects($this->any())->method('checkNamedRoute')->willReturnMap([['denied_route', [], $account, FALSE, FALSE], ['allowed_route', [], $account, FALSE, TRUE]]);
     $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
     // All fine - we can redirect.
     $request = $this->getRequestStub('index.php', 'GET');
     $this->assertTrue($checker->canRedirect($request), 'Can redirect');
     // The script name is not index.php.
     $request = $this->getRequestStub('statistics.php', 'GET');
     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect script name not index.php');
     // The request method is not GET.
     $request = $this->getRequestStub('index.php', 'POST');
     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect other than GET method');
     // Maintenance mode is on.
     $state = $this->getMockBuilder('Drupal\\Core\\State\\StateInterface')->getMock();
     $state->expects($this->any())->method('get')->with('system.maintenance_mode')->will($this->returnValue(TRUE));
     // Route access check, deny access.
     $request = $this->getRequestStub('index.php', 'GET');
     $this->assertFalse($checker->canRedirect($request, 'denied_route'), 'Can not redirect');
     // Route access check, deny access.
     $request = $this->getRequestStub('index.php', 'GET');
     $this->assertTrue($checker->canRedirect($request, 'allowed_route'), 'Can redirect');
     $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
     $request = $this->getRequestStub('index.php', 'GET');
     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect if maintenance mode is on');
     // We are at a admin path.
     $state = $this->getMockBuilder('Drupal\\Core\\State\\StateInterface')->getMock();
     $state->expects($this->any())->method('get')->with('system.maintenance_mode')->will($this->returnValue(FALSE));
     //    $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state);
     //
     //    $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
     //      ->disableOriginalConstructor()
     //      ->getMock();
     //    $route->expects($this->any())
     //      ->method('getOption')
     //      ->with('_admin_route')
     //      ->will($this->returnValue('system.admin_config_search'));
     //
     //    $request = $this->getRequestStub('index.php', 'GET',
     //      array(RouteObjectInterface::ROUTE_OBJECT => $route));
     //    $this->assertFalse($checker->canRedirect($request), 'Cannot redirect if we are requesting a admin path');
     //
     //    // We are at admin path with ignore_admin_path set to TRUE.
     //    $config['redirect.settings']['ignore_admin_path'] = TRUE;
     //    $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state);
     //
     //    $request = $this->getRequestStub('index.php', 'GET',
     //      array(RouteObjectInterface::ROUTE_OBJECT => $route));
     //    $this->assertTrue($checker->canRedirect($request), 'Can redirect a admin with ignore_admin_path set to TRUE');
 }
 /**
  * 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.
         $headers = ['X-Drupal-Cache-Tags' => 'rendered'];
         $event->setResponse(new RedirectResponse($url->toString(), 301, $headers));
     }
 }
 /**
  * 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);
     }
 }