57908066f1f9dfd225fa24223697650896efb71e
[yaffs-website] / web / modules / contrib / video_embed_field / tests / src / Unit / ProviderUrlParseTest.php
1 <?php
2
3 namespace Drupal\Tests\video_embed_field\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Tests\video_embed_field\Kernel\MockHttpClient;
7 use Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo;
8 use Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube;
9
10 /**
11  * Test that URL parsing for various providers is functioning.
12  *
13  * @group video_embed_field
14  */
15 class ProviderUrlParseTest extends UnitTestCase {
16
17   /**
18    * Test URL parsing works as expected.
19    *
20    * @dataProvider urlsWithExpectedIds
21    */
22   public function testUrlParsing($provider, $url, $expected) {
23     $this->assertEquals($expected, $provider::getIdFromInput($url));
24   }
25
26   /**
27    * A data provider for URL parsing test cases.
28    *
29    * @return array
30    *   An array of test cases.
31    */
32   public function urlsWithExpectedIds() {
33     return [
34       // Youtube passing cases.
35       'YouTube: Standard URL' => [
36         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
37         'https://www.youtube.com/watch?v=fdbFVWupSsw',
38         'fdbFVWupSsw',
39       ],
40       'YouTube: Non HTTPS' => [
41         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
42         'http://www.youtube.com/watch?v=fdbFVWupSsw',
43         'fdbFVWupSsw',
44       ],
45       'YouTube: Non WWW' => [
46         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
47         'https://youtube.com/watch?v=fdbFVWupSsw',
48         'fdbFVWupSsw',
49       ],
50       'YouTube: Special Characters' => [
51         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
52         'https://youtube.com/watch?v=fdbFV_Wup-Ssw',
53         'fdbFV_Wup-Ssw',
54       ],
55       'YouTube: Short URL' => [
56         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
57         'https://youtu.be/fdbFVWupSsw',
58         'fdbFVWupSsw',
59       ],
60       'YouTube: With Language Preference' => [
61         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
62         'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=fr-ca',
63         'fdbFV_Wup-Ssw',
64       ],
65       'YouTube: Added Query String' => [
66         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
67         'https://youtube.com/watch?v=fdbFVWupSsw&some_param=value&t=150',
68         'fdbFVWupSsw',
69       ],
70       'YouTube: Added Query String in first position' => [
71         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
72         'https://youtube.com/watch?feature=player_detailpage&v=fdbFV_Wup-Ssw',
73         'fdbFV_Wup-Ssw',
74       ],
75       'YouTube: Short URL Added Query String' => [
76         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
77         'https://youtu.be/fdbFVWupSsw?some_param=other&another=something&t=55',
78         'fdbFVWupSsw',
79       ],
80       // Youtube failing cases.
81       'YouTube: Non-youtube domain with ?v param' => [
82         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
83         'https://www.otherdomain.com/watch?v=fdbFVWupSsw',
84         FALSE,
85       ],
86       'YouTube: Malformed String' => [
87         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
88         $this->randomMachineName(),
89         FALSE,
90       ],
91       'YouTube: Playlist URL' => [
92         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
93         'https://www.youtube.com/watch?v=-A2Nc3TRpi0&list=PLs4n2zZ8S1eszdZZwDSQ1G8iP95DmJHSh',
94         FALSE,
95       ],
96       'YouTube: Playlist URL (reversed params)' => [
97         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
98         'https://www.youtube.com/watch?list=PLs4n2zZ8S1eszdZZwDSQ1G8iP95DmJHSh&v=-A2Nc3TRpi0',
99         FALSE,
100       ],
101       // Youtube Playlists passing cases.
102       'YouTube Playlist' => [
103         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
104         'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
105         'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
106       ],
107       'YouTube Playlist: Reversed param order' => [
108         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
109         'https://www.youtube.com/watch?list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB&v=xoJH3qZwsHc',
110         'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
111       ],
112       'YouTube Playlist: Underscore in ID' => [
113         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
114         'https://www.youtube.com/watch?list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e8_7X5eB&v=xoJH3qZwsHc',
115         'PLpeDXSh4nHjQCIZmkxg3VSdpR5e8_7X5eB',
116       ],
117       'YouTube Playlist: No HTTPs' => [
118         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
119         'http://www.youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
120         'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
121       ],
122       'YouTube Playlist: No www' => [
123         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
124         'https://youtube.com/watch?v=xoJH3qZwsHc&list=PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
125         'PLpeDXSh4nHjQCIZmkxg3VSdpR5e87X5eB',
126       ],
127       'Youtube Playlist: Hyphens' => [
128         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
129         'https://www.youtube.com/watch?list=PLg7vT2Yor-Q72v4NPNlWXWmT6iJ4t___k&v=5gdSMPaJOf4',
130         'PLg7vT2Yor-Q72v4NPNlWXWmT6iJ4t___k',
131       ],
132       // Youtube Playlists failing cases.
133       'YouTube Playlist: Invalid ID' => [
134         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
135         'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=!@#123',
136         FALSE,
137       ],
138       'YouTube Playlist: No ID' => [
139         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
140         'https://www.youtube.com/watch?v=xoJH3qZwsHc&list=',
141         FALSE,
142       ],
143       'YouTube Playlist: No List' => [
144         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
145         'https://www.youtube.com/watch?v=xoJH3qZwsHc',
146         FALSE,
147       ],
148       // Vimeo passing cases.
149       'Vimeo: Normal URL' => [
150         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
151         'https://vimeo.com/138627894',
152         '138627894',
153       ],
154       'Vimeo: WWW URL' => [
155         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
156         'https://www.vimeo.com/138627894',
157         '138627894',
158       ],
159       'Vimeo: Non HTTPS' => [
160         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
161         'http://www.vimeo.com/138627894',
162         '138627894',
163       ],
164       'Vimeo: Channel URL' => [
165         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
166         'https://vimeo.com/channels/staffpicks/138627894',
167         '138627894',
168       ],
169       'Vimeo: Private Video' => [
170         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
171         'https://vimeo.com/173101914/aab5894fec',
172         '173101914',
173       ],
174       'Vimeo: with timeindex' => [
175         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
176         'https://vimeo.com/193517656#t=160s',
177         '193517656',
178       ],
179       // Vimeo failing cases.
180       'Vimeo: Malformed String' => [
181         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
182         $this->randomMachineName(),
183         FALSE,
184       ],
185       'Vimeo: Non numeric channel page' => [
186         'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
187         'https://vimeo.com/channels/staffpicks/some-page',
188         FALSE,
189       ],
190     ];
191   }
192
193   /**
194    * Test the langauge parsing feature.
195    *
196    * @dataProvider languageParseTestCases
197    */
198   public function testYouTubeLanguageParsing($url, $expected) {
199     $provider = new YouTube([
200       'input' => $url,
201     ], '', [], new MockHttpClient());
202     $embed = $provider->renderEmbedCode(100, 100, TRUE);
203     $language = isset($embed['#query']['cc_lang_pref']) ? $embed['#query']['cc_lang_pref'] : FALSE;
204     $this->assertEquals($expected, $language);
205   }
206
207   /**
208    * A data provider for testYouTubeLanguageParsing.
209    *
210    * @return array
211    *   An array of test cases.
212    */
213   public function languageParseTestCases() {
214     return [
215       'Simple Preference' => [
216         'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=fr',
217         'fr',
218       ],
219       'Preference with Hyphen' => [
220         'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=fr-ca',
221         'fr-ca',
222       ],
223       'Invalid Language' => [
224         'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=<test>',
225         FALSE,
226       ],
227       'Multiple Parameters' => [
228         'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=au&anotherparam=1',
229         'au',
230       ],
231     ];
232   }
233
234   /**
235    * Test the YouTube time index parsing.
236    *
237    * @dataProvider youTubeTimeIndexTestCases
238    */
239   public function testYouTubeTimeIndex($url, $expected) {
240     $provider = new YouTube([
241       'input' => $url,
242     ], '', [], new MockHttpClient());
243     $embed = $provider->renderEmbedCode(100, 100, TRUE);
244     $this->assertEquals($expected, $embed['#query']['start']);
245   }
246
247   /**
248    * A data provider for testYouTubeTimeIndex.
249    *
250    * @return array
251    *   An array of test cases.
252    */
253   public function youTubeTimeIndexTestCases() {
254     return [
255       'Simple Timeindex' => [
256         'https://www.youtube.com/watch?v=fdbFVWupSsw&t=15',
257         '15',
258       ],
259       'No Timeindex' => [
260         'https://www.youtube.com/watch?v=fdbFVWupSsw',
261         '0',
262       ],
263       'Invalid Timeindex' => [
264         'https://www.youtube.com/watch?v=fdbFVWupSsw&t=time',
265         '0',
266       ],
267     ];
268   }
269
270   /**
271    * Test the Vimeo time index integration.
272    *
273    * @dataProvider vimeoTimeIndexTestCases
274    */
275   public function testVimeoTimeIndex($url, $expected, $exception_expected = FALSE) {
276     $exception_triggered = FALSE;
277     try {
278       $provider = new Vimeo([
279         'input' => $url,
280       ], '', [], new MockHttpClient());
281     }
282     catch (\Exception $e) {
283       $exception_triggered = TRUE;
284     }
285
286     $this->assertEquals($exception_expected, $exception_triggered);
287
288     if (!$exception_triggered) {
289       $embed = $provider->renderEmbedCode(100, 100, TRUE);
290       $this->assertEquals($expected, isset($embed['#fragment']) ? $embed['#fragment'] : FALSE);
291     }
292   }
293
294   /**
295    * A data provider for testVimeoTimeIndex.
296    *
297    * @return array
298    *   An array of test cases.
299    */
300   public function vimeoTimeIndexTestCases() {
301     return [
302       'Standard time index' => [
303         'https://vimeo.com/193517656#t=150s',
304         't=150s',
305         FALSE,
306       ],
307       'Empty start time' => [
308         'https://vimeo.com/193517656#t=',
309         NULL,
310         TRUE,
311       ],
312       'Empty start time (with seconds)' => [
313         'https://vimeo.com/193517656#t=s',
314         NULL,
315         TRUE,
316       ],
317       'Non numeric start time' => [
318         'https://vimeo.com/193517656#t=STARTs',
319         NULL,
320         TRUE,
321       ],
322       'Non t fragment' => [
323         'https://vimeo.com/193517656#o=150s',
324         NULL,
325         TRUE,
326       ],
327       'No seconds' => [
328         'https://vimeo.com/193517656#t=15',
329         NULL,
330         TRUE,
331       ],
332     ];
333   }
334
335 }