Version 1
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / NodeAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 /**
6  * Tests permission access control around nodes.
7  *
8  * @group content_moderation
9  */
10 class NodeAccessTest extends ModerationStateTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = [
18     'content_moderation',
19     'block',
20     'block_content',
21     'node',
22     'node_access_test_empty',
23   ];
24
25   /**
26    * Permissions to grant admin user.
27    *
28    * @var array
29    */
30   protected $permissions = [
31     'administer content moderation',
32     'access administration pages',
33     'administer content types',
34     'administer nodes',
35     'view latest version',
36     'view any unpublished content',
37     'access content overview',
38     'use editorial transition create_new_draft',
39     'use editorial transition publish',
40     'bypass node access',
41   ];
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUp() {
47     parent::setUp();
48     $this->drupalLogin($this->adminUser);
49     $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
50     $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
51
52     // Rebuild permissions because hook_node_grants() is implemented by the
53     // node_access_test_empty module.
54     node_access_rebuild();
55   }
56
57   /**
58    * Verifies that a non-admin user can still access the appropriate pages.
59    */
60   public function testPageAccess() {
61     $this->drupalLogin($this->adminUser);
62
63     // Create a node to test with.
64     $this->drupalPostForm('node/add/moderated_content', [
65       'title[0][value]' => 'moderated content',
66     ], t('Save and Create New Draft'));
67     $node = $this->getNodeByTitle('moderated content');
68     if (!$node) {
69       $this->fail('Test node was not saved correctly.');
70     }
71
72     $view_path = 'node/' . $node->id();
73     $edit_path = 'node/' . $node->id() . '/edit';
74     $latest_path = 'node/' . $node->id() . '/latest';
75
76     // Now make a new user and verify that the new user's access is correct.
77     $user = $this->createUser([
78       'use editorial transition create_new_draft',
79       'view latest version',
80       'view any unpublished content',
81     ]);
82     $this->drupalLogin($user);
83
84     $this->drupalGet($edit_path);
85     $this->assertResponse(403);
86
87     $this->drupalGet($latest_path);
88     $this->assertResponse(403);
89     $this->drupalGet($view_path);
90     $this->assertResponse(200);
91
92     // Publish the node.
93     $this->drupalLogin($this->adminUser);
94     $this->drupalPostForm($edit_path, [], t('Save and Publish'));
95
96     // Ensure access works correctly for anonymous users.
97     $this->drupalLogout();
98
99     $this->drupalGet($edit_path);
100     $this->assertResponse(403);
101
102     $this->drupalGet($latest_path);
103     $this->assertResponse(403);
104     $this->drupalGet($view_path);
105     $this->assertResponse(200);
106
107     // Create a forward revision for the 'Latest revision' tab.
108     $this->drupalLogin($this->adminUser);
109     $this->drupalPostForm($edit_path, [
110       'title[0][value]' => 'moderated content revised',
111     ], t('Save and Create New Draft'));
112
113     $this->drupalLogin($user);
114
115     $this->drupalGet($edit_path);
116     $this->assertResponse(403);
117
118     $this->drupalGet($latest_path);
119     $this->assertResponse(200);
120     $this->drupalGet($view_path);
121     $this->assertResponse(200);
122
123     // Now make another user, who should not be able to see forward revisions.
124     $user = $this->createUser([
125       'use editorial transition create_new_draft',
126     ]);
127     $this->drupalLogin($user);
128
129     $this->drupalGet($edit_path);
130     $this->assertResponse(403);
131
132     $this->drupalGet($latest_path);
133     $this->assertResponse(403);
134     $this->drupalGet($view_path);
135     $this->assertResponse(200);
136   }
137
138 }