route = new Route('test'); $this->accessManager = $this->getMock('Drupal\Core\Access\AccessManagerInterface'); $this->currentUser = $this->getMock('Drupal\Core\Session\AccountInterface'); } /** * Sets up a chain router with matchRequest. */ 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); } /** * Tests the matchRequest() function for access allowed. */ 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); } /** * Tests the matchRequest() function for access denied. */ 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->setExpectedException(AccessDeniedHttpException::class); $this->router->matchRequest($request); } /** * Tests the matchRequest() function for access denied with reason message. */ 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->setExpectedException(AccessDeniedHttpException::class, $reason); $this->router->matchRequest($request); } /** * Ensure that methods are passed to the wrapped router. * * @covers ::__call */ public function testCall() { $mock_router = $this->getMock('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); } }