You are here

public function RouteNoneTest::testProcessOutbound in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php \Drupal\KernelTests\Core\RouteProcessor\RouteNoneTest::testProcessOutbound()

Tests the output process.

File

core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php, line 46

Class

RouteNoneTest
Tests the <none> route processor.

Namespace

Drupal\KernelTests\Core\RouteProcessor

Code

public function testProcessOutbound() {
  $expected_cacheability = (new BubbleableMetadata())
    ->setCacheMaxAge(Cache::PERMANENT);
  $request_stack = \Drupal::requestStack();

  /** @var \Symfony\Component\Routing\RequestContext $request_context */
  $request_context = \Drupal::service('router.request_context');

  // Test request with subdir on homepage.
  $server = [
    'SCRIPT_NAME' => '/subdir/index.php',
    'SCRIPT_FILENAME' => $this->root . '/index.php',
    'SERVER_NAME' => 'http://www.example.com',
  ];
  $request = Request::create('/subdir', 'GET', [], [], [], $server);
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, '<front>');
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
  $request_stack
    ->push($request);
  $request_context
    ->fromRequest($request);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [], TRUE, TRUE), $url);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('#test-fragment');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [
    'fragment' => 'test-fragment',
  ], TRUE), $url);

  // Test request with subdir on other page.
  $server = [
    'SCRIPT_NAME' => '/subdir/index.php',
    'SCRIPT_FILENAME' => $this->root . '/index.php',
    'SERVER_NAME' => 'http://www.example.com',
  ];
  $request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, 'node.add');
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add'));
  $request_stack
    ->push($request);
  $request_context
    ->fromRequest($request);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [], TRUE, TRUE), $url);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('#test-fragment');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [
    'fragment' => 'test-fragment',
  ], TRUE), $url);

  // Test request without subdir on the homepage.
  $server = [
    'SCRIPT_NAME' => '/index.php',
    'SCRIPT_FILENAME' => $this->root . '/index.php',
    'SERVER_NAME' => 'http://www.example.com',
  ];
  $request = Request::create('/', 'GET', [], [], [], $server);
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, '<front>');
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
  $request_stack
    ->push($request);
  $request_context
    ->fromRequest($request);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [], TRUE, TRUE), $url);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('#test-fragment');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [
    'fragment' => 'test-fragment',
  ], TRUE), $url);

  // Test request without subdir on other page.
  $server = [
    'SCRIPT_NAME' => '/index.php',
    'SCRIPT_FILENAME' => $this->root . '/index.php',
    'SERVER_NAME' => 'http://www.example.com',
  ];
  $request = Request::create('/node/add', 'GET', [], [], [], $server);
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, 'node.add');
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add'));
  $request_stack
    ->push($request);
  $request_context
    ->fromRequest($request);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [], TRUE, TRUE), $url);
  $url = GeneratedUrl::createFromObject($expected_cacheability)
    ->setGeneratedUrl('#test-fragment');
  $this
    ->assertEquals($this->urlGenerator
    ->generateFromRoute('<none>', [], [
    'fragment' => 'test-fragment',
  ], TRUE), $url);
}