[BUGFIX] Use parse_url to determine absolute urls in ImageService 71/41771/4
authorFrans Saris <franssaris@gmail.com>
Tue, 21 Jul 2015 11:50:22 +0000 (13:50 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 21 Jul 2015 14:30:47 +0000 (16:30 +0200)
Since it is possible to have protocol relative URIs and parse_url
only identifies a hostname once a scheme or scheme relative prefix is
applied, we can check for a hostname here.

Resolves: #68423
Releases: master, 6.2
Change-Id: I3eb51d9b5175657454657bbd7f75cfafa6bd9760
Reviewed-on: http://review.typo3.org/41771
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Frederic Gaus <frederic.gaus@flagbit.de>
Tested-by: Frederic Gaus <frederic.gaus@flagbit.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/extbase/Classes/Service/ImageService.php
typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php [new file with mode: 0644]

index 8858342..7a516ec 100644 (file)
@@ -69,7 +69,7 @@ class ImageService implements \TYPO3\CMS\Core\SingletonInterface {
                $imageUrl = $image->getPublicUrl();
 
                // no prefix in case of an already fully qualified URL (having a schema)
-               if (strpos($imageUrl, '://')) {
+               if (parse_url($imageUrl, PHP_URL_HOST) !== NULL) {
                        $uriPrefix = '';
                } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
                        $uriPrefix = $GLOBALS['TSFE']->absRefPrefix;
diff --git a/typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php b/typo3/sysext/extbase/Tests/Unit/Service/ImageScriptServiceTest.php
new file mode 100644 (file)
index 0000000..44a0525
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Resource\FileReference;
+use TYPO3\CMS\Core\Resource\ProcessedFile;
+use TYPO3\CMS\Extbase\Service\EnvironmentService;
+use TYPO3\CMS\Extbase\Service\ImageService;
+
+/**
+ * Test case
+ */
+class ImageServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * @var ImageService
+        */
+       protected $subject;
+
+       /**
+        * @var EnvironmentService|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $environmentService;
+
+       /**
+        * Initialize ImageService and environment service mock
+        */
+       protected function setUp() {
+               $this->subject = new ImageService();
+               $this->environmentService = $this->getMock(EnvironmentService::class);
+               $this->inject($this->subject, 'environmentService', $this->environmentService);
+       }
+
+       /**
+        * @test
+        */
+       public function fileIsUnwrappedFromReferenceForProcessing() {
+               $reference = $this->getAccessibleMock(FileReference::class, array(), array(), '', FALSE);
+               $file = $this->getMock(File::class, array(), array(), '', FALSE);
+               $file->expects($this->once())->method('process')->willReturn($this->getMock(ProcessedFile::class, array(), array(), '', FALSE));
+               $reference->expects($this->once())->method('getOriginalFile')->willReturn($file);
+               $reference->_set('file', $file);
+
+               $this->subject->applyProcessingInstructions($reference, array());
+       }
+
+       /**
+        * @return array
+        */
+       public function prefixIsCorrectlyAppliedToGetImageUriDataProvider() {
+               return array(
+                       'with scheme' => array('http://foo.bar/img.jpg', 'http://foo.bar/img.jpg'),
+                       'scheme relative' => array('//foo.bar/img.jpg', '//foo.bar/img.jpg'),
+                       'without scheme' => array('foo.bar/img.jpg', '/prefix/foo.bar/img.jpg'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider prefixIsCorrectlyAppliedToGetImageUriDataProvider
+        */
+       public function prefixIsCorrectlyAppliedToGetImageUri($imageUri, $expected) {
+               $this->environmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->willReturn(TRUE);
+               $GLOBALS['TSFE'] = new \stdClass();
+               $GLOBALS['TSFE']->absRefPrefix = '/prefix/';
+
+               $file = $this->getMock(File::class, array(), array(), '', FALSE);
+               $file->expects($this->once())->method('getPublicUrl')->willReturn($imageUri);
+
+               $this->assertSame($expected, $this->subject->getImageUri($file));
+       }
+
+}