TYPO3 CMS  TYPO3_7-6
YouTubeRendererTest.php
Go to the documentation of this file.
1 <?php
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 
23 
28 {
32  protected $subject;
33 
37  protected function setUp()
38  {
39  parent::setUp();
41  $_SERVER['HTTP_HOST'] = 'test.server.org';
42 
44  $youTubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
45  $youTubeHelper->expects($this->any())->method('getOnlineMediaId')->will($this->returnValue('7331'));
46 
47  $this->subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
48  $this->subject ->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youTubeHelper));
49  }
50 
55  {
56  $this->assertSame(1, $this->subject->getPriority());
57  }
58 
62  public function canRenderReturnsTrueOnCorrectFile()
63  {
65  $fileResourceMock1 = $this->getMock(File::class, [], [], '', false);
66  $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('video/youtube'));
68  $fileResourceMock2 = $this->getMock(File::class, [], [], '', false);
69  $fileResourceMock2->expects($this->any())->method('getMimeType')->will($this->returnValue('video/unknown'));
70  $fileResourceMock2->expects($this->any())->method('getExtension')->will($this->returnValue('youtube'));
71 
72  $this->assertTrue($this->subject->canRender($fileResourceMock1));
73  $this->assertTrue($this->subject->canRender($fileResourceMock2));
74  }
75 
79  public function canRenderReturnsFalseOnCorrectFile()
80  {
82  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
83  $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/vimeo'));
84 
85  $this->assertFalse($this->subject->canRender($fileResourceMock));
86  }
87 
91  public function renderOutputIsCorrect()
92  {
94  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
95 
96  $this->assertSame(
97  '<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"></iframe>',
98  $this->subject->render($fileResourceMock, '300m', '200')
99  );
100  }
101 
105  public function renderOutputWithLoopIsCorrect()
106  {
108  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
109 
110  $this->assertSame(
111  '<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"></iframe>',
112  $this->subject->render($fileResourceMock, '300m', '200', ['loop' => 1])
113  );
114  }
115 
119  public function renderOutputWithAutoplayIsCorrect()
120  {
122  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
123 
124  $this->assertSame(
125  '<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"></iframe>',
126  $this->subject->render($fileResourceMock, '300m', '200', ['autoplay' => 1])
127  );
128  }
129 
133  public function renderOutputWithAutoplayFromFileReferenceIsCorrect()
134  {
136  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
137 
139  $fileReferenceMock = $this->getMock(FileReference::class, [], [], '', false);
140  $fileReferenceMock->expects($this->any())->method('getProperty')->will($this->returnValue(1));
141  $fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);
142 
143  $this->assertSame(
144  '<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"></iframe>',
145  $this->subject->render($fileReferenceMock, '300m', '200')
146  );
147  }
148 
152  public function renderOutputWithAutoplayAndWithoutControlsIsCorrect()
153  {
155  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
156 
157  $this->assertSame(
158  '<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"></iframe>',
159  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
160  );
161  }
162 
164  {
165  return [
166  'no options given' => [
167  '<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"></iframe>',
168  null
169  ],
170  'with option controls = foo as invalid string' => [
171  '<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"></iframe>',
172  ['controls' => 'foo']
173  ],
174  'with option controls = true as string' => [
175  '<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"></iframe>',
176  ['controls' => 'true']
177  ],
178  'with option controls = false as string' => [
179  '<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"></iframe>',
180  ['controls' => 'false']
181  ],
182  'with option controls = true as boolean' => [
183  '<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"></iframe>',
184  ['controls' => true]
185  ],
186  'with option controls = false as boolean' => [
187  '<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"></iframe>',
188  ['controls' => false]
189  ],
190  'with option controls = 0 as string' => [
191  '<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"></iframe>',
192  ['controls' => '0']
193  ],
194  'with option controls = 1 as string' => [
195  '<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"></iframe>',
196  ['controls' => '1']
197  ],
198  'with option controls = 2 as string' => [
199  '<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"></iframe>',
200  ['controls' => '2']
201  ],
202  'with option controls = 3 as string' => [
203  '<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"></iframe>',
204  ['controls' => '3']
205  ],
206  'with option controls = negative number as string' => [
207  '<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"></iframe>',
208  ['controls' => '-42']
209  ],
210  'with option controls = 0 as int' => [
211  '<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"></iframe>',
212  ['controls' => 0]
213  ],
214  'with option controls = 1 as int' => [
215  '<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"></iframe>',
216  ['controls' => 1]
217  ],
218  'with option controls = 2 as int' => [
219  '<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"></iframe>',
220  ['controls' => 2]
221  ],
222  'with option controls = 3 as int' => [
223  '<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"></iframe>',
224  ['controls' => 3]
225  ],
226  'with option controls = negative number as int' => [
227  '<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"></iframe>',
228  ['controls' => -42]
229  ],
230  ];
231  }
232 
237  public function renderOutputWithDefaultControlsIsCorrect($expected, $options)
238  {
240  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
241 
242  $this->assertSame(
243  $expected,
244  $this->subject->render($fileResourceMock, '300m', '200', $options)
245  );
246  }
247 
251  public function renderOutputWithRelatedVideosTurnedOffIsCorrect()
252  {
254  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
255 
256  $this->assertSame(
257  '<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"></iframe>',
258  $this->subject->render($fileResourceMock, '300m', '200', ['relatedVideos' => 0])
259  );
260  }
261 
265  public function renderOutputWithDisabledNoCookieIsCorrect()
266  {
268  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
269 
270  $this->assertSame(
271  '<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>',
272  $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'no-cookie' => 0])
273  );
274  }
275 
279  public function renderOutputIsEscaped()
280  {
282  $youtubeHelper = $this->getAccessibleMock(YouTubeHelper::class, ['getOnlineMediaId'], ['youtube']);
283  $youtubeHelper->expects($this->any())->method('getOnlineMediaId')->will(
284  $this->returnValue('7331<script>danger</script>\'"random"quotes;')
285  );
286 
287  $subject = $this->getAccessibleMock(YouTubeRenderer::class, ['getOnlineMediaHelper'], []);
288  $subject->expects($this->any())->method('getOnlineMediaHelper')->will($this->returnValue($youtubeHelper));
289 
291  $fileResourceMock = $this->getMock(File::class, [], [], '', false);
292 
293  $this->assertSame(
294  '<iframe src="https://www.youtube-nocookie.com/embed/7331%3Cscript%3Edanger%3C%2Fscript%3E%27%22random%22quotes%3B?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>',
295  $subject->render($fileResourceMock, '300m', '200')
296  );
297  }
298 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)