function FilterFormatAccessTest::testFormatPermissions in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/filter/src/Tests/FilterFormatAccessTest.php \Drupal\filter\Tests\FilterFormatAccessTest::testFormatPermissions()
Tests the Filter format access permissions functionality.
File
- core/
modules/ filter/ src/ Tests/ FilterFormatAccessTest.php, line 125 - Contains \Drupal\filter\Tests\FilterFormatAccessTest.
Class
- FilterFormatAccessTest
- Tests access to text formats.
Namespace
Drupal\filter\TestsCode
function testFormatPermissions() {
// Make sure that a regular user only has access to the text formats for
// which they were granted access.
$fallback_format = entity_load('filter_format', filter_fallback_format());
$this
->assertTrue($this->allowedFormat
->access('use', $this->webUser), 'A regular user has access to use a text format they were granted access to.');
$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.');
$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.');
$this
->assertEqual(AccessResult::neutral()
->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.');
$this
->assertTrue($fallback_format
->access('use', $this->webUser), 'A regular user has access to use the fallback format.');
$this
->assertEqual(AccessResult::allowed(), $fallback_format
->access('use', $this->webUser, TRUE), 'A regular user has access to use the fallback format.');
// Perform similar checks as above, but now against the entire list of
// available formats for this user.
$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.');
$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.');
$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.');
// Make sure that a regular user only has permission to use the format
// they were granted access to.
$this
->assertTrue($this->webUser
->hasPermission($this->allowedFormat
->getPermissionName()), 'A regular user has permission to use the allowed text format.');
$this
->assertFalse($this->webUser
->hasPermission($this->disallowedFormat
->getPermissionName()), 'A regular user does not have permission to use the disallowed text format.');
// Make sure that the allowed format appears on the node form and that
// the disallowed format does not.
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/page');
$elements = $this
->xpath('//select[@name=:name]/option', array(
':name' => 'body[0][format]',
':option' => $this->allowedFormat
->id(),
));
$options = array();
foreach ($elements as $element) {
$options[(string) $element['value']] = $element;
}
$this
->assertTrue(isset($options[$this->allowedFormat
->id()]), 'The allowed text format appears as an option when adding a new node.');
$this
->assertFalse(isset($options[$this->disallowedFormat
->id()]), 'The disallowed text format does not appear as an option when adding a new node.');
$this
->assertFalse(isset($options[filter_fallback_format()]), 'The fallback format does not appear as an option when adding a new node.');
// Check regular user access to the filter tips pages.
$this
->drupalGet('filter/tips/' . $this->allowedFormat
->id());
$this
->assertResponse(200);
$this
->drupalGet('filter/tips/' . $this->disallowedFormat
->id());
$this
->assertResponse(403);
$this
->drupalGet('filter/tips/' . filter_fallback_format());
$this
->assertResponse(200);
$this
->drupalGet('filter/tips/invalid-format');
$this
->assertResponse(404);
// Check admin user access to the filter tips pages.
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('filter/tips/' . $this->allowedFormat
->id());
$this
->assertResponse(200);
$this
->drupalGet('filter/tips/' . $this->disallowedFormat
->id());
$this
->assertResponse(200);
$this
->drupalGet('filter/tips/' . filter_fallback_format());
$this
->assertResponse(200);
$this
->drupalGet('filter/tips/invalid-format');
$this
->assertResponse(404);
}