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' => 'container',
64 'video-embed-field-provider-youtube',
68 '#type' => 'video_embed_iframe',
69 '#provider' => 'youtube',
70 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
80 'allowfullscreen' => 'allowfullscreen',
90 'YouTube: Time-index Embed Code' => [
91 'https://www.youtube.com/watch?v=fdbFVWupSsw&t=100',
93 'type' => 'video_embed_field_video',
98 'responsive' => FALSE,
102 '#type' => 'container',
105 'video-embed-field-provider-youtube',
109 '#type' => 'video_embed_iframe',
110 '#provider' => 'youtube',
111 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
120 'frameborder' => '0',
121 'allowfullscreen' => 'allowfullscreen',
131 'YouTube: Language Specified Embed Code' => [
132 'https://www.youtube.com/watch?v=fdbFVWupSsw&hl=fr',
134 'type' => 'video_embed_field_video',
139 'responsive' => FALSE,
143 '#type' => 'container',
146 'video-embed-field-provider-youtube',
150 '#type' => 'video_embed_iframe',
151 '#provider' => 'youtube',
152 '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
157 'cc_lang_pref' => 'fr',
162 'frameborder' => '0',
163 'allowfullscreen' => 'allowfullscreen',
173 'Vimeo: Thumbnail' => [
174 'https://vimeo.com/80896303',
176 'type' => 'video_embed_field_thumbnail',
181 '#uri' => 'public://video_thumbnails/80896303.jpg',
184 'Vimeo: Embed Code' => [
185 'https://vimeo.com/80896303',
187 'type' => 'video_embed_field_video',
192 'responsive' => FALSE,
196 '#type' => 'container',
199 'video-embed-field-provider-vimeo',
203 '#type' => 'video_embed_iframe',
204 '#provider' => 'vimeo',
205 '#url' => 'https://player.vimeo.com/video/80896303',
212 'frameborder' => '0',
213 'allowfullscreen' => 'allowfullscreen',
223 'Vimeo: Autoplaying Embed Code' => [
224 'https://vimeo.com/80896303#t=150s',
226 'type' => 'video_embed_field_video',
231 'responsive' => FALSE,
235 '#type' => 'container',
238 'video-embed-field-provider-vimeo',
242 '#type' => 'video_embed_iframe',
243 '#provider' => 'vimeo',
244 '#url' => 'https://player.vimeo.com/video/80896303',
248 '#fragment' => 't=150s',
252 'frameborder' => '0',
253 'allowfullscreen' => 'allowfullscreen',
263 'Linked Thumbnail: Content' => [
264 'https://vimeo.com/80896303',
266 'type' => 'video_embed_field_thumbnail',
267 'settings' => ['link_image_to' => Thumbnail::LINK_CONTENT],
273 '#uri' => 'public://video_thumbnails/80896303.jpg',
275 '#url' => 'entity.entity_test.canonical',
278 'Linked Thumbnail: Provider' => [
279 'https://vimeo.com/80896303',
281 'type' => 'video_embed_field_thumbnail',
282 'settings' => ['link_image_to' => Thumbnail::LINK_PROVIDER],
288 '#uri' => 'public://video_thumbnails/80896303.jpg',
290 '#url' => 'https://vimeo.com/80896303',
293 'Colorbox Modal: Linked Image & Autoplay' => [
294 'https://vimeo.com/80896303',
296 'type' => 'video_embed_field_colorbox',
298 'link_image_to' => Thumbnail::LINK_PROVIDER,
302 'responsive' => FALSE,
306 '#type' => 'container',
308 'data-video-embed-field-modal' => '<div class="video-embed-field-provider-vimeo"><iframe width="500" height="500" frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/80896303?autoplay=1"></iframe></div>',
309 'class' => ['video-embed-field-launch-modal'],
313 'video_embed_field/colorbox',
314 'video_embed_field/responsive-video',
326 '#uri' => 'public://video_thumbnails/80896303.jpg',
328 '#url' => 'https://vimeo.com/80896303',
332 'Colorbox Modal: Responsive' => [
333 'https://vimeo.com/80896303',
335 'type' => 'video_embed_field_colorbox',
337 'link_image_to' => Thumbnail::LINK_PROVIDER,
341 'responsive' => TRUE,
342 'modal_max_width' => 999,
346 '#type' => 'container',
348 'data-video-embed-field-modal' => '<div class="video-embed-field-provider-vimeo 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>',
350 'video-embed-field-launch-modal',
355 'video_embed_field/colorbox',
356 'video_embed_field/responsive-video',
368 '#uri' => 'public://video_thumbnails/80896303.jpg',
370 '#url' => 'https://vimeo.com/80896303',
374 'Lazy load formatter' => [
375 'https://vimeo.com/80896303',
377 'type' => 'video_embed_field_lazyload',
379 'link_image_to' => Thumbnail::LINK_PROVIDER,
383 'responsive' => TRUE,
384 'modal_max_width' => 999,
388 '#type' => 'container',
390 'data-video-embed-field-lazy' => '<div class="video-embed-field-provider-vimeo video-embed-field-responsive-video"><iframe width="900" height="450" frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/80896303?autoplay=1"></iframe></div>',
392 'video-embed-field-lazy',
397 'video_embed_field/lazy-load',
410 '#uri' => 'public://video_thumbnails/80896303.jpg',
412 '#url' => 'https://vimeo.com/80896303',
415 '#type' => 'html_tag',
419 'video-embed-field-lazy-play',
426 'Video: Responsive' => [
427 'https://vimeo.com/80896303',
429 'type' => 'video_embed_field_video',
434 'responsive' => TRUE,
438 '#type' => 'container',
440 'library' => ['video_embed_field/responsive-video'],
444 'video-embed-field-provider-vimeo',
445 'video-embed-field-responsive-video'
449 '#type' => 'video_embed_iframe',
450 '#provider' => 'vimeo',
451 '#url' => 'https://player.vimeo.com/video/80896303',
458 'frameborder' => '0',
459 'allowfullscreen' => 'allowfullscreen',
469 'YouTube Playlist' => [
470 'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
472 'type' => 'video_embed_field_video',
477 'responsive' => FALSE,
481 '#type' => 'container',
484 'video-embed-field-provider-youtube-playlist',
488 '#type' => 'video_embed_iframe',
489 '#provider' => 'youtube_playlist',
490 '#url' => 'https://www.youtube.com/embed/videoseries',
492 'list' => 'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
497 'frameborder' => '0',
498 'allowfullscreen' => 'allowfullscreen',
508 'No provider (video formatter)' => [
509 'http://example.com/not/a/video/url',
511 'type' => 'video_embed_field_video',
515 '#theme' => 'video_embed_field_missing_provider',
518 'No provider (thumbnail formatter)' => [
519 'http://example.com/not/a/video/url',
521 'type' => 'video_embed_field_thumbnail',
525 '#theme' => 'video_embed_field_missing_provider',
528 'No provider (colorbox modal)' => [
529 'http://example.com/not/a/video/url',
531 'type' => 'video_embed_field_colorbox',
534 '#type' => 'container',
536 '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.',
537 'class' => ['video-embed-field-launch-modal'],
541 'video_embed_field/colorbox',
542 'video_embed_field/responsive-video',
551 '#theme' => 'video_embed_field_missing_provider',
559 * Test the embed field.
561 * @dataProvider renderedFieldTestCases
563 public function testEmbedField($url, $settings, $expected_field_item_output) {
565 $field_output = $this->getPreparedFieldOutput($url, $settings);
567 // Assert the specific field output at delta 1 matches the expected test
569 $this->assertEquals($expected_field_item_output, $field_output[0]);
573 * Get and prepare the output of a field.
577 * @param array $settings
578 * An array of formatter settings.
581 * The rendered prepared field output.
583 protected function getPreparedFieldOutput($url, $settings) {
584 $entity = EntityTest::create();
585 $entity->{$this->fieldName}->value = $url;
588 $field_output = $this->container->get('renderer')
589 ->executeInRenderContext(new RenderContext(), function () use ($entity, $settings) {
590 return $entity->{$this->fieldName}->view($settings);
593 // Prepare the field output to make it easier to compare our test data
595 array_walk_recursive($field_output[0], function (&$value) {
596 // Prevent circular references with comparing field output that
597 // contains url objects.
598 if ($value instanceof Url) {
599 $value = $value->isRouted() ? $value->getRouteName() : $value->getUri();
601 // Trim to prevent stray whitespace for the colorbox formatters with
603 $value = $this->stripWhitespace($value);
606 return $field_output;