[BUGFIX] Handle docroot relative paths correctly in ResourceCompressor 80/54880/2
authorMarkus Klein <markus.klein@typo3.org>
Thu, 26 Oct 2017 22:30:15 +0000 (00:30 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Wed, 29 Nov 2017 22:46:33 +0000 (23:46 +0100)
Resolves: #82863
Releases: master, 8.7
Change-Id: Iaf775d83c526017ff882d21c8f6d7c5c1f78a038
Reviewed-on: https://review.typo3.org/54880
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/ResourceCompressorIntegrationTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/TestableResourceCompressor.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import.css [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import_non_root.css [new file with mode: 0644]

index 26d2cf1..0543b9b 100644 (file)
@@ -462,7 +462,7 @@ class ResourceCompressor
         } elseif (strpos($filename, '../') === 0) {
             $file = GeneralUtility::resolveBackPath(PATH_typo3 . $filename);
         } else {
-            $file = PATH_site . $fileNameWithoutSlash;
+            $file = PATH_site . $filename;
         }
 
         // check if the file exists, and if so, return the path relative to TYPO3_mainDir
diff --git a/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorIntegrationTest.php b/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorIntegrationTest.php
new file mode 100644 (file)
index 0000000..c493bb5
--- /dev/null
@@ -0,0 +1,132 @@
+<?php
+
+namespace TYPO3\CMS\Core\Tests\Unit\Resource;
+
+/*
+ * 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\Tests\Unit\Resource\ResourceCompressorTest\Fixtures\TestableResourceCompressor;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Testcase for the ResourceCompressor class
+ */
+class ResourceCompressorIntegrationTest extends BaseTestCase
+{
+
+    /**
+     * @var TestableResourceCompressor
+     */
+    protected $resourceCompressor;
+
+    /**
+     * @var string
+     */
+    protected $fixtureDir;
+
+    /**
+     * @var string
+     */
+    protected $fixtureDirFromTest;
+
+    public function setUp()
+    {
+        $this->fixtureDir = 'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/';
+        $this->fixtureDirFromTest = GeneralUtility::fixWindowsFilePath(__DIR__ . '/ResourceCompressorTest/Fixtures/');
+    }
+
+    /**
+     * @test
+     */
+    public function constructorCreatesTargetDirectory()
+    {
+        $this->resourceCompressor = new TestableResourceCompressor();
+        $dir = PATH_site . $this->resourceCompressor->getTargetDirectory();
+        self::assertFileExists($dir);
+    }
+
+    /**
+     * @test
+     */
+    public function constructorCreatesHtaccessFileIfSet()
+    {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess'] = true;
+        $this->resourceCompressor = new TestableResourceCompressor();
+        $htaccessPath = PATH_site . $this->resourceCompressor->getTargetDirectory() . '.htaccess';
+        self::assertStringEqualsFile($htaccessPath, $this->resourceCompressor->getHtaccessTemplate());
+    }
+
+    /**
+     * @test
+     */
+    public function constructorDoesNotCreateHtaccessFileIfSetToFalse()
+    {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['generateApacheHtaccess'] = false;
+        $this->resourceCompressor = new TestableResourceCompressor();
+        $htaccessPath = PATH_site . $this->resourceCompressor->getTargetDirectory() . '.htaccess';
+        self::assertFileNotExists($htaccessPath);
+    }
+
+    /**
+     * @test
+     */
+    public function concatenateCssFiles()
+    {
+        $files = [
+            'sampleFile1' => [
+                'excludeFromConcatenation' => false,
+                'file' => $this->fixtureDir . 'css_input_with_import.css',
+                'media' => 'screen',
+                'forceOnTop' => false,
+            ],
+        ];
+        $this->resourceCompressor = new TestableResourceCompressor();
+        $concatFiles = $this->resourceCompressor->concatenateCssFiles($files);
+        $mergedFile = array_pop($concatFiles);
+        $expected = file_get_contents(
+            $this->fixtureDirFromTest . 'expected' . DIRECTORY_SEPARATOR . 'merged-css_input_with_import.css'
+        );
+        self::assertStringEqualsFile(GeneralUtility::fixWindowsFilePath(PATH_site . $mergedFile['file']), $expected);
+    }
+
+    /**
+     * @test
+     */
+    public function concatenateCssFilesWorksWithFileFromNonRootPath()
+    {
+        $testFile = PATH_site . 'typo3temp/var/transient/css_input_with_import.css';
+        $this->testFilesToDelete[] = $testFile;
+        copy(PATH_typo3 . $this->fixtureDir . 'css_input_with_import.css', $testFile);
+        $files = [
+            'sampleFile1' => [
+                'excludeFromConcatenation' => false,
+                'file' => 'typo3temp/var/transient/css_input_with_import.css',
+                'media' => 'screen',
+                'forceOnTop' => false,
+            ],
+        ];
+        $this->resourceCompressor = new TestableResourceCompressor();
+        $concatFiles = $this->resourceCompressor->concatenateCssFiles($files);
+        $mergedFile = array_pop($concatFiles);
+        $expected = file_get_contents(
+            $this->fixtureDirFromTest . 'expected' . DIRECTORY_SEPARATOR . 'merged-css_input_with_import_non_root.css'
+        );
+        self::assertStringEqualsFile(GeneralUtility::fixWindowsFilePath(PATH_site . $mergedFile['file']), $expected);
+    }
+
+    public function tearDown()
+    {
+        $this->testFilesToDelete[] = PATH_site . $this->resourceCompressor->getTargetDirectory();
+        parent::tearDown();
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/TestableResourceCompressor.php b/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/TestableResourceCompressor.php
new file mode 100644 (file)
index 0000000..d30c811
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+
+namespace TYPO3\CMS\Core\Tests\Unit\Resource\ResourceCompressorTest\Fixtures;
+
+/*
+ * 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\ResourceCompressor;
+
+class TestableResourceCompressor extends ResourceCompressor
+{
+    protected $targetDirectory = 'typo3temp/var/tests/resource/';
+
+    public function getTargetDirectory()
+    {
+        return $this->targetDirectory;
+    }
+
+    public function getHtaccessTemplate()
+    {
+        return $this->htaccessTemplate;
+    }
+}
diff --git a/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import.css b/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import.css
new file mode 100644 (file)
index 0000000..c9f1f2d
--- /dev/null
@@ -0,0 +1,28 @@
+
+/* moved by compressor */
+@import "../../../typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/import1.css";@import "../../../typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/import2.css";@import url("http://example.com/style.css");@import url("//example.com/style.css");
+/* moved by compressor */
+body {
+  margin: 0;
+  padding: 0;
+  background: #edf5fa;
+  font: 76%/170% Verdana, sans-serif;
+  color: #494949;
+}
+
+.this .is .a .test {
+  font: 1em/100% Verdana, sans-serif;
+  color: #494949;
+}
+.this
+.is
+.a
+.test {
+font: 1em/100% Verdana, sans-serif;
+color: #494949;
+}
+
+textarea, select {
+  font: 1em/160% Verdana, sans-serif;
+  color: #494949;
+}
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import_non_root.css b/typo3/sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/expected/merged-css_input_with_import_non_root.css
new file mode 100644 (file)
index 0000000..eba30c4
--- /dev/null
@@ -0,0 +1,28 @@
+
+/* moved by compressor */
+@import "../../../typo3temp/var/transient/import1.css";@import "../../../typo3temp/var/transient/import2.css";@import url("http://example.com/style.css");@import url("//example.com/style.css");
+/* moved by compressor */
+body {
+  margin: 0;
+  padding: 0;
+  background: #edf5fa;
+  font: 76%/170% Verdana, sans-serif;
+  color: #494949;
+}
+
+.this .is .a .test {
+  font: 1em/100% Verdana, sans-serif;
+  color: #494949;
+}
+.this
+.is
+.a
+.test {
+font: 1em/100% Verdana, sans-serif;
+color: #494949;
+}
+
+textarea, select {
+  font: 1em/160% Verdana, sans-serif;
+  color: #494949;
+}
\ No newline at end of file