[FEATURE] Enhance youtube id getting 16/23616/4
authorSteffen Ritter <info@rs-websystems.de>
Wed, 4 Sep 2013 11:23:59 +0000 (13:23 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 9 Sep 2013 10:13:39 +0000 (12:13 +0200)
The preg_match to fetch the video id from an url do cover only a few
variation. The enhancement covers a lot more possibilities

Releases: 6.2
Fixes: #51728
Change-Id: Ie9d0b3b5dbfff4e129d37fcf351d1d8c5fcdfc98
Reviewed-on: https://review.typo3.org/23616
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/frontend/Classes/MediaWizard/MediaWizardProvider.php
typo3/sysext/frontend/Tests/Unit/MediaWizard/MediaWizardProviderTest.php [new file with mode: 0644]

index dfaa689..c264649 100644 (file)
@@ -114,15 +114,31 @@ class MediaWizardProvider implements \TYPO3\CMS\Frontend\MediaWizard\MediaWizard
         */
        protected function process_youtube($url) {
                $videoId = '';
-               if (strpos($url, '/user/') !== FALSE) {
-                       // it's a channel
-                       $parts = explode('/', $url);
-                       $videoId = $parts[count($parts) - 1];
-               } elseif (preg_match('/(v=|v\\/|.be\\/)([^(\\&|$)]*)/', $url, $matches)) {
-                       $videoId = $matches[2];
+
+               $pattern = '%
+               ^(?:https?://)?                                                                 # Optional URL scheme Either http or https
+               (?:www\.)?                                                                              # Optional www subdomain
+               (?:                                                                                             # Group host alternatives:
+                       youtu\.be/                                                                      #  Either youtu.be/,
+                       |youtube(?:                                                                     #  or youtube.com/
+                               -nocookie                                                               #   optional nocookie domain
+                       )?\.com/(?:
+                               [^/]+/.+/                                                               #   Either /something/other_params/ for channels,
+                               |(?:v|e(?:                                                              #   or v/ or e/,
+                                       mbed                                                            #    optional mbed for embed/
+                               )?)/
+                               |.*[?&]v=                                                               #   or ?v= or ?other_param&v=
+                       )
+               )                                                                                               # End host alternatives.
+               ([^"&?/ ]{11})                                                                  # 11 characters (Length of Youtube video ids).
+               (?:.+)?$                                                                                # Optional other ending URL parameters.
+               %xs';
+               if (preg_match($pattern, $url, $matches)) {
+                       $videoId = $matches[1];
                }
+
                if ($videoId) {
-                       $url = $this->getUrlSchema() . 'www.youtube.com/v/' . $videoId . '?fs=1';
+                       $url = $this->getUrlSchema() . 'www.youtube.com/embed/' . $videoId . '?fs=1';
                }
                return $url;
        }
diff --git a/typo3/sysext/frontend/Tests/Unit/MediaWizard/MediaWizardProviderTest.php b/typo3/sysext/frontend/Tests/Unit/MediaWizard/MediaWizardProviderTest.php
new file mode 100644 (file)
index 0000000..5b2f26d
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+namespace TYPO3\CMS\Frontend\Tests\Unit\MediaWizard;
+
+/***************************************************************
+ * Copyright notice
+ *
+ * (c) 2009-2013 Sebastian Fischer (typo3@marketing-factory.de)
+ * All rights reserved
+ *
+ * This script is part of the TYPO3 project. The TYPO3 project is
+ * free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * The GNU General Public License can be found at
+ * http://www.gnu.org/copyleft/gpl.html.
+ *
+ * This script is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * Testcase for TYPO3\CMS\Frontend\MediaWizard\MediaWizardProvider
+ *
+ * @author Sebastian Fischer <typo3@marketing-factory.de>
+ */
+class MediaWizardProviderTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+       /**
+        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Frontend\MediaWizard\MediaWizardProvider
+        */
+       private $fixture;
+
+       /**
+        * Setup
+        */
+       public function setUp() {
+               $this->fixture = $this->getAccessibleMock('\\TYPO3\\CMS\\Frontend\\MediaWizard\\MediaWizardProvider', array('dummy'), array(), '', FALSE);
+       }
+
+       /**
+        * Tear down
+        */
+       public function tearDown() {
+               unset($this->fixture);
+       }
+
+       /**
+        * @return array
+        */
+       public function process_youtubeDataProvider() {
+               return array(
+                       'http://youtu.be/2PMeCSQ--08' => array(
+                               'http://youtu.be/2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/embed/2PMeCSQ--08' => array(
+                               'http://www.youtube.com/embed/2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/watch?v=2PMeCSQ--08' => array(
+                               'http://www.youtube.com/watch?v=2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/?v=2PMeCSQ--08' => array(
+                               'http://www.youtube.com/?v=2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/v/2PMeCSQ--08' => array(
+                               'http://www.youtube.com/v/2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/e/2PMeCSQ--08' => array(
+                               'http://www.youtube.com/e/2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/watch?feature=player_embedded&v=2PMeCSQ--08' => array(
+                               'http://www.youtube.com/watch?feature=player_embedded&v=2PMeCSQ--08',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+                       'http://www.youtube.com/watch?v=2PMeCSQ--08&list=PLGWGc5dfbzn_pvtJg7XskLva9XZpNTI88' => array(
+                               'http://www.youtube.com/watch?v=2PMeCSQ--08&list=PLGWGc5dfbzn_pvtJg7XskLva9XZpNTI88',
+                               'http://www.youtube.com/embed/2PMeCSQ--08?fs=1'
+                       ),
+               );
+       }
+
+       /**
+        * Test for process youtube
+        *
+        * @param string $url
+        * @param string $expected
+        * @return void
+        * @dataProvider process_youtubeDataProvider
+        * @test
+        */
+       public function process_youtube($url, $expected) {
+               $result = $this->fixture->_call('process_youtube', $url);
+               $this->assertEquals($expected, $result);
+       }
+}
+
+?>
\ No newline at end of file