You are here

public function RedirectResponseSubscriberTest::testDestinationRedirect in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/EventSubscriber/RedirectResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\RedirectResponseSubscriberTest::testDestinationRedirect()
  2. 9 core/tests/Drupal/Tests/Core/EventSubscriber/RedirectResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\RedirectResponseSubscriberTest::testDestinationRedirect()

Tests destination detection and redirection.

@covers ::checkRedirectUrl @dataProvider providerTestDestinationRedirect

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object with destination query set.

string|bool $expected: The expected target URL or FALSE.

File

core/tests/Drupal/Tests/Core/EventSubscriber/RedirectResponseSubscriberTest.php, line 77

Class

RedirectResponseSubscriberTest
@coversDefaultClass \Drupal\Core\EventSubscriber\RedirectResponseSubscriber @group EventSubscriber

Namespace

Drupal\Tests\Core\EventSubscriber

Code

public function testDestinationRedirect(Request $request, $expected) {
  $dispatcher = new EventDispatcher(\Drupal::getContainer());
  $kernel = $this
    ->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
  $response = new RedirectResponse('http://example.com/drupal');
  $request->headers
    ->set('HOST', 'example.com');
  $listener = new RedirectResponseSubscriber($this->urlAssembler, $this->requestContext);
  $dispatcher
    ->addListener(KernelEvents::RESPONSE, [
    $listener,
    'checkRedirectUrl',
  ]);
  $event = new ResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST, $response);
  $dispatcher
    ->dispatch($event, KernelEvents::RESPONSE);
  $target_url = $event
    ->getResponse()
    ->getTargetUrl();
  if ($expected) {
    $this
      ->assertEquals($expected, $target_url);
  }
  else {
    $this
      ->assertEquals('http://example.com/drupal', $target_url);
  }
}