public function FieldApiTest::testTeaserFilter in Field Formatter Filter 2.0.x
Same name and namespace in other branches
- 8 tests/src/Kernel/FieldApiTest.php \Drupal\Tests\field_formatter_filter\Kernel\FieldApiTest::testTeaserFilter()
Test enabling the filter formatter. Check before, during and after.
File
- tests/
src/ Kernel/ FieldApiTest.php, line 66
Class
- FieldApiTest
- Tests applying the filter formatter to a node.
Namespace
Drupal\Tests\field_formatter_filter\KernelCode
public function testTeaserFilter() {
$entity_type = 'node';
$bundle = 'fff_article';
$view_mode = 'teaser';
$this
->createContentType([
'type' => $bundle,
]);
$node = $this
->createTestNode($bundle);
// Verify that rendering the teaser normally shows unwanted text.
$build = $this->container
->get('entity.manager')
->getViewBuilder($entity_type)
->view($node, $view_mode);
$output = \Drupal::service('renderer')
->renderRoot($build);
$this
->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Teaser view of node contains expected markup');
$this
->assertTrue((bool) preg_match("/<img/", $output), 'Teaser view of node contains messy markup');
// Now enable the module.
$this->container
->get('module_installer')
->install([
'field_formatter_filter',
], TRUE);
// Re-check that all is well, issue #2868519 implies it may damage normal
// display.
$build = $this->container
->get('entity.manager')
->getViewBuilder($entity_type)
->view($node, $view_mode);
$output = \Drupal::service('renderer')
->renderRoot($build);
$this
->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Teaser view of node contains expected markup');
// Now edit the teaser view mode settings to use our safe markup filter.
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
$display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load("{$entity_type}.{$bundle}.{$view_mode}");
$component = $display
->getComponent('body');
// Adjust the body field formatter component and save the display.
/*
$component = [
'type' => 'text_summary_or_trimmed',
'label' => 'hidden',
'weight' => 10,
'settings' => [
'trim_length' => 600,
],
'third_party_settings' => [
'field_formatter_filter' => [
'format' => 'teaser_safe_text',
],
],
];
*/
$component['third_party_settings']['field_formatter_filter']['format'] = 'teaser_safe_text';
$display
->setComponent('body', $component)
->save();
// Now re-render the teaser, and assert that the text has been sanitised.
// display.
$build = $this->container
->get('entity.manager')
->getViewBuilder($entity_type)
->view($node, $view_mode);
$output = \Drupal::service('renderer')
->renderRoot($build);
$this
->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Teaser view of node contains expected markup');
$this
->assertFalse((bool) preg_match("/<img/", $output), 'Teaser view of node does not contain messy markup');
// Normal mode of operations successfully tested.
// Now try to break it...
// Assign an invalid value to the formatter.
// As if the text format was deleted?
// May trigger "Error: Call to a member function filters() on null".
$component['third_party_settings']['field_formatter_filter']['format'] = 'invalid_text_format';
$display
->setComponent('body', $component)
->save();
$build = $this->container
->get('entity.manager')
->getViewBuilder($entity_type)
->view($node, $view_mode);
$output = \Drupal::service('renderer')
->renderRoot($build);
$this
->assertTrue((bool) preg_match("/the real content of the body text/", $output), 'Teaser view of node contains expected markup');
$this
->assertFalse((bool) preg_match("/<img/", $output), 'Teaser view of node does not contain messy markup');
}