Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / media / tests / src / Functional / FieldFormatter / OEmbedFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional\FieldFormatter;
4
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;
11
12 /**
13  * @covers \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter
14  *
15  * @group media
16  */
17 class OEmbedFormatterTest extends MediaFunctionalTestBase {
18
19   use OEmbedTestTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = [
25     'field_ui',
26     'link',
27     'media_test_oembed',
28   ];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp() {
34     parent::setUp();
35     $this->lockHttpClientToFixtures();
36   }
37
38   /**
39    * Data provider for testRender().
40    *
41    * @see ::testRender()
42    *
43    * @return array
44    */
45   public function providerRender() {
46     return [
47       'Vimeo video' => [
48         'https://vimeo.com/7073899',
49         'video_vimeo.json',
50         [],
51         [
52           'iframe' => [
53             'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
54             'width' => 480,
55             'height' => 360,
56           ],
57         ],
58       ],
59       'Vimeo video, resized' => [
60         'https://vimeo.com/7073899',
61         'video_vimeo.json?maxwidth=100&maxheight=100',
62         ['max_width' => 100, 'max_height' => 100],
63         [
64           'iframe' => [
65             'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
66             'width' => 100,
67             'height' => 100,
68           ],
69         ],
70       ],
71       'tweet' => [
72         'https://twitter.com/drupaldevdays/status/935643039741202432',
73         'rich_twitter.json',
74         [],
75         [
76           'iframe' => [
77             'src' => '/media/oembed?url=https%3A//twitter.com/drupaldevdays/status/935643039741202432',
78             'width' => 550,
79             'height' => 360,
80           ],
81         ],
82       ],
83       'Flickr photo' => [
84         'https://www.flickr.com/photos/amazeelabs/26497866357',
85         'photo_flickr.json',
86         [],
87         [
88           'img' => [
89             'src' => '/core/misc/druplicon.png',
90             'width' => 88,
91             'height' => 100,
92           ],
93         ],
94       ],
95     ];
96   }
97
98   /**
99    * Tests that oEmbed media types' display can be configured correctly.
100    */
101   public function testDisplayConfiguration() {
102     $account = $this->drupalCreateUser(['administer media display']);
103     $this->drupalLogin($account);
104
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!');
113   }
114
115   /**
116    * Tests the oEmbed field formatter.
117    *
118    * @param string $url
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.
128    *
129    * @dataProvider providerRender
130    */
131   public function testRender($url, $resource_url, array $formatter_settings, array $selectors) {
132     $account = $this->drupalCreateUser(['view media']);
133     $this->drupalLogin($account);
134
135     $media_type = $this->createMediaType('oembed:video');
136
137     $source = $media_type->getSource();
138     $source_field = $source->getSourceFieldDefinition($media_type);
139
140     EntityViewDisplay::create([
141       'targetEntityType' => 'media',
142       'bundle' => $media_type->id(),
143       'mode' => 'full',
144       'status' => TRUE,
145     ])->removeComponent('thumbnail')
146       ->setComponent($source_field->getName(), [
147         'type' => 'oembed',
148         'settings' => $formatter_settings,
149       ])
150       ->save();
151
152     $this->hijackProviderEndpoints();
153
154     ResourceController::setResourceUrl($url, $this->getFixturesDirectory() . '/' . $resource_url);
155     UrlResolver::setEndpointUrl($url, $resource_url);
156
157     $entity = Media::create([
158       'bundle' => $media_type->id(),
159       $source_field->getName() => $url,
160     ]);
161     $entity->save();
162
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);
169       }
170     }
171   }
172
173 }