Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / filter / tests / src / Functional / FilterFormatAccessTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests access to text formats.
12  *
13  * @group Access
14  * @group filter
15  */
16 class FilterFormatAccessTest extends BrowserTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['block', 'filter', 'node'];
24
25   /**
26    * A user with administrative permissions.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * A user with 'administer filters' permission.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $filterAdminUser;
38
39   /**
40    * A user with permission to create and edit own content.
41    *
42    * @var \Drupal\user\UserInterface
43    */
44   protected $webUser;
45
46   /**
47    * An object representing an allowed text format.
48    *
49    * @var object
50    */
51   protected $allowedFormat;
52
53   /**
54    * An object representing a secondary allowed text format.
55    *
56    * @var object
57    */
58   protected $secondAllowedFormat;
59
60   /**
61    * An object representing a disallowed text format.
62    *
63    * @var object
64    */
65   protected $disallowedFormat;
66
67   protected function setUp() {
68     parent::setUp();
69
70     $this->drupalPlaceBlock('page_title_block');
71
72     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
73
74     // Create a user who can administer text formats, but does not have
75     // specific permission to use any of them.
76     $this->filterAdminUser = $this->drupalCreateUser([
77       'administer filters',
78       'create page content',
79       'edit any page content',
80     ]);
81
82     // Create three text formats. Two text formats are created for all users so
83     // that the drop-down list appears for all tests.
84     $this->drupalLogin($this->filterAdminUser);
85     $formats = [];
86     for ($i = 0; $i < 3; $i++) {
87       $edit = [
88         'format' => Unicode::strtolower($this->randomMachineName()),
89         'name' => $this->randomMachineName(),
90       ];
91       $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
92       $this->resetFilterCaches();
93       $formats[] = FilterFormat::load($edit['format']);
94     }
95     list($this->allowedFormat, $this->secondAllowedFormat, $this->disallowedFormat) = $formats;
96     $this->drupalLogout();
97
98     // Create a regular user with access to two of the formats.
99     $this->webUser = $this->drupalCreateUser([
100       'create page content',
101       'edit any page content',
102       $this->allowedFormat->getPermissionName(),
103       $this->secondAllowedFormat->getPermissionName(),
104     ]);
105
106     // Create an administrative user who has access to use all three formats.
107     $this->adminUser = $this->drupalCreateUser([
108       'administer filters',
109       'create page content',
110       'edit any page content',
111       $this->allowedFormat->getPermissionName(),
112       $this->secondAllowedFormat->getPermissionName(),
113       $this->disallowedFormat->getPermissionName(),
114     ]);
115     $this->drupalPlaceBlock('local_tasks_block');
116   }
117
118   /**
119    * Tests the Filter format access permissions functionality.
120    */
121   public function testFormatPermissions() {
122     // Make sure that a regular user only has access to the text formats for
123     // which they were granted access.
124     $fallback_format = FilterFormat::load(filter_fallback_format());
125     $disallowed_format_name = $this->disallowedFormat->getPermissionName();
126     $this->assertTrue($this->allowedFormat->access('use', $this->webUser), 'A regular user has access to use a text format they were granted access to.');
127     $this->assertEqual(AccessResult::allowed()->addCacheContexts(['user.permissions']), $this->allowedFormat->access('use', $this->webUser, TRUE), 'A regular user has access to use a text format they were granted access to.');
128     $this->assertFalse($this->disallowedFormat->access('use', $this->webUser), 'A regular user does not have access to use a text format they were not granted access to.');
129     $this->assertEqual(AccessResult::neutral("The '$disallowed_format_name' permission is required.")->cachePerPermissions(), $this->disallowedFormat->access('use', $this->webUser, TRUE), 'A regular user does not have access to use a text format they were not granted access to.');
130     $this->assertTrue($fallback_format->access('use', $this->webUser), 'A regular user has access to use the fallback format.');
131     $this->assertEqual(AccessResult::allowed(), $fallback_format->access('use', $this->webUser, TRUE), 'A regular user has access to use the fallback format.');
132
133     // Perform similar checks as above, but now against the entire list of
134     // available formats for this user.
135     $this->assertTrue(in_array($this->allowedFormat->id(), array_keys(filter_formats($this->webUser))), 'The allowed format appears in the list of available formats for a regular user.');
136     $this->assertFalse(in_array($this->disallowedFormat->id(), array_keys(filter_formats($this->webUser))), 'The disallowed format does not appear in the list of available formats for a regular user.');
137     $this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_formats($this->webUser))), 'The fallback format appears in the list of available formats for a regular user.');
138
139     // Make sure that a regular user only has permission to use the format
140     // they were granted access to.
141     $this->assertTrue($this->webUser->hasPermission($this->allowedFormat->getPermissionName()), 'A regular user has permission to use the allowed text format.');
142     $this->assertFalse($this->webUser->hasPermission($this->disallowedFormat->getPermissionName()), 'A regular user does not have permission to use the disallowed text format.');
143
144     // Make sure that the allowed format appears on the node form and that
145     // the disallowed format does not.
146     $this->drupalLogin($this->webUser);
147     $this->drupalGet('node/add/page');
148     $elements = $this->xpath('//select[@name=:name]/option', [
149       ':name' => 'body[0][format]',
150       ':option' => $this->allowedFormat->id(),
151     ]);
152     $options = [];
153     foreach ($elements as $element) {
154       $options[$element->getValue()] = $element;
155     }
156     $this->assertTrue(isset($options[$this->allowedFormat->id()]), 'The allowed text format appears as an option when adding a new node.');
157     $this->assertFalse(isset($options[$this->disallowedFormat->id()]), 'The disallowed text format does not appear as an option when adding a new node.');
158     $this->assertFalse(isset($options[filter_fallback_format()]), 'The fallback format does not appear as an option when adding a new node.');
159
160     // Check regular user access to the filter tips pages.
161     $this->drupalGet('filter/tips/' . $this->allowedFormat->id());
162     $this->assertResponse(200);
163     $this->drupalGet('filter/tips/' . $this->disallowedFormat->id());
164     $this->assertResponse(403);
165     $this->drupalGet('filter/tips/' . filter_fallback_format());
166     $this->assertResponse(200);
167     $this->drupalGet('filter/tips/invalid-format');
168     $this->assertResponse(404);
169
170     // Check admin user access to the filter tips pages.
171     $this->drupalLogin($this->adminUser);
172     $this->drupalGet('filter/tips/' . $this->allowedFormat->id());
173     $this->assertResponse(200);
174     $this->drupalGet('filter/tips/' . $this->disallowedFormat->id());
175     $this->assertResponse(200);
176     $this->drupalGet('filter/tips/' . filter_fallback_format());
177     $this->assertResponse(200);
178     $this->drupalGet('filter/tips/invalid-format');
179     $this->assertResponse(404);
180   }
181
182   /**
183    * Tests if text format is available to a role.
184    */
185   public function testFormatRoles() {
186     // Get the role ID assigned to the regular user.
187     $roles = $this->webUser->getRoles(TRUE);
188     $rid = $roles[0];
189
190     // Check that this role appears in the list of roles that have access to an
191     // allowed text format, but does not appear in the list of roles that have
192     // access to a disallowed text format.
193     $this->assertTrue(in_array($rid, array_keys(filter_get_roles_by_format($this->allowedFormat))), 'A role which has access to a text format appears in the list of roles that have access to that format.');
194     $this->assertFalse(in_array($rid, array_keys(filter_get_roles_by_format($this->disallowedFormat))), 'A role which does not have access to a text format does not appear in the list of roles that have access to that format.');
195
196     // Check that the correct text format appears in the list of formats
197     // available to that role.
198     $this->assertTrue(in_array($this->allowedFormat->id(), array_keys(filter_get_formats_by_role($rid))), 'A text format which a role has access to appears in the list of formats available to that role.');
199     $this->assertFalse(in_array($this->disallowedFormat->id(), array_keys(filter_get_formats_by_role($rid))), 'A text format which a role does not have access to does not appear in the list of formats available to that role.');
200
201     // Check that the fallback format is always allowed.
202     $this->assertEqual(filter_get_roles_by_format(FilterFormat::load(filter_fallback_format())), user_role_names(), 'All roles have access to the fallback format.');
203     $this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_get_formats_by_role($rid))), 'The fallback format appears in the list of allowed formats for any role.');
204   }
205
206   /**
207    * Tests editing a page using a disallowed text format.
208    *
209    * Verifies that regular users and administrators are able to edit a page, but
210    * not allowed to change the fields which use an inaccessible text format.
211    * Also verifies that fields which use a text format that does not exist can
212    * be edited by administrators only, but that the administrator is forced to
213    * choose a new format before saving the page.
214    */
215   public function testFormatWidgetPermissions() {
216     $body_value_key = 'body[0][value]';
217     $body_format_key = 'body[0][format]';
218
219     // Create node to edit.
220     $this->drupalLogin($this->adminUser);
221     $edit = [];
222     $edit['title[0][value]'] = $this->randomMachineName(8);
223     $edit[$body_value_key] = $this->randomMachineName(16);
224     $edit[$body_format_key] = $this->disallowedFormat->id();
225     $this->drupalPostForm('node/add/page', $edit, t('Save'));
226     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
227
228     // Try to edit with a less privileged user.
229     $this->drupalLogin($this->webUser);
230     $this->drupalGet('node/' . $node->id());
231     $this->clickLink(t('Edit'));
232
233     // Verify that body field is read-only and contains replacement value.
234     $this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
235
236     // Verify that title can be changed, but preview displays original body.
237     $new_edit = [];
238     $new_edit['title[0][value]'] = $this->randomMachineName(8);
239     $this->drupalPostForm(NULL, $new_edit, t('Preview'));
240     $this->assertText($edit[$body_value_key], 'Old body found in preview.');
241
242     // Save and verify that only the title was changed.
243     $this->drupalPostForm('node/' . $node->id() . '/edit', $new_edit, t('Save'));
244     $this->assertNoText($edit['title[0][value]'], 'Old title not found.');
245     $this->assertText($new_edit['title[0][value]'], 'New title found.');
246     $this->assertText($edit[$body_value_key], 'Old body found.');
247
248     // Check that even an administrator with "administer filters" permission
249     // cannot edit the body field if they do not have specific permission to
250     // use its stored format. (This must be disallowed so that the
251     // administrator is never forced to switch the text format to something
252     // else.)
253     $this->drupalLogin($this->filterAdminUser);
254     $this->drupalGet('node/' . $node->id() . '/edit');
255     $this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
256
257     // Disable the text format used above.
258     $this->disallowedFormat->disable()->save();
259     $this->resetFilterCaches();
260
261     // Log back in as the less privileged user and verify that the body field
262     // is still disabled, since the less privileged user should not be able to
263     // edit content that does not have an assigned format.
264     $this->drupalLogin($this->webUser);
265     $this->drupalGet('node/' . $node->id() . '/edit');
266     $this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
267
268     // Log back in as the filter administrator and verify that the body field
269     // can be edited.
270     $this->drupalLogin($this->filterAdminUser);
271     $this->drupalGet('node/' . $node->id() . '/edit');
272     $this->assertNoFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", NULL, 'Text format access denied message not found.');
273     $this->assertFieldByXPath("//select[@name='$body_format_key']", NULL, 'Text format selector found.');
274
275     // Verify that trying to save the node without selecting a new text format
276     // produces an error message, and does not result in the node being saved.
277     $old_title = $new_edit['title[0][value]'];
278     $new_title = $this->randomMachineName(8);
279     $edit = [];
280     $edit['title[0][value]'] = $new_title;
281     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
282     $this->assertText(t('@name field is required.', ['@name' => t('Text format')]), 'Error message is displayed.');
283     $this->drupalGet('node/' . $node->id());
284     $this->assertText($old_title, 'Old title found.');
285     $this->assertNoText($new_title, 'New title not found.');
286
287     // Now select a new text format and make sure the node can be saved.
288     $edit[$body_format_key] = filter_fallback_format();
289     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
290     $this->assertUrl('node/' . $node->id());
291     $this->assertText($new_title, 'New title found.');
292     $this->assertNoText($old_title, 'Old title not found.');
293
294     // Switch the text format to a new one, then disable that format and all
295     // other formats on the site (leaving only the fallback format).
296     $this->drupalLogin($this->adminUser);
297     $edit = [$body_format_key => $this->allowedFormat->id()];
298     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
299     $this->assertUrl('node/' . $node->id());
300     foreach (filter_formats() as $format) {
301       if (!$format->isFallbackFormat()) {
302         $format->disable()->save();
303       }
304     }
305
306     // Since there is now only one available text format, the widget for
307     // selecting a text format would normally not display when the content is
308     // edited. However, we need to verify that the filter administrator still
309     // is forced to make a conscious choice to reassign the text to a different
310     // format.
311     $this->drupalLogin($this->filterAdminUser);
312     $old_title = $new_title;
313     $new_title = $this->randomMachineName(8);
314     $edit = [];
315     $edit['title[0][value]'] = $new_title;
316     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
317     $this->assertText(t('@name field is required.', ['@name' => t('Text format')]), 'Error message is displayed.');
318     $this->drupalGet('node/' . $node->id());
319     $this->assertText($old_title, 'Old title found.');
320     $this->assertNoText($new_title, 'New title not found.');
321     $edit[$body_format_key] = filter_fallback_format();
322     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
323     $this->assertUrl('node/' . $node->id());
324     $this->assertText($new_title, 'New title found.');
325     $this->assertNoText($old_title, 'Old title not found.');
326   }
327
328   /**
329    * Rebuilds text format and permission caches in the thread running the tests.
330    */
331   protected function resetFilterCaches() {
332     filter_formats_reset();
333   }
334
335 }