function EntityAccessControlHandlerTest::testEntityAccess

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

Ensures entity access is properly working.

File

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

Class

EntityAccessControlHandlerTest
Tests the entity access control handler.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityAccess() {
    // Set up a non-admin user that is allowed to view test entities.
    \Drupal::currentUser()->setAccount($this->createUser([
        'uid' => 2,
    ], [
        'view test entity',
    ]));
    // Use the 'entity_test_label' entity type in order to test the 'view label'
    // access operation.
    $entity = EntityTestLabel::create([
        'name' => 'test',
    ]);
    // The current user is allowed to view entities.
    $this->assertEntityAccess([
        'create' => FALSE,
        'update' => FALSE,
        'delete' => FALSE,
        'view' => TRUE,
        'view label' => TRUE,
    ], $entity);
    // The custom user is not allowed to perform any operation on test entities,
    // except for viewing their label.
    $custom_user = $this->createUser();
    $this->assertEntityAccess([
        'create' => FALSE,
        'update' => FALSE,
        'delete' => FALSE,
        'view' => FALSE,
        'view label' => TRUE,
    ], $entity, $custom_user);
}

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