function FieldAccessTest::testFieldAccess

Same name in this branch
  1. 8.x drupal/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php \Drupal\field\Tests\FieldAccessTest::testFieldAccess()
  2. 8.x drupal/core/modules/system/lib/Drupal/system/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

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php, line 58
Contains \Drupal\system\Tests\Entity\FieldAccessTest.

Class

FieldAccessTest
Tests the functionality of field access.

Namespace

Drupal\system\Tests\Entity

Code

function testFieldAccess() {
  $values = array(
    'name' => $this
      ->randomName(),
    '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
    ->getNGEntity()), 'Access to the field was denied.');
  $entity->field_test_text = 'access alter value';
  $this
    ->assertFalse($entity->field_test_text
    ->access('view', $account
    ->getNGEntity()), 'Access to the field was denied.');
  $entity->field_test_text = 'standard value';
  $this
    ->assertTrue($entity->field_test_text
    ->access('view', $account
    ->getNGEntity()), 'Access to the field was granted.');
}