class PathValidatorTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest
  2. 9 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest
  5. 10 core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest
  6. 10 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest

@coversDefaultClass \Drupal\Core\Path\PathValidator @group Routing

Hierarchy

Expanded class hierarchy of PathValidatorTest

File

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

Namespace

Drupal\Tests\Core\Path
View source
class PathValidatorTest extends UnitTestCase {
    
    /**
     * The mocked access aware router.
     *
     * @var \Drupal\Core\Routing\AccessAwareRouterInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $accessAwareRouter;
    
    /**
     * The mocked access unaware router.
     * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $accessUnawareRouter;
    
    /**
     * The mocked account.
     *
     * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $account;
    
    /**
     * The path processor.
     *
     * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $pathProcessor;
    
    /**
     * The tested path validator.
     *
     * @var \Drupal\Core\Path\PathValidator
     */
    protected $pathValidator;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->accessAwareRouter = $this->createMock('Drupal\\Core\\Routing\\AccessAwareRouterInterface');
        $this->accessUnawareRouter = $this->createMock('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface');
        $this->account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
        $this->pathProcessor = $this->createMock('Drupal\\Core\\PathProcessor\\InboundPathProcessorInterface');
        $this->pathValidator = new PathValidator($this->accessAwareRouter, $this->accessUnawareRouter, $this->account, $this->pathProcessor);
    }
    
    /**
     * Tests the isValid() method for the frontpage.
     *
     * @covers ::isValid
     */
    public function testIsValidWithFrontpage() : void {
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->assertTrue($this->pathValidator
            ->isValid('<front>'));
    }
    
    /**
     * Tests the isValid() method for <none> (used for jump links).
     *
     * @covers ::isValid
     */
    public function testIsValidWithNone() : void {
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->assertTrue($this->pathValidator
            ->isValid('<none>'));
    }
    
    /**
     * Tests the isValid() method for an external URL.
     *
     * @covers ::isValid
     */
    public function testIsValidWithExternalUrl() : void {
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->assertTrue($this->pathValidator
            ->isValid('https://www.drupal.org'));
    }
    
    /**
     * Tests the isValid() method with an invalid external URL.
     *
     * @covers ::isValid
     */
    public function testIsValidWithInvalidExternalUrl() : void {
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->assertFalse($this->pathValidator
            ->isValid('http://'));
    }
    
    /**
     * Tests the isValid() method with a 'link to any page' permission.
     *
     * @covers ::isValid
     * @covers ::getPathAttributes
     */
    public function testIsValidWithLinkToAnyPageAccount() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(TRUE);
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessUnawareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willReturn([
            RouteObjectInterface::ROUTE_NAME => 'test_route',
            '_raw_variables' => new InputBag([
                'key' => 'value',
            ]),
        ]);
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertTrue($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * Tests the isValid() method without the 'link to any page' permission.
     *
     * @covers ::isValid
     */
    public function testIsValidWithoutLinkToAnyPageAccount() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willReturn([
            RouteObjectInterface::ROUTE_NAME => 'test_route',
            '_raw_variables' => new InputBag([
                'key' => 'value',
            ]),
        ]);
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertTrue($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * Tests the isValid() method with a path alias.
     *
     * @covers ::isValid
     */
    public function testIsValidWithPathAlias() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willReturn([
            RouteObjectInterface::ROUTE_NAME => 'test_route',
            '_raw_variables' => new InputBag([
                'key' => 'value',
            ]),
        ]);
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->with('/path-alias', $this->anything())
            ->willReturn('/test-path');
        $this->assertTrue($this->pathValidator
            ->isValid('path-alias'));
    }
    
    /**
     * Tests the isValid() method with a user without access to the path.
     *
     * @covers ::isValid
     * @covers ::getPathAttributes
     */
    public function testIsValidWithAccessDenied() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willThrowException(new AccessDeniedHttpException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * @covers ::isValid
     * @covers ::getPathAttributes
     */
    public function testIsValidWithResourceNotFound() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willThrowException(new ResourceNotFoundException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * @covers ::isValid
     * @covers ::getPathAttributes
     */
    public function testIsValidWithParamNotConverted() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willThrowException(new ParamNotConvertedException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * @covers ::isValid
     * @covers ::getPathAttributes
     */
    public function testIsValidWithMethodNotAllowed() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willThrowException(new MethodNotAllowedException([]));
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('test-path'));
    }
    
    /**
     * Tests the isValid() method with a not working param converting.
     *
     * @covers ::isValid
     */
    public function testIsValidWithFailingParameterConverting() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/entity-test/1')
            ->willThrowException(new ParamNotConvertedException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('entity-test/1'));
    }
    
    /**
     * Tests the isValid() method with a non-existent path.
     *
     * @covers ::isValid
     */
    public function testIsValidWithNotExistingPath() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessUnawareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/not-existing-path')
            ->willThrowException(new ResourceNotFoundException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $this->assertFalse($this->pathValidator
            ->isValid('not-existing-path'));
    }
    
    /**
     * Tests the getUrlIfValid() method when there is access.
     *
     * @covers ::getUrlIfValid
     * @covers ::getPathAttributes
     */
    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());
    }
    
    /**
     * Tests the getUrlIfValid() method with a query in the path.
     *
     * @covers ::getUrlIfValid
     */
    public function testGetUrlIfValidWithQuery() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path?k=bar')
            ->willReturn([
            RouteObjectInterface::ROUTE_NAME => 'test_route',
            '_raw_variables' => new InputBag(),
        ]);
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $url = $this->pathValidator
            ->getUrlIfValid('test-path?k=bar');
        $this->assertInstanceOf('Drupal\\Core\\Url', $url);
        $this->assertEquals('test_route', $url->getRouteName());
        $this->assertEquals([
            'k' => 'bar',
        ], $url->getOptions()['query']);
    }
    
    /**
     * Tests the getUrlIfValid() method where there is no access.
     *
     * @covers ::getUrlIfValid
     */
    public function testGetUrlIfValidWithoutAccess() : void {
        $this->account
            ->expects($this->once())
            ->method('hasPermission')
            ->with('link to any page')
            ->willReturn(FALSE);
        $this->accessAwareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willThrowException(new AccessDeniedHttpException());
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $url = $this->pathValidator
            ->getUrlIfValid('test-path');
        $this->assertFalse($url);
    }
    
    /**
     * Tests the getUrlIfValid() method with a front page + query + fragments.
     *
     * @covers ::getUrlIfValid
     */
    public function testGetUrlIfValidWithFrontPageAndQueryAndFragments() : void {
        $url = $this->pathValidator
            ->getUrlIfValid('<front>?hei=sen#berg');
        $this->assertEquals('<front>', $url->getRouteName());
        $this->assertEquals([
            'hei' => 'sen',
        ], $url->getOptions()['query']);
        $this->assertEquals('berg', $url->getOptions()['fragment']);
    }
    
    /**
     * Tests the getUrlIfValidWithoutAccessCheck() method.
     *
     * @covers ::getUrlIfValidWithoutAccessCheck
     * @covers ::getPathAttributes
     */
    public function testGetUrlIfValidWithoutAccessCheck() : void {
        $this->account
            ->expects($this->never())
            ->method('hasPermission')
            ->with('link to any page');
        $this->accessAwareRouter
            ->expects($this->never())
            ->method('match');
        $this->accessUnawareRouter
            ->expects($this->once())
            ->method('match')
            ->with('/test-path')
            ->willReturn([
            RouteObjectInterface::ROUTE_NAME => 'test_route',
            '_raw_variables' => new InputBag([
                'key' => 'value',
            ]),
        ]);
        $this->pathProcessor
            ->expects($this->once())
            ->method('processInbound')
            ->willReturnArgument(0);
        $url = $this->pathValidator
            ->getUrlIfValidWithoutAccessCheck('test-path');
        $this->assertInstanceOf('Drupal\\Core\\Url', $url);
        $this->assertEquals('test_route', $url->getRouteName());
        $this->assertEquals([
            'key' => 'value',
        ], $url->getRouteParameters());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PathValidatorTest::$accessAwareRouter protected property The mocked access aware router.
PathValidatorTest::$accessUnawareRouter protected property The mocked access unaware router.
PathValidatorTest::$account protected property The mocked account.
PathValidatorTest::$pathProcessor protected property The path processor.
PathValidatorTest::$pathValidator protected property The tested path validator.
PathValidatorTest::setUp protected function Overrides UnitTestCase::setUp
PathValidatorTest::testGetUrlIfValidWithAccess public function Tests the getUrlIfValid() method when there is access.
PathValidatorTest::testGetUrlIfValidWithFrontPageAndQueryAndFragments public function Tests the getUrlIfValid() method with a front page + query + fragments.
PathValidatorTest::testGetUrlIfValidWithoutAccess public function Tests the getUrlIfValid() method where there is no access.
PathValidatorTest::testGetUrlIfValidWithoutAccessCheck public function Tests the getUrlIfValidWithoutAccessCheck() method.
PathValidatorTest::testGetUrlIfValidWithQuery public function Tests the getUrlIfValid() method with a query in the path.
PathValidatorTest::testIsValidWithAccessDenied public function Tests the isValid() method with a user without access to the path.
PathValidatorTest::testIsValidWithExternalUrl public function Tests the isValid() method for an external URL.
PathValidatorTest::testIsValidWithFailingParameterConverting public function Tests the isValid() method with a not working param converting.
PathValidatorTest::testIsValidWithFrontpage public function Tests the isValid() method for the frontpage.
PathValidatorTest::testIsValidWithInvalidExternalUrl public function Tests the isValid() method with an invalid external URL.
PathValidatorTest::testIsValidWithLinkToAnyPageAccount public function Tests the isValid() method with a &#039;link to any page&#039; permission.
PathValidatorTest::testIsValidWithMethodNotAllowed public function @covers ::isValid
@covers ::getPathAttributes
PathValidatorTest::testIsValidWithNone public function Tests the isValid() method for &lt;none&gt; (used for jump links).
PathValidatorTest::testIsValidWithNotExistingPath public function Tests the isValid() method with a non-existent path.
PathValidatorTest::testIsValidWithoutLinkToAnyPageAccount public function Tests the isValid() method without the &#039;link to any page&#039; permission.
PathValidatorTest::testIsValidWithParamNotConverted public function @covers ::isValid
@covers ::getPathAttributes
PathValidatorTest::testIsValidWithPathAlias public function Tests the isValid() method with a path alias.
PathValidatorTest::testIsValidWithResourceNotFound public function @covers ::isValid
@covers ::getPathAttributes
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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