Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / workspaces / tests / src / Functional / WorkspacePermissionsTest.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\workspaces\Entity\Workspace;
7
8 /**
9  * Tests permission controls on workspaces.
10  *
11  * @group workspaces
12  */
13 class WorkspacePermissionsTest extends BrowserTestBase {
14
15   use WorkspaceTestUtilities;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['workspaces'];
21
22   /**
23    * Verifies that a user can create but not edit a workspace.
24    */
25   public function testCreateWorkspace() {
26     $editor = $this->drupalCreateUser([
27       'access administration pages',
28       'administer site configuration',
29       'create workspace',
30     ]);
31
32     // Login as a limited-access user and create a workspace.
33     $this->drupalLogin($editor);
34     $this->createWorkspaceThroughUi('Bears', 'bears');
35
36     // Now edit that same workspace; We shouldn't be able to do so, since
37     // we don't have edit permissions.
38     /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $etm */
39     $etm = \Drupal::service('entity_type.manager');
40     /** @var \Drupal\workspaces\WorkspaceInterface $bears */
41     $entity_list = $etm->getStorage('workspace')->loadByProperties(['label' => 'Bears']);
42     $bears = current($entity_list);
43
44     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
45     $this->assertSession()->statusCodeEquals(403);
46   }
47
48   /**
49    * Verifies that a user can create and edit only their own workspace.
50    */
51   public function testEditOwnWorkspace() {
52     $permissions = [
53       'access administration pages',
54       'administer site configuration',
55       'create workspace',
56       'edit own workspace',
57     ];
58
59     $editor1 = $this->drupalCreateUser($permissions);
60
61     // Login as a limited-access user and create a workspace.
62     $this->drupalLogin($editor1);
63     $this->createWorkspaceThroughUi('Bears', 'bears');
64
65     // Now edit that same workspace; We should be able to do so.
66     $bears = Workspace::load('bears');
67
68     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
69     $this->assertSession()->statusCodeEquals(200);
70
71     $page = $this->getSession()->getPage();
72     $page->fillField('label', 'Bears again');
73     $page->fillField('id', 'bears');
74     $page->findButton('Save')->click();
75     $page->hasContent('Bears again (bears)');
76
77     // Now login as a different user and ensure they don't have edit access,
78     // and vice versa.
79     $editor2 = $this->drupalCreateUser($permissions);
80
81     $this->drupalLogin($editor2);
82     $this->createWorkspaceThroughUi('Packers', 'packers');
83     $packers = Workspace::load('packers');
84
85     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/edit");
86     $this->assertSession()->statusCodeEquals(200);
87
88     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
89     $this->assertSession()->statusCodeEquals(403);
90   }
91
92   /**
93    * Verifies that a user can edit any workspace.
94    */
95   public function testEditAnyWorkspace() {
96     $permissions = [
97       'access administration pages',
98       'administer site configuration',
99       'create workspace',
100       'edit own workspace',
101     ];
102
103     $editor1 = $this->drupalCreateUser($permissions);
104
105     // Login as a limited-access user and create a workspace.
106     $this->drupalLogin($editor1);
107     $this->createWorkspaceThroughUi('Bears', 'bears');
108
109     // Now edit that same workspace; We should be able to do so.
110     $bears = Workspace::load('bears');
111
112     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
113     $this->assertSession()->statusCodeEquals(200);
114
115     $page = $this->getSession()->getPage();
116     $page->fillField('label', 'Bears again');
117     $page->fillField('id', 'bears');
118     $page->findButton('Save')->click();
119     $page->hasContent('Bears again (bears)');
120
121     // Now login as a different user and ensure they don't have edit access,
122     // and vice versa.
123     $admin = $this->drupalCreateUser(array_merge($permissions, ['edit any workspace']));
124
125     $this->drupalLogin($admin);
126     $this->createWorkspaceThroughUi('Packers', 'packers');
127     $packers = Workspace::load('packers');
128
129     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/edit");
130     $this->assertSession()->statusCodeEquals(200);
131
132     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/edit");
133     $this->assertSession()->statusCodeEquals(200);
134   }
135
136   /**
137    * Verifies that a user can create and delete only their own workspace.
138    */
139   public function testDeleteOwnWorkspace() {
140     $permissions = [
141       'access administration pages',
142       'administer site configuration',
143       'create workspace',
144       'delete own workspace',
145     ];
146     $editor1 = $this->drupalCreateUser($permissions);
147
148     // Login as a limited-access user and create a workspace.
149     $this->drupalLogin($editor1);
150     $bears = $this->createWorkspaceThroughUi('Bears', 'bears');
151
152     // Now try to delete that same workspace; We should be able to do so.
153     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
154     $this->assertSession()->statusCodeEquals(200);
155
156     // Now login as a different user and ensure they don't have edit access,
157     // and vice versa.
158     $editor2 = $this->drupalCreateUser($permissions);
159
160     $this->drupalLogin($editor2);
161     $packers = $this->createWorkspaceThroughUi('Packers', 'packers');
162
163     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/delete");
164     $this->assertSession()->statusCodeEquals(200);
165
166     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
167     $this->assertSession()->statusCodeEquals(403);
168   }
169
170   /**
171    * Verifies that a user can delete any workspace.
172    */
173   public function testDeleteAnyWorkspace() {
174     $permissions = [
175       'access administration pages',
176       'administer site configuration',
177       'create workspace',
178       'delete own workspace',
179     ];
180     $editor1 = $this->drupalCreateUser($permissions);
181
182     // Login as a limited-access user and create a workspace.
183     $this->drupalLogin($editor1);
184     $bears = $this->createWorkspaceThroughUi('Bears', 'bears');
185
186     // Now edit that same workspace; We should be able to do so.
187     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
188     $this->assertSession()->statusCodeEquals(200);
189
190     // Now login as a different user and ensure they have delete access on both
191     // workspaces.
192     $admin = $this->drupalCreateUser(array_merge($permissions, ['delete any workspace']));
193
194     $this->drupalLogin($admin);
195     $packers = $this->createWorkspaceThroughUi('Packers', 'packers');
196
197     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$packers->id()}/delete");
198     $this->assertSession()->statusCodeEquals(200);
199
200     $this->drupalGet("/admin/config/workflow/workspaces/manage/{$bears->id()}/delete");
201     $this->assertSession()->statusCodeEquals(200);
202
203     // Check that the default workspace can not be deleted, even by a user with
204     // the "delete any workspace" permission.
205     $this->drupalGet("/admin/config/workflow/workspaces/manage/live/delete");
206     $this->assertSession()->statusCodeEquals(403);
207   }
208
209 }