You are here

public function FilterAPITest::testFilterFormatAPI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testFilterFormatAPI()
  2. 10 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testFilterFormatAPI()

Tests the following functions for a variety of formats:

  • \Drupal\filter\Entity\FilterFormatInterface::getHtmlRestrictions()
  • \Drupal\filter\Entity\FilterFormatInterface::getFilterTypes()

File

core/modules/filter/tests/src/Kernel/FilterAPITest.php, line 87

Class

FilterAPITest
Tests the behavior of the API of the Filter module.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testFilterFormatAPI() {

  // Test on filtered_html.
  $filtered_html_format = FilterFormat::load('filtered_html');
  $this
    ->assertSame($filtered_html_format
    ->getHtmlRestrictions(), [
    'allowed' => [
      'p' => FALSE,
      'br' => FALSE,
      'strong' => FALSE,
      'a' => [
        'href' => TRUE,
        'hreflang' => TRUE,
      ],
      '*' => [
        'style' => FALSE,
        'on*' => FALSE,
        'lang' => TRUE,
        'dir' => [
          'ltr' => TRUE,
          'rtl' => TRUE,
        ],
      ],
    ],
  ], 'FilterFormatInterface::getHtmlRestrictions() works as expected for the filtered_html format.');
  $this
    ->assertSame($filtered_html_format
    ->getFilterTypes(), [
    FilterInterface::TYPE_HTML_RESTRICTOR,
    FilterInterface::TYPE_MARKUP_LANGUAGE,
  ], 'FilterFormatInterface::getFilterTypes() works as expected for the filtered_html format.');

  // Test on full_html.
  $full_html_format = FilterFormat::load('full_html');
  $this
    ->assertSame($full_html_format
    ->getHtmlRestrictions(), FALSE, 'FilterFormatInterface::getHtmlRestrictions() works as expected for the full_html format.');
  $this
    ->assertSame($full_html_format
    ->getFilterTypes(), [], 'FilterFormatInterface::getFilterTypes() works as expected for the full_html format.');

  // Test on stupid_filtered_html, where nothing is allowed.
  $stupid_filtered_html_format = FilterFormat::create([
    'format' => 'stupid_filtered_html',
    'name' => 'Stupid Filtered HTML',
    'filters' => [
      'filter_html' => [
        'status' => 1,
        'settings' => [
          // Nothing is allowed.
          'allowed_html' => '',
        ],
      ],
    ],
  ]);
  $stupid_filtered_html_format
    ->save();
  $this
    ->assertSame($stupid_filtered_html_format
    ->getHtmlRestrictions(), [
    'allowed' => [],
  ], 'FilterFormatInterface::getHtmlRestrictions() works as expected for the stupid_filtered_html format.');
  $this
    ->assertSame($stupid_filtered_html_format
    ->getFilterTypes(), [
    FilterInterface::TYPE_HTML_RESTRICTOR,
  ], 'FilterFormatInterface::getFilterTypes() works as expected for the stupid_filtered_html format.');

  // Test on very_restricted_html, where there's two different filters of the
  // FilterInterface::TYPE_HTML_RESTRICTOR type, each restricting in different ways.
  $very_restricted_html_format = FilterFormat::create([
    'format' => 'very_restricted_html',
    'name' => 'Very Restricted HTML',
    'filters' => [
      'filter_html' => [
        'status' => 1,
        'settings' => [
          'allowed_html' => '<p> <br> <a href> <strong>',
        ],
      ],
      'filter_test_restrict_tags_and_attributes' => [
        'status' => 1,
        'settings' => [
          'restrictions' => [
            'allowed' => [
              'p' => TRUE,
              'br' => FALSE,
              'a' => [
                'href' => TRUE,
              ],
              'em' => TRUE,
            ],
          ],
        ],
      ],
    ],
  ]);
  $very_restricted_html_format
    ->save();
  $this
    ->assertSame($very_restricted_html_format
    ->getHtmlRestrictions(), [
    'allowed' => [
      'p' => FALSE,
      'br' => FALSE,
      'a' => [
        'href' => TRUE,
      ],
      '*' => [
        'style' => FALSE,
        'on*' => FALSE,
        'lang' => TRUE,
        'dir' => [
          'ltr' => TRUE,
          'rtl' => TRUE,
        ],
      ],
    ],
  ], 'FilterFormatInterface::getHtmlRestrictions() works as expected for the very_restricted_html format.');
  $this
    ->assertSame($very_restricted_html_format
    ->getFilterTypes(), [
    FilterInterface::TYPE_HTML_RESTRICTOR,
  ], 'FilterFormatInterface::getFilterTypes() works as expected for the very_restricted_html format.');

  // Test on nonsensical_restricted_html, where the allowed attribute values
  // contain asterisks, which do not have any meaning, but which we also
  // cannot prevent because configuration can be modified outside of forms.
  $nonsensical_restricted_html = FilterFormat::create([
    'format' => 'nonsensical_restricted_html',
    'name' => 'Nonsensical Restricted HTML',
    'filters' => [
      'filter_html' => [
        'status' => 1,
        'settings' => [
          'allowed_html' => '<a> <b class> <c class="*"> <d class="foo bar-* *">',
        ],
      ],
    ],
  ]);
  $nonsensical_restricted_html
    ->save();
  $this
    ->assertSame($nonsensical_restricted_html
    ->getHtmlRestrictions(), [
    'allowed' => [
      'a' => FALSE,
      'b' => [
        'class' => TRUE,
      ],
      'c' => [
        'class' => TRUE,
      ],
      'd' => [
        'class' => [
          'foo' => TRUE,
          'bar-*' => TRUE,
        ],
      ],
      '*' => [
        'style' => FALSE,
        'on*' => FALSE,
        'lang' => TRUE,
        'dir' => [
          'ltr' => TRUE,
          'rtl' => TRUE,
        ],
      ],
    ],
  ], 'FilterFormatInterface::getHtmlRestrictions() works as expected for the nonsensical_restricted_html format.');
  $this
    ->assertSame($very_restricted_html_format
    ->getFilterTypes(), [
    FilterInterface::TYPE_HTML_RESTRICTOR,
  ], 'FilterFormatInterface::getFilterTypes() works as expected for the very_restricted_html format.');
}