function FilterFormatAccessTest::testFormatPermissions

Same name in other branches
  1. 9 core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php \Drupal\Tests\filter\Functional\FilterFormatAccessTest::testFormatPermissions()
  2. 8.9.x core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php \Drupal\Tests\filter\Functional\FilterFormatAccessTest::testFormatPermissions()
  3. 10 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 124

Class

FilterFormatAccessTest
Tests access to text formats.

Namespace

Drupal\Tests\filter\Functional

Code

public function testFormatPermissions() : void {
    // 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->assertEquals(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->assertEquals(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->assertEquals(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 and fallback format do not.
    $this->drupalLogin($this->webUser);
    $this->drupalGet('node/add/page');
    $this->assertSession()
        ->optionExists('body[0][format]', $this->allowedFormat
        ->id());
    $this->assertSession()
        ->optionNotExists('body[0][format]', $this->disallowedFormat
        ->id());
    $this->assertSession()
        ->optionNotExists('body[0][format]', filter_fallback_format());
    // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.