3 namespace Drupal\Tests\Core\Entity;
5 use Drupal\Core\Cache\Context\CacheContextsManager;
6 use Drupal\Core\DependencyInjection\Container;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\node\NodeInterface;
10 use Symfony\Component\HttpFoundation\ParameterBag;
11 use Symfony\Component\Routing\Route;
12 use Drupal\Core\Access\AccessibleInterface;
13 use Drupal\Core\Access\AccessResult;
14 use Drupal\Core\Entity\EntityAccessCheck;
15 use Drupal\Core\Entity\EntityInterface;
16 use Drupal\Tests\UnitTestCase;
19 * Unit test of entity access checking system.
21 * @coversDefaultClass \Drupal\Core\Entity\EntityAccessCheck
26 class EntityAccessCheckTest extends UnitTestCase {
31 protected function setUp() {
32 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
33 $container = new Container();
34 $container->set('cache_contexts_manager', $cache_contexts_manager);
35 \Drupal::setContainer($container);
39 * Tests the method for checking access to routes.
41 public function testAccess() {
42 $route = new Route('/foo/{var_name}', [], ['_entity_access' => 'var_name.update'], ['parameters' => ['var_name' => ['type' => 'entity:node']]]);
43 /** @var \Drupal\Core\Session\AccountInterface $account */
44 $account = $this->prophesize(AccountInterface::class)->reveal();
46 /** @var \Drupal\node\NodeInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
47 $node = $this->prophesize(NodeInterface::class);
48 $node->access('update', $account, TRUE)->willReturn(AccessResult::allowed());
49 $node = $node->reveal();
51 /** @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
52 $route_match = $this->prophesize(RouteMatchInterface::class);
53 $route_match->getRawParameters()->willReturn(new ParameterBag(['var_name' => 1]));
54 $route_match->getParameters()->willReturn(new ParameterBag(['var_name' => $node]));
55 $route_match = $route_match->reveal();
57 $access_check = new EntityAccessCheck();
58 $this->assertEquals(AccessResult::allowed(), $access_check->access($route, $route_match, $account));
64 public function testAccessWithTypePlaceholder() {
65 $route = new Route('/foo/{entity_type}/{var_name}', [], ['_entity_access' => 'var_name.update'], ['parameters' => ['var_name' => ['type' => 'entity:{entity_type}']]]);
66 /** @var \Drupal\Core\Session\AccountInterface $account */
67 $account = $this->prophesize(AccountInterface::class)->reveal();
69 /** @var \Drupal\node\NodeInterface|\Prophecy\Prophecy\ObjectProphecy $node */
70 $node = $this->prophesize(NodeInterface::class);
71 $node->access('update', $account, TRUE)->willReturn(AccessResult::allowed());
72 $node = $node->reveal();
74 /** @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
75 $route_match = $this->createRouteMatchForObject($node);
77 $access_check = new EntityAccessCheck();
78 $this->assertEquals(AccessResult::allowed(), $access_check->access($route, $route_match, $account));
84 public function testAccessWithDifferentRouteParameters() {
88 ['_entity_access' => 'var_name.update'],
89 ['parameters' => ['var_name' => ['type' => 'entity:node']]]
91 /** @var \Drupal\Core\Session\AccountInterface $account */
92 $account = $this->prophesize(AccountInterface::class)->reveal();
93 $access_check = new EntityAccessCheck();
95 // Confirm an EntityInterface route parameter's ::access() is called.
96 /** @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy $node */
97 $node = $this->prophesize(EntityInterface::class);
98 $node->access('update', $account, TRUE)->willReturn(AccessResult::allowed());
99 $route_match = $this->createRouteMatchForObject($node->reveal());
100 $this->assertEquals(AccessResult::allowed(), $access_check->access($route, $route_match, $account));
102 // AccessibleInterface is not entity-like: ::access() should not be called.
103 /** @var \Drupal\Core\Access\AccessibleInterface|\Prophecy\Prophecy\ObjectProphecy $node */
104 $node = $this->prophesize(AccessibleInterface::class);
105 $node->access('update', $account, TRUE)->willReturn(AccessResult::allowed());
106 $route_match = $this->createRouteMatchForObject($node->reveal());
107 $this->assertEquals(AccessResult::neutral(), $access_check->access($route, $route_match, $account));
111 * Wrap any object with a route match, and return that.
113 * @param \stdClass $object
114 * Any object, including prophesized mocks based on interfaces.
115 * @return RouteMatchInterface
116 * A prophesized RouteMatchInterface.
118 private function createRouteMatchForObject(\stdClass $object) {
119 $route_match = $this->prophesize(RouteMatchInterface::class);
120 $route_match->getRawParameters()->willReturn(new ParameterBag(['entity_type' => 'node', 'var_name' => 1]));
121 $route_match->getParameters()->willReturn(new ParameterBag(['entity_type' => 'node', 'var_name' => $object]));
122 return $route_match->reveal();