function UrlGeneratorTest::testAliasGenerationWithParameters

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php \Drupal\Tests\Core\Routing\UrlGeneratorTest::testAliasGenerationWithParameters()
  2. 8.9.x core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php \Drupal\Tests\Core\Routing\UrlGeneratorTest::testAliasGenerationWithParameters()
  3. 10 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 309

Class

UrlGeneratorTest
Confirm that the UrlGenerator is functioning properly.

Namespace

Drupal\Tests\Core\Routing

Code

public function testAliasGenerationWithParameters() : void {
    $url = $this->generator
        ->generate('test_2', [
        'Lassie' => '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', [
        'Lassie' => 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', [
        'Lassie' => 7,
    ], $options, '/test/two/7?page=1#bottom', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT));
    $path = $this->generator
        ->getPathFromRoute('test_2', [
        'Lassie' => '5',
    ]);
    $this->assertEquals('test/two/5', $path);
    // Specify a query parameter with NULL.
    $options = [
        'query' => [
            'page' => NULL,
        ],
        'fragment' => 'bottom',
    ];
    $this->assertGenerateFromRoute('test_2', [
        'Lassie' => 5,
    ], $options, '/goodbye/cruel/world?page#bottom', (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.