Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / NodeAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\node\Entity\NodeType;
6
7 /**
8  * Tests permission access control around nodes.
9  *
10  * @group content_moderation
11  */
12 class NodeAccessTest extends ModerationStateTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = [
20     'content_moderation',
21     'block',
22     'block_content',
23     'node',
24     'node_access_test',
25   ];
26
27   /**
28    * Permissions to grant admin user.
29    *
30    * @var array
31    */
32   protected $permissions = [
33     'administer workflows',
34     'access administration pages',
35     'administer content types',
36     'administer nodes',
37     'view latest version',
38     'view any unpublished content',
39     'access content overview',
40     'use editorial transition create_new_draft',
41     'use editorial transition publish',
42     'bypass node access',
43   ];
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     parent::setUp();
50     $this->drupalLogin($this->adminUser);
51     $this->createContentTypeFromUi('Moderated content', 'moderated_content', FALSE);
52     $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
53
54     // Add the private field to the node type.
55     node_access_test_add_field(NodeType::load('moderated_content'));
56
57     // Rebuild permissions because hook_node_grants() is implemented by the
58     // node_access_test_empty module.
59     node_access_rebuild();
60   }
61
62   /**
63    * Verifies that a non-admin user can still access the appropriate pages.
64    */
65   public function testPageAccess() {
66     // Initially disable access grant records in
67     // node_access_test_node_access_records().
68     \Drupal::state()->set('node_access_test.private', TRUE);
69
70     $this->drupalLogin($this->adminUser);
71
72     // Access the node form before moderation is enabled, the publication state
73     // should now be visible.
74     $this->drupalGet('node/add/moderated_content');
75     $this->assertSession()->fieldExists('Published');
76
77     // Now enable the workflow.
78     $this->enableModerationThroughUi('moderated_content', 'editorial');
79
80     // Access that the status field is no longer visible.
81     $this->drupalGet('node/add/moderated_content');
82     $this->assertSession()->fieldNotExists('Published');
83
84     // Create a node to test with.
85     $this->drupalPostForm(NULL, [
86       'title[0][value]' => 'moderated content',
87       'moderation_state[0][state]' => 'draft',
88     ], t('Save'));
89     $node = $this->getNodeByTitle('moderated content');
90     if (!$node) {
91       $this->fail('Test node was not saved correctly.');
92     }
93
94     $view_path = 'node/' . $node->id();
95     $edit_path = 'node/' . $node->id() . '/edit';
96     $latest_path = 'node/' . $node->id() . '/latest';
97
98     // Now make a new user and verify that the new user's access is correct.
99     $user = $this->createUser([
100       'use editorial transition create_new_draft',
101       'view latest version',
102       'view any unpublished content',
103     ]);
104     $this->drupalLogin($user);
105
106     $this->drupalGet($edit_path);
107     $this->assertResponse(403);
108
109     $this->drupalGet($latest_path);
110     $this->assertResponse(403);
111     $this->drupalGet($view_path);
112     $this->assertResponse(200);
113
114     // Publish the node.
115     $this->drupalLogin($this->adminUser);
116     $this->drupalPostForm($edit_path, [
117       'moderation_state[0][state]' => 'published',
118     ], t('Save'));
119
120     // Ensure access works correctly for anonymous users.
121     $this->drupalLogout();
122
123     $this->drupalGet($edit_path);
124     $this->assertResponse(403);
125
126     $this->drupalGet($latest_path);
127     $this->assertResponse(403);
128     $this->drupalGet($view_path);
129     $this->assertResponse(200);
130
131     // Create a pending revision for the 'Latest revision' tab.
132     $this->drupalLogin($this->adminUser);
133     $this->drupalPostForm($edit_path, [
134       'title[0][value]' => 'moderated content revised',
135       'moderation_state[0][state]' => 'draft',
136     ], t('Save'));
137
138     $this->drupalLogin($user);
139
140     $this->drupalGet($edit_path);
141     $this->assertResponse(403);
142
143     $this->drupalGet($latest_path);
144     $this->assertResponse(200);
145     $this->drupalGet($view_path);
146     $this->assertResponse(200);
147
148     // Now make another user, who should not be able to see pending revisions.
149     $user = $this->createUser([
150       'use editorial transition create_new_draft',
151     ]);
152     $this->drupalLogin($user);
153
154     $this->drupalGet($edit_path);
155     $this->assertResponse(403);
156
157     $this->drupalGet($latest_path);
158     $this->assertResponse(403);
159     $this->drupalGet($view_path);
160     $this->assertResponse(200);
161
162     // Now create a private node that the user is not granted access to by the
163     // node grants, but is granted access via hook_node_access().
164     // @see node_access_test_node_access
165     $node = $this->createNode([
166       'type' => 'moderated_content',
167       'private' => TRUE,
168       'uid' => $this->adminUser->id(),
169     ]);
170     $user = $this->createUser([
171       'use editorial transition publish',
172     ]);
173     $this->drupalLogin($user);
174
175     // Grant access to the node via node_access_test_node_access().
176     \Drupal::state()->set('node_access_test.allow_uid', $user->id());
177
178     $this->drupalGet($node->toUrl());
179     $this->assertResponse(200);
180
181     // Verify the moderation form is in place by publishing the node.
182     $this->drupalPostForm(NULL, [], t('Apply'));
183     $node = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($node->id());
184     $this->assertEquals('published', $node->moderation_state->value);
185   }
186
187 }