View source
<?php
namespace Drupal\Tests\media\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\file\Entity\File;
use Drupal\filter\FilterPluginCollection;
use Drupal\filter\FilterProcessResult;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
abstract class MediaEmbedFilterTestBase extends KernelTestBase {
use MediaTypeCreationTrait;
use TestFileCreationTrait;
use UserCreationTrait {
createUser as drupalCreateUser;
createRole as drupalCreateRole;
}
const EMBEDDED_ENTITY_UUID = 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63';
protected static $modules = [
'field',
'file',
'filter',
'image',
'media',
'system',
'text',
'user',
];
protected $image;
protected $embeddedEntity;
protected function setUp() {
parent::setUp();
$this
->installSchema('file', [
'file_usage',
]);
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('file');
$this
->installEntitySchema('media');
$this
->installEntitySchema('user');
$this
->installConfig('filter');
$this
->installConfig('image');
$this
->installConfig('media');
$this
->installConfig('system');
$admin_user = $this
->drupalCreateUser([]);
$user = $this
->drupalCreateUser([
'access content',
'view media',
]);
$this->container
->set('current_user', $user);
$this->image = File::create([
'uri' => $this
->getTestFiles('image')[0]->uri,
'uid' => 2,
]);
$this->image
->setPermanent();
$this->image
->save();
$media_type = $this
->createMediaType('image', [
'id' => 'image',
]);
EntityViewMode::create([
'id' => 'media.foobar',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => $this
->randomMachineName(),
])
->save();
EntityViewDisplay::create([
'targetEntityType' => 'media',
'bundle' => $media_type
->id(),
'mode' => 'foobar',
'status' => TRUE,
])
->removeComponent('thumbnail')
->removeComponent('created')
->removeComponent('uid')
->setComponent('field_media_image', [
'label' => 'visually_hidden',
'type' => 'image',
'settings' => [
'image_style' => 'medium',
'image_link' => 'file',
],
'third_party_settings' => [],
'weight' => 1,
'region' => 'content',
])
->save();
$media = Media::create([
'uuid' => static::EMBEDDED_ENTITY_UUID,
'bundle' => 'image',
'name' => 'Screaming hairy armadillo',
'field_media_image' => [
[
'target_id' => $this->image
->id(),
'alt' => 'default alt',
'title' => 'default title',
],
],
])
->setOwner($user);
$media
->save();
$this->embeddedEntity = $media;
}
protected function createEmbedCode(array $attributes) {
$dom = Html::load('<drupal-media>This placeholder should not be rendered.</drupal-media>');
$xpath = new \DOMXPath($dom);
$drupal_entity = $xpath
->query('//drupal-media')[0];
foreach ($attributes as $attribute => $value) {
$drupal_entity
->setAttribute($attribute, $value);
}
return Html::serialize($dom);
}
protected function applyFilter($text, $langcode = 'en') {
$this
->assertStringContainsString('<drupal-media', $text);
$this
->assertStringContainsString('This placeholder should not be rendered.', $text);
$filter_result = $this
->processText($text, $langcode);
$output = $filter_result
->getProcessedText();
$this
->assertStringNotContainsString('<drupal-media', $output);
$this
->assertStringNotContainsString('This placeholder should not be rendered.', $output);
$this
->setRawContent($output);
return $filter_result;
}
protected function assertHasAttributes(\SimpleXMLElement $element, array $expected_attributes) {
foreach ($expected_attributes as $attribute => $value) {
if ($value === NULL) {
$this
->assertNull($element[$attribute]);
}
else {
$this
->assertSame((string) $value, (string) $element[$attribute]);
}
}
}
protected function processText($text, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, array $filter_ids = [
'media_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;
}
}