class EntityAccessCheckTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityAccessCheckTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityAccessCheckTest
  3. 11.x core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php \Drupal\Tests\Core\Entity\EntityAccessCheckTest

Unit test of entity access checking system.

@coversDefaultClass \Drupal\Core\Entity\EntityAccessCheck

@group Access @group Entity

Hierarchy

Expanded class hierarchy of EntityAccessCheckTest

File

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

Namespace

Drupal\Tests\Core\Entity
View source
class EntityAccessCheckTest extends UnitTestCase {
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)
      ->reveal();
    $container = new Container();
    $container->set('cache_contexts_manager', $cache_contexts_manager);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests the method for checking access to routes.
   */
  public function testAccess() : void {
    $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();
    /** @var \Drupal\node\NodeInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
    $node = $this->prophesize(NodeInterface::class);
    $node->access('update', $account, TRUE)
      ->willReturn(AccessResult::allowed());
    $node = $node->reveal();
    /** @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRawParameters()
      ->willReturn(new InputBag([
      '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));
  }
  
  /**
   * @covers ::access
   */
  public function testAccessWithTypePlaceholder() : void {
    $route = new Route('/foo/{entity_type}/{var_name}', [], [
      '_entity_access' => 'var_name.update',
    ], [
      'parameters' => [
        'var_name' => [
          'type' => 'entity:{entity_type}',
        ],
      ],
    ]);
    /** @var \Drupal\Core\Session\AccountInterface $account */
    $account = $this->prophesize(AccountInterface::class)
      ->reveal();
    /** @var \Drupal\node\NodeInterface|\Prophecy\Prophecy\ObjectProphecy $node */
    $node = $this->prophesize(NodeInterface::class);
    $node->access('update', $account, TRUE)
      ->willReturn(AccessResult::allowed());
    $node = $node->reveal();
    /** @var \Drupal\Core\Routing\RouteMatchInterface|\Prophecy\Prophecy\ObjectProphecy $route_match */
    $route_match = $this->createRouteMatchForObject($node);
    $access_check = new EntityAccessCheck();
    $this->assertEquals(AccessResult::allowed(), $access_check->access($route, $route_match, $account));
  }
  
  /**
   * @covers ::access
   */
  public function testAccessWithDifferentRouteParameters() : void {
    $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));
  }
  
  /**
   * Wrap any object with a route match, and return that.
   *
   * @param object $object
   *   Any object, including prophesized mocks based on interfaces.
   *
   * @return \Drupal\Core\Routing\RouteMatchInterface
   *   A prophesized RouteMatchInterface.
   */
  private function createRouteMatchForObject(\stdClass $object) {
    $route_match = $this->prophesize(RouteMatchInterface::class);
    $route_match->getRawParameters()
      ->willReturn(new InputBag([
      'entity_type' => 'node',
      'var_name' => 1,
    ]));
    $route_match->getParameters()
      ->willReturn(new ParameterBag([
      'entity_type' => 'node',
      'var_name' => $object,
    ]));
    return $route_match->reveal();
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
EntityAccessCheckTest::createRouteMatchForObject private function Wrap any object with a route match, and return that.
EntityAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
EntityAccessCheckTest::testAccess public function Tests the method for checking access to routes.
EntityAccessCheckTest::testAccessWithDifferentRouteParameters public function @covers ::access[[api-linebreak]]
EntityAccessCheckTest::testAccessWithTypePlaceholder public function @covers ::access[[api-linebreak]]
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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