function OEmbedFormatterTest::testRender

Same name in other branches
  1. 9 core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php \Drupal\Tests\media\Functional\FieldFormatter\OEmbedFormatterTest::testRender()
  2. 8.9.x 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.

bool $self_closing: Indicator if the HTML element is self closing i.e. <p/> vs <p></p>.

File

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

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, bool $self_closing) : void {
    $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);
        if ($self_closing) {
            self::assertStringContainsString("</{$selector}", $element->getParent()
                ->getHtml());
        }
        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.