View source
<?php
namespace Drupal\Tests\field_formatter_filter\Kernel;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\filter\Entity\FilterFormat;
class FieldApiTest extends EntityKernelTestBase {
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
public static $modules = [
'node',
'system',
'user',
'text',
'field_ui',
'field',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig([
'system',
'field',
'node',
'user',
]);
$this
->createTextFormats();
}
public function testCreateContentType() {
$this
->createContentType([
'type' => 'fff_article',
]);
}
public function testTeaserFilter() {
$entity_type = 'node';
$bundle = 'fff_article';
$view_mode = 'teaser';
$this
->createContentType([
'type' => $bundle,
]);
$node = $this
->createTestNode($bundle);
$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');
$this->container
->get('module_installer')
->install([
'field_formatter_filter',
], TRUE);
$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');
$display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load("{$entity_type}.{$bundle}.{$view_mode}");
$component = $display
->getComponent('body');
$component['third_party_settings']['field_formatter_filter']['format'] = 'teaser_safe_text';
$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');
$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');
}
public function createContentType(array $values = []) {
$content_type = $this
->drupalCreateContentType($values);
$content_type
->set('display_submitted', FALSE);
$content_type
->save();
}
public function xcreateContentType(array $values = []) {
$bundle = $values['type'];
$entity_type = 'node';
$type = NodeType::create([
'type' => $bundle,
]);
$type
->set('display_submitted', FALSE);
$type
->save();
node_add_body_field($type);
\Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load("{$entity_type}.{$bundle}.default")
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load("{$entity_type}.{$bundle}.default")
->save();
EntityViewDisplay::load("{$entity_type}.{$bundle}.teaser")
->save();
}
private function createTestNode($bundle) {
$path = __DIR__ . '/../..';
$body = file_get_contents($path . '/sample-markup.txt');
$node = Node::create([
'type' => $bundle,
'title' => 'Test this is filtered',
'uid' => 1,
'body' => [
'value' => $body,
'format' => 'full_html',
],
]);
$validated = $node
->validate();
$saved = $node
->save();
return $node;
}
private function createTextFormats() {
$format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$format
->save();
$format = FilterFormat::create([
'format' => 'teaser_safe_text',
'name' => 'Teaser safe text',
]);
$format
->setFilterConfig('filter_html', [
'status' => 1,
'settings' => [
'allowed_html' => '<p> <br> <a href hreflang> <em> <strong>',
],
]);
$format
->save();
}
}