You are here

public function FilterFormatAccessTest::testFormatPermissions in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php \Drupal\Tests\filter\Functional\FilterFormatAccessTest::testFormatPermissions()

Tests the Filter format access permissions functionality.

File

core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php, line 125

Class

FilterFormatAccessTest
Tests access to text formats.

Namespace

Drupal\Tests\filter\Functional

Code

public function testFormatPermissions() {

  // Make sure that a regular user only has access to the text formats for
  // which they were granted access.
  $fallback_format = FilterFormat::load(filter_fallback_format());
  $disallowed_format_name = $this->disallowedFormat
    ->getPermissionName();
  $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("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.');
  $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
    ->assertContains($this->allowedFormat
    ->id(), array_keys(filter_formats($this->webUser)), 'The allowed format appears in the list of available formats for a regular user.');
  $this
    ->assertNotContains($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
    ->assertContains(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', [
    ':name' => 'body[0][format]',
    ':option' => $this->allowedFormat
      ->id(),
  ]);
  $options = [];
  foreach ($elements as $element) {
    $options[$element
      ->getValue()] = $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
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('filter/tips/' . $this->disallowedFormat
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalGet('filter/tips/' . filter_fallback_format());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('filter/tips/invalid-format');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Check admin user access to the filter tips pages.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('filter/tips/' . $this->allowedFormat
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('filter/tips/' . $this->disallowedFormat
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('filter/tips/' . filter_fallback_format());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('filter/tips/invalid-format');
  $this
    ->assertSession()
    ->statusCodeEquals(404);
}