5ce00a5221f8917a2f8f56b63fe51ad0ee361fdb
[yaffs-website] / web / modules / contrib / video_embed_field / tests / src / Kernel / FieldOutputTest.php
1 <?php
2
3 namespace Drupal\Tests\video_embed_field\Kernel;
4
5 use Drupal\Core\Render\RenderContext;
6 use Drupal\Core\Url;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\video_embed_field\Plugin\Field\FieldFormatter\Thumbnail;
9
10 /**
11  * Test the embed field formatters are functioning.
12  *
13  * @group video_embed_field
14  */
15 class FieldOutputTest extends KernelTestBase {
16
17   use StripWhitespaceTrait;
18
19   /**
20    * The test cases.
21    */
22   public function renderedFieldTestCases() {
23     return [
24       'YouTube: Thumbnail' => [
25         'https://www.youtube.com/watch?v=fdbFVWupSsw',
26         [
27           'type' => 'video_embed_field_thumbnail',
28           'settings' => [],
29         ],
30         [
31           '#theme' => 'image',
32           '#uri' => 'public://video_thumbnails/fdbFVWupSsw.jpg',
33         ],
34       ],
35       'YouTube: Thumbnail With Image Style' => [
36         'https://www.youtube.com/watch?v=fdbFVWupSsw',
37         [
38           'type' => 'video_embed_field_thumbnail',
39           'settings' => [
40             'image_style' => 'thumbnail',
41           ],
42         ],
43         [
44           '#theme' => 'image_style',
45           '#uri' => 'public://video_thumbnails/fdbFVWupSsw.jpg',
46           '#style_name' => 'thumbnail',
47         ],
48       ],
49       'YouTube: Embed Code' => [
50         'https://www.youtube.com/watch?v=fdbFVWupSsw',
51         [
52           'type' => 'video_embed_field_video',
53           'settings' => [
54             'width' => 100,
55             'height' => 100,
56             'autoplay' => TRUE,
57             'responsive' => FALSE,
58           ],
59         ],
60         [
61           '#type' => 'container',
62           '#attributes' => [
63             'class' => [
64               'video-embed-field-provider-youtube',
65             ],
66           ],
67           'children' => [
68             '#type' => 'video_embed_iframe',
69             '#provider' => 'youtube',
70             '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
71             '#query' => [
72               'autoplay' => '1',
73               'start' => '0',
74               'rel' => '0',
75             ],
76             '#attributes' => [
77               'width' => '100',
78               'height' => '100',
79               'frameborder' => '0',
80               'allowfullscreen' => 'allowfullscreen',
81             ],
82             '#cache' => [
83               'contexts' => [
84                 'user.permissions',
85               ],
86             ],
87           ],
88         ],
89       ],
90       'YouTube: Time-index Embed Code' => [
91         'https://www.youtube.com/watch?v=fdbFVWupSsw&t=100',
92         [
93           'type' => 'video_embed_field_video',
94           'settings' => [
95             'width' => 100,
96             'height' => 100,
97             'autoplay' => TRUE,
98             'responsive' => FALSE,
99           ],
100         ],
101         [
102           '#type' => 'container',
103           '#attributes' => [
104             'class' => [
105               'video-embed-field-provider-youtube',
106             ],
107           ],
108           'children' => [
109             '#type' => 'video_embed_iframe',
110             '#provider' => 'youtube',
111             '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
112             '#query' => [
113               'autoplay' => '1',
114               'start' => '100',
115               'rel' => '0',
116             ],
117             '#attributes' => [
118               'width' => '100',
119               'height' => '100',
120               'frameborder' => '0',
121               'allowfullscreen' => 'allowfullscreen',
122             ],
123             '#cache' => [
124               'contexts' => [
125                 'user.permissions',
126               ],
127             ],
128           ],
129         ],
130       ],
131       'YouTube: Language Specified Embed Code' => [
132         'https://www.youtube.com/watch?v=fdbFVWupSsw&hl=fr',
133         [
134           'type' => 'video_embed_field_video',
135           'settings' => [
136             'width' => 100,
137             'height' => 100,
138             'autoplay' => TRUE,
139             'responsive' => FALSE,
140           ],
141         ],
142         [
143           '#type' => 'container',
144           '#attributes' => [
145             'class' => [
146               'video-embed-field-provider-youtube',
147             ],
148           ],
149           'children' => [
150             '#type' => 'video_embed_iframe',
151             '#provider' => 'youtube',
152             '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
153             '#query' => [
154               'autoplay' => '1',
155               'start' => '0',
156               'rel' => '0',
157               'cc_lang_pref' => 'fr',
158             ],
159             '#attributes' => [
160               'width' => '100',
161               'height' => '100',
162               'frameborder' => '0',
163               'allowfullscreen' => 'allowfullscreen',
164             ],
165             '#cache' => [
166               'contexts' => [
167                 'user.permissions',
168               ],
169             ],
170           ],
171         ],
172       ],
173       'Vimeo: Thumbnail' => [
174         'https://vimeo.com/80896303',
175         [
176           'type' => 'video_embed_field_thumbnail',
177           'settings' => [],
178         ],
179         [
180           '#theme' => 'image',
181           '#uri' => 'public://video_thumbnails/80896303.jpg',
182         ],
183       ],
184       'Vimeo: Embed Code' => [
185         'https://vimeo.com/80896303',
186         [
187           'type' => 'video_embed_field_video',
188           'settings' => [
189             'width' => 100,
190             'height' => 100,
191             'autoplay' => TRUE,
192             'responsive' => FALSE,
193           ],
194         ],
195         [
196           '#type' => 'container',
197           '#attributes' => [
198             'class' => [
199               'video-embed-field-provider-vimeo',
200             ],
201           ],
202           'children' => [
203             '#type' => 'video_embed_iframe',
204             '#provider' => 'vimeo',
205             '#url' => 'https://player.vimeo.com/video/80896303',
206             '#query' => [
207               'autoplay' => '1',
208             ],
209             '#attributes' => [
210               'width' => '100',
211               'height' => '100',
212               'frameborder' => '0',
213               'allowfullscreen' => 'allowfullscreen',
214             ],
215             '#cache' => [
216               'contexts' => [
217                 'user.permissions',
218               ],
219             ],
220           ],
221         ],
222       ],
223       'Vimeo: Autoplaying Embed Code' => [
224         'https://vimeo.com/80896303#t=150s',
225         [
226           'type' => 'video_embed_field_video',
227           'settings' => [
228             'width' => 100,
229             'height' => 100,
230             'autoplay' => TRUE,
231             'responsive' => FALSE,
232           ],
233         ],
234         [
235           '#type' => 'container',
236           '#attributes' => [
237             'class' => [
238               'video-embed-field-provider-vimeo',
239             ],
240           ],
241           'children' => [
242             '#type' => 'video_embed_iframe',
243             '#provider' => 'vimeo',
244             '#url' => 'https://player.vimeo.com/video/80896303',
245             '#query' => [
246               'autoplay' => '1',
247             ],
248             '#fragment' => 't=150s',
249             '#attributes' => [
250               'width' => '100',
251               'height' => '100',
252               'frameborder' => '0',
253               'allowfullscreen' => 'allowfullscreen',
254             ],
255             '#cache' => [
256               'contexts' => [
257                 'user.permissions',
258               ],
259             ],
260           ],
261         ],
262       ],
263       'Linked Thumbnail: Content' => [
264         'https://vimeo.com/80896303',
265         [
266           'type' => 'video_embed_field_thumbnail',
267           'settings' => ['link_image_to' => Thumbnail::LINK_CONTENT],
268         ],
269         [
270           '#type' => 'link',
271           '#title' => [
272             '#theme' => 'image',
273             '#uri' => 'public://video_thumbnails/80896303.jpg',
274           ],
275           '#url' => 'entity.entity_test.canonical',
276         ],
277       ],
278       'Linked Thumbnail: Provider' => [
279         'https://vimeo.com/80896303',
280         [
281           'type' => 'video_embed_field_thumbnail',
282           'settings' => ['link_image_to' => Thumbnail::LINK_PROVIDER],
283         ],
284         [
285           '#type' => 'link',
286           '#title' => [
287             '#theme' => 'image',
288             '#uri' => 'public://video_thumbnails/80896303.jpg',
289           ],
290           '#url' => 'https://vimeo.com/80896303',
291         ],
292       ],
293       'Colorbox Modal: Linked Image & Autoplay' => [
294         'https://vimeo.com/80896303',
295         [
296           'type' => 'video_embed_field_colorbox',
297           'settings' => [
298             'link_image_to' => Thumbnail::LINK_PROVIDER,
299             'autoplay' => TRUE,
300             'width' => 500,
301             'height' => 500,
302             'responsive' => FALSE,
303           ],
304         ],
305         [
306           '#type' => 'container',
307           '#attributes' => [
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'],
310           ],
311           '#attached' => [
312             'library' => [
313               'video_embed_field/colorbox',
314               'video_embed_field/responsive-video',
315             ],
316           ],
317           '#cache' => [
318             'contexts' => [
319               'user.permissions',
320             ],
321           ],
322           'children' => [
323             '#type' => 'link',
324             '#title' => [
325               '#theme' => 'image',
326               '#uri' => 'public://video_thumbnails/80896303.jpg',
327             ],
328             '#url' => 'https://vimeo.com/80896303',
329           ],
330         ],
331       ],
332       'Colorbox Modal: Responsive' => [
333         'https://vimeo.com/80896303',
334         [
335           'type' => 'video_embed_field_colorbox',
336           'settings' => [
337             'link_image_to' => Thumbnail::LINK_PROVIDER,
338             'autoplay' => TRUE,
339             'width' => 900,
340             'height' => 450,
341             'responsive' => TRUE,
342             'modal_max_width' => 999,
343           ],
344         ],
345         [
346           '#type' => 'container',
347           '#attributes' => [
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>',
349             'class' => [
350               'video-embed-field-launch-modal',
351             ],
352           ],
353           '#attached' => [
354             'library' => [
355               'video_embed_field/colorbox',
356               'video_embed_field/responsive-video',
357             ],
358           ],
359           '#cache' => [
360             'contexts' => [
361               'user.permissions',
362             ],
363           ],
364           'children' => [
365             '#type' => 'link',
366             '#title' => [
367               '#theme' => 'image',
368               '#uri' => 'public://video_thumbnails/80896303.jpg',
369             ],
370             '#url' => 'https://vimeo.com/80896303',
371           ],
372         ],
373       ],
374       'Lazy load formatter' => [
375         'https://vimeo.com/80896303',
376         [
377           'type' => 'video_embed_field_lazyload',
378           'settings' => [
379             'link_image_to' => Thumbnail::LINK_PROVIDER,
380             'autoplay' => TRUE,
381             'width' => 900,
382             'height' => 450,
383             'responsive' => TRUE,
384             'modal_max_width' => 999,
385           ],
386         ],
387         [
388           '#type' => 'container',
389           '#attributes' => [
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>',
391             'class' => [
392               'video-embed-field-lazy',
393             ],
394           ],
395           '#attached' => [
396             'library' => [
397               'video_embed_field/lazy-load',
398             ],
399           ],
400           '#cache' => [
401             'contexts' => [
402               'user.permissions',
403             ],
404           ],
405           'children' => [
406             [
407               '#type' => 'link',
408               '#title' => [
409                 '#theme' => 'image',
410                 '#uri' => 'public://video_thumbnails/80896303.jpg',
411               ],
412               '#url' => 'https://vimeo.com/80896303',
413             ],
414             [
415               '#type' => 'html_tag',
416               '#tag' => 'div',
417               '#attributes' => [
418                 'class' => [
419                   'video-embed-field-lazy-play',
420                 ],
421               ],
422             ],
423           ],
424         ],
425       ],
426       'Video: Responsive' => [
427         'https://vimeo.com/80896303',
428         [
429           'type' => 'video_embed_field_video',
430           'settings' => [
431             'width' => 100,
432             'height' => 100,
433             'autoplay' => TRUE,
434             'responsive' => TRUE,
435           ],
436         ],
437         [
438           '#type' => 'container',
439           '#attached' => [
440             'library' => ['video_embed_field/responsive-video'],
441           ],
442           '#attributes' => [
443             'class' => [
444               'video-embed-field-provider-vimeo',
445               'video-embed-field-responsive-video'
446             ],
447           ],
448           'children' => [
449             '#type' => 'video_embed_iframe',
450             '#provider' => 'vimeo',
451             '#url' => 'https://player.vimeo.com/video/80896303',
452             '#query' => [
453               'autoplay' => '1',
454             ],
455             '#attributes' => [
456               'width' => '100',
457               'height' => '100',
458               'frameborder' => '0',
459               'allowfullscreen' => 'allowfullscreen',
460             ],
461             '#cache' => [
462               'contexts' => [
463                 'user.permissions',
464               ],
465             ],
466           ],
467         ],
468       ],
469       'YouTube Playlist' => [
470         'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
471         [
472           'type' => 'video_embed_field_video',
473           'settings' => [
474             'width' => 100,
475             'height' => 100,
476             'autoplay' => TRUE,
477             'responsive' => FALSE,
478           ],
479         ],
480         [
481           '#type' => 'container',
482           '#attributes' => [
483             'class' => [
484               'video-embed-field-provider-youtube-playlist',
485             ],
486           ],
487           'children' => [
488             '#type' => 'video_embed_iframe',
489             '#provider' => 'youtube_playlist',
490             '#url' => 'https://www.youtube.com/embed/videoseries',
491             '#query' => [
492               'list' => 'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
493             ],
494             '#attributes' => [
495               'width' => '100',
496               'height' => '100',
497               'frameborder' => '0',
498               'allowfullscreen' => 'allowfullscreen',
499             ],
500             '#cache' => [
501               'contexts' => [
502                 'user.permissions',
503               ],
504             ],
505           ],
506         ],
507       ],
508       'No provider (video formatter)' => [
509         'http://example.com/not/a/video/url',
510         [
511           'type' => 'video_embed_field_video',
512           'settings' => [],
513         ],
514         [
515           '#theme' => 'video_embed_field_missing_provider',
516         ],
517       ],
518       'No provider (thumbnail formatter)' => [
519         'http://example.com/not/a/video/url',
520         [
521           'type' => 'video_embed_field_thumbnail',
522           'settings' => [],
523         ],
524         [
525           '#theme' => 'video_embed_field_missing_provider',
526         ],
527       ],
528       'No provider (colorbox modal)' => [
529         'http://example.com/not/a/video/url',
530         [
531           'type' => 'video_embed_field_colorbox',
532         ],
533         [
534           '#type' => 'container',
535           '#attributes' => [
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'],
538           ],
539           '#attached' => [
540             'library' => [
541               'video_embed_field/colorbox',
542               'video_embed_field/responsive-video',
543             ],
544           ],
545           '#cache' => [
546             'contexts' => [
547               'user.permissions',
548             ],
549           ],
550           'children' => [
551             '#theme' => 'video_embed_field_missing_provider',
552           ],
553         ],
554       ],
555     ];
556   }
557
558   /**
559    * Test the embed field.
560    *
561    * @dataProvider renderedFieldTestCases
562    */
563   public function testEmbedField($url, $settings, $expected_field_item_output) {
564
565     $field_output = $this->getPreparedFieldOutput($url, $settings);
566
567     // Assert the specific field output at delta 1 matches the expected test
568     // data.
569     $this->assertEquals($expected_field_item_output, $field_output[0]);
570   }
571
572   /**
573    * Get and prepare the output of a field.
574    *
575    * @param string $url
576    *   The video URL.
577    * @param array $settings
578    *   An array of formatter settings.
579    *
580    * @return array
581    *   The rendered prepared field output.
582    */
583   protected function getPreparedFieldOutput($url, $settings) {
584     $entity = EntityTest::create();
585     $entity->{$this->fieldName}->value = $url;
586     $entity->save();
587
588     $field_output = $this->container->get('renderer')
589       ->executeInRenderContext(new RenderContext(), function () use ($entity, $settings) {
590         return $entity->{$this->fieldName}->view($settings);
591       });
592
593     // Prepare the field output to make it easier to compare our test data
594     // values against.
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();
600       }
601       // Trim to prevent stray whitespace for the colorbox formatters with
602       // early rendering.
603       $value = $this->stripWhitespace($value);
604     });
605
606     return $field_output;
607   }
608
609 }