AccessAwareRouterTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Routing/AccessAwareRouterTest.php
View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\AccessAwareRouter;
use Drupal\Core\Routing\AccessAwareRouterInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Route;
class AccessAwareRouterTest extends UnitTestCase {
protected $route;
protected $chainRouter;
protected $accessManager;
protected $currentUser;
protected $router;
protected function setUp() {
parent::setUp();
$this->route = new Route('test');
$this->accessManager = $this
->createMock('Drupal\\Core\\Access\\AccessManagerInterface');
$this->currentUser = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
}
protected function setupRouter() {
$this->chainRouter = $this
->getMockBuilder('Symfony\\Cmf\\Component\\Routing\\ChainRouter')
->disableOriginalConstructor()
->getMock();
$this->chainRouter
->expects($this
->once())
->method('matchRequest')
->will($this
->returnValue([
RouteObjectInterface::ROUTE_OBJECT => $this->route,
]));
$this->router = new AccessAwareRouter($this->chainRouter, $this->accessManager, $this->currentUser);
}
public function testMatchRequestAllowed() {
$this
->setupRouter();
$request = new Request();
$access_result = AccessResult::allowed();
$this->accessManager
->expects($this
->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$parameters = $this->router
->matchRequest($request);
$expected = [
RouteObjectInterface::ROUTE_OBJECT => $this->route,
AccessAwareRouterInterface::ACCESS_RESULT => $access_result,
];
$this
->assertSame($expected, $request->attributes
->all());
$this
->assertSame($expected, $parameters);
}
public function testMatchRequestDenied() {
$this
->setupRouter();
$request = new Request();
$access_result = AccessResult::forbidden();
$this->accessManager
->expects($this
->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$this
->expectException(AccessDeniedHttpException::class);
$this->router
->matchRequest($request);
}
public function testCheckAccessResultWithReason() {
$this
->setupRouter();
$request = new Request();
$reason = $this
->getRandomGenerator()
->string();
$access_result = AccessResult::forbidden($reason);
$this->accessManager
->expects($this
->once())
->method('checkRequest')
->with($request)
->willReturn($access_result);
$this
->expectException(AccessDeniedHttpException::class);
$this
->expectExceptionMessage($reason);
$this->router
->matchRequest($request);
}
public function testCall() {
$mock_router = $this
->createMock('Symfony\\Component\\Routing\\RouterInterface');
$this->chainRouter = $this
->getMockBuilder('Symfony\\Cmf\\Component\\Routing\\ChainRouter')
->disableOriginalConstructor()
->setMethods([
'add',
])
->getMock();
$this->chainRouter
->expects($this
->once())
->method('add')
->with($mock_router)
->willReturnSelf();
$this->router = new AccessAwareRouter($this->chainRouter, $this->accessManager, $this->currentUser);
$this->router
->add($mock_router);
}
}