summaryrefslogtreecommitdiffstats
path: root/tests/src/Kernel/SubstitutionPluginTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/src/Kernel/SubstitutionPluginTest.php')
-rw-r--r--tests/src/Kernel/SubstitutionPluginTest.php66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/src/Kernel/SubstitutionPluginTest.php b/tests/src/Kernel/SubstitutionPluginTest.php
index 2ec483e..12a182d 100644
--- a/tests/src/Kernel/SubstitutionPluginTest.php
+++ b/tests/src/Kernel/SubstitutionPluginTest.php
@@ -3,14 +3,21 @@
namespace Drupal\Tests\linkit\Kernel;
use Drupal\entity_test\Entity\EntityTest;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\linkit\Plugin\Linkit\Substitution\Canonical as CanonicalSubstitutionPlugin;
use Drupal\linkit\Plugin\Linkit\Substitution\File as FileSubstitutionPlugin;
+use Drupal\linkit\Plugin\Linkit\Substitution\Media as MediaSubstitutionPlugin;
+use Drupal\media_entity\Entity\Media;
+use Drupal\media_entity\Entity\MediaBundle;
/**
* Tests the substitution plugins.
*
* @group linkit
+ *
+ * @requires module media_entity
*/
class SubstitutionPluginTest extends LinkitKernelTestBase {
@@ -36,6 +43,10 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
public static $modules = [
'file',
'entity_test',
+ 'media_entity',
+ 'image',
+ 'field',
+ 'linkit_media_test',
];
/**
@@ -48,6 +59,11 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$this->installEntitySchema('file');
$this->installEntitySchema('entity_test');
+ $this->installEntitySchema('media');
+ $this->installEntitySchema('media_bundle');
+ $this->installEntitySchema('field_storage_config');
+ $this->installEntitySchema('field_config');
+ $this->installSchema('file', ['file_usage']);
}
/**
@@ -88,4 +104,54 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$this->assertFalse(CanonicalSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is not applicable the canonical substitution.');
}
+ /**
+ * Test the media substitution.
+ */
+ public function testMediaSubstitution() {
+ // Set up media bundle and fields.
+ MediaBundle::create([
+ 'label' => 'test',
+ 'id' => 'test',
+ 'description' => 'test bundle.',
+ 'type' => 'test_type',
+ ])->save();
+ FieldStorageConfig::create([
+ 'field_name' => 'field_media_file',
+ 'entity_type' => 'media',
+ 'type' => 'file',
+ 'settings' => [],
+ ])->save();
+ FieldConfig::create([
+ 'entity_type' => 'media',
+ 'bundle' => 'test',
+ 'field_name' => 'field_media_file',
+ 'label' => 'Media field',
+ 'settings' => [
+ 'file_extensions' => 'txt',
+ ],
+ ])->save();
+ $file = File::create([
+ 'uid' => 1,
+ 'filename' => 'druplicon.txt',
+ 'uri' => 'public://druplicon.txt',
+ 'filemime' => 'text/plain',
+ 'status' => FILE_STATUS_PERMANENT,
+ ]);
+ $file->save();
+ $media = Media::create([
+ 'bundle' => 'test',
+ 'field_media_file' => ['target_id' => $file->id()],
+ ]);
+ $media->save();
+
+ $media_substitution = $this->substitutionManager->createInstance('media');
+ $this->assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/druplicon.txt', $media_substitution->getUrl($media)->getGeneratedUrl());
+
+ $entity_type = $this->entityTypeManager->getDefinition('media');
+ $this->assertTrue(MediaSubstitutionPlugin::isApplicable($entity_type), 'The entity type Media is applicable the media substitution.');
+
+ $entity_type = $this->entityTypeManager->getDefinition('file');
+ $this->assertFalse(MediaSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is not applicable the media substitution.');
+ }
+
}