[BUGFIX] SimpleFileBackend::setCacheDirectory returns valid path name 57/46157/2
authorJan Helke <typo3@helke.de>
Fri, 22 Jan 2016 09:58:24 +0000 (10:58 +0100)
committerFrank Nägler <frank.naegler@typo3.org>
Fri, 22 Jan 2016 14:12:26 +0000 (15:12 +0100)
The comment for the method
\TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend::setCacheDirectory()
states clearly, that it is possible to provide an absolute path as
cache directory. However, in the lower part of the function, it is
stated, that if ($cacheDirectory0 == '/') the documentRoot should be
set to '/'. That results in a returned path like '//...'.
This causes problems within
\TYPO3\CMS\Core\Utility\GeneralUtility::validPathStr(), because pathes
with '//' in it are always invalid.

Resolves: #72635
Releases: master, 7.6, 6.2
Change-Id: I30e7743ba9835c99382b7c04153f91e688ead5fc
Reviewed-on: https://review.typo3.org/46157
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Jan Helke <typo3@helke.de>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/Cache/Backend/SimpleFileBackend.php
typo3/sysext/core/Tests/Unit/Cache/Backend/FileBackendTest.php

index 48f91bf..7b30f73 100644 (file)
@@ -175,7 +175,7 @@ class SimpleFileBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend im
         } else {
             if ($cacheDirectory[0] == '/') {
                 // Absolute path to cache directory.
-                $documentRoot = '/';
+                $documentRoot = '';
             }
             if (TYPO3_OS === 'WIN') {
                 if (substr($cacheDirectory, 0, strlen($documentRoot)) === $documentRoot) {
index 1d50909..4d48efc 100644 (file)
@@ -56,6 +56,102 @@ class FileBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @test
      */
+    public function setCacheDirectoryAllowsAbsolutePathWithoutTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('/tmp/foo');
+        $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsAbsolutePathWithTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('/tmp/foo/');
+        $this->assertEquals('/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsRelativePathWithoutTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('tmp/foo');
+        // get PATH_site without trailing slash
+        $path = realpath(PATH_site);
+        $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsRelativePathWithTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('tmp/foo/');
+        // get PATH_site without trailing slash
+        $path = realpath(PATH_site);
+        $this->assertEquals($path . '/tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsRelativeDottedPathWithoutTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('../tmp/foo');
+        // get PATH_site without trailing slash
+        $path = realpath(PATH_site);
+        $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsRelativeDottedPathWithTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('../tmp/foo/');
+        // get PATH_site without trailing slash
+        $path = realpath(PATH_site);
+        $this->assertEquals($path . '/../tmp/foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsAbsoluteDottedPathWithoutTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('/tmp/../foo');
+        $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
+    public function setCacheDirectoryAllowsAbsoluteDottedPathWithTrailingSlash()
+    {
+        $backend = $this->getAccessibleMock(\TYPO3\CMS\Core\Cache\Backend\FileBackend::class, array('dummy'), array(), '', false);
+        $backend->_set('cacheIdentifier', 'test');
+        $backend->setCacheDirectory('/tmp/../foo/');
+        $this->assertEquals('/tmp/../foo/test/', $backend->_get('temporaryCacheDirectory'));
+    }
+
+    /**
+     * @test
+     */
     public function getCacheDirectoryReturnsTheCurrentCacheDirectory()
     {
         $mockCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, array(), array(), '', false);