You are here

public function UrlGeneratorTest::testAliasGenerationWithParameters in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php \Drupal\Tests\Core\Routing\UrlGeneratorTest::testAliasGenerationWithParameters()

Confirms that generated routes will have aliased paths.

File

core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php, line 306

Class

UrlGeneratorTest
Confirm that the UrlGenerator is functioning properly.

Namespace

Drupal\Tests\Core\Routing

Code

public function testAliasGenerationWithParameters() {
  $url = $this->generator
    ->generate('test_2', [
    'narf' => '5',
  ]);
  $this
    ->assertEquals('/goodbye/cruel/world', $url);

  // No cacheability to test; UrlGenerator::generate() doesn't support
  // collecting cacheability metadata.
  $this->routeProcessorManager
    ->expects($this
    ->any())
    ->method('processOutbound')
    ->with($this
    ->anything());
  $options = [
    'fragment' => 'top',
  ];

  // Extra parameters should appear in the query string.
  $this
    ->assertGenerateFromRoute('test_1', [
    'zoo' => 5,
  ], $options, '/hello/world?zoo=5#top', (new BubbleableMetadata())
    ->setCacheMaxAge(Cache::PERMANENT));
  $options = [
    'query' => [
      'page' => '1',
    ],
    'fragment' => 'bottom',
  ];
  $this
    ->assertGenerateFromRoute('test_2', [
    'narf' => 5,
  ], $options, '/goodbye/cruel/world?page=1#bottom', (new BubbleableMetadata())
    ->setCacheMaxAge(Cache::PERMANENT));

  // Changing the parameters, the route still matches but there is no alias.
  $this
    ->assertGenerateFromRoute('test_2', [
    'narf' => 7,
  ], $options, '/test/two/7?page=1#bottom', (new BubbleableMetadata())
    ->setCacheMaxAge(Cache::PERMANENT));
  $path = $this->generator
    ->getPathFromRoute('test_2', [
    'narf' => '5',
  ]);
  $this
    ->assertEquals('test/two/5', $path);

  // Specify a query parameter with NULL.
  $options = [
    'query' => [
      'page' => NULL,
    ],
    'fragment' => 'bottom',
  ];
  $this
    ->assertGenerateFromRoute('test_2', [
    'narf' => 5,
  ], $options, '/goodbye/cruel/world?page#bottom', (new BubbleableMetadata())
    ->setCacheMaxAge(Cache::PERMANENT));
}