View source
<?php
namespace Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Kernel\MediaEmbedFilterTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class DrupalMediaEmbedCollectorTest extends MediaEmbedFilterTestBase {
use NodeCreationTrait, ContentTypeCreationTrait;
const EMBEDDED_ENTITY_UUID_2 = 'f3548e06-eb82-4c04-8499-3eb886da8f34';
private $calculator;
protected static $modules = [
'node',
'system',
'field',
'filter',
'text',
'user',
'depcalc',
'media',
'path_alias',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('path_alias');
$this
->installEntitySchema('node');
$this
->installConfig('node');
$this
->installSchema('node', 'node_access');
$this
->createContentType([
'type' => 'page',
]);
Media::create([
'uuid' => static::EMBEDDED_ENTITY_UUID_2,
'bundle' => 'image',
'name' => 'Another media',
])
->save();
$this->calculator = \Drupal::service('entity.dependency.calculator');
}
public function testExtractEmbeddedMediaEntities(array $embed_attributes) {
$embed_code = '';
foreach ($embed_attributes as $embed_attribute) {
$embed_code .= $this
->createEmbedCode($embed_attribute);
}
$node = $this
->createNode([
'body' => [
[
'value' => $embed_code,
'format' => filter_default_format(),
],
],
]);
try {
$wrapper = new DependentEntityWrapper($node);
} catch (\Exception $exception) {
$this
->markTestIncomplete($exception
->getMessage());
}
$dependencies = $this->calculator
->calculateDependencies($wrapper, new DependencyStack());
foreach ($embed_attributes as $embed_attribute) {
$this
->assertArrayHasKey($embed_attribute['data-entity-uuid'], $dependencies);
}
}
public function providerTestExtractEmbeddedMediaEntities() {
return [
'embed_media' => [
[
[
'data-entity-type' => 'media',
'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID,
],
],
],
'embed_multiple_media' => [
[
[
'data-entity-type' => 'media',
'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID,
],
[
'data-entity-type' => 'media',
'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID_2,
],
],
],
];
}
}