[FEATURE] Render additionalAttributes and data-* attr in media renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Rendering / YouTubeRendererTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Rendering;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper;
20 use TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Class YouTubeRendererTest
25 */
26 class YouTubeRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
28 /**
29 * @var YouTubeRenderer|\PHPUnit_Framework_MockObject_MockObject
30 */
31 protected $subject;
32
33 /**
34 * Set up the test
35 */
36 protected function setUp()
37 {
38 parent::setUp();
39 GeneralUtility::flushInternalRuntimeCaches();
40 $_SERVER['HTTP_HOST'] = 'test.server.org';
41
42 /** @var YouTubeHelper|\PHPUnit_Framework_MockObject_MockObject $youTubeHelper */
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
50 /**
51 * @test
52 */
53 public function getPriorityReturnsCorrectValue()
54 {
55 $this->assertSame(1, $this->subject->getPriority());
56 }
57
58 /**
59 * @test
60 */
61 public function canRenderReturnsTrueOnCorrectFile()
62 {
63 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock1 */
64 $fileResourceMock1 = $this->createMock(File::class);
65 $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('video/youtube'));
66 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock2 */
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
75 /**
76 * @test
77 */
78 public function canRenderReturnsFalseOnCorrectFile()
79 {
80 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
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
87 /**
88 * @test
89 */
90 public function renderOutputWithLoopIsCorrect()
91 {
92 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
93 $fileResourceMock = $this->createMock(File::class);
94
95 $this->assertSame(
96 '<iframe src="https://www.youtube.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"></iframe>',
97 $this->subject->render($fileResourceMock, '300m', '200', ['loop' => 1])
98 );
99 }
100
101 /**
102 * @test
103 */
104 public function renderOutputWithAutoplayIsCorrect()
105 {
106 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
107 $fileResourceMock = $this->createMock(File::class);
108
109 $this->assertSame(
110 '<iframe src="https://www.youtube.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"></iframe>',
111 $this->subject->render($fileResourceMock, '300m', '200', ['autoplay' => 1])
112 );
113 }
114
115 /**
116 * @test
117 */
118 public function renderOutputWithAutoplayFromFileReferenceIsCorrect()
119 {
120 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
121 $fileResourceMock = $this->createMock(File::class);
122
123 /** @var FileReference|\PHPUnit_Framework_MockObject_MockObject $fileReferenceMock */
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.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"></iframe>',
130 $this->subject->render($fileReferenceMock, '300m', '200')
131 );
132 }
133
134 /**
135 * @test
136 */
137 public function renderOutputWithAutoplayAndWithoutControlsIsCorrect()
138 {
139 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
140 $fileResourceMock = $this->createMock(File::class);
141
142 $this->assertSame(
143 '<iframe src="https://www.youtube.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"></iframe>',
144 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
145 );
146 }
147
148 public function renderOutputWithControlsDataProvider()
149 {
150 return [
151 'no options given' => [
152 '<iframe src="https://www.youtube.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"></iframe>',
153 null
154 ],
155 'with option controls = foo as invalid string' => [
156 '<iframe src="https://www.youtube.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"></iframe>',
157 ['controls' => 'foo']
158 ],
159 'with option controls = true as string' => [
160 '<iframe src="https://www.youtube.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"></iframe>',
161 ['controls' => 'true']
162 ],
163 'with option controls = false as string' => [
164 '<iframe src="https://www.youtube.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"></iframe>',
165 ['controls' => 'false']
166 ],
167 'with option controls = true as boolean' => [
168 '<iframe src="https://www.youtube.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"></iframe>',
169 ['controls' => true]
170 ],
171 'with option controls = false as boolean' => [
172 '<iframe src="https://www.youtube.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"></iframe>',
173 ['controls' => false]
174 ],
175 'with option controls = 0 as string' => [
176 '<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"></iframe>',
177 ['controls' => '0']
178 ],
179 'with option controls = 1 as string' => [
180 '<iframe src="https://www.youtube.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"></iframe>',
181 ['controls' => '1']
182 ],
183 'with option controls = 2 as string' => [
184 '<iframe src="https://www.youtube.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"></iframe>',
185 ['controls' => '2']
186 ],
187 'with option controls = 3 as string' => [
188 '<iframe src="https://www.youtube.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"></iframe>',
189 ['controls' => '3']
190 ],
191 'with option controls = negative number as string' => [
192 '<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"></iframe>',
193 ['controls' => '-42']
194 ],
195 'with option controls = 0 as int' => [
196 '<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"></iframe>',
197 ['controls' => 0]
198 ],
199 'with option controls = 1 as int' => [
200 '<iframe src="https://www.youtube.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"></iframe>',
201 ['controls' => 1]
202 ],
203 'with option controls = 2 as int' => [
204 '<iframe src="https://www.youtube.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"></iframe>',
205 ['controls' => 2]
206 ],
207 'with option controls = 3 as int' => [
208 '<iframe src="https://www.youtube.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"></iframe>',
209 ['controls' => 3]
210 ],
211 'with option controls = negative number as int' => [
212 '<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"></iframe>',
213 ['controls' => -42]
214 ],
215 ];
216 }
217
218 /**
219 * @test
220 * @dataProvider renderOutputWithControlsDataProvider
221 */
222 public function renderOutputWithDefaultControlsIsCorrect($expected, $options)
223 {
224 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
225 $fileResourceMock = $this->createMock(File::class);
226
227 $this->assertSame(
228 $expected,
229 $this->subject->render($fileResourceMock, '300m', '200', $options)
230 );
231 }
232
233 /**
234 * @test
235 */
236 public function renderOutputWithRelatedVideosTurnedOffIsCorrect()
237 {
238 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
239 $fileResourceMock = $this->createMock(File::class);
240
241 $this->assertSame(
242 '<iframe src="https://www.youtube.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"></iframe>',
243 $this->subject->render($fileResourceMock, '300m', '200', ['relatedVideos' => 0])
244 );
245 }
246
247 /**
248 * @test
249 */
250 public function renderOutputWithAdditionalAttributes()
251 {
252 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
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&amp;showinfo=0" allowfullscreen foo="bar" custom-play="preload" width="300" height="200"></iframe>',
257 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'additionalAttributes' => ['foo' => 'bar', 'custom-play' => 'preload']])
258 );
259 }
260
261 /**
262 * @test
263 */
264 public function renderOutputWithDataAttributesForCustomization()
265 {
266 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
267 $fileResourceMock = $this->createMock(File::class);
268
269 $this->assertSame(
270 '<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 data-player-handler="youTube" data-custom-playerId="player-123" width="300" height="200"></iframe>',
271 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'data' => ['player-handler' => 'youTube', 'custom-playerId' => 'player-123']])
272 );
273 }
274
275 /**
276 * @test
277 */
278 public function renderOutputWithCombinationOfDataAndAdditionalAttributes()
279 {
280 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
281 $fileResourceMock = $this->createMock(File::class);
282
283 $this->assertSame(
284 '<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 foo="bar" custom-play="preload" data-player-handler="youTube" data-custom-playerId="player-123" width="300" height="200"></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 }