[TASK] Reduce logic in render methods of YouTube and Vimeo Renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Rendering / YouTubeRendererTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Rendering;
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
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper;
21 use TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Class YouTubeRendererTest
27 */
28 class YouTubeRendererTest extends UnitTestCase
29 {
30 /**
31 * @var YouTubeRenderer|\PHPUnit_Framework_MockObject_MockObject
32 */
33 protected $subject;
34
35 /**
36 * Set up the test
37 */
38 protected function setUp()
39 {
40 parent::setUp();
41 GeneralUtility::flushInternalRuntimeCaches();
42 $_SERVER['HTTP_HOST'] = 'test.server.org';
43
44 /** @var YouTubeHelper|\PHPUnit_Framework_MockObject_MockObject $youTubeHelper */
45 $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
46 $youTubeHelper->expects($this->any())->method('getOnlineMediaId')->will($this->returnValue('7331'));
47
48 $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
49 $this->subject ->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youTubeHelper));
50 }
51
52 /**
53 * @test
54 */
55 public function getPriorityReturnsCorrectValue()
56 {
57 $this->assertSame(1, $this->subject->getPriority());
58 }
59
60 /**
61 * @test
62 */
63 public function canRenderReturnsTrueOnCorrectFile()
64 {
65 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock1 */
66 $fileResourceMock1 = $this->createMock(File::class);
67 $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('video/youtube'));
68 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock2 */
69 $fileResourceMock2 = $this->createMock(File::class);
70 $fileResourceMock2->expects($this->any())->method('getMimeType')->will($this->returnValue('video/unknown'));
71 $fileResourceMock2->expects($this->any())->method('getExtension')->will($this->returnValue('youtube'));
72
73 $this->assertTrue($this->subject->canRender($fileResourceMock1));
74 $this->assertTrue($this->subject->canRender($fileResourceMock2));
75 }
76
77 /**
78 * @test
79 */
80 public function canRenderReturnsFalseOnCorrectFile()
81 {
82 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
83 $fileResourceMock = $this->createMock(File::class);
84 $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/vimeo'));
85
86 $this->assertFalse($this->subject->canRender($fileResourceMock));
87 }
88
89 /**
90 * @test
91 */
92 public function renderOutputWithLoopIsCorrect()
93 {
94 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
95 $fileResourceMock = $this->createMock(File::class);
96
97 $this->assertSame(
98 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;loop=1&amp;playlist=7331&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
99 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 2, 'loop' => 1])
100 );
101 }
102
103 /**
104 * @test
105 */
106 public function renderOutputWithAutoplayIsCorrect()
107 {
108 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
109 $fileResourceMock = $this->createMock(File::class);
110
111 $this->assertSame(
112 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
113 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 2, 'autoplay' => 1])
114 );
115 }
116
117 /**
118 * @test
119 */
120 public function renderOutputWithAutoplayFromFileReferenceIsCorrect()
121 {
122 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
123 $fileResourceMock = $this->createMock(File::class);
124
125 /** @var FileReference|\PHPUnit_Framework_MockObject_MockObject $fileReferenceMock */
126 $fileReferenceMock = $this->createMock(FileReference::class);
127 $fileReferenceMock->expects($this->any())->method('getProperty')->will($this->returnValue(1));
128 $fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);
129
130 $this->assertSame(
131 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
132 $this->subject->render($fileReferenceMock, '300m', '200', ['controls' => 2])
133 );
134 }
135
136 /**
137 * @test
138 */
139 public function renderOutputWithAutoplayAndWithoutControlsIsCorrect()
140 {
141 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
142 $fileResourceMock = $this->createMock(File::class);
143
144 $this->assertSame(
145 '<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&amp;showinfo=0" allowfullscreen width="300" height="200" allow="autoplay; fullscreen"></iframe>',
146 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
147 );
148 }
149
150 public function renderOutputWithControlsDataProvider()
151 {
152 return [
153 'no options given' => [
154 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
171 ['controls' => true]
172 ],
173 'with option controls = false as boolean' => [
174 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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&amp;showinfo=0" 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&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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&amp;showinfo=0" 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&amp;showinfo=0" 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=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
207 ['controls' => 2]
208 ],
209 'with option controls = 3 as int' => [
210 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" 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=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
215 ['controls' => -42]
216 ],
217 ];
218 }
219
220 /**
221 * @test
222 * @dataProvider renderOutputWithControlsDataProvider
223 */
224 public function renderOutputWithDefaultControlsIsCorrect($expected, $options)
225 {
226 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
227 $fileResourceMock = $this->createMock(File::class);
228
229 $this->assertSame(
230 $expected,
231 $this->subject->render($fileResourceMock, '300m', '200', $options)
232 );
233 }
234
235 /**
236 * @test
237 */
238 public function renderOutputWithRelatedVideosTurnedOffIsCorrect()
239 {
240 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
241 $fileResourceMock = $this->createMock(File::class);
242
243 $this->assertSame(
244 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;rel=0&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
245 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 2, 'relatedVideos' => 0])
246 );
247 }
248
249 /**
250 * @test
251 */
252 public function renderOutputWithAdditionalAttributes()
253 {
254 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
255 $fileResourceMock = $this->createMock(File::class);
256
257 $this->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&amp;showinfo=0" allowfullscreen foo="bar" custom-play="preload" width="300" height="200" allow="fullscreen"></iframe>',
259 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
260 );
261 }
262
263 /**
264 * @test
265 */
266 public function renderOutputWithDataAttributesForCustomization()
267 {
268 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
269 $fileResourceMock = $this->createMock(File::class);
270
271 $this->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&amp;showinfo=0" 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
277 /**
278 * @test
279 */
280 public function renderOutputWithCombinationOfDataAndAdditionalAttributes()
281 {
282 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
283 $fileResourceMock = $this->createMock(File::class);
284
285 $this->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&amp;showinfo=0" 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
291 /**
292 * @test
293 */
294 public function renderOutputWithDisabledNoCookieIsCorrect()
295 {
296 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
297 $fileResourceMock = $this->createMock(File::class);
298
299 $this->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&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
301 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
302 );
303 }
304
305 /**
306 * @test
307 */
308 public function renderOutputWithModestbrandingIsCorrect()
309 {
310 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
311 $fileResourceMock = $this->createMock(File::class);
312
313 $this->assertSame(
314 '<iframe src="https://www.youtube-nocookie.com/embed/7331?autohide=1&amp;controls=2&amp;modestbranding=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200" allow="fullscreen"></iframe>',
315 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 2, 'modestbranding' => 1])
316 );
317 }
318
319 /**
320 * @test
321 */
322 public function renderOutputWithCustomAllowIsCorrect()
323 {
324 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
325 $fileResourceMock = $this->createMock(File::class);
326
327 $this->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&amp;showinfo=0" allowfullscreen width="300" height="200" allow="foo; bar"></iframe>',
329 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'allow' => 'foo; bar'])
330 );
331 }
332
333 /**
334 * @test
335 */
336 public function renderOutputWithCustomAllowAndAutoplayIsCorrect()
337 {
338 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
339 $fileResourceMock = $this->createMock(File::class);
340
341 $this->assertSame(
342 '<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&amp;showinfo=0" 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 }