Interim commit.
[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' => 'video_embed_iframe',
62           '#provider' => 'youtube',
63           '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
64           '#query' => [
65             'autoplay' => '1',
66             'start' => '0',
67             'rel' => '0',
68           ],
69           '#attributes' => [
70             'width' => '100',
71             'height' => '100',
72             'frameborder' => '0',
73             'allowfullscreen' => 'allowfullscreen',
74           ],
75           '#cache' => [
76             'contexts' => [
77               'user.permissions',
78             ],
79           ],
80         ],
81       ],
82       'YouTube: Time-index Embed Code' => [
83         'https://www.youtube.com/watch?v=fdbFVWupSsw&t=100',
84         [
85           'type' => 'video_embed_field_video',
86           'settings' => [
87             'width' => 100,
88             'height' => 100,
89             'autoplay' => TRUE,
90             'responsive' => FALSE,
91           ],
92         ],
93         [
94           '#type' => 'video_embed_iframe',
95           '#provider' => 'youtube',
96           '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
97           '#query' => [
98             'autoplay' => '1',
99             'start' => '100',
100             'rel' => '0',
101           ],
102           '#attributes' => [
103             'width' => '100',
104             'height' => '100',
105             'frameborder' => '0',
106             'allowfullscreen' => 'allowfullscreen',
107           ],
108           '#cache' => [
109             'contexts' => [
110               'user.permissions',
111             ],
112           ],
113         ],
114       ],
115       'YouTube: Language Specified Embed Code' => [
116         'https://www.youtube.com/watch?v=fdbFVWupSsw&hl=fr',
117         [
118           'type' => 'video_embed_field_video',
119           'settings' => [
120             'width' => 100,
121             'height' => 100,
122             'autoplay' => TRUE,
123             'responsive' => FALSE,
124           ],
125         ],
126         [
127           '#type' => 'video_embed_iframe',
128           '#provider' => 'youtube',
129           '#url' => 'https://www.youtube.com/embed/fdbFVWupSsw',
130           '#query' => [
131             'autoplay' => '1',
132             'start' => '0',
133             'rel' => '0',
134             'cc_lang_pref' => 'fr',
135           ],
136           '#attributes' => [
137             'width' => '100',
138             'height' => '100',
139             'frameborder' => '0',
140             'allowfullscreen' => 'allowfullscreen',
141           ],
142           '#cache' => [
143             'contexts' => [
144               'user.permissions',
145             ],
146           ],
147         ],
148       ],
149       'Vimeo: Thumbnail' => [
150         'https://vimeo.com/80896303',
151         [
152           'type' => 'video_embed_field_thumbnail',
153           'settings' => [],
154         ],
155         [
156           '#theme' => 'image',
157           '#uri' => 'public://video_thumbnails/80896303.jpg',
158         ],
159       ],
160       'Vimeo: Embed Code' => [
161         'https://vimeo.com/80896303',
162         [
163           'type' => 'video_embed_field_video',
164           'settings' => [
165             'width' => 100,
166             'height' => 100,
167             'autoplay' => TRUE,
168             'responsive' => FALSE,
169           ],
170         ],
171         [
172           '#type' => 'video_embed_iframe',
173           '#provider' => 'vimeo',
174           '#url' => 'https://player.vimeo.com/video/80896303',
175           '#query' => [
176             'autoplay' => '1',
177           ],
178           '#attributes' => [
179             'width' => '100',
180             'height' => '100',
181             'frameborder' => '0',
182             'allowfullscreen' => 'allowfullscreen',
183           ],
184           '#cache' => [
185             'contexts' => [
186               'user.permissions',
187             ],
188           ],
189         ],
190       ],
191       'Vimeo: Autoplaying Embed Code' => [
192         'https://vimeo.com/80896303#t=150s',
193         [
194           'type' => 'video_embed_field_video',
195           'settings' => [
196             'width' => 100,
197             'height' => 100,
198             'autoplay' => TRUE,
199             'responsive' => FALSE,
200           ],
201         ],
202         [
203           '#type' => 'video_embed_iframe',
204           '#provider' => 'vimeo',
205           '#url' => 'https://player.vimeo.com/video/80896303',
206           '#query' => [
207             'autoplay' => '1',
208           ],
209           '#fragment' => 't=150s',
210           '#attributes' => [
211             'width' => '100',
212             'height' => '100',
213             'frameborder' => '0',
214             'allowfullscreen' => 'allowfullscreen',
215           ],
216           '#cache' => [
217             'contexts' => [
218               'user.permissions',
219             ],
220           ],
221         ],
222       ],
223       'Linked Thumbnail: Content' => [
224         'https://vimeo.com/80896303',
225         [
226           'type' => 'video_embed_field_thumbnail',
227           'settings' => ['link_image_to' => Thumbnail::LINK_CONTENT],
228         ],
229         [
230           '#type' => 'link',
231           '#title' => [
232             '#theme' => 'image',
233             '#uri' => 'public://video_thumbnails/80896303.jpg',
234           ],
235           '#url' => 'entity.entity_test.canonical',
236         ],
237       ],
238       'Linked Thumbnail: Provider' => [
239         'https://vimeo.com/80896303',
240         [
241           'type' => 'video_embed_field_thumbnail',
242           'settings' => ['link_image_to' => Thumbnail::LINK_PROVIDER],
243         ],
244         [
245           '#type' => 'link',
246           '#title' => [
247             '#theme' => 'image',
248             '#uri' => 'public://video_thumbnails/80896303.jpg',
249           ],
250           '#url' => 'https://vimeo.com/80896303',
251         ],
252       ],
253       'Colorbox Modal: Linked Image & Autoplay' => [
254         'https://vimeo.com/80896303',
255         [
256           'type' => 'video_embed_field_colorbox',
257           'settings' => [
258             'link_image_to' => Thumbnail::LINK_PROVIDER,
259             'autoplay' => TRUE,
260             'width' => 500,
261             'height' => 500,
262             'responsive' => FALSE,
263           ],
264         ],
265         [
266           '#type' => 'container',
267           '#attributes' => [
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'],
270           ],
271           '#attached' => [
272             'library' => [
273               'video_embed_field/colorbox',
274               'video_embed_field/responsive-video',
275             ],
276           ],
277           '#cache' => [
278             'contexts' => [
279               'user.permissions',
280             ],
281           ],
282           'children' => [
283             '#type' => 'link',
284             '#title' => [
285               '#theme' => 'image',
286               '#uri' => 'public://video_thumbnails/80896303.jpg',
287             ],
288             '#url' => 'https://vimeo.com/80896303',
289           ],
290         ],
291       ],
292       'Colorbox Modal: Responsive' => [
293         'https://vimeo.com/80896303',
294         [
295           'type' => 'video_embed_field_colorbox',
296           'settings' => [
297             'link_image_to' => Thumbnail::LINK_PROVIDER,
298             'autoplay' => TRUE,
299             'width' => 900,
300             'height' => 450,
301             'responsive' => TRUE,
302             'modal_max_width' => 999,
303           ],
304         ],
305         [
306           '#type' => 'container',
307           '#attributes' => [
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>',
309             'class' => [
310               'video-embed-field-launch-modal',
311             ],
312           ],
313           '#attached' => [
314             'library' => [
315               'video_embed_field/colorbox',
316               'video_embed_field/responsive-video',
317             ],
318           ],
319           '#cache' => [
320             'contexts' => [
321               'user.permissions',
322             ],
323           ],
324           'children' => [
325             '#type' => 'link',
326             '#title' => [
327               '#theme' => 'image',
328               '#uri' => 'public://video_thumbnails/80896303.jpg',
329             ],
330             '#url' => 'https://vimeo.com/80896303',
331           ],
332         ],
333       ],
334       'Video: Responsive' => [
335         'https://vimeo.com/80896303',
336         [
337           'type' => 'video_embed_field_video',
338           'settings' => [
339             'width' => 100,
340             'height' => 100,
341             'autoplay' => TRUE,
342             'responsive' => TRUE,
343           ],
344         ],
345         [
346           '#type' => 'container',
347           '#attached' => [
348             'library' => ['video_embed_field/responsive-video'],
349           ],
350           '#attributes' => [
351             'class' => ['video-embed-field-responsive-video'],
352           ],
353           'children' => [
354             '#type' => 'video_embed_iframe',
355             '#provider' => 'vimeo',
356             '#url' => 'https://player.vimeo.com/video/80896303',
357             '#query' => [
358               'autoplay' => '1',
359             ],
360             '#attributes' => [
361               'width' => '100',
362               'height' => '100',
363               'frameborder' => '0',
364               'allowfullscreen' => 'allowfullscreen',
365             ],
366             '#cache' => [
367               'contexts' => [
368                 'user.permissions',
369               ],
370             ],
371           ],
372         ],
373       ],
374       'YouTube Playlist' => [
375         'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
376         [
377           'type' => 'video_embed_field_video',
378           'settings' => [
379             'width' => 100,
380             'height' => 100,
381             'autoplay' => TRUE,
382             'responsive' => FALSE,
383           ],
384         ],
385         [
386           '#type' => 'video_embed_iframe',
387           '#provider' => 'youtube_playlist',
388           '#url' => 'https://www.youtube.com/embed/videoseries',
389           '#query' => [
390             'list' => 'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
391           ],
392           '#attributes' => [
393             'width' => '100',
394             'height' => '100',
395             'frameborder' => '0',
396             'allowfullscreen' => 'allowfullscreen',
397           ],
398           '#cache' => [
399             'contexts' => [
400               'user.permissions',
401             ],
402           ],
403         ],
404       ],
405       'No provider (video formatter)' => [
406         'http://example.com/not/a/video/url',
407         [
408           'type' => 'video_embed_field_video',
409           'settings' => [],
410         ],
411         [
412           '#theme' => 'video_embed_field_missing_provider',
413         ],
414       ],
415       'No provider (thumbnail formatter)' => [
416         'http://example.com/not/a/video/url',
417         [
418           'type' => 'video_embed_field_thumbnail',
419           'settings' => [],
420         ],
421         [
422           '#theme' => 'video_embed_field_missing_provider',
423         ],
424       ],
425       'No provider (colorbox modal)' => [
426         'http://example.com/not/a/video/url',
427         [
428           'type' => 'video_embed_field_colorbox',
429         ],
430         [
431           '#type' => 'container',
432           '#attributes' => [
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'],
435           ],
436           '#attached' => [
437             'library' => [
438               'video_embed_field/colorbox',
439               'video_embed_field/responsive-video',
440             ],
441           ],
442           '#cache' => [
443             'contexts' => [
444               'user.permissions',
445             ],
446           ],
447           'children' => [
448             '#theme' => 'video_embed_field_missing_provider',
449           ],
450         ],
451       ],
452     ];
453   }
454
455   /**
456    * Test the embed field.
457    *
458    * @dataProvider renderedFieldTestCases
459    */
460   public function testEmbedField($url, $settings, $expected_field_item_output) {
461
462     $field_output = $this->getPreparedFieldOutput($url, $settings);
463
464     // Assert the specific field output at delta 1 matches the expected test
465     // data.
466     $this->assertEquals($expected_field_item_output, $field_output[0]);
467   }
468
469   /**
470    * Get and prepare the output of a field.
471    *
472    * @param string $url
473    *   The video URL.
474    * @param array $settings
475    *   An array of formatter settings.
476    *
477    * @return array
478    *   The rendered prepared field output.
479    */
480   protected function getPreparedFieldOutput($url, $settings) {
481     $entity = EntityTest::create();
482     $entity->{$this->fieldName}->value = $url;
483     $entity->save();
484
485     $field_output = $this->container->get('renderer')->executeInRenderContext(new RenderContext(), function() use ($entity, $settings) {
486       return $entity->{$this->fieldName}->view($settings);
487     });
488
489     // Prepare the field output to make it easier to compare our test data
490     // values against.
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();
496       }
497       // Trim to prevent stray whitespace for the colorbox formatters with
498       // early rendering.
499       $value = $this->stripWhitespace($value);
500     });
501
502     return $field_output;
503   }
504
505 }