View source
<?php
namespace Drupal\Tests\Core\Path;
use Drupal\Core\ParamConverter\ParamNotConvertedException;
use Drupal\Core\Path\PathValidator;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\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;
class PathValidatorTest extends UnitTestCase {
protected $accessAwareRouter;
protected $accessUnawareRouter;
protected $account;
protected $pathProcessor;
protected $pathValidator;
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);
}
public function testIsValidWithFrontpage() {
$this->accessAwareRouter
->expects($this
->never())
->method('match');
$this
->assertTrue($this->pathValidator
->isValid('<front>'));
}
public function testIsValidWithNone() {
$this->accessAwareRouter
->expects($this
->never())
->method('match');
$this
->assertTrue($this->pathValidator
->isValid('<none>'));
}
public function testIsValidWithExternalUrl() {
$this->accessAwareRouter
->expects($this
->never())
->method('match');
$this
->assertTrue($this->pathValidator
->isValid('https://www.drupal.org'));
}
public function testIsValidWithInvalidExternalUrl() {
$this->accessAwareRouter
->expects($this
->never())
->method('match');
$this
->assertFalse($this->pathValidator
->isValid('http://'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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'));
}
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());
$url = $this->pathValidator
->getUrlIfValid('/test-path');
$this
->assertInstanceOf('Drupal\\Core\\Url', $url);
$this
->assertEquals('test_route', $url
->getRouteName());
$this
->assertEquals([
'key' => 'value',
], $url
->getRouteParameters());
}
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']);
}
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);
}
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']);
}
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());
}
}