‪TYPO3CMS  ‪main
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\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 final class ‪YouTubeRendererTest extends UnitTestCase
33 {
34  protected ‪YouTubeRenderer&MockObject ‪$subject;
35 
36  protected function ‪setUp(): void
37  {
38  parent::setUp();
39  ‪$_SERVER['HTTP_HOST'] = 'test.server.org';
40 
41  $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
42  $youTubeHelper->method('getOnlineMediaId')->willReturn('7331');
43 
44  $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper', 'shouldIncludeFrameBorderAttribute']);
45  $this->subject->method('shouldIncludeFrameBorderAttribute')->willReturn(false);
46  $this->subject->method('getOnlineMediaHelper')->willReturn($youTubeHelper);
47  }
48 
49  #[Test]
50  public function ‪getPriorityReturnsCorrectValue(): void
51  {
52  self::assertSame(1, $this->subject->getPriority());
53  }
54 
55  #[Test]
56  public function ‪canRenderReturnsTrueOnCorrectFile(): void
57  {
58  $fileResourceMock1 = $this->createMock(File::class);
59  $fileResourceMock1->method('getMimeType')->willReturn('video/youtube');
60 
61  $fileResourceMock2 = $this->createMock(File::class);
62  $fileResourceMock2->method('getMimeType')->willReturn('video/unknown');
63  $fileResourceMock2->method('getExtension')->willReturn('youtube');
64 
65  self::assertTrue($this->subject->canRender($fileResourceMock1));
66  self::assertTrue($this->subject->canRender($fileResourceMock2));
67  }
68 
69  #[Test]
70  public function ‪canRenderReturnsFalseOnCorrectFile(): void
71  {
72  $fileResourceMock = $this->createMock(File::class);
73  $fileResourceMock->method('getMimeType')->willReturn('video/vimeo');
74 
75  self::assertFalse($this->subject->canRender($fileResourceMock));
76  }
77 
78  #[Test]
79  public function ‪renderOutputWithLoopIsCorrect(): void
80  {
81  $fileResourceMock = $this->createMock(File::class);
82 
83  self::assertSame(
84  '<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>',
85  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'loop' => 1])
86  );
87  }
88 
89  #[Test]
90  public function ‪renderOutputWithAutoplayIsCorrect(): 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;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>',
96  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'autoplay' => 1])
97  );
98  }
99 
100  #[Test]
102  {
103  $fileResourceMock = $this->createMock(File::class);
104 
105  $fileReferenceMock = $this->createMock(FileReference::class);
106  $fileReferenceMock->method('getProperty')->willReturn(1);
107  $fileReferenceMock->method('getOriginalFile')->willReturn($fileResourceMock);
108 
109  self::assertSame(
110  '<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>',
111  $this->subject->render($fileReferenceMock, '300m', '200', ['controls' => 1])
112  );
113  }
114 
115  #[Test]
117  {
118  $fileResourceMock = $this->createMock(File::class);
119 
120  self::assertSame(
121  '<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>',
122  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
123  );
124  }
125 
126  public static function ‪renderOutputWithControlsDataProvider(): array
127  {
128  return [
129  'no options given, visible player controls (default)' => [
130  '<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>',
131  [],
132  ],
133  'with option controls = foo as invalid string' => [
134  '<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>',
135  ['controls' => 'foo'],
136  ],
137  'with option controls = true as string' => [
138  '<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>',
139  ['controls' => 'true'],
140  ],
141  'with option controls = false as string' => [
142  '<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>',
143  ['controls' => 'false'],
144  ],
145  'with option controls = true as boolean' => [
146  '<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>',
147  ['controls' => true],
148  ],
149  'with option controls = false as boolean, hide player controls' => [
150  '<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>',
151  ['controls' => false],
152  ],
153  'with option controls = 0 as string' => [
154  '<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>',
155  ['controls' => '0'],
156  ],
157  'with option controls = 1 as 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' => '1'],
160  ],
161  'with option controls = 2 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' => '2'],
164  ],
165  'with option controls = 3 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' => '3'],
168  ],
169  'with option controls = negative number as string' => [
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' => '-42'],
172  ],
173  'with option controls = 0 as int' => [
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' => 0],
176  ],
177  'with option controls = 1 as int' => [
178  '<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>',
179  ['controls' => 1],
180  ],
181  'with option controls = 2 as int' => [
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 = 3 as int' => [
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' => 3],
188  ],
189  'with option controls = negative number as int' => [
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' => -42],
192  ],
193  ];
194  }
195 
196  #[DataProvider('renderOutputWithControlsDataProvider')]
197  #[Test]
198  public function ‪renderOutputWithDefaultControlsIsCorrect($expected, $options): void
199  {
200  $fileResourceMock = $this->createMock(File::class);
201 
202  self::assertSame(
203  $expected,
204  $this->subject->render($fileResourceMock, '300m', '200', $options)
205  );
206  }
207 
208  #[Test]
210  {
211  $fileResourceMock = $this->createMock(File::class);
212 
213  self::assertSame(
214  '<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>',
215  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'relatedVideos' => 0])
216  );
217  }
218 
219  #[Test]
221  {
222  $fileResourceMock = $this->createMock(File::class);
223 
224  self::assertSame(
225  '<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>',
226  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload', '<"\'>sanitize^&test' => '<>"\'test']])
227  );
228  }
229 
230  #[Test]
232  {
233  $fileResourceMock = $this->createMock(File::class);
234 
235  self::assertSame(
236  '<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>',
237  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123']])
238  );
239  }
240 
241  #[Test]
243  {
244  $fileResourceMock = $this->createMock(File::class);
245 
246  self::assertSame(
247  '<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>',
248  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123'], 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
249  );
250  }
251 
252  #[Test]
254  {
255  $fileResourceMock = $this->createMock(File::class);
256 
257  self::assertSame(
258  '<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>',
259  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
260  );
261  }
262 
263  #[Test]
265  {
266  $fileResourceMock = $this->createMock(File::class);
267 
268  self::assertSame(
269  '<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>',
270  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'modestbranding' => 1])
271  );
272  }
273 
274  #[Test]
276  {
277  $fileResourceMock = $this->createMock(File::class);
278 
279  self::assertSame(
280  '<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>',
281  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
282  );
283  }
284 
285  #[Test]
287  {
288  $fileResourceMock = $this->createMock(File::class);
289 
290  self::assertSame(
291  '<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>',
292  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1, 'allow' => 'foo; bar'])
293  );
294  }
295 
296  #[Test]
297  public function ‪renderOutputIsEscaped(): void
298  {
299  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
300  $youtubeHelper->method('getOnlineMediaId')->willReturn('7331<script>danger</script>\'"random"quotes;');
301 
302  ‪$subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper', 'shouldIncludeFrameBorderAttribute']);
303  ‪$subject->method('shouldIncludeFrameBorderAttribute')->willReturn(false);
304  ‪$subject->method('getOnlineMediaHelper')->willReturn($youtubeHelper);
305 
306  $fileResourceMock = $this->createMock(File::class);
307 
308  self::assertSame(
309  '<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>',
310  ‪$subject->‪render($fileResourceMock, '300m', '200')
311  );
312  }
313 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithLoopIsCorrect
‪renderOutputWithLoopIsCorrect()
Definition: YouTubeRendererTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest
Definition: YouTubeRendererTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAdditionalAttributes
‪renderOutputWithAdditionalAttributes()
Definition: YouTubeRendererTest.php:220
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowIsCorrect
‪renderOutputWithCustomAllowIsCorrect()
Definition: YouTubeRendererTest.php:275
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDisabledNoCookieIsCorrect
‪renderOutputWithDisabledNoCookieIsCorrect()
Definition: YouTubeRendererTest.php:253
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDefaultControlsIsCorrect
‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
Definition: YouTubeRendererTest.php:198
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayAndWithoutControlsIsCorrect
‪renderOutputWithAutoplayAndWithoutControlsIsCorrect()
Definition: YouTubeRendererTest.php:116
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsFalseOnCorrectFile
‪canRenderReturnsFalseOnCorrectFile()
Definition: YouTubeRendererTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithRelatedVideosTurnedOffIsCorrect
‪renderOutputWithRelatedVideosTurnedOffIsCorrect()
Definition: YouTubeRendererTest.php:209
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayIsCorrect
‪renderOutputWithAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:90
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputIsEscaped
‪renderOutputIsEscaped()
Definition: YouTubeRendererTest.php:297
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDataAttributesForCustomization
‪renderOutputWithDataAttributesForCustomization()
Definition: YouTubeRendererTest.php:231
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪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:50
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowAndAutoplayIsCorrect
‪renderOutputWithCustomAllowAndAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:286
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:30
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCombinationOfDataAndAdditionalAttributes
‪renderOutputWithCombinationOfDataAndAdditionalAttributes()
Definition: YouTubeRendererTest.php:242
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithControlsDataProvider
‪static renderOutputWithControlsDataProvider()
Definition: YouTubeRendererTest.php:126
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayFromFileReferenceIsCorrect
‪renderOutputWithAutoplayFromFileReferenceIsCorrect()
Definition: YouTubeRendererTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\setUp
‪setUp()
Definition: YouTubeRendererTest.php:36
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[])
Definition: YouTubeRenderer.php:88
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsTrueOnCorrectFile
‪canRenderReturnsTrueOnCorrectFile()
Definition: YouTubeRendererTest.php:56
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper
Definition: YouTubeHelper.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithModestbrandingIsCorrect
‪renderOutputWithModestbrandingIsCorrect()
Definition: YouTubeRendererTest.php:264