class FieldAccessTest
Same name in this branch
- 9 core/modules/field/tests/src/Functional/FieldAccessTest.php \Drupal\Tests\field\Functional\FieldAccessTest
Same name and namespace in other branches
- 11.x core/modules/field/tests/src/Functional/FieldAccessTest.php \Drupal\Tests\field\Functional\FieldAccessTest
- 11.x core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php \Drupal\KernelTests\Core\Field\FieldAccessTest
- 10 core/modules/field/tests/src/Functional/FieldAccessTest.php \Drupal\Tests\field\Functional\FieldAccessTest
- 10 core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php \Drupal\KernelTests\Core\Field\FieldAccessTest
- 8.9.x core/modules/field/tests/src/Functional/FieldAccessTest.php \Drupal\Tests\field\Functional\FieldAccessTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Field/FieldAccessTest.php \Drupal\KernelTests\Core\Field\FieldAccessTest
Tests Field level access hooks.
@group Entity
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Field\FieldAccessTest implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of FieldAccessTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Field/ FieldAccessTest.php, line 15
Namespace
Drupal\KernelTests\Core\FieldView source
class FieldAccessTest extends KernelTestBase {
/**
* Modules to load code from.
*
* @var array
*/
protected static $modules = [
'entity_test',
'field',
'system',
'text',
'filter',
'user',
];
/**
* Holds the currently active global user ID that initiated the test run.
*
* The user ID gets replaced during the test and needs to be kept here so that
* it can be restored at the end of the test run.
*
* @var int
*/
protected $activeUid;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Install field configuration.
$this->installConfig([
'field',
]);
$this->installEntitySchema('entity_test');
$this->installEntitySchema('entity_test_mul');
$this->installEntitySchema('entity_test_mul_langcode_key');
$this->installEntitySchema('entity_test_mul_changed');
$this->installEntitySchema('entity_test_rev');
$this->installEntitySchema('entity_test_mulrev');
$this->installEntitySchema('entity_test_mulrev_changed');
// The users table is needed for creating dummy user accounts.
$this->installEntitySchema('user');
// Register entity_test text field.
$this->container
->get('module_handler')
->loadInclude('entity_test', 'install');
entity_test_install();
}
/**
* Tests hook_entity_field_access() and hook_entity_field_access_alter().
*
* @see entity_test_entity_field_access()
* @see entity_test_entity_field_access_alter()
*/
public function testFieldAccess() {
$values = [
'name' => $this->randomMachineName(),
'user_id' => 1,
'field_test_text' => [
'value' => 'no access value',
'format' => 'full_html',
],
];
$entity = EntityTest::create($values);
// Create a dummy user account for testing access with.
$values = [
'name' => 'test',
];
$account = User::create($values);
$this->assertFalse($entity->field_test_text
->access('view', $account), 'Access to the field was denied.');
$expected = AccessResult::forbidden()->addCacheableDependency($entity);
$this->assertEquals($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->assertEquals($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->assertEquals(AccessResult::allowed(), $entity->field_test_text
->access('view', $account, TRUE), 'Access to the field was granted.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.