3 namespace Drupal\Tests\media\Functional\FieldFormatter;
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\media\Entity\Media;
7 use Drupal\media_test_oembed\Controller\ResourceController;
8 use Drupal\media_test_oembed\UrlResolver;
9 use Drupal\Tests\media\Functional\MediaFunctionalTestBase;
10 use Drupal\Tests\media\Traits\OEmbedTestTrait;
13 * @covers \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter
17 class OEmbedFormatterTest extends MediaFunctionalTestBase {
24 public static $modules = [
33 protected function setUp() {
35 $this->lockHttpClientToFixtures();
39 * Data provider for testRender().
45 public function providerRender() {
48 'https://vimeo.com/7073899',
53 'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
59 'Vimeo video, resized' => [
60 'https://vimeo.com/7073899',
61 'video_vimeo.json?maxwidth=100&maxheight=100',
62 ['max_width' => 100, 'max_height' => 100],
65 'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
72 'https://twitter.com/drupaldevdays/status/935643039741202432',
77 'src' => '/media/oembed?url=https%3A//twitter.com/drupaldevdays/status/935643039741202432',
84 'https://www.flickr.com/photos/amazeelabs/26497866357',
89 'src' => '/core/misc/druplicon.png',
99 * Tests that oEmbed media types' display can be configured correctly.
101 public function testDisplayConfiguration() {
102 $account = $this->drupalCreateUser(['administer media display']);
103 $this->drupalLogin($account);
105 $media_type = $this->createMediaType('oembed:video');
106 $this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/display');
107 $assert = $this->assertSession();
108 $assert->statusCodeEquals(200);
109 // Test that the formatter doesn't try to check applicability for fields
110 // which do not have a specific target bundle.
111 // @see https://www.drupal.org/project/drupal/issues/2976795.
112 $assert->pageTextNotContains('Can only flip STRING and INTEGER values!');
116 * Tests the oEmbed field formatter.
119 * The canonical URL of the media asset to test.
120 * @param string $resource_url
121 * The oEmebd resource URL of the media asset to test.
122 * @param mixed $formatter_settings
123 * Settings for the oEmbed field formatter.
124 * @param array $selectors
125 * An array of arrays. Each key is a CSS selector targeting an element in
126 * the rendered output, and each value is an array of attributes, keyed by
127 * name, that the element is expected to have.
129 * @dataProvider providerRender
131 public function testRender($url, $resource_url, array $formatter_settings, array $selectors) {
132 $account = $this->drupalCreateUser(['view media']);
133 $this->drupalLogin($account);
135 $media_type = $this->createMediaType('oembed:video');
137 $source = $media_type->getSource();
138 $source_field = $source->getSourceFieldDefinition($media_type);
140 EntityViewDisplay::create([
141 'targetEntityType' => 'media',
142 'bundle' => $media_type->id(),
145 ])->removeComponent('thumbnail')
146 ->setComponent($source_field->getName(), [
148 'settings' => $formatter_settings,
152 $this->hijackProviderEndpoints();
154 ResourceController::setResourceUrl($url, $this->getFixturesDirectory() . '/' . $resource_url);
155 UrlResolver::setEndpointUrl($url, $resource_url);
157 $entity = Media::create([
158 'bundle' => $media_type->id(),
159 $source_field->getName() => $url,
163 $this->drupalGet($entity->toUrl());
164 $assert = $this->assertSession();
165 $assert->statusCodeEquals(200);
166 foreach ($selectors as $selector => $attributes) {
167 foreach ($attributes as $attribute => $value) {
168 $assert->elementAttributeContains('css', $selector, $attribute, $value);