3 namespace Drupal\Tests\node\Functional;
4 use Drupal\Component\Utility\Unicode;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
9 * Tests the interaction of the node access system with fields.
13 class NodeAccessFieldTest extends NodeTestBase {
20 public static $modules = ['node_access_test', 'field_ui'];
23 * A user with permission to bypass access content.
25 * @var \Drupal\user\UserInterface
30 * A user with permission to manage content types and fields.
32 * @var \Drupal\user\UserInterface
34 protected $contentAdminUser;
37 * The name of the created field.
43 protected function setUp() {
46 node_access_rebuild();
49 $this->adminUser = $this->drupalCreateUser(['access content', 'bypass node access']);
50 $this->contentAdminUser = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields']);
52 // Add a custom field to the page content type.
53 $this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
54 FieldStorageConfig::create([
55 'field_name' => $this->fieldName,
56 'entity_type' => 'node',
60 'field_name' => $this->fieldName,
61 'entity_type' => 'node',
64 entity_get_display('node', 'page', 'default')
65 ->setComponent($this->fieldName)
67 entity_get_form_display('node', 'page', 'default')
68 ->setComponent($this->fieldName)
73 * Tests administering fields when node access is restricted.
75 public function testNodeAccessAdministerField() {
76 // Create a page node.
78 $value = $fieldData[0]['value'] = $this->randomMachineName();
79 $node = $this->drupalCreateNode([$this->fieldName => $fieldData]);
81 // Log in as the administrator and confirm that the field value is present.
82 $this->drupalLogin($this->adminUser);
83 $this->drupalGet('node/' . $node->id());
84 $this->assertText($value, 'The saved field value is visible to an administrator.');
86 // Log in as the content admin and try to view the node.
87 $this->drupalLogin($this->contentAdminUser);
88 $this->drupalGet('node/' . $node->id());
89 $this->assertText('Access denied', 'Access is denied for the content admin.');
91 // Modify the field default as the content admin.
93 $default = 'Sometimes words have two meanings';
94 $edit["default_value_input[{$this->fieldName}][0][value]"] = $default;
95 $this->drupalPostForm(
96 "admin/structure/types/manage/page/fields/node.page.{$this->fieldName}",
101 // Log in as the administrator.
102 $this->drupalLogin($this->adminUser);
104 // Confirm that the existing node still has the correct field value.
105 $this->drupalGet('node/' . $node->id());
106 $this->assertText($value, 'The original field value is visible to an administrator.');
108 // Confirm that the new default value appears when creating a new node.
109 $this->drupalGet('node/add/page');
110 $this->assertRaw($default, 'The updated default value is displayed when creating a new node.');