‪TYPO3CMS  10.4
YouTubeRendererTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪YouTubeRendererTest extends UnitTestCase
30 {
34  protected ‪$subject;
35 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  $_SERVER['HTTP_HOST'] = 'test.server.org';
43 
45  $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
46  $youTubeHelper->expects(self::any())->method('getOnlineMediaId')->willReturn('7331');
47 
48  $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
49  $this->subject ->expects(self::any())->method('getOnlineMediaHelper')->willReturn($youTubeHelper);
50  }
51 
55  public function ‪getPriorityReturnsCorrectValue()
56  {
57  self::assertSame(1, $this->subject->getPriority());
58  }
59 
63  public function ‪canRenderReturnsTrueOnCorrectFile()
64  {
66  $fileResourceMock1 = $this->createMock(File::class);
67  $fileResourceMock1->expects(self::any())->method('getMimeType')->willReturn('video/youtube');
69  $fileResourceMock2 = $this->createMock(File::class);
70  $fileResourceMock2->expects(self::any())->method('getMimeType')->willReturn('video/unknown');
71  $fileResourceMock2->expects(self::any())->method('getExtension')->willReturn('youtube');
72 
73  self::assertTrue($this->subject->canRender($fileResourceMock1));
74  self::assertTrue($this->subject->canRender($fileResourceMock2));
75  }
76 
81  {
83  $fileResourceMock = $this->createMock(File::class);
84  $fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('video/vimeo');
85 
86  self::assertFalse($this->subject->canRender($fileResourceMock));
87  }
88 
92  public function ‪renderOutputWithLoopIsCorrect()
93  {
95  $fileResourceMock = $this->createMock(File::class);
96 
97  self::assertSame(
98  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;loop=1&amp;playlist=7331&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
99  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'loop' => 1])
100  );
101  }
102 
106  public function ‪renderOutputWithAutoplayIsCorrect()
107  {
109  $fileResourceMock = $this->createMock(File::class);
110 
111  self::assertSame(
112  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;autoplay=1&amp;mute=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
113  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'autoplay' => 1])
114  );
115  }
116 
121  {
123  $fileResourceMock = $this->createMock(File::class);
124 
126  $fileReferenceMock = $this->createMock(FileReference::class);
127  $fileReferenceMock->expects(self::any())->method('getProperty')->willReturn(1);
128  $fileReferenceMock->expects(self::any())->method('getOriginalFile')->willReturn($fileResourceMock);
129 
130  self::assertSame(
131  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;autoplay=1&amp;mute=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
132  $this->subject->render($fileReferenceMock, '300m', '200', ['controls' => 1])
133  );
134  }
135 
140  {
142  $fileResourceMock = $this->createMock(File::class);
143 
144  self::assertSame(
145  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;autoplay=1&amp;mute=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
146  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
147  );
148  }
149 
151  {
152  return [
153  'no options given, visible player controls (default)' => [
154  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
155  []
156  ],
157  'with option controls = foo as invalid string' => [
158  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
159  ['controls' => 'foo']
160  ],
161  'with option controls = true as string' => [
162  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
163  ['controls' => 'true']
164  ],
165  'with option controls = false as string' => [
166  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
167  ['controls' => 'false']
168  ],
169  'with option controls = true as boolean' => [
170  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
171  ['controls' => true]
172  ],
173  'with option controls = false as boolean, hide player controls' => [
174  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
175  ['controls' => false]
176  ],
177  'with option controls = 0 as string' => [
178  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
179  ['controls' => '0']
180  ],
181  'with option controls = 1 as string' => [
182  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
183  ['controls' => '1']
184  ],
185  'with option controls = 2 as string' => [
186  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
187  ['controls' => '2']
188  ],
189  'with option controls = 3 as string' => [
190  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
191  ['controls' => '3']
192  ],
193  'with option controls = negative number as string' => [
194  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
195  ['controls' => '-42']
196  ],
197  'with option controls = 0 as int' => [
198  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
199  ['controls' => 0]
200  ],
201  'with option controls = 1 as int' => [
202  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
203  ['controls' => 1]
204  ],
205  'with option controls = 2 as int' => [
206  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
207  ['controls' => 1]
208  ],
209  'with option controls = 3 as int' => [
210  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
211  ['controls' => 3]
212  ],
213  'with option controls = negative number as int' => [
214  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
215  ['controls' => -42]
216  ],
217  ];
218  }
219 
224  public function ‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
225  {
227  $fileResourceMock = $this->createMock(File::class);
228 
229  self::assertSame(
230  $expected,
231  $this->subject->render($fileResourceMock, '300m', '200', $options)
232  );
233  }
234 
239  {
241  $fileResourceMock = $this->createMock(File::class);
242 
243  self::assertSame(
244  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;rel=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
245  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'relatedVideos' => 0])
246  );
247  }
248 
253  {
255  $fileResourceMock = $this->createMock(File::class);
256 
257  self::assertSame(
258  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen foo="bar" custom-play="preload" sanitizetest="&lt;&gt;&quot;&apos;test" width="300" height="200" allow="fullscreen"></iframe>',
259  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload', '<"\'>sanitize^&test' => '<>"\'test']])
260  );
261  }
262 
267  {
269  $fileResourceMock = $this->createMock(File::class);
270 
271  self::assertSame(
272  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen data-player-handler="youTube" data-custom-playerId="player-123" width="300" height="200" allow="fullscreen"></iframe>',
273  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123']])
274  );
275  }
276 
281  {
283  $fileResourceMock = $this->createMock(File::class);
284 
285  self::assertSame(
286  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen foo="bar" custom-play="preload" data-player-handler="youTube" data-custom-playerId="player-123" width="300" height="200" allow="fullscreen"></iframe>',
287  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123'], 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
288  );
289  }
290 
295  {
297  $fileResourceMock = $this->createMock(File::class);
298 
299  self::assertSame(
300  '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
301  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
302  );
303  }
304 
309  {
311  $fileResourceMock = $this->createMock(File::class);
312 
313  self::assertSame(
314  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;modestbranding=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
315  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'modestbranding' => 1])
316  );
317  }
318 
323  {
325  $fileResourceMock = $this->createMock(File::class);
326 
327  self::assertSame(
328  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
329  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
330  );
331  }
332 
337  {
339  $fileResourceMock = $this->createMock(File::class);
340 
341  self::assertSame(
342  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;autoplay=1&amp;mute=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
343  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1, 'allow' => 'foo; bar'])
344  );
345  }
346 
350  public function ‪renderOutputIsEscaped()
351  {
353  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
354  $youtubeHelper->expects(self::any())->method('getOnlineMediaId')->willReturn('7331<script>danger</script>\'"random"quotes;');
355 
356  ‪$subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
357  ‪$subject->expects(self::any())->method('getOnlineMediaHelper')->willReturn($youtubeHelper);
358 
360  $fileResourceMock = $this->createMock(File::class);
361 
362  self::assertSame(
363  '<iframe src="https://www.youtube-nocookie.com/embed/7331%3Cscript%3Edanger%3C%2Fscript%3E%27%22random%22quotes%3B?autohide=1&amp;controls=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
364  ‪$subject->‪render($fileResourceMock, '300m', '200')
365  );
366  }
367 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithLoopIsCorrect
‪renderOutputWithLoopIsCorrect()
Definition: YouTubeRendererTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest
Definition: YouTubeRendererTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAdditionalAttributes
‪renderOutputWithAdditionalAttributes()
Definition: YouTubeRendererTest.php:251
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowIsCorrect
‪renderOutputWithCustomAllowIsCorrect()
Definition: YouTubeRendererTest.php:321
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDisabledNoCookieIsCorrect
‪renderOutputWithDisabledNoCookieIsCorrect()
Definition: YouTubeRendererTest.php:293
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDefaultControlsIsCorrect
‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
Definition: YouTubeRendererTest.php:223
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayAndWithoutControlsIsCorrect
‪renderOutputWithAutoplayAndWithoutControlsIsCorrect()
Definition: YouTubeRendererTest.php:138
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\$subject
‪YouTubeRenderer PHPUnit Framework MockObject MockObject $subject
Definition: YouTubeRendererTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsFalseOnCorrectFile
‪canRenderReturnsFalseOnCorrectFile()
Definition: YouTubeRendererTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithRelatedVideosTurnedOffIsCorrect
‪renderOutputWithRelatedVideosTurnedOffIsCorrect()
Definition: YouTubeRendererTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayIsCorrect
‪renderOutputWithAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputIsEscaped
‪renderOutputIsEscaped()
Definition: YouTubeRendererTest.php:349
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDataAttributesForCustomization
‪renderOutputWithDataAttributesForCustomization()
Definition: YouTubeRendererTest.php:265
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\getPriorityReturnsCorrectValue
‪getPriorityReturnsCorrectValue()
Definition: YouTubeRendererTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowAndAutoplayIsCorrect
‪renderOutputWithCustomAllowAndAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:335
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCombinationOfDataAndAdditionalAttributes
‪renderOutputWithCombinationOfDataAndAdditionalAttributes()
Definition: YouTubeRendererTest.php:279
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayFromFileReferenceIsCorrect
‪renderOutputWithAutoplayFromFileReferenceIsCorrect()
Definition: YouTubeRendererTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\setUp
‪setUp()
Definition: YouTubeRendererTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithControlsDataProvider
‪renderOutputWithControlsDataProvider()
Definition: YouTubeRendererTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsTrueOnCorrectFile
‪canRenderReturnsTrueOnCorrectFile()
Definition: YouTubeRendererTest.php:62
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper
Definition: YouTubeHelper.php:26
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: YouTubeRenderer.php:91
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithModestbrandingIsCorrect
‪renderOutputWithModestbrandingIsCorrect()
Definition: YouTubeRendererTest.php:307