3 namespace Drupal\Tests\video_embed_field\Kernel;
5 use Drupal\Core\Render\RenderContext;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\video_embed_field\Plugin\Field\FieldFormatter\Thumbnail;
11 * Test the embed field formatters are functioning.
13 * @group video_embed_field
15 class FieldOutputTest extends KernelTestBase {
17 use StripWhitespaceTrait;
22 public function renderedFieldTestCases() {
24 'YouTube: Thumbnail' => [
25 'https://www.youtube.com/watch?v=fdbFVWupSsw',
27 'type' => 'video_embed_field_thumbnail',
32 '#uri' => 'public://video_thumbnails/fdbFVWupSsw.jpg',
35 'YouTube: Thumbnail With Image Style' => [
36 'https://www.youtube.com/watch?v=fdbFVWupSsw',
38 'type' => 'video_embed_field_thumbnail',
40 'image_style' => 'thumbnail',
44 '#theme' => 'image_style',
45 '#uri' => 'public://video_thumbnails/fdbFVWupSsw.jpg',
46 '#style_name' => 'thumbnail',
49 'YouTube: Embed Code' => [
50 'https://www.youtube.com/watch?v=fdbFVWupSsw',
52 'type' => 'video_embed_field_video',
57 'responsive' => FALSE,
61 '#type' => 'video_embed_iframe',
62 '#provider' => 'youtube',
63 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
73 'allowfullscreen' => 'allowfullscreen',
82 'YouTube: Time-index Embed Code' => [
83 'https://www.youtube.com/watch?v=fdbFVWupSsw&t=100',
85 'type' => 'video_embed_field_video',
90 'responsive' => FALSE,
94 '#type' => 'video_embed_iframe',
95 '#provider' => 'youtube',
96 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
105 'frameborder' => '0',
106 'allowfullscreen' => 'allowfullscreen',
115 'YouTube: Language Specified Embed Code' => [
116 'https://www.youtube.com/watch?v=fdbFVWupSsw&hl=fr',
118 'type' => 'video_embed_field_video',
123 'responsive' => FALSE,
127 '#type' => 'video_embed_iframe',
128 '#provider' => 'youtube',
129 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
134 'cc_lang_pref' => 'fr',
139 'frameborder' => '0',
140 'allowfullscreen' => 'allowfullscreen',
149 'Vimeo: Thumbnail' => [
150 'https://vimeo.com/80896303',
152 'type' => 'video_embed_field_thumbnail',
157 '#uri' => 'public://video_thumbnails/80896303.jpg',
160 'Vimeo: Embed Code' => [
161 'https://vimeo.com/80896303',
163 'type' => 'video_embed_field_video',
168 'responsive' => FALSE,
172 '#type' => 'video_embed_iframe',
173 '#provider' => 'vimeo',
174 '#url' => 'https://player.vimeo.com/video/80896303',
181 'frameborder' => '0',
182 'allowfullscreen' => 'allowfullscreen',
191 'Vimeo: Autoplaying Embed Code' => [
192 'https://vimeo.com/80896303#t=150s',
194 'type' => 'video_embed_field_video',
199 'responsive' => FALSE,
203 '#type' => 'video_embed_iframe',
204 '#provider' => 'vimeo',
205 '#url' => 'https://player.vimeo.com/video/80896303',
209 '#fragment' => 't=150s',
213 'frameborder' => '0',
214 'allowfullscreen' => 'allowfullscreen',
223 'Linked Thumbnail: Content' => [
224 'https://vimeo.com/80896303',
226 'type' => 'video_embed_field_thumbnail',
227 'settings' => ['link_image_to' => Thumbnail::LINK_CONTENT],
233 '#uri' => 'public://video_thumbnails/80896303.jpg',
235 '#url' => 'entity.entity_test.canonical',
238 'Linked Thumbnail: Provider' => [
239 'https://vimeo.com/80896303',
241 'type' => 'video_embed_field_thumbnail',
242 'settings' => ['link_image_to' => Thumbnail::LINK_PROVIDER],
248 '#uri' => 'public://video_thumbnails/80896303.jpg',
250 '#url' => 'https://vimeo.com/80896303',
253 'Colorbox Modal: Linked Image & Autoplay' => [
254 'https://vimeo.com/80896303',
256 'type' => 'video_embed_field_colorbox',
258 'link_image_to' => Thumbnail::LINK_PROVIDER,
262 'responsive' => FALSE,
266 '#type' => 'container',
268 'data-video-embed-field-modal' => '<iframe width="500" height="500" frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/80896303?autoplay=1"></iframe>',
269 'class' => ['video-embed-field-launch-modal'],
273 'video_embed_field/colorbox',
274 'video_embed_field/responsive-video',
286 '#uri' => 'public://video_thumbnails/80896303.jpg',
288 '#url' => 'https://vimeo.com/80896303',
292 'Colorbox Modal: Responsive' => [
293 'https://vimeo.com/80896303',
295 'type' => 'video_embed_field_colorbox',
297 'link_image_to' => Thumbnail::LINK_PROVIDER,
301 'responsive' => TRUE,
302 'modal_max_width' => 999,
306 '#type' => 'container',
308 'data-video-embed-field-modal' => '<div class="video-embed-field-responsive-video video-embed-field-responsive-modal" style="width:999px;"><iframe width="900" height="450" frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/80896303?autoplay=1"></iframe></div>',
310 'video-embed-field-launch-modal',
315 'video_embed_field/colorbox',
316 'video_embed_field/responsive-video',
328 '#uri' => 'public://video_thumbnails/80896303.jpg',
330 '#url' => 'https://vimeo.com/80896303',
334 'Video: Responsive' => [
335 'https://vimeo.com/80896303',
337 'type' => 'video_embed_field_video',
342 'responsive' => TRUE,
346 '#type' => 'container',
348 'library' => ['video_embed_field/responsive-video'],
351 'class' => ['video-embed-field-responsive-video'],
354 '#type' => 'video_embed_iframe',
355 '#provider' => 'vimeo',
356 '#url' => 'https://player.vimeo.com/video/80896303',
363 'frameborder' => '0',
364 'allowfullscreen' => 'allowfullscreen',
374 'YouTube Playlist' => [
375 'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
377 'type' => 'video_embed_field_video',
382 'responsive' => FALSE,
386 '#type' => 'video_embed_iframe',
387 '#provider' => 'youtube_playlist',
388 '#url' => 'https://www.youtube.com/embed/videoseries',
390 'list' => 'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
395 'frameborder' => '0',
396 'allowfullscreen' => 'allowfullscreen',
405 'No provider (video formatter)' => [
406 'http://example.com/not/a/video/url',
408 'type' => 'video_embed_field_video',
412 '#theme' => 'video_embed_field_missing_provider',
415 'No provider (thumbnail formatter)' => [
416 'http://example.com/not/a/video/url',
418 'type' => 'video_embed_field_thumbnail',
422 '#theme' => 'video_embed_field_missing_provider',
425 'No provider (colorbox modal)' => [
426 'http://example.com/not/a/video/url',
428 'type' => 'video_embed_field_colorbox',
431 '#type' => 'container',
433 'data-video-embed-field-modal' => 'No video provider was found to handle the given URL. See <a href="https://www.drupal.org/node/2842927">the documentation</a> for more information.',
434 'class' => ['video-embed-field-launch-modal'],
438 'video_embed_field/colorbox',
439 'video_embed_field/responsive-video',
448 '#theme' => 'video_embed_field_missing_provider',
456 * Test the embed field.
458 * @dataProvider renderedFieldTestCases
460 public function testEmbedField($url, $settings, $expected_field_item_output) {
462 $field_output = $this->getPreparedFieldOutput($url, $settings);
464 // Assert the specific field output at delta 1 matches the expected test
466 $this->assertEquals($expected_field_item_output, $field_output[0]);
470 * Get and prepare the output of a field.
474 * @param array $settings
475 * An array of formatter settings.
478 * The rendered prepared field output.
480 protected function getPreparedFieldOutput($url, $settings) {
481 $entity = EntityTest::create();
482 $entity->{$this->fieldName}->value = $url;
485 $field_output = $this->container->get('renderer')->executeInRenderContext(new RenderContext(), function() use ($entity, $settings) {
486 return $entity->{$this->fieldName}->view($settings);
489 // Prepare the field output to make it easier to compare our test data
491 array_walk_recursive($field_output[0], function (&$value) {
492 // Prevent circular references with comparing field output that
493 // contains url objects.
494 if ($value instanceof Url) {
495 $value = $value->isRouted() ? $value->getRouteName() : $value->getUri();
497 // Trim to prevent stray whitespace for the colorbox formatters with
499 $value = $this->stripWhitespace($value);
502 return $field_output;