You are here

function FieldAccessTest::testFieldAccess in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/field/src/Tests/FieldAccessTest.php \Drupal\field\Tests\FieldAccessTest::testFieldAccess()
  2. 8 core/modules/system/src/Tests/Entity/FieldAccessTest.php \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/FieldAccessTest.php \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()

Tests hook_entity_field_access() and hook_entity_field_access_alter().

See also

entity_test_entity_field_access()

entity_test_entity_field_access_alter()

File

core/modules/system/src/Tests/Entity/FieldAccessTest.php, line 54
Contains \Drupal\system\Tests\Entity\FieldAccessTest.

Class

FieldAccessTest
Tests Field level access hooks.

Namespace

Drupal\system\Tests\Entity

Code

function testFieldAccess() {
  $values = array(
    'name' => $this
      ->randomMachineName(),
    'user_id' => 1,
    'field_test_text' => array(
      'value' => 'no access value',
      'format' => 'full_html',
    ),
  );
  $entity = entity_create('entity_test', $values);

  // Create a dummy user account for testing access with.
  $values = array(
    'name' => 'test',
  );
  $account = entity_create('user', $values);
  $this
    ->assertFalse($entity->field_test_text
    ->access('view', $account), 'Access to the field was denied.');
  $expected = AccessResult::forbidden()
    ->cacheUntilEntityChanges($entity);
  $this
    ->assertEqual($expected, $entity->field_test_text
    ->access('view', $account, TRUE), 'Access to the field was denied.');
  $entity->field_test_text = 'access alter value';
  $this
    ->assertFalse($entity->field_test_text
    ->access('view', $account), 'Access to the field was denied.');
  $this
    ->assertEqual($expected, $entity->field_test_text
    ->access('view', $account, TRUE), 'Access to the field was denied.');
  $entity->field_test_text = 'standard value';
  $this
    ->assertTrue($entity->field_test_text
    ->access('view', $account), 'Access to the field was granted.');
  $this
    ->assertEqual(AccessResult::allowed(), $entity->field_test_text
    ->access('view', $account, TRUE), 'Access to the field was granted.');
}