TYPO3 CMS  TYPO3_8-7
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 
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 
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 
78  public function canRenderReturnsFalseOnCorrectFile()
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 
118  public function renderOutputWithAutoplayFromFileReferenceIsCorrect()
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 
137  public function renderOutputWithAutoplayAndWithoutControlsIsCorrect()
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 
236  public function renderOutputWithRelatedVideosTurnedOffIsCorrect()
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 
250  public function renderOutputWithDisabledNoCookieIsCorrect()
251  {
253  $fileResourceMock = $this->createMock(File::class);
254 
255  $this->assertSame(
256  '<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>',
257  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
258  );
259  }
260 
264  public function renderOutputWithModestbrandingIsCorrect()
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=1&amp;modestbranding=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
271  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 1, 'modestbranding' => 1])
272  );
273  }
274 
278  public function renderOutputWithCustomAllowIsCorrect()
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 width="300" height="200" allow="foo; bar"></iframe>',
285  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
286  );
287  }
288 
292  public function renderOutputWithCustomAllowAndAutoplayIsCorrect()
293  {
295  $fileResourceMock = $this->createMock(File::class);
296 
297  $this->assertSame(
298  '<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>',
299  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1, 'allow' => 'foo; bar'])
300  );
301  }
302 
306  public function renderOutputIsEscaped()
307  {
309  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
310  $youtubeHelper->expects($this->any())->method('getOnlineMediaId')->will($this->returnValue('7331<script>danger</script>\'"random"quotes;'));
311 
312  $subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
313  $subject->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youtubeHelper));
314 
316  $fileResourceMock = $this->createMock(File::class);
317 
318  $this->assertSame(
319  '<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>',
320  $subject->render($fileResourceMock, '300m', '200')
321  );
322  }
323 }