View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\Routing\Route;
use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessCheck;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\UnitTestCase;
class EntityAccessCheckTest extends UnitTestCase {
protected function setUp() : void {
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class)
->reveal();
$container = new Container();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
public function testAccess() {
$route = new Route('/foo/{var_name}', [], [
'_entity_access' => 'var_name.update',
], [
'parameters' => [
'var_name' => [
'type' => 'entity:node',
],
],
]);
$account = $this
->prophesize(AccountInterface::class)
->reveal();
$node = $this
->prophesize(NodeInterface::class);
$node
->access('update', $account, TRUE)
->willReturn(AccessResult::allowed());
$node = $node
->reveal();
$route_match = $this
->prophesize(RouteMatchInterface::class);
$route_match
->getRawParameters()
->willReturn(new ParameterBag([
'var_name' => 1,
]));
$route_match
->getParameters()
->willReturn(new ParameterBag([
'var_name' => $node,
]));
$route_match = $route_match
->reveal();
$access_check = new EntityAccessCheck();
$this
->assertEquals(AccessResult::allowed(), $access_check
->access($route, $route_match, $account));
}
public function testAccessWithTypePlaceholder() {
$route = new Route('/foo/{entity_type}/{var_name}', [], [
'_entity_access' => 'var_name.update',
], [
'parameters' => [
'var_name' => [
'type' => 'entity:{entity_type}',
],
],
]);
$account = $this
->prophesize(AccountInterface::class)
->reveal();
$node = $this
->prophesize(NodeInterface::class);
$node
->access('update', $account, TRUE)
->willReturn(AccessResult::allowed());
$node = $node
->reveal();
$route_match = $this
->createRouteMatchForObject($node);
$access_check = new EntityAccessCheck();
$this
->assertEquals(AccessResult::allowed(), $access_check
->access($route, $route_match, $account));
}
public function testAccessWithDifferentRouteParameters() {
$route = new Route('/foo/{var_name}', [], [
'_entity_access' => 'var_name.update',
], [
'parameters' => [
'var_name' => [
'type' => 'entity:node',
],
],
]);
$account = $this
->prophesize(AccountInterface::class)
->reveal();
$access_check = new EntityAccessCheck();
$node = $this
->prophesize(EntityInterface::class);
$node
->access('update', $account, TRUE)
->willReturn(AccessResult::allowed());
$route_match = $this
->createRouteMatchForObject($node
->reveal());
$this
->assertEquals(AccessResult::allowed(), $access_check
->access($route, $route_match, $account));
$node = $this
->prophesize(AccessibleInterface::class);
$node
->access('update', $account, TRUE)
->willReturn(AccessResult::allowed());
$route_match = $this
->createRouteMatchForObject($node
->reveal());
$this
->assertEquals(AccessResult::neutral(), $access_check
->access($route, $route_match, $account));
}
private function createRouteMatchForObject(\stdClass $object) {
$route_match = $this
->prophesize(RouteMatchInterface::class);
$route_match
->getRawParameters()
->willReturn(new ParameterBag([
'entity_type' => 'node',
'var_name' => 1,
]));
$route_match
->getParameters()
->willReturn(new ParameterBag([
'entity_type' => 'node',
'var_name' => $object,
]));
return $route_match
->reveal();
}
}