function EntityAccessTest::testEntityAccess

Ensures entity access is properly working.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php, line 52
Contains Drupal\system\Tests\Entity\EntityAccessTest.

Class

EntityAccessTest
Tests the entity access controller.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityAccess() {

  // Set up a non-admin user that is allowed to view test entities.
  $user = $this
    ->drupalCreateUser(array(
    'view test entity',
  ));
  $this
    ->drupalLogin($user);
  $entity = entity_create('entity_test', array(
    'name' => 'test',
  ));
  $entity
    ->save();

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

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