3 namespace Drupal\Tests\video_embed_field\Unit;
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;
11 * Test that URL parsing for various providers is functioning.
13 * @group video_embed_field
15 class ProviderUrlParseTest extends UnitTestCase {
18 * Test URL parsing works as expected.
20 * @dataProvider urlsWithExpectedIds
22 public function testUrlParsing($provider, $url, $expected) {
23 $this->assertEquals($expected, $provider::getIdFromInput($url));
27 * A data provider for URL parsing test cases.
30 * An array of test cases.
32 public function urlsWithExpectedIds() {
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',
40 'YouTube: Non HTTPS' => [
41 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
42 'http://www.youtube.com/watch?v=fdbFVWupSsw',
45 'YouTube: Non WWW' => [
46 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
47 'https://youtube.com/watch?v=fdbFVWupSsw',
50 'YouTube: Special Characters' => [
51 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
52 'https://youtube.com/watch?v=fdbFV_Wup-Ssw',
55 'YouTube: Short URL' => [
56 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
57 'https://youtu.be/fdbFVWupSsw',
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',
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',
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',
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',
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',
86 'YouTube: Malformed String' => [
87 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTube',
88 $this->randomMachineName(),
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',
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',
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',
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',
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',
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',
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',
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',
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',
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=',
143 'YouTube Playlist: No List' => [
144 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\YouTubePlaylist',
145 'https://www.youtube.com/watch?v=xoJH3qZwsHc',
148 // Vimeo passing cases.
149 'Vimeo: Normal URL' => [
150 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
151 'https://vimeo.com/138627894',
154 'Vimeo: WWW URL' => [
155 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
156 'https://www.vimeo.com/138627894',
159 'Vimeo: Non HTTPS' => [
160 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
161 'http://www.vimeo.com/138627894',
164 'Vimeo: Channel URL' => [
165 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
166 'https://vimeo.com/channels/staffpicks/138627894',
169 'Vimeo: Private Video' => [
170 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
171 'https://vimeo.com/173101914/aab5894fec',
174 'Vimeo: with timeindex' => [
175 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
176 'https://vimeo.com/193517656#t=160s',
179 // Vimeo failing cases.
180 'Vimeo: Malformed String' => [
181 'Drupal\video_embed_field\Plugin\video_embed_field\Provider\Vimeo',
182 $this->randomMachineName(),
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',
194 * Test the langauge parsing feature.
196 * @dataProvider languageParseTestCases
198 public function testYouTubeLanguageParsing($url, $expected) {
199 $provider = new YouTube([
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);
208 * A data provider for testYouTubeLanguageParsing.
211 * An array of test cases.
213 public function languageParseTestCases() {
215 'Simple Preference' => [
216 'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=fr',
219 'Preference with Hyphen' => [
220 'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=fr-ca',
223 'Invalid Language' => [
224 'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=<test>',
227 'Multiple Parameters' => [
228 'https://youtube.com/watch?v=fdbFV_Wup-Ssw&hl=au&anotherparam=1',
235 * Test the YouTube time index parsing.
237 * @dataProvider youTubeTimeIndexTestCases
239 public function testYouTubeTimeIndex($url, $expected) {
240 $provider = new YouTube([
242 ], '', [], new MockHttpClient());
243 $embed = $provider->renderEmbedCode(100, 100, TRUE);
244 $this->assertEquals($expected, $embed['#query']['start']);
248 * A data provider for testYouTubeTimeIndex.
251 * An array of test cases.
253 public function youTubeTimeIndexTestCases() {
255 'Simple Timeindex' => [
256 'https://www.youtube.com/watch?v=fdbFVWupSsw&t=15',
260 'https://www.youtube.com/watch?v=fdbFVWupSsw',
263 'Invalid Timeindex' => [
264 'https://www.youtube.com/watch?v=fdbFVWupSsw&t=time',
271 * Test the Vimeo time index integration.
273 * @dataProvider vimeoTimeIndexTestCases
275 public function testVimeoTimeIndex($url, $expected, $exception_expected = FALSE) {
276 $exception_triggered = FALSE;
278 $provider = new Vimeo([
280 ], '', [], new MockHttpClient());
282 catch (\Exception $e) {
283 $exception_triggered = TRUE;
286 $this->assertEquals($exception_expected, $exception_triggered);
288 if (!$exception_triggered) {
289 $embed = $provider->renderEmbedCode(100, 100, TRUE);
290 $this->assertEquals($expected, isset($embed['#fragment']) ? $embed['#fragment'] : FALSE);
295 * A data provider for testVimeoTimeIndex.
298 * An array of test cases.
300 public function vimeoTimeIndexTestCases() {
302 'Standard time index' => [
303 'https://vimeo.com/193517656#t=150s',
307 'Empty start time' => [
308 'https://vimeo.com/193517656#t=',
312 'Empty start time (with seconds)' => [
313 'https://vimeo.com/193517656#t=s',
317 'Non numeric start time' => [
318 'https://vimeo.com/193517656#t=STARTs',
322 'Non t fragment' => [
323 'https://vimeo.com/193517656#o=150s',
328 'https://vimeo.com/193517656#t=15',