Version 1
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeAccessFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4 use Drupal\Component\Utility\Unicode;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7
8 /**
9  * Tests the interaction of the node access system with fields.
10  *
11  * @group node
12  */
13 class NodeAccessFieldTest extends NodeTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['node_access_test', 'field_ui'];
21
22   /**
23    * A user with permission to bypass access content.
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $adminUser;
28
29   /**
30    * A user with permission to manage content types and fields.
31    *
32    * @var \Drupal\user\UserInterface
33    */
34   protected $contentAdminUser;
35
36   /**
37    * The name of the created field.
38    *
39    * @var string
40    */
41   protected $fieldName;
42
43   protected function setUp() {
44     parent::setUp();
45
46     node_access_rebuild();
47
48     // Create some users.
49     $this->adminUser = $this->drupalCreateUser(['access content', 'bypass node access']);
50     $this->contentAdminUser = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields']);
51
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',
57       'type' => 'text'
58     ])->save();
59     FieldConfig::create([
60       'field_name' => $this->fieldName,
61       'entity_type' => 'node',
62       'bundle' => 'page',
63     ])->save();
64     entity_get_display('node', 'page', 'default')
65       ->setComponent($this->fieldName)
66       ->save();
67     entity_get_form_display('node', 'page', 'default')
68       ->setComponent($this->fieldName)
69       ->save();
70   }
71
72   /**
73    * Tests administering fields when node access is restricted.
74    */
75   public function testNodeAccessAdministerField() {
76     // Create a page node.
77     $fieldData = [];
78     $value = $fieldData[0]['value'] = $this->randomMachineName();
79     $node = $this->drupalCreateNode([$this->fieldName => $fieldData]);
80
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.');
85
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.');
90
91     // Modify the field default as the content admin.
92     $edit = [];
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}",
97       $edit,
98       t('Save settings')
99     );
100
101     // Log in as the administrator.
102     $this->drupalLogin($this->adminUser);
103
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.');
107
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.');
111   }
112
113 }