3 namespace Drupal\Tests\field\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
13 class FieldAccessTest extends FieldTestBase {
20 public static $modules = ['node', 'field_test'];
23 * Node entity to use in this test.
25 * @var \Drupal\node\Entity\Node
30 * Field value to test display on nodes.
34 protected $testViewFieldValue;
36 protected function setUp() {
39 $web_user = $this->drupalCreateUser(['view test_view_field content']);
40 $this->drupalLogin($web_user);
42 // Create content type.
43 $content_type_info = $this->drupalCreateContentType();
44 $content_type = $content_type_info->id();
47 'field_name' => 'test_view_field',
48 'entity_type' => 'node',
51 FieldStorageConfig::create($field_storage)->save();
53 'field_name' => $field_storage['field_name'],
54 'entity_type' => 'node',
55 'bundle' => $content_type,
57 FieldConfig::create($field)->save();
59 // Assign display properties for the 'default' and 'teaser' view modes.
60 foreach (['default', 'teaser'] as $view_mode) {
61 entity_get_display('node', $content_type, $view_mode)
62 ->setComponent($field_storage['field_name'])
67 $this->testViewFieldValue = 'This is some text';
69 $settings['type'] = $content_type;
70 $settings['title'] = 'Field view access test';
71 $settings['test_view_field'] = [['value' => $this->testViewFieldValue]];
72 $this->node = $this->drupalCreateNode($settings);
76 * Test that hook_entity_field_access() is called.
78 public function testFieldAccess() {
80 // Assert the text is visible.
81 $this->drupalGet('node/' . $this->node->id());
82 $this->assertText($this->testViewFieldValue);
84 // Assert the text is not visible for anonymous users.
85 // The field_test module implements hook_entity_field_access() which will
86 // specifically target the 'test_view_field' field.
87 $this->drupalLogout();
88 $this->drupalGet('node/' . $this->node->id());
89 $this->assertNoText($this->testViewFieldValue);