3 namespace Drupal\Tests\filter\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\filter\Entity\FilterFormat;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests access to text formats.
16 class FilterFormatAccessTest extends BrowserTestBase {
23 public static $modules = ['block', 'filter', 'node'];
26 * A user with administrative permissions.
28 * @var \Drupal\user\UserInterface
33 * A user with 'administer filters' permission.
35 * @var \Drupal\user\UserInterface
37 protected $filterAdminUser;
40 * A user with permission to create and edit own content.
42 * @var \Drupal\user\UserInterface
47 * An object representing an allowed text format.
51 protected $allowedFormat;
54 * An object representing a secondary allowed text format.
58 protected $secondAllowedFormat;
61 * An object representing a disallowed text format.
65 protected $disallowedFormat;
67 protected function setUp() {
70 $this->drupalPlaceBlock('page_title_block');
72 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
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([
78 'create page content',
79 'edit any page content',
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);
86 for ($i = 0; $i < 3; $i++) {
88 'format' => Unicode::strtolower($this->randomMachineName()),
89 'name' => $this->randomMachineName(),
91 $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
92 $this->resetFilterCaches();
93 $formats[] = FilterFormat::load($edit['format']);
95 list($this->allowedFormat, $this->secondAllowedFormat, $this->disallowedFormat) = $formats;
96 $this->drupalLogout();
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(),
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(),
115 $this->drupalPlaceBlock('local_tasks_block');
119 * Tests the Filter format access permissions functionality.
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.');
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.');
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.');
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(),
153 foreach ($elements as $element) {
154 $options[$element->getValue()] = $element;
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.');
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);
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);
183 * Tests if text format is available to a role.
185 public function testFormatRoles() {
186 // Get the role ID assigned to the regular user.
187 $roles = $this->webUser->getRoles(TRUE);
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.');
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.');
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.');
207 * Tests editing a page using a disallowed text format.
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.
215 public function testFormatWidgetPermissions() {
216 $body_value_key = 'body[0][value]';
217 $body_format_key = 'body[0][format]';
219 // Create node to edit.
220 $this->drupalLogin($this->adminUser);
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]']);
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'));
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.');
236 // Verify that title can be changed, but preview displays original body.
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.');
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.');
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
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.');
257 // Disable the text format used above.
258 $this->disallowedFormat->disable()->save();
259 $this->resetFilterCaches();
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.');
268 // Log back in as the filter administrator and verify that the body field
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.');
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);
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.');
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.');
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();
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
311 $this->drupalLogin($this->filterAdminUser);
312 $old_title = $new_title;
313 $new_title = $this->randomMachineName(8);
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.');
329 * Rebuilds text format and permission caches in the thread running the tests.
331 protected function resetFilterCaches() {
332 filter_formats_reset();