‪TYPO3CMS  9.5
YouTubeRendererTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪YouTubeRendererTest extends UnitTestCase
28 {
32  protected ‪$subject;
33 
37  protected function ‪setUp()
38  {
39  parent::setUp();
40  $_SERVER['HTTP_HOST'] = 'test.server.org';
41 
43  $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
44  $youTubeHelper->expects($this->any())->method('getOnlineMediaId')->will($this->returnValue('7331'));
45 
46  $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
47  $this->subject ->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youTubeHelper));
48  }
49 
53  public function ‪getPriorityReturnsCorrectValue()
54  {
55  $this->assertSame(1, $this->subject->getPriority());
56  }
57 
61  public function ‪canRenderReturnsTrueOnCorrectFile()
62  {
64  $fileResourceMock1 = $this->createMock(File::class);
65  $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('video/youtube'));
67  $fileResourceMock2 = $this->createMock(File::class);
68  $fileResourceMock2->expects($this->any())->method('getMimeType')->will($this->returnValue('video/unknown'));
69  $fileResourceMock2->expects($this->any())->method('getExtension')->will($this->returnValue('youtube'));
70 
71  $this->assertTrue($this->subject->canRender($fileResourceMock1));
72  $this->assertTrue($this->subject->canRender($fileResourceMock2));
73  }
74 
79  {
81  $fileResourceMock = $this->createMock(File::class);
82  $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/vimeo'));
83 
84  $this->assertFalse($this->subject->canRender($fileResourceMock));
85  }
86 
90  public function ‪renderOutputWithLoopIsCorrect()
91  {
93  $fileResourceMock = $this->createMock(File::class);
94 
95  $this->assertSame(
96  '<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>',
97  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'loop' => 1])
98  );
99  }
100 
104  public function ‪renderOutputWithAutoplayIsCorrect()
105  {
107  $fileResourceMock = $this->createMock(File::class);
108 
109  $this->assertSame(
110  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;autoplay=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($fileResourceMock, '300m', '200', ['controls' => 1, 'autoplay' => 1])
112  );
113  }
114 
119  {
121  $fileResourceMock = $this->createMock(File::class);
122 
124  $fileReferenceMock = $this->createMock(FileReference::class);
125  $fileReferenceMock->expects($this->any())->method('getProperty')->will($this->returnValue(1));
126  $fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);
127 
128  $this->assertSame(
129  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=1&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
130  $this->subject->render($fileReferenceMock, '300m', '200', ['controls' => 1])
131  );
132  }
133 
138  {
140  $fileResourceMock = $this->createMock(File::class);
141 
142  $this->assertSame(
143  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
144  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
145  );
146  }
147 
149  {
150  return [
151  'no options given, visible player controls (default)' => [
152  '<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>',
153  []
154  ],
155  'with option controls = foo as invalid string' => [
156  '<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>',
157  ['controls' => 'foo']
158  ],
159  'with option controls = true as string' => [
160  '<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>',
161  ['controls' => 'true']
162  ],
163  'with option controls = false as string' => [
164  '<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>',
165  ['controls' => 'false']
166  ],
167  'with option controls = true as boolean' => [
168  '<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>',
169  ['controls' => true]
170  ],
171  'with option controls = false as boolean, hide player controls' => [
172  '<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>',
173  ['controls' => false]
174  ],
175  'with option controls = 0 as string' => [
176  '<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>',
177  ['controls' => '0']
178  ],
179  'with option controls = 1 as string' => [
180  '<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>',
181  ['controls' => '1']
182  ],
183  'with option controls = 2 as string' => [
184  '<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>',
185  ['controls' => '2']
186  ],
187  'with option controls = 3 as string' => [
188  '<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>',
189  ['controls' => '3']
190  ],
191  'with option controls = negative number as string' => [
192  '<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>',
193  ['controls' => '-42']
194  ],
195  'with option controls = 0 as int' => [
196  '<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>',
197  ['controls' => 0]
198  ],
199  'with option controls = 1 as int' => [
200  '<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>',
201  ['controls' => 1]
202  ],
203  'with option controls = 2 as int' => [
204  '<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>',
205  ['controls' => 1]
206  ],
207  'with option controls = 3 as int' => [
208  '<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>',
209  ['controls' => 3]
210  ],
211  'with option controls = negative number as int' => [
212  '<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>',
213  ['controls' => -42]
214  ],
215  ];
216  }
217 
222  public function ‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
223  {
225  $fileResourceMock = $this->createMock(File::class);
226 
227  $this->assertSame(
228  $expected,
229  $this->subject->render($fileResourceMock, '300m', '200', $options)
230  );
231  }
232 
237  {
239  $fileResourceMock = $this->createMock(File::class);
240 
241  $this->assertSame(
242  '<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>',
243  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'relatedVideos' => 0])
244  );
245  }
246 
251  {
253  $fileResourceMock = $this->createMock(File::class);
254 
255  $this->assertSame(
256  '<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>',
257  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload', '<"\'>sanitize^&test' => '<>"\'test']])
258  );
259  }
260 
265  {
267  $fileResourceMock = $this->createMock(File::class);
268 
269  $this->assertSame(
270  '<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>',
271  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123']])
272  );
273  }
274 
279  {
281  $fileResourceMock = $this->createMock(File::class);
282 
283  $this->assertSame(
284  '<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>',
285  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123'], 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
286  );
287  }
288 
293  {
295  $fileResourceMock = $this->createMock(File::class);
296 
297  $this->assertSame(
298  '<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>',
299  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
300  );
301  }
302 
307  {
309  $fileResourceMock = $this->createMock(File::class);
310 
311  $this->assertSame(
312  '<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>',
313  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'modestbranding' => 1])
314  );
315  }
316 
321  {
323  $fileResourceMock = $this->createMock(File::class);
324 
325  $this->assertSame(
326  '<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>',
327  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
328  );
329  }
330 
335  {
337  $fileResourceMock = $this->createMock(File::class);
338 
339  $this->assertSame(
340  '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=0&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
341  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1, 'allow' => 'foo; bar'])
342  );
343  }
344 
348  public function ‪renderOutputIsEscaped()
349  {
351  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
352  $youtubeHelper->expects($this->any())->method('getOnlineMediaId')->will($this->returnValue('7331<script>danger</script>\'"random"quotes;'));
353 
354  ‪$subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
355  ‪$subject->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youtubeHelper));
356 
358  $fileResourceMock = $this->createMock(File::class);
359 
360  $this->assertSame(
361  '<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>',
362  ‪$subject->‪render($fileResourceMock, '300m', '200')
363  );
364  }
365 }
‪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:28
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAdditionalAttributes
‪renderOutputWithAdditionalAttributes()
Definition: YouTubeRendererTest.php:249
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowIsCorrect
‪renderOutputWithCustomAllowIsCorrect()
Definition: YouTubeRendererTest.php:319
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\$subject
‪YouTubeRenderer PHPUnit_Framework_MockObject_MockObject $subject
Definition: YouTubeRendererTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDisabledNoCookieIsCorrect
‪renderOutputWithDisabledNoCookieIsCorrect()
Definition: YouTubeRendererTest.php:291
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDefaultControlsIsCorrect
‪renderOutputWithDefaultControlsIsCorrect($expected, $options)
Definition: YouTubeRendererTest.php:221
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayAndWithoutControlsIsCorrect
‪renderOutputWithAutoplayAndWithoutControlsIsCorrect()
Definition: YouTubeRendererTest.php:136
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsFalseOnCorrectFile
‪canRenderReturnsFalseOnCorrectFile()
Definition: YouTubeRendererTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering
Definition: AudioTagRendererTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithRelatedVideosTurnedOffIsCorrect
‪renderOutputWithRelatedVideosTurnedOffIsCorrect()
Definition: YouTubeRendererTest.php:235
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayIsCorrect
‪renderOutputWithAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:103
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputIsEscaped
‪renderOutputIsEscaped()
Definition: YouTubeRendererTest.php:347
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithDataAttributesForCustomization
‪renderOutputWithDataAttributesForCustomization()
Definition: YouTubeRendererTest.php:263
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\getPriorityReturnsCorrectValue
‪getPriorityReturnsCorrectValue()
Definition: YouTubeRendererTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCustomAllowAndAutoplayIsCorrect
‪renderOutputWithCustomAllowAndAutoplayIsCorrect()
Definition: YouTubeRendererTest.php:333
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:27
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithCombinationOfDataAndAdditionalAttributes
‪renderOutputWithCombinationOfDataAndAdditionalAttributes()
Definition: YouTubeRendererTest.php:277
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithAutoplayFromFileReferenceIsCorrect
‪renderOutputWithAutoplayFromFileReferenceIsCorrect()
Definition: YouTubeRendererTest.php:117
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\setUp
‪setUp()
Definition: YouTubeRendererTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithControlsDataProvider
‪renderOutputWithControlsDataProvider()
Definition: YouTubeRendererTest.php:147
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\canRenderReturnsTrueOnCorrectFile
‪canRenderReturnsTrueOnCorrectFile()
Definition: YouTubeRendererTest.php:60
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper
Definition: YouTubeHelper.php:25
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: YouTubeRenderer.php:89
‪TYPO3\CMS\Core\Tests\Unit\Resource\Rendering\YouTubeRendererTest\renderOutputWithModestbrandingIsCorrect
‪renderOutputWithModestbrandingIsCorrect()
Definition: YouTubeRendererTest.php:305