‪TYPO3CMS  11.5
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 
20 use PHPUnit\Framework\MockObject\MockObject;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪YouTubeRendererTest extends UnitTestCase
31 {
35  protected ‪$subject;
36 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  $_SERVER['HTTP_HOST'] = 'test.server.org';
44 
45  $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
46  $youTubeHelper->method('getOnlineMediaId')->willReturn('7331');
47 
48  $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
49  $this->subject->method('getOnlineMediaHelper')->willReturn($youTubeHelper);
50  }
51 
55  public function ‪getPriorityReturnsCorrectValue(): void
56  {
57  self::assertSame(1, $this->subject->getPriority());
58  }
59 
63  public function ‪canRenderReturnsTrueOnCorrectFile(): void
64  {
65  $fileResourceMock1 = $this->createMock(File::class);
66  $fileResourceMock1->method('getMimeType')->willReturn('video/youtube');
67 
68  $fileResourceMock2 = $this->createMock(File::class);
69  $fileResourceMock2->method('getMimeType')->willReturn('video/unknown');
70  $fileResourceMock2->method('getExtension')->willReturn('youtube');
71 
72  self::assertTrue($this->subject->canRender($fileResourceMock1));
73  self::assertTrue($this->subject->canRender($fileResourceMock2));
74  }
75 
79  public function ‪canRenderReturnsFalseOnCorrectFile(): void
80  {
81  $fileResourceMock = $this->createMock(File::class);
82  $fileResourceMock->method('getMimeType')->willReturn('video/vimeo');
83 
84  self::assertFalse($this->subject->canRender($fileResourceMock));
85  }
86 
90  public function ‪renderOutputWithLoopIsCorrect(): void
91  {
92  $fileResourceMock = $this->createMock(File::class);
93 
94  self::assertSame(
95  '<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>',
96  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'loop' => 1])
97  );
98  }
99 
103  public function ‪renderOutputWithAutoplayIsCorrect(): void
104  {
105  $fileResourceMock = $this->createMock(File::class);
106 
107  self::assertSame(
108  '<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>',
109  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'autoplay' => 1])
110  );
111  }
112 
117  {
118  $fileResourceMock = $this->createMock(File::class);
119 
120  $fileReferenceMock = $this->createMock(FileReference::class);
121  $fileReferenceMock->method('getProperty')->willReturn(1);
122  $fileReferenceMock->method('getOriginalFile')->willReturn($fileResourceMock);
123 
124  self::assertSame(
125  '<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>',
126  $this->subject->render($fileReferenceMock, '300m', '200', ['controls' => 1])
127  );
128  }
129 
134  {
135  $fileResourceMock = $this->createMock(File::class);
136 
137  self::assertSame(
138  '<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>',
139  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
140  );
141  }
142 
143  public function ‪renderOutputWithControlsDataProvider(): array
144  {
145  return [
146  'no options given, visible player controls (default)' => [
147  '<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>',
148  [],
149  ],
150  'with option controls = foo as invalid string' => [
151  '<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>',
152  ['controls' => 'foo'],
153  ],
154  'with option controls = true as string' => [
155  '<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>',
156  ['controls' => 'true'],
157  ],
158  'with option controls = false as string' => [
159  '<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>',
160  ['controls' => 'false'],
161  ],
162  'with option controls = true as boolean' => [
163  '<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>',
164  ['controls' => true],
165  ],
166  'with option controls = false as boolean, hide player controls' => [
167  '<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>',
168  ['controls' => false],
169  ],
170  'with option controls = 0 as string' => [
171  '<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>',
172  ['controls' => '0'],
173  ],
174  'with option controls = 1 as string' => [
175  '<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>',
176  ['controls' => '1'],
177  ],
178  'with option controls = 2 as string' => [
179  '<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>',
180  ['controls' => '2'],
181  ],
182  'with option controls = 3 as string' => [
183  '<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>',
184  ['controls' => '3'],
185  ],
186  'with option controls = negative number as string' => [
187  '<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>',
188  ['controls' => '-42'],
189  ],
190  'with option controls = 0 as int' => [
191  '<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>',
192  ['controls' => 0],
193  ],
194  'with option controls = 1 as int' => [
195  '<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>',
196  ['controls' => 1],
197  ],
198  'with option controls = 2 as int' => [
199  '<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>',
200  ['controls' => 1],
201  ],
202  'with option controls = 3 as int' => [
203  '<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>',
204  ['controls' => 3],
205  ],
206  'with option controls = negative number as int' => [
207  '<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>',
208  ['controls' => -42],
209  ],
210  ];
211  }
212 
217  public function ‪renderOutputWithDefaultControlsIsCorrect($expected, $options): void
218  {
219  $fileResourceMock = $this->createMock(File::class);
220 
221  self::assertSame(
222  $expected,
223  $this->subject->render($fileResourceMock, '300m', '200', $options)
224  );
225  }
226 
231  {
232  $fileResourceMock = $this->createMock(File::class);
233 
234  self::assertSame(
235  '<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>',
236  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'relatedVideos' => 0])
237  );
238  }
239 
243  public function ‪renderOutputWithAdditionalAttributes(): void
244  {
245  $fileResourceMock = $this->createMock(File::class);
246 
247  self::assertSame(
248  '<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>',
249  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload', '<"\'>sanitize^&test' => '<>"\'test']])
250  );
251  }
252 
257  {
258  $fileResourceMock = $this->createMock(File::class);
259 
260  self::assertSame(
261  '<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>',
262  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123']])
263  );
264  }
265 
270  {
271  $fileResourceMock = $this->createMock(File::class);
272 
273  self::assertSame(
274  '<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>',
275  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123'], 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
276  );
277  }
278 
282  public function ‪renderOutputWithDisabledNoCookieIsCorrect(): void
283  {
284  $fileResourceMock = $this->createMock(File::class);
285 
286  self::assertSame(
287  '<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>',
288  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
289  );
290  }
291 
295  public function ‪renderOutputWithModestbrandingIsCorrect(): void
296  {
297  $fileResourceMock = $this->createMock(File::class);
298 
299  self::assertSame(
300  '<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>',
301  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'modestbranding' => 1])
302  );
303  }
304 
308  public function ‪renderOutputWithCustomAllowIsCorrect(): void
309  {
310  $fileResourceMock = $this->createMock(File::class);
311 
312  self::assertSame(
313  '<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>',
314  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
315  );
316  }
317 
322  {
323  $fileResourceMock = $this->createMock(File::class);
324 
325  self::assertSame(
326  '<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>',
327  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1, 'allow' => 'foo; bar'])
328  );
329  }
330 
334  public function ‪renderOutputIsEscaped(): void
335  {
336  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
337  $youtubeHelper->method('getOnlineMediaId')->willReturn('7331<script>danger</script>\'"random"quotes;');
338 
339  ‪$subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
340  ‪$subject->method('getOnlineMediaHelper')->willReturn($youtubeHelper);
341 
342  $fileResourceMock = $this->createMock(File::class);
343 
344  self::assertSame(
345  '<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>',
346  ‪$subject->‪render($fileResourceMock, '300m', '200')
347  );
348  }
349 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithLoopIsCorrect
‪renderOutputWithLoopIsCorrect()
Definition: YouTubeRendererTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest
Definition: YouTubeRendererTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAdditionalAttributes
‪renderOutputWithAdditionalAttributes()
Definition: YouTubeRendererTest.php:242
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowIsCorrect
‪renderOutputWithCustomAllowIsCorrect()
Definition: YouTubeRendererTest.php:307
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDisabledNoCookieIsCorrect
‪renderOutputWithDisabledNoCookieIsCorrect()
Definition: YouTubeRendererTest.php:281
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDefaultControlsIsCorrect
‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
Definition: YouTubeRendererTest.php:216
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayAndWithoutControlsIsCorrect
‪renderOutputWithAutoplayAndWithoutControlsIsCorrect()
Definition: YouTubeRendererTest.php:132
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsFalseOnCorrectFile
‪canRenderReturnsFalseOnCorrectFile()
Definition: YouTubeRendererTest.php:78
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithRelatedVideosTurnedOffIsCorrect
‪renderOutputWithRelatedVideosTurnedOffIsCorrect()
Definition: YouTubeRendererTest.php:229
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayIsCorrect
‪renderOutputWithAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputIsEscaped
‪renderOutputIsEscaped()
Definition: YouTubeRendererTest.php:333
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDataAttributesForCustomization
‪renderOutputWithDataAttributesForCustomization()
Definition: YouTubeRendererTest.php:255
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\$subject
‪YouTubeRenderer MockObject $subject
Definition: YouTubeRendererTest.php:34
‪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:320
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCombinationOfDataAndAdditionalAttributes
‪renderOutputWithCombinationOfDataAndAdditionalAttributes()
Definition: YouTubeRendererTest.php:268
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayFromFileReferenceIsCorrect
‪renderOutputWithAutoplayFromFileReferenceIsCorrect()
Definition: YouTubeRendererTest.php:115
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\setUp
‪setUp()
Definition: YouTubeRendererTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithControlsDataProvider
‪renderOutputWithControlsDataProvider()
Definition: YouTubeRendererTest.php:142
‪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:294