You are here

public function EntityAccessCheckTest::testAccessWithDifferentRouteParameters in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityAccessCheckTest::testAccessWithDifferentRouteParameters()
  2. 10 core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityAccessCheckTest::testAccessWithDifferentRouteParameters()

@covers ::access

File

core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php, line 84

Class

EntityAccessCheckTest
Unit test of entity access checking system.

Namespace

Drupal\Tests\Core\Entity

Code

public function testAccessWithDifferentRouteParameters() {
  $route = new Route('/foo/{var_name}', [], [
    '_entity_access' => 'var_name.update',
  ], [
    'parameters' => [
      'var_name' => [
        'type' => 'entity:node',
      ],
    ],
  ]);

  /** @var \Drupal\Core\Session\AccountInterface $account */
  $account = $this
    ->prophesize(AccountInterface::class)
    ->reveal();
  $access_check = new EntityAccessCheck();

  // Confirm an EntityInterface route parameter's ::access() is called.

  /** @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy $node */
  $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));

  // AccessibleInterface is not entity-like: ::access() should not be called.

  /** @var \Drupal\Core\Access\AccessibleInterface|\Prophecy\Prophecy\ObjectProphecy $node */
  $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));
}