View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\simpletest\KernelTestBase;
class FieldAccessTest extends KernelTestBase {
public static $modules = [
'entity_test',
'field',
'system',
'text',
'filter',
'user',
];
protected $activeUid;
protected function setUp() {
parent::setUp();
$this
->installConfig(array(
'field',
));
$this
->installEntitySchema('user');
module_load_install('entity_test');
entity_test_install();
}
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);
$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.');
}
}