You are here

public function EntityAccessControlHandlerTest::testEntityAccess in Drupal 8

Same name and namespace in other branches
  1. 9 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 110

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);
}