function OEmbedFormatterTest::testRender

Same name and namespace in other branches
  1. 8.9.x core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest::testRender()
  2. 10 core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest::testRender()
  3. 11.x 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 <a href="/api/drupal/core%21modules%21media%21src%21Plugin%21Field%21FieldFormatter%21OEmbedFormatter.php/class/OEmbedFormatter/9" title="Plugin implementation of the &#039;oembed&#039; formatter." class="local">\Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter</a>

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));
            }
        }
    }
}

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