[BUGFIX] Repair relative path resolvement in ResourceCompressor 86/57386/3
authorPascal Rinker <info@crynton.com>
Wed, 25 Apr 2018 09:51:47 +0000 (11:51 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 10 Jul 2018 14:21:03 +0000 (16:21 +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/57386
Tested-by: TYPO3com <no-reply@typo3.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 0543b9b..775b760 100644 (file)
@@ -465,10 +465,9 @@ class ResourceCompressor
             $file = PATH_site . $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)) {
-            $mainDirDepth = substr_count(TYPO3_mainDir, '/');
-            return str_repeat('../', $mainDirDepth) . str_replace(PATH_site, '', $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 9b45992..164f198 100644 (file)
@@ -22,6 +22,12 @@ use TYPO3\CMS\Core\Resource\ResourceCompressor;
 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;
@@ -398,4 +404,81 @@ 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 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)
+    {
+        $_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.');
+    }
 }