You are here

public function SubstitutionPluginTest::testMediaSubstitution in Linkit 8.5

Test the media substitution.

File

tests/src/Kernel/SubstitutionPluginTest.php, line 140

Class

SubstitutionPluginTest
Tests the substitution plugins.

Namespace

Drupal\Tests\linkit\Kernel

Code

public function testMediaSubstitution() {

  // Set up media bundle and fields.
  $media_type = MediaType::create([
    'label' => 'test',
    'id' => 'test',
    'description' => 'Test type.',
    'source' => 'file',
  ]);
  $media_type
    ->save();
  $source_field = $media_type
    ->getSource()
    ->createSourceField($media_type);
  $source_field
    ->getFieldStorageDefinition()
    ->save();
  $source_field
    ->save();
  $media_type
    ->set('source_configuration', [
    'source_field' => $source_field
      ->getName(),
  ])
    ->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',
    $source_field
      ->getName() => [
      'target_id' => $file
        ->id(),
    ],
  ]);
  $media
    ->save();
  $media_substitution = $this->substitutionManager
    ->createInstance('media');
  $expected = $GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/druplicon.txt';
  $this
    ->assertEquals($expected, $media_substitution
    ->getUrl($media)
    ->getGeneratedUrl());

  // Ensure the url is identical when media entities have a standalone URL
  // enabled.
  \Drupal::configFactory()
    ->getEditable('media.settings')
    ->set('standalone_url', TRUE)
    ->save();
  $this
    ->assertEquals($expected, $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.');
}