comparison core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\taxonomy\Functional; 3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Component\Utility\Unicode;
4 6
5 /** 7 /**
6 * Tests the taxonomy vocabulary permissions. 8 * Tests the taxonomy vocabulary permissions.
7 * 9 *
8 * @group taxonomy 10 * @group taxonomy
9 */ 11 */
10 class VocabularyPermissionsTest extends TaxonomyTestBase { 12 class VocabularyPermissionsTest extends TaxonomyTestBase {
11 13
14 /**
15 * Modules to enable.
16 *
17 * @var array
18 */
19 public static $modules = ['help'];
20
12 protected function setUp() { 21 protected function setUp() {
13 parent::setUp(); 22 parent::setUp();
14 23
15 $this->drupalPlaceBlock('page_title_block'); 24 $this->drupalPlaceBlock('page_title_block');
25 $this->drupalPlaceBlock('local_actions_block');
26 $this->drupalPlaceBlock('help_block');
27 }
28
29 /**
30 * Create, edit and delete a vocabulary via the user interface.
31 */
32 public function testVocabularyPermissionsVocabulary() {
33 // VocabularyTest.php already tests for user with "administer taxonomy"
34 // permission.
35
36 // Test as user without proper permissions.
37 $authenticated_user = $this->drupalCreateUser([]);
38 $this->drupalLogin($authenticated_user);
39
40 $assert_session = $this->assertSession();
41
42 // Visit the main taxonomy administration page.
43 $this->drupalGet('admin/structure/taxonomy');
44 $assert_session->statusCodeEquals(403);
45
46 // Test as user with "access taxonomy overview" permissions.
47 $proper_user = $this->drupalCreateUser(['access taxonomy overview']);
48 $this->drupalLogin($proper_user);
49
50 // Visit the main taxonomy administration page.
51 $this->drupalGet('admin/structure/taxonomy');
52 $assert_session->statusCodeEquals(200);
53 $assert_session->pageTextContains('Vocabulary name');
54 $assert_session->linkNotExists('Add vocabulary');
55 }
56
57 /**
58 * Test the vocabulary overview permission.
59 */
60 public function testTaxonomyVocabularyOverviewPermissions() {
61 // Create two vocabularies, one with two terms, the other without any term.
62 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary1 , $vocabulary2 */
63 $vocabulary1 = $this->createVocabulary();
64 $vocabulary2 = $this->createVocabulary();
65 $vocabulary1_id = $vocabulary1->id();
66 $vocabulary2_id = $vocabulary2->id();
67 $this->createTerm($vocabulary1);
68 $this->createTerm($vocabulary1);
69
70 // Assert expected help texts on first vocabulary.
71 $edit_help_text = t('You can reorganize the terms in @capital_name using their drag-and-drop handles, and group terms under a parent term by sliding them under and to the right of the parent.', ['@capital_name' => Unicode::ucfirst($vocabulary1->label())]);
72 $no_edit_help_text = t('@capital_name contains the following terms.', ['@capital_name' => Unicode::ucfirst($vocabulary1->label())]);
73
74 $assert_session = $this->assertSession();
75
76 // Logged in as admin user with 'administer taxonomy' permission.
77 $admin_user = $this->drupalCreateUser(['administer taxonomy']);
78 $this->drupalLogin($admin_user);
79 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
80 $assert_session->statusCodeEquals(200);
81 $assert_session->linkExists('Edit');
82 $assert_session->linkExists('Delete');
83 $assert_session->linkExists('Add term');
84 $assert_session->buttonExists('Save');
85 $assert_session->pageTextContains('Weight');
86 $assert_session->pageTextContains($edit_help_text);
87
88 // Visit vocabulary overview without terms. 'Add term' should be shown.
89 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
90 $assert_session->statusCodeEquals(200);
91 $assert_session->pageTextContains('No terms available');
92 $assert_session->linkExists('Add term');
93
94 // Login as a user without any of the required permissions.
95 $no_permission_user = $this->drupalCreateUser();
96 $this->drupalLogin($no_permission_user);
97 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
98 $assert_session->statusCodeEquals(403);
99 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
100 $assert_session->statusCodeEquals(403);
101
102 // Log in as a user with only the overview permission, neither edit nor
103 // delete operations must be available and no Save button.
104 $overview_only_user = $this->drupalCreateUser(['access taxonomy overview']);
105 $this->drupalLogin($overview_only_user);
106 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
107 $assert_session->statusCodeEquals(200);
108 $assert_session->linkNotExists('Edit');
109 $assert_session->linkNotExists('Delete');
110 $assert_session->buttonNotExists('Save');
111 $assert_session->pageTextNotContains('Weight');
112 $assert_session->linkNotExists('Add term');
113 $assert_session->pageTextContains($no_edit_help_text);
114
115 // Visit vocabulary overview without terms. 'Add term' should not be shown.
116 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
117 $assert_session->statusCodeEquals(200);
118 $assert_session->pageTextContains('No terms available');
119 $assert_session->linkNotExists('Add term');
120
121 // Login as a user with permission to edit terms, only edit link should be
122 // visible.
123 $edit_user = $this->createUser([
124 'access taxonomy overview',
125 'edit terms in ' . $vocabulary1_id,
126 'edit terms in ' . $vocabulary2_id,
127 ]);
128 $this->drupalLogin($edit_user);
129 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
130 $assert_session->statusCodeEquals(200);
131 $assert_session->linkExists('Edit');
132 $assert_session->linkNotExists('Delete');
133 $assert_session->buttonExists('Save');
134 $assert_session->pageTextContains('Weight');
135 $assert_session->linkNotExists('Add term');
136 $assert_session->pageTextContains($edit_help_text);
137
138 // Visit vocabulary overview without terms. 'Add term' should not be shown.
139 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
140 $assert_session->statusCodeEquals(200);
141 $assert_session->pageTextContains('No terms available');
142 $assert_session->linkNotExists('Add term');
143
144 // Login as a user with permission only to delete terms.
145 $edit_delete_user = $this->createUser([
146 'access taxonomy overview',
147 'delete terms in ' . $vocabulary1_id,
148 'delete terms in ' . $vocabulary2_id,
149 ]);
150 $this->drupalLogin($edit_delete_user);
151 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
152 $assert_session->statusCodeEquals(200);
153 $assert_session->linkNotExists('Edit');
154 $assert_session->linkExists('Delete');
155 $assert_session->linkNotExists('Add term');
156 $assert_session->buttonNotExists('Save');
157 $assert_session->pageTextNotContains('Weight');
158 $assert_session->pageTextContains($no_edit_help_text);
159
160 // Visit vocabulary overview without terms. 'Add term' should not be shown.
161 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
162 $assert_session->statusCodeEquals(200);
163 $assert_session->pageTextContains('No terms available');
164 $assert_session->linkNotExists('Add term');
165
166 // Login as a user with permission to edit and delete terms.
167 $edit_delete_user = $this->createUser([
168 'access taxonomy overview',
169 'edit terms in ' . $vocabulary1_id,
170 'delete terms in ' . $vocabulary1_id,
171 'edit terms in ' . $vocabulary2_id,
172 'delete terms in ' . $vocabulary2_id,
173 ]);
174 $this->drupalLogin($edit_delete_user);
175 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
176 $assert_session->statusCodeEquals(200);
177 $assert_session->linkExists('Edit');
178 $assert_session->linkExists('Delete');
179 $assert_session->linkNotExists('Add term');
180 $assert_session->buttonExists('Save');
181 $assert_session->pageTextContains('Weight');
182 $assert_session->pageTextContains($edit_help_text);
183
184 // Visit vocabulary overview without terms. 'Add term' should not be shown.
185 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
186 $assert_session->statusCodeEquals(200);
187 $assert_session->pageTextContains('No terms available');
188 $assert_session->linkNotExists('Add term');
189
190 // Login as a user with permission to create new terms, only add new term
191 // link should be visible.
192 $edit_user = $this->createUser([
193 'access taxonomy overview',
194 'create terms in ' . $vocabulary1_id,
195 'create terms in ' . $vocabulary2_id,
196 ]);
197 $this->drupalLogin($edit_user);
198 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
199 $assert_session->statusCodeEquals(200);
200 $assert_session->linkNotExists('Edit');
201 $assert_session->linkNotExists('Delete');
202 $assert_session->linkExists('Add term');
203 $assert_session->buttonNotExists('Save');
204 $assert_session->pageTextNotContains('Weight');
205 $assert_session->pageTextContains($no_edit_help_text);
206
207 // Visit vocabulary overview without terms. 'Add term' should not be shown.
208 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
209 $assert_session->statusCodeEquals(200);
210 $assert_session->pageTextContains('No terms available');
211 $assert_session->linkExists('Add term');
16 } 212 }
17 213
18 /** 214 /**
19 * Create, edit and delete a taxonomy term via the user interface. 215 * Create, edit and delete a taxonomy term via the user interface.
20 */ 216 */
40 236
41 // Verify that the creation message contains a link to a term. 237 // Verify that the creation message contains a link to a term.
42 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']); 238 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']);
43 $this->assert(isset($view_link), 'The message area contains a link to a term'); 239 $this->assert(isset($view_link), 'The message area contains a link to a term');
44 240
45 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); 241 $terms = \Drupal::entityTypeManager()
242 ->getStorage('taxonomy_term')
243 ->loadByProperties(['name' => $edit['name[0][value]']]);
46 $term = reset($terms); 244 $term = reset($terms);
47 245
48 // Edit the term. 246 // Edit the term.
49 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); 247 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
50 $this->assertResponse(200); 248 $this->assertResponse(200);
60 258
61 // Confirm deletion. 259 // Confirm deletion.
62 $this->drupalPostForm(NULL, NULL, t('Delete')); 260 $this->drupalPostForm(NULL, NULL, t('Delete'));
63 $this->assertRaw(t('Deleted term %name.', ['%name' => $edit['name[0][value]']]), 'Term deleted.'); 261 $this->assertRaw(t('Deleted term %name.', ['%name' => $edit['name[0][value]']]), 'Term deleted.');
64 262
263 // Test as user with "create" permissions.
264 $user = $this->drupalCreateUser(["create terms in {$vocabulary->id()}"]);
265 $this->drupalLogin($user);
266
267 $assert_session = $this->assertSession();
268
269 // Create a new term.
270 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
271 $assert_session->statusCodeEquals(200);
272 $assert_session->fieldExists('name[0][value]');
273
274 // Submit the term.
275 $edit = [];
276 $edit['name[0][value]'] = $this->randomMachineName();
277
278 $this->drupalPostForm(NULL, $edit, t('Save'));
279 $assert_session->pageTextContains(t('Created new term @name.', ['@name' => $edit['name[0][value]']]));
280
281 $terms = \Drupal::entityTypeManager()
282 ->getStorage('taxonomy_term')
283 ->loadByProperties(['name' => $edit['name[0][value]']]);
284 $term = reset($terms);
285
286 // Ensure that edit and delete access is denied.
287 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
288 $assert_session->statusCodeEquals(403);
289 $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
290 $assert_session->statusCodeEquals(403);
291
65 // Test as user with "edit" permissions. 292 // Test as user with "edit" permissions.
66 $user = $this->drupalCreateUser(["edit terms in {$vocabulary->id()}"]); 293 $user = $this->drupalCreateUser(["edit terms in {$vocabulary->id()}"]);
67 $this->drupalLogin($user); 294 $this->drupalLogin($user);
68 295
69 // Visit the main taxonomy administration page. 296 // Visit the main taxonomy administration page.