comparison core/modules/node/tests/src/Functional/NodeAccessFieldTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10 * Tests the interaction of the node access system with fields.
11 *
12 * @group node
13 */
14 class NodeAccessFieldTest extends NodeTestBase {
15
16 /**
17 * Modules to enable.
18 *
19 * @var array
20 */
21 public static $modules = ['node_access_test', 'field_ui'];
22
23 /**
24 * A user with permission to bypass access content.
25 *
26 * @var \Drupal\user\UserInterface
27 */
28 protected $adminUser;
29
30 /**
31 * A user with permission to manage content types and fields.
32 *
33 * @var \Drupal\user\UserInterface
34 */
35 protected $contentAdminUser;
36
37 /**
38 * The name of the created field.
39 *
40 * @var string
41 */
42 protected $fieldName;
43
44 protected function setUp() {
45 parent::setUp();
46
47 node_access_rebuild();
48
49 // Create some users.
50 $this->adminUser = $this->drupalCreateUser(['access content', 'bypass node access']);
51 $this->contentAdminUser = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields']);
52
53 // Add a custom field to the page content type.
54 $this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
55 FieldStorageConfig::create([
56 'field_name' => $this->fieldName,
57 'entity_type' => 'node',
58 'type' => 'text'
59 ])->save();
60 FieldConfig::create([
61 'field_name' => $this->fieldName,
62 'entity_type' => 'node',
63 'bundle' => 'page',
64 ])->save();
65 entity_get_display('node', 'page', 'default')
66 ->setComponent($this->fieldName)
67 ->save();
68 entity_get_form_display('node', 'page', 'default')
69 ->setComponent($this->fieldName)
70 ->save();
71 }
72
73 /**
74 * Tests administering fields when node access is restricted.
75 */
76 public function testNodeAccessAdministerField() {
77 // Create a page node.
78 $fieldData = [];
79 $value = $fieldData[0]['value'] = $this->randomMachineName();
80 $node = $this->drupalCreateNode([$this->fieldName => $fieldData]);
81
82 // Log in as the administrator and confirm that the field value is present.
83 $this->drupalLogin($this->adminUser);
84 $this->drupalGet('node/' . $node->id());
85 $this->assertText($value, 'The saved field value is visible to an administrator.');
86
87 // Log in as the content admin and try to view the node.
88 $this->drupalLogin($this->contentAdminUser);
89 $this->drupalGet('node/' . $node->id());
90 $this->assertText('Access denied', 'Access is denied for the content admin.');
91
92 // Modify the field default as the content admin.
93 $edit = [];
94 $default = 'Sometimes words have two meanings';
95 $edit["default_value_input[{$this->fieldName}][0][value]"] = $default;
96 $this->drupalPostForm(
97 "admin/structure/types/manage/page/fields/node.page.{$this->fieldName}",
98 $edit,
99 t('Save settings')
100 );
101
102 // Log in as the administrator.
103 $this->drupalLogin($this->adminUser);
104
105 // Confirm that the existing node still has the correct field value.
106 $this->drupalGet('node/' . $node->id());
107 $this->assertText($value, 'The original field value is visible to an administrator.');
108
109 // Confirm that the new default value appears when creating a new node.
110 $this->drupalGet('node/add/page');
111 $this->assertRaw($default, 'The updated default value is displayed when creating a new node.');
112 }
113
114 }