From f6c2b2e55f80a8193b15f294ae974d2832ba900d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=CC=8Ceslav=20Przywara?= <ceslav@przywara.cz>
Date: Mon, 7 Aug 2017 11:40:11 +0200
Subject: [PATCH] [BUGFIX] YouTubeRenderer "controls" option

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/53650
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
---
 .../Resource/Rendering/YouTubeRenderer.php    |   8 +-
 .../Rendering/YouTubeRendererTest.php         | 103 +++++++++++++++---
 2 files changed, 92 insertions(+), 19 deletions(-)

diff --git a/typo3/sysext/core/Classes/Resource/Rendering/YouTubeRenderer.php b/typo3/sysext/core/Classes/Resource/Rendering/YouTubeRenderer.php
index a96e7e39bd63..d4d2c4020cb3 100644
--- a/typo3/sysext/core/Classes/Resource/Rendering/YouTubeRenderer.php
+++ b/typo3/sysext/core/Classes/Resource/Rendering/YouTubeRenderer.php
@@ -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';
         }
diff --git a/typo3/sysext/core/Tests/Unit/Resource/Rendering/YouTubeRendererTest.php b/typo3/sysext/core/Tests/Unit/Resource/Rendering/YouTubeRendererTest.php
index b9deafba13a1..de31b01e66c1 100644
--- a/typo3/sysext/core/Tests/Unit/Resource/Rendering/YouTubeRendererTest.php
+++ b/typo3/sysext/core/Tests/Unit/Resource/Rendering/YouTubeRendererTest.php
@@ -84,20 +84,6 @@ class YouTubeRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $this->assertFalse($this->subject->canRender($fileResourceMock));
     }
 
-    /**
-     * @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
      */
@@ -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
      */
-- 
GitLab