function FilterAPITest::testFilterFormatAPI
Same name in other branches
- 9 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testFilterFormatAPI()
- 8.9.x core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testFilterFormatAPI()
- 10 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testFilterFormatAPI()
Tests that HTML restrictions and filter types are correct.
@covers \Drupal\filter\Entity\FilterFormat::getHtmlRestrictions @covers \Drupal\filter\Entity\FilterFormat::getFilterTypes
File
-
core/
modules/ filter/ tests/ src/ Kernel/ FilterAPITest.php, line 95
Class
- FilterAPITest
- Tests the behavior of the API of the Filter module.
Namespace
Drupal\Tests\filter\KernelCode
public function testFilterFormatAPI() : void {
// 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' => [
'*' => [
'style' => FALSE,
'on*' => FALSE,
'lang' => TRUE,
'dir' => [
'ltr' => TRUE,
'rtl' => TRUE,
],
],
],
], '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-* *"> <e *>',
],
],
],
]);
$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,
],
],
'e' => [
'*' => 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.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.