123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- namespace Drupal\Tests\Core\Path;
- use Drupal\Core\ParamConverter\ParamNotConvertedException;
- use Drupal\Core\Path\PathValidator;
- use Drupal\Tests\UnitTestCase;
- use Symfony\Cmf\Component\Routing\RouteObjectInterface;
- use Symfony\Component\HttpFoundation\ParameterBag;
- use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
- use Symfony\Component\Routing\Exception\MethodNotAllowedException;
- use Symfony\Component\Routing\Exception\ResourceNotFoundException;
- /**
- * @coversDefaultClass \Drupal\Core\Path\PathValidator
- * @group Routing
- */
- 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() {
- 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() {
- $this->accessAwareRouter->expects($this->never())
- ->method('match');
- $this->assertTrue($this->pathValidator->isValid('<front>'));
- }
- /**
- * Tests the isValid() method for <none> (used for jumplinks).
- *
- * @covers ::isValid
- */
- public function testIsValidWithNone() {
- $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() {
- $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() {
- $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() {
- $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 ParameterBag(['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() {
- $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 ParameterBag(['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() {
- $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 ParameterBag(['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() {
- $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() {
- $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() {
- $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() {
- $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() {
- $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 not existing path.
- *
- * @covers ::isValid
- */
- public function testIsValidWithNotExistingPath() {
- $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() {
- $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 ParameterBag(['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() {
- $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 ParameterBag()]);
- $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() {
- $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() {
- $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() {
- $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 ParameterBag(['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());
- }
- }
|