[TASK] Always use HTTPS for youtube and vimeo
[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\Tests\UnitTestCase;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Class YouTubeRendererTest
26 */
27 class YouTubeRendererTest extends UnitTestCase
28 {
29 /**
30 * @var YouTubeRenderer|\PHPUnit_Framework_MockObject_MockObject
31 */
32 protected $subject;
33
34 /**
35 * Set up the test
36 */
37 protected function setUp()
38 {
39 parent::setUp();
40 GeneralUtility::flushInternalRuntimeCaches();
41 $_SERVER['HTTP_HOST'] = 'test.server.org';
42 $_SERVER['REQUEST_URI'] = '/test-page/';
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->getMock(File::class, [], [], '', false);
67 $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('video/youtube'));
68 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock2 */
69 $fileResourceMock2 = $this->getMock(File::class, [], [], '', false);
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->getMock(File::class, [], [], '', false);
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 renderOutputIsCorrect()
93 {
94 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
95 $fileResourceMock = $this->getMock(File::class, [], [], '', false);
96
97 $this->assertSame(
98 '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;controls=2&amp;enablejsapi=1&amp;origin=http://test.server.org/test-page/&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
99 $this->subject->render($fileResourceMock, '300m', '200')
100 );
101 }
102
103 /**
104 * @test
105 */
106 public function renderOutputWithLoopIsCorrect()
107 {
108 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
109 $fileResourceMock = $this->getMock(File::class, [], [], '', false);
110
111 $this->assertSame(
112 '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;controls=2&amp;loop=1&amp;enablejsapi=1&amp;origin=http://test.server.org/test-page/&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
113 $this->subject->render($fileResourceMock, '300m', '200', ['loop' => 1])
114 );
115 }
116
117 /**
118 * @test
119 */
120 public function renderOutputWithAutoplayIsCorrect()
121 {
122 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
123 $fileResourceMock = $this->getMock(File::class, [], [], '', false);
124
125 $this->assertSame(
126 '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;controls=2&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http://test.server.org/test-page/&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
127 $this->subject->render($fileResourceMock, '300m', '200', ['autoplay' => 1])
128 );
129 }
130
131 /**
132 * @test
133 */
134 public function renderOutputWithAutoplayFromFileReferenceIsCorrect()
135 {
136 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
137 $fileResourceMock = $this->getMock(File::class, [], [], '', false);
138
139 /** @var FileReference|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
140 $fileReferenceMock = $this->getMock(FileReference::class, [], [], '', false);
141 $fileReferenceMock->expects($this->any())->method('getProperty')->will($this->returnValue(1));
142 $fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);
143
144 $this->assertSame(
145 '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;controls=2&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http://test.server.org/test-page/&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
146 $this->subject->render($fileReferenceMock, '300m', '200')
147 );
148 }
149
150 /**
151 * @test
152 */
153 public function renderOutputWithAutoplayAndWithoutControllsIsCorrect()
154 {
155 /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
156 $fileResourceMock = $this->getMock(File::class, [], [], '', false);
157
158 $this->assertSame(
159 '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http://test.server.org/test-page/&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
160 $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
161 );
162 }
163 }