function EntityAccessControlHandlerTest::testDefaultEntityAccess

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testDefaultEntityAccess()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testDefaultEntityAccess()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest::testDefaultEntityAccess()

Ensures default entity access is checked when necessary.

This ensures that the default checkAccess() implementation of the entity access control handler is considered if hook_entity_access() has not explicitly forbidden access. Therefore the default checkAccess() implementation can forbid access, even after access was already explicitly allowed by hook_entity_access().

See also

\Drupal\entity_test\EntityTestAccessControlHandler::checkAccess()

entity_test_entity_access()

File

core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php, line 155

Class

EntityAccessControlHandlerTest
Tests the entity access control handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testDefaultEntityAccess() {
    // Set up a non-admin user that is allowed to view test entities.
    \Drupal::currentUser()->setAccount($this->createUser([
        'uid' => 2,
    ], [
        'view test entity',
    ]));
    $entity = EntityTest::create([
        'name' => 'forbid_access',
    ]);
    // The user is denied access to the entity.
    $this->assertEntityAccess([
        'create' => FALSE,
        'update' => FALSE,
        'delete' => FALSE,
        'view' => FALSE,
    ], $entity);
}

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