function FilterAPITest::testFilterFormatAPI in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/filter/src/Tests/FilterAPITest.php \Drupal\filter\Tests\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/ src/ Tests/ FilterAPITest.php, line 105 - Contains \Drupal\filter\Tests\FilterAPITest.
Class
- FilterAPITest
- Tests the behavior of the API of the Filter module.
Namespace
Drupal\filter\TestsCode
function testFilterFormatAPI() {
// Test on filtered_html.
$filtered_html_format = entity_load('filter_format', 'filtered_html');
$this
->assertIdentical($filtered_html_format
->getHtmlRestrictions(), array(
'allowed' => array(
'p' => FALSE,
'br' => FALSE,
'strong' => FALSE,
'a' => array(
'href' => TRUE,
'hreflang' => TRUE,
),
'*' => array(
'style' => FALSE,
'on*' => FALSE,
'lang' => TRUE,
'dir' => array(
'ltr' => TRUE,
'rtl' => TRUE,
),
),
),
), 'FilterFormatInterface::getHtmlRestrictions() works as expected for the filtered_html format.');
$this
->assertIdentical($filtered_html_format
->getFilterTypes(), array(
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 = entity_load('filter_format', 'full_html');
$this
->assertIdentical($full_html_format
->getHtmlRestrictions(), FALSE, 'FilterFormatInterface::getHtmlRestrictions() works as expected for the full_html format.');
$this
->assertIdentical($full_html_format
->getFilterTypes(), array(), 'FilterFormatInterface::getFilterTypes() works as expected for the full_html format.');
// Test on stupid_filtered_html, where nothing is allowed.
$stupid_filtered_html_format = entity_create('filter_format', array(
'format' => 'stupid_filtered_html',
'name' => 'Stupid Filtered HTML',
'filters' => array(
'filter_html' => array(
'status' => 1,
'settings' => array(
'allowed_html' => '',
),
),
),
));
$stupid_filtered_html_format
->save();
$this
->assertIdentical($stupid_filtered_html_format
->getHtmlRestrictions(), array(
'allowed' => array(),
), 'FilterFormatInterface::getHtmlRestrictions() works as expected for the stupid_filtered_html format.');
$this
->assertIdentical($stupid_filtered_html_format
->getFilterTypes(), array(
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 = entity_create('filter_format', array(
'format' => 'very_restricted_html',
'name' => 'Very Restricted HTML',
'filters' => array(
'filter_html' => array(
'status' => 1,
'settings' => array(
'allowed_html' => '<p> <br> <a href> <strong>',
),
),
'filter_test_restrict_tags_and_attributes' => array(
'status' => 1,
'settings' => array(
'restrictions' => array(
'allowed' => array(
'p' => TRUE,
'br' => FALSE,
'a' => array(
'href' => TRUE,
),
'em' => TRUE,
),
),
),
),
),
));
$very_restricted_html_format
->save();
$this
->assertIdentical($very_restricted_html_format
->getHtmlRestrictions(), array(
'allowed' => array(
'p' => FALSE,
'br' => FALSE,
'a' => array(
'href' => TRUE,
),
'*' => array(
'style' => FALSE,
'on*' => FALSE,
'lang' => TRUE,
'dir' => array(
'ltr' => TRUE,
'rtl' => TRUE,
),
),
),
), 'FilterFormatInterface::getHtmlRestrictions() works as expected for the very_restricted_html format.');
$this
->assertIdentical($very_restricted_html_format
->getFilterTypes(), array(
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 = \Drupal\filter\Entity\FilterFormat::create(array(
'format' => 'nonsensical_restricted_html',
'name' => 'Nonsensical Restricted HTML',
'filters' => array(
'filter_html' => array(
'status' => 1,
'settings' => array(
'allowed_html' => '<a> <b class> <c class="*"> <d class="foo bar-* *">',
),
),
),
));
$nonsensical_restricted_html
->save();
$this
->assertIdentical($nonsensical_restricted_html
->getHtmlRestrictions(), array(
'allowed' => array(
'a' => FALSE,
'b' => array(
'class' => TRUE,
),
'c' => array(
'class' => TRUE,
),
'd' => array(
'class' => array(
'foo' => TRUE,
'bar-*' => TRUE,
),
),
'*' => array(
'style' => FALSE,
'on*' => FALSE,
'lang' => TRUE,
'dir' => array(
'ltr' => TRUE,
'rtl' => TRUE,
),
),
),
), 'FilterFormatInterface::getHtmlRestrictions() works as expected for the nonsensical_restricted_html format.');
$this
->assertIdentical($very_restricted_html_format
->getFilterTypes(), array(
FilterInterface::TYPE_HTML_RESTRICTOR,
), 'FilterFormatInterface::getFilterTypes() works as expected for the very_restricted_html format.');
}