[BUGFIX] YouTubeRenderer "controls" option 08/53708/2
authorČeslav Przywara <ceslav@przywara.cz>
Mon, 7 Aug 2017 09:40:11 +0000 (11:40 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Tue, 15 Aug 2017 13:55:21 +0000 (15:55 +0200)
The "controls" parameter appended to YouTube embed URL is either set to
value provided in $options array or to default backwards-compatible
value (controls=2).

Specifically, if controls option is set to 0, the controls parameter
must be present in URL, because default value for it is 1.

Fix is applied to related unit test too.

Resolves: #82044
Releases: master, 8.7, 7.6
Change-Id: I1519b5f515f85eb473f590762171b250d26f32c4
Reviewed-on: https://review.typo3.org/53708
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/core/Classes/Resource/Rendering/YouTubeRenderer.php
typo3/sysext/core/Tests/Unit/Resource/Rendering/YouTubeRendererTest.php

index a96e7e3..d4d2c40 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\Resource\FileReference;
 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\MathUtility;
 
 /**
  * YouTube renderer class
@@ -106,9 +107,10 @@ class YouTubeRenderer implements FileRendererInterface
         $videoId = $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
 
         $urlParams = ['autohide=1'];
-        if (!isset($options['controls']) || !empty($options['controls'])) {
-            $urlParams[] = 'controls=2';
-        }
+
+        $options['controls'] = MathUtility::canBeInterpretedAsInteger($options['controls']) ? (int)$options['controls'] : 2;
+        $options['controls'] = MathUtility::forceIntegerInRange($options['controls'], 0, 2);
+        $urlParams[] = 'controls=' . $options['controls'];
         if (!empty($options['autoplay'])) {
             $urlParams[] = 'autoplay=1';
         }
index b9deafb..de31b01 100644 (file)
@@ -87,20 +87,6 @@ class YouTubeRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
     /**
      * @test
      */
-    public function renderOutputIsCorrect()
-    {
-        /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
-        $fileResourceMock = $this->createMock(File::class);
-
-        $this->assertSame(
-            '<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>',
-            $this->subject->render($fileResourceMock, '300m', '200')
-        );
-    }
-
-    /**
-     * @test
-     */
     public function renderOutputWithLoopIsCorrect()
     {
         /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
@@ -134,7 +120,7 @@ class YouTubeRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
         $fileResourceMock = $this->createMock(File::class);
 
-        /** @var FileReference|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
+        /** @var FileReference|\PHPUnit_Framework_MockObject_MockObject $fileReferenceMock */
         $fileReferenceMock = $this->createMock(FileReference::class);
         $fileReferenceMock->expects($this->any())->method('getProperty')->will($this->returnValue(1));
         $fileReferenceMock->expects($this->any())->method('getOriginalFile')->willReturn($fileResourceMock);
@@ -154,11 +140,96 @@ class YouTubeRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $fileResourceMock = $this->createMock(File::class);
 
         $this->assertSame(
-            '<iframe src="https://www.youtube.com/embed/7331?autohide=1&amp;autoplay=1&amp;enablejsapi=1&amp;origin=http%3A%2F%2Ftest.server.org&amp;showinfo=0" allowfullscreen width="300" height="200"></iframe>',
+            '<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>',
             $this->subject->render($fileResourceMock, '300m', '200', ['controls' => 0, 'autoplay' => 1])
         );
     }
 
+    public function renderOutputWithControlsDataProvider()
+    {
+        return [
+            'no options given' => [
+                '<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>',
+                null
+            ],
+            'with option controls = foo as invalid string' => [
+                '<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>',
+                ['controls' => 'foo']
+            ],
+            'with option controls = true as string' => [
+                '<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>',
+                ['controls' => 'true']
+            ],
+            'with option controls = false as string' => [
+                '<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>',
+                ['controls' => 'false']
+            ],
+            'with option controls = true as boolean' => [
+                '<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>',
+                ['controls' => true]
+            ],
+            'with option controls = false as boolean' => [
+                '<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>',
+                ['controls' => false]
+            ],
+            'with option controls = 0 as string' => [
+                '<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>',
+                ['controls' => '0']
+            ],
+            'with option controls = 1 as string' => [
+                '<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>',
+                ['controls' => '1']
+            ],
+            'with option controls = 2 as string' => [
+                '<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>',
+                ['controls' => '2']
+            ],
+            'with option controls = 3 as string' => [
+                '<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>',
+                ['controls' => '3']
+            ],
+            'with option controls = negative number as string' => [
+                '<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>',
+                ['controls' => '-42']
+            ],
+            'with option controls = 0 as int' => [
+                '<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>',
+                ['controls' => 0]
+            ],
+            'with option controls = 1 as int' => [
+                '<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>',
+                ['controls' => 1]
+            ],
+            'with option controls = 2 as int' => [
+                '<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>',
+                ['controls' => 2]
+            ],
+            'with option controls = 3 as int' => [
+                '<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>',
+                ['controls' => 3]
+            ],
+            'with option controls = negative number as int' => [
+                '<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>',
+                ['controls' => -42]
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider renderOutputWithControlsDataProvider
+     */
+    public function renderOutputWithDefaultControlsIsCorrect($expected, $options)
+    {
+        /** @var File|\PHPUnit_Framework_MockObject_MockObject $fileResourceMock */
+        $fileResourceMock = $this->createMock(File::class);
+
+        $this->assertSame(
+            $expected,
+            $this->subject->render($fileResourceMock, '300m', '200', $options)
+        );
+    }
+
     /**
      * @test
      */