[BUGFIX] Repair relative path resolvement in ResourceCompressor 03/56803/11
authorPascal Rinker <info@crynton.com>
Wed, 25 Apr 2018 09:51:47 +0000 (11:51 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 26 Jun 2018 22:22:05 +0000 (00:22 +0200)
This patch repairs the relative path resolvment when using EXT:
for JavaScript/CSS paths.

Resolves: #84261
Releases: master, 8.7
Change-Id: Ieeb35041bf049728c41465edb617529550629258
Reviewed-on: https://review.typo3.org/56803
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benjamin Kott <benjamin.kott@outlook.com>
Tested-by: Benjamin Kott <benjamin.kott@outlook.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Classes/Resource/ResourceCompressor.php
typo3/sysext/core/Tests/Unit/Resource/BaseTestCase.php
typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest.php

index bfd8408..5858f13 100644 (file)
@@ -474,9 +474,9 @@ class ResourceCompressor
             $file = Environment::getPublicPath() . '/' . $filename;
         }
 
-        // check if the file exists, and if so, return the path relative to TYPO3_mainDir
+        // check if the file exists, and if so, return the path relative to PATH_thisScript
         if (is_file($file)) {
-            return '../' . str_replace(Environment::getPublicPath() . '/', '', $file);
+            return rtrim(PathUtility::getRelativePathTo($file), '/');
         }
         // none of above conditions were met, fallback to default behaviour
         return $filename;
index 5d45e70..41dc8ec 100644 (file)
@@ -32,6 +32,7 @@ abstract class BaseTestCase extends \TYPO3\TestingFramework\Core\Unit\UnitTestCa
 
     protected function setUp()
     {
+        parent::setUp();
         $this->mountDir = $this->getUniqueId('mount-');
         $this->basedir = $this->getUniqueId('base-');
         vfsStream::setup($this->basedir);
index 8a5641a..1f65b42 100644 (file)
@@ -25,6 +25,12 @@ use TYPO3\CMS\Core\Utility\PathUtility;
 class ResourceCompressorTest extends BaseTestCase
 {
     /**
+     * Restore Environment after the test
+     * @var bool
+     */
+    protected $backupEnvironment = true;
+
+    /**
      * @var ResourceCompressor|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
      */
     protected $subject;
@@ -528,4 +534,97 @@ class ResourceCompressorTest extends BaseTestCase
         }
         $this->assertEquals(file_get_contents($expected), $compressedCss, 'Group of file CSS assets optimized correctly.');
     }
+
+    /**
+     * @return array
+     */
+    public function getVariousFilenamesFromMainDirInBackendContextDataProvider(): array
+    {
+        return [
+            // Get filename using EXT:
+            [
+                'EXT:core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css',
+                'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css'
+            ],
+            // Get filename using relative path
+            [
+                'typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css',
+                'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css'
+            ],
+            [
+                'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css',
+                'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css'
+            ],
+            [
+                'typo3temp/assets/compressed/.htaccess',
+                '../typo3temp/assets/compressed/.htaccess'
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider getVariousFilenamesFromMainDirInBackendContextDataProvider
+     * @param string $filename input that will be fired on the extension
+     * @param string $expected
+     */
+    public function getVariousFilenamesFromMainDirInBackendContext(string $filename, string $expected)
+    {
+        $rootPath = \dirname($_SERVER['SCRIPT_NAME']);
+        $this->subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']);
+        $this->subject->setRootPath($rootPath . '/');
+
+        $relativeToRootPath = $this->subject->_call('getFilenameFromMainDir', $filename);
+        $this->assertSame($expected, $relativeToRootPath, 'Path to the file relative to the path converted correctly.');
+    }
+
+    /**
+     * @return array
+     */
+    public function getVariousFilenamesFromMainDirInFrontendContextDataProvider(): array
+    {
+        return [
+            // Get filename using EXT:
+            [
+                'EXT:core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css',
+                'typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css'
+            ],
+            // Get filename using relative path
+            [
+                'typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css',
+                'typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/charset.css'
+            ],
+            [
+                'typo3temp/assets/compressed/.htaccess',
+                'typo3temp/assets/compressed/.htaccess'
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider getVariousFilenamesFromMainDirInFrontendContextDataProvider
+     * @param string $filename input that will be fired on the extension
+     * @param string $expected
+     */
+    public function getVariousFilenamesFromMainDirInFrontendContext(string $filename, string $expected)
+    {
+        Environment::initialize(
+            Environment::getContext(),
+            true,
+            false,
+            Environment::getProjectPath(),
+            Environment::getPublicPath(),
+            Environment::getVarPath(),
+            Environment::getConfigPath(),
+            PATH_site . 'index.php',
+            TYPO3_OS === 'WIN' ? 'WINDOWS' : 'UNIX'
+        );
+        $_SERVER['ORIG_SCRIPT_NAME'] = '/index.php';
+        $this->subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']);
+        $this->subject->setRootPath(PATH_site);
+
+        $relativeToRootPath = $this->subject->_call('getFilenameFromMainDir', $filename);
+        $this->assertSame($expected, $relativeToRootPath, 'Path to the file relative to the path converted correctly.');
+    }
 }