You are here

function EntityAccessControlHandlerTest::testEntityAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php \Drupal\system\Tests\Entity\EntityAccessControlHandlerTest::testEntityAccess()

Ensures entity access is properly working.

File

core/modules/system/src/Tests/Entity/EntityAccessControlHandlerTest.php, line 44
Contains \Drupal\system\Tests\Entity\EntityAccessControlHandlerTest.

Class

EntityAccessControlHandlerTest
Tests the entity access control handler.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityAccess() {

  // Set up a non-admin user that is allowed to view test entities.
  \Drupal::currentUser()
    ->setAccount($this
    ->createUser(array(
    'uid' => 2,
  ), array(
    'view test entity',
  )));
  $entity = entity_create('entity_test', array(
    'name' => 'test',
  ));

  // The current user is allowed to view entities.
  $this
    ->assertEntityAccess(array(
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => TRUE,
  ), $entity);

  // The custom user is not allowed to perform any operation on test entities.
  $custom_user = $this
    ->createUser();
  $this
    ->assertEntityAccess(array(
    'create' => FALSE,
    'update' => FALSE,
    'delete' => FALSE,
    'view' => FALSE,
  ), $entity, $custom_user);
}