function PathValidatorTest::testGetUrlIfValidWithAccess

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithAccess()
  2. 8.9.x core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithAccess()
  3. 11.x core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithAccess()

Tests the getUrlIfValid() method when there is access.

@covers ::getUrlIfValid
@covers ::getPathAttributes

File

core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php, line 330

Class

PathValidatorTest
@coversDefaultClass \Drupal\Core\Path\PathValidator[[api-linebreak]] @group Routing

Namespace

Drupal\Tests\Core\Path

Code

public function testGetUrlIfValidWithAccess() : void {
  $this->account
    ->expects($this->exactly(2))
    ->method('hasPermission')
    ->with('link to any page')
    ->willReturn(FALSE);
  $this->accessAwareRouter
    ->expects($this->exactly(2))
    ->method('match')
    ->with('/test-path')
    ->willReturn([
    RouteObjectInterface::ROUTE_NAME => 'test_route',
    '_raw_variables' => new InputBag([
      'key' => 'value',
    ]),
  ]);
  $this->pathProcessor
    ->expects($this->exactly(2))
    ->method('processInbound')
    ->willReturnArgument(0);
  $url = $this->pathValidator
    ->getUrlIfValid('test-path');
  $this->assertInstanceOf('Drupal\\Core\\Url', $url);
  $this->assertEquals('test_route', $url->getRouteName());
  $this->assertEquals([
    'key' => 'value',
  ], $url->getRouteParameters());
  // Test with leading /.
  $url = $this->pathValidator
    ->getUrlIfValid('/test-path');
  $this->assertInstanceOf('Drupal\\Core\\Url', $url);
  $this->assertEquals('test_route', $url->getRouteName());
  $this->assertEquals([
    'key' => 'value',
  ], $url->getRouteParameters());
}

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