View source
<?php
namespace Drupal\Tests\entity_embed\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\filter\FilterPluginCollection;
use Drupal\filter\FilterProcessResult;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
abstract class EntityEmbedFilterTestBase extends KernelTestBase {
use NodeCreationTrait {
createNode as drupalCreateNode;
}
use UserCreationTrait {
createUser as drupalCreateUser;
createRole as drupalCreateRole;
}
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
const EMBEDDED_ENTITY_UUID = 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63';
protected static $modules = [
'embed',
'entity_embed',
'field',
'filter',
'node',
'system',
'text',
'user',
];
protected $embeddedEntity;
protected function setUp() {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig('filter');
$this
->installConfig('node');
$admin_user = $this
->drupalCreateUser([]);
$user = $this
->drupalCreateUser([
'access content',
]);
$this->container
->set('current_user', $user);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->embeddedEntity = $this
->drupalCreateNode([
'title' => 'Embed Test Node',
'uuid' => static::EMBEDDED_ENTITY_UUID,
]);
}
protected function createEmbedCode(array $attributes) {
$dom = Html::load('<drupal-entity>This placeholder should not be rendered.</drupal-entity>');
$xpath = new \DOMXPath($dom);
$drupal_entity = $xpath
->query('//drupal-entity')[0];
foreach ($attributes as $attribute => $value) {
$drupal_entity
->setAttribute($attribute, $value);
}
return Html::serialize($dom);
}
protected function applyFilter($text, $langcode = 'en') {
$this
->assertStringContainsString('<drupal-entity', $text);
$this
->assertStringContainsString('This placeholder should not be rendered.', $text);
$filter_result = $this
->processText($text, $langcode);
$output = $filter_result
->getProcessedText();
$this
->assertStringNotContainsString('<drupal-entity', $output);
$this
->assertStringNotContainsString('This placeholder should not be rendered.', $output);
$this
->setRawContent($output);
return $filter_result;
}
protected function assertHasAttributes(\SimpleXMLElement $element, array $attributes) {
foreach ($attributes as $attribute => $value) {
$this
->assertSame((string) $value, (string) $element[$attribute]);
}
}
protected function processText($text, $langcode = 'und', array $filter_ids = [
'entity_embed',
]) {
$manager = $this->container
->get('plugin.manager.filter');
$bag = new FilterPluginCollection($manager, []);
$filters = [];
foreach ($filter_ids as $filter_id) {
$filters[] = $bag
->get($filter_id);
}
$render_context = new RenderContext();
$filter_result = $this->container
->get('renderer')
->executeInRenderContext($render_context, function () use ($text, $filters, $langcode) {
$metadata = new BubbleableMetadata();
foreach ($filters as $filter) {
$result = $filter
->process($text, $langcode);
$metadata = $metadata
->merge($result);
$text = $result
->getProcessedText();
}
return (new FilterProcessResult($text))
->merge($metadata);
});
if (!$render_context
->isEmpty()) {
$filter_result = $filter_result
->merge($render_context
->pop());
}
return $filter_result;
}
}