function EntityAccessCheckTest::testAccessWithDifferentRouteParameters

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.