You are here

public function OEmbedFormatterTest::testRender in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest::testRender()
  2. 9 core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest::testRender()

Tests the oEmbed field formatter.

@dataProvider providerRender

Parameters

string $url: The canonical URL of the media asset to test.

string $resource_url: The oEmbed resource URL of the media asset to test.

array $formatter_settings: Settings for the oEmbed field formatter.

array $selectors: An array of arrays. Each key is a CSS selector targeting an element in the rendered output, and each value is an array of attributes, keyed by name, that the element is expected to have.

File

core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php, line 172

Class

OEmbedFormatterTest
@covers \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter

Namespace

Drupal\Tests\media\Functional\FieldFormatter

Code

public function testRender($url, $resource_url, array $formatter_settings, array $selectors) {
  $account = $this
    ->drupalCreateUser([
    'view media',
  ]);
  $this
    ->drupalLogin($account);
  $media_type = $this
    ->createMediaType('oembed:video');
  $source = $media_type
    ->getSource();
  $source_field = $source
    ->getSourceFieldDefinition($media_type);
  EntityViewDisplay::create([
    'targetEntityType' => 'media',
    'bundle' => $media_type
      ->id(),
    'mode' => 'full',
    'status' => TRUE,
  ])
    ->removeComponent('thumbnail')
    ->setComponent($source_field
    ->getName(), [
    'type' => 'oembed',
    'settings' => $formatter_settings,
  ])
    ->save();
  $this
    ->hijackProviderEndpoints();
  ResourceController::setResourceUrl($url, $this
    ->getFixturesDirectory() . '/' . $resource_url);
  UrlResolver::setEndpointUrl($url, $resource_url);
  $entity = Media::create([
    'bundle' => $media_type
      ->id(),
    $source_field
      ->getName() => $url,
  ]);
  $entity
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl());
  $assert = $this
    ->assertSession();
  $assert
    ->statusCodeEquals(200);
  foreach ($selectors as $selector => $attributes) {
    $element = $assert
      ->elementExists('css', $selector);
    foreach ($attributes as $attribute => $value) {
      if (isset($value)) {
        $this
          ->assertStringContainsString($value, $element
          ->getAttribute($attribute));
      }
      else {
        $this
          ->assertFalse($element
          ->hasAttribute($attribute));
      }
    }
  }
}