function EntityLinksTest::testMediaEntity

@legacy-covers ::getUrl @legacy-covers \Drupal\media\Entity\MediaLinkTarget

Attributes

#[DataProvider('providerTestMediaEntity')]

Parameters

bool $standalone_url_setting: Whether the standalone_url setting is off (Drupal's default) or on.

string $media_source: Which media source to use.

array $media_entity_values: Which values to assign to the media entity.

string $expected_url: The expected URL.

string[] $expected_cache_tags: The expected cache tags.

File

core/modules/filter/tests/src/Kernel/EntityLinksTest.php, line 246

Class

EntityLinksTest
Tests the behavior of generating entity URLs when using entity links in CKEditor.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testMediaEntity(bool $standalone_url_setting, string $media_source, array $media_entity_values, string $expected_url, array $expected_cache_tags) : void {
  \Drupal::configFactory()->getEditable('media.settings')
    ->set('standalone_url', $standalone_url_setting)
    ->save();
  // Create media type using the given source plugin.
  $media_type = MediaType::create([
    'label' => 'test',
    'id' => 'test',
    'description' => 'Test type.',
    'source' => $media_source,
  ]);
  $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();
  // @see \Drupal\media\Plugin\media\Source\File
  if ($media_source === 'file') {
    $file = File::create([
      'uid' => 1,
      'filename' => 'druplicon.txt',
      'uri' => 'public://druplicon.txt',
      'filemime' => 'text/plain',
      'status' => FileInterface::STATUS_PERMANENT,
    ]);
    $file->save();
  }
  $media = Media::create([
    'bundle' => 'test',
    $source_field->getName() => $media_entity_values,
  ]);
  $media->save();
  $expected_url = str_replace('<SITE_DIRECTORY>', $this->siteDirectory, $expected_url);
  $this->assertFilterProcessResult(sprintf('<a data-entity-type="media" data-entity-uuid="%s" href="something?query=string#fragment">Link text</a>', $media->uuid()), 'en', (new FilterProcessResult())->setProcessedText(sprintf('<a href="%s?query=string#fragment">Link text</a>', $expected_url))
    ->setCacheTags($expected_cache_tags)
    ->setCacheContexts([])
    ->setCacheMaxAge(Cache::PERMANENT));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.