[TASK] Convert some FAL unit test to functional 24/49324/8
authorChristian Kuhn <lolli@schwarzbu.ch>
Tue, 2 Aug 2016 16:07:50 +0000 (18:07 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 3 Aug 2016 13:43:21 +0000 (15:43 +0200)
Pick a number of FAL related unit tests that give a
lot of headaches and tend to break often and convert
them to functional tests.

Change-Id: Ic34053af8aec3c5936331de361fda5bef2149fe8
Resolves: #77381
Releases: master
Reviewed-on: https://review.typo3.org/49324
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Sebastian Bumann <bumann.sebastian@gmail.com>
Tested-by: Sebastian Bumann <bumann.sebastian@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Tests/Functional/Resource/ResourceStorageTest.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Resource/ResourceStorageTest.php

diff --git a/typo3/sysext/core/Tests/Functional/Resource/ResourceStorageTest.php b/typo3/sysext/core/Tests/Functional/Resource/ResourceStorageTest.php
new file mode 100644 (file)
index 0000000..0461e36
--- /dev/null
@@ -0,0 +1,178 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Functional\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\Database\ConnectionPool;
+use TYPO3\CMS\Core\Resource\Driver\LocalDriver;
+use TYPO3\CMS\Core\Resource\Folder;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Resource\ResourceStorage;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Test case
+ */
+class ResourceStorageTest extends FunctionalTestCase
+{
+
+    protected $defaultStorageRecord = [
+        'pid' => 0,
+        'name' => 'test storage',
+        'description' => '',
+        'driver' => 'Local',
+        'processingfolder' => 'typo3temp/assets/_processed_/',
+        'configuration' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
+                <T3FlexForms>
+                    <data>
+                        <sheet index="sDEF">
+                            <language index="lDEF">
+                                <field index="basePath">
+                                    <value index="vDEF">fileadmin/</value>
+                                </field>
+                                <field index="pathType">
+                                    <value index="vDEF">relative</value>
+                                </field>
+                                <field index="caseSensitive">
+                                    <value index="vDEF">1</value>
+                                </field>
+                            </language>
+                        </sheet>
+                    </data>
+                </T3FlexForms>
+                ',
+        'is_online' => true,
+        'is_browsable' => true,
+        'is_public' => true,
+        'is_writable' => true,
+        'is_default' => 0,
+    ];
+
+    /**
+     * @test
+     */
+    public function getNestedProcessingFolderTest()
+    {
+        $this->setUpBackendUserFromFixture(1);
+        $driver = new LocalDriver(['basePath' => 'fileadmin/', 'pathType' => 'relative', 'caseSensitive' => '1']);
+        $subject = new ResourceStorage($driver, $this->defaultStorageRecord);
+        $subject->setEvaluatePermissions(false);
+
+        (new ConnectionPool())->getConnectionForTable('sys_file_storage')
+            ->insert('sys_file_storage', $this->defaultStorageRecord);
+        GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/_processed_');
+        GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/adirectory');
+        GeneralUtility::mkdir_deep(PATH_site . 'typo3temp/assets/_processed_/');
+        file_put_contents(PATH_site . 'fileadmin/adirectory/bar.txt', 'myData');
+        clearstatcache();
+        $subject->addFileMount('/adirectory/', ['read_only' => false]);
+        $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/adirectory/bar.txt');
+
+        $rootProcessingFolder = $subject->getProcessingFolder();
+        $processingFolder = $subject->getProcessingFolder($file);
+
+        $this->assertInstanceOf(Folder::class, $processingFolder);
+        $this->assertNotEquals($rootProcessingFolder, $processingFolder);
+
+        for ($i = ResourceStorage::PROCESSING_FOLDER_LEVELS; $i>0; $i--) {
+            $processingFolder = $processingFolder->getParentFolder();
+        }
+        $this->assertEquals($rootProcessingFolder, $processingFolder);
+    }
+
+    /**
+     * @param string $targetDirectory
+     * @param string $fileMountFolder
+     * @param bool $isFileMountReadOnly
+     * @param bool $checkWriteAccess
+     * @param bool $expectedResult
+     * @test
+     * @dataProvider isWithinFileMountBoundariesDataProvider
+     */
+    public function isWithinFileMountBoundariesRespectsReadOnlyFileMounts($targetDirectory, $fileMountFolder, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
+    {
+        $fileName = 'bar.txt';
+        $this->setUpBackendUserFromFixture(1);
+        (new ConnectionPool())->getConnectionForTable('sys_file_storage')
+            ->insert('sys_file_storage', $this->defaultStorageRecord);
+        GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/_processed_');
+        GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $targetDirectory);
+        if ($fileMountFolder !== $targetDirectory) {
+            GeneralUtility::mkdir_deep(PATH_site . 'fileadmin/' . $fileMountFolder);
+        }
+        file_put_contents(PATH_site . 'fileadmin/' . $targetDirectory . '/' . $fileName, 'myData');
+        clearstatcache();
+        $file = ResourceFactory::getInstance()->getFileObjectFromCombinedIdentifier('1:/' . $targetDirectory . '/' . $fileName);
+        $driver = new LocalDriver(['basePath' => 'fileadmin/', 'pathType' => 'relative', 'caseSensitive' => '1']);
+
+        $subject = new ResourceStorage($driver, $this->defaultStorageRecord);
+        $subject->setEvaluatePermissions(true);
+
+        // read_only = true -> no write access for user, so checkinf for second argument true should assert false
+        $subject->addFileMount('/' . $fileMountFolder . '/', ['read_only' => $isFileMountReadOnly]);
+        $this->assertSame($expectedResult, $subject->isWithinFileMountBoundaries($file, $checkWriteAccess));
+    }
+
+    /**
+     * @return array
+     */
+    public function isWithinFileMountBoundariesDataProvider()
+    {
+        return [
+            'Access to file in ro file mount denied for write request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'fooBaz',
+                '$isFileMountReadOnly' => true,
+                '$checkWriteAccess' => true,
+                '$expectedResult' => false,
+            ],
+            'Access to file in ro file mount allowed for read request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'fooBaz',
+                '$isFileMountReadOnly' => true,
+                '$checkWriteAccess' => false,
+                '$expectedResult' => true,
+            ],
+            'Access to file in rw file mount allowed for write request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'fooBaz',
+                '$isFileMountReadOnly' => false,
+                '$checkWriteAccess' => true,
+                '$expectedResult' => true,
+            ],
+            'Access to file in rw file mount allowed for read request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'fooBaz',
+                '$isFileMountReadOnly' => false,
+                '$checkWriteAccess' => false,
+                '$expectedResult' => true,
+            ],
+            'Access to file not in file mount denied for write request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'barBaz',
+                '$isFileMountReadOnly' => false,
+                '$checkWriteAccess' => true,
+                '$expectedResult' => false,
+            ],
+            'Access to file not in file mount denied for read request' => [
+                '$targetDirectory' => 'fooBaz',
+                '$fileMountFolder' => 'barBaz',
+                '$isFileMountReadOnly' => false,
+                '$checkWriteAccess' => false,
+                '$expectedResult' => false,
+            ],
+        ];
+    }
+}
index 065385b..e5c170d 100644 (file)
@@ -14,13 +14,7 @@ namespace TYPO3\CMS\Core\Tests\Unit\Resource;
  * The TYPO3 project - inspiring people to share!
  */
 
-use Doctrine\DBAL\Driver\Statement;
-use Prophecy\Argument;
-use TYPO3\CMS\Core\Database\Connection;
-use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
-use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
-use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Resource\Driver\AbstractDriver;
 use TYPO3\CMS\Core\Resource\Driver\LocalDriver;
 use TYPO3\CMS\Core\Resource\File;
@@ -29,7 +23,6 @@ use TYPO3\CMS\Core\Resource\Folder;
 use TYPO3\CMS\Core\Resource\FolderInterface;
 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
-use TYPO3\CMS\Core\Resource\ResourceStorageInterface;
 use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -212,120 +205,6 @@ class ResourceStorageTest extends BaseTestCase
     /**
      * @return array
      */
-    public function isWithinFileMountBoundariesDataProvider()
-    {
-        return array(
-            'Access to file in ro file mount denied for write request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/fooBaz/',
-                '$isFileMountReadOnly' => true,
-                '$checkWriteAccess' => true,
-                '$expectedResult' => false,
-            ),
-            'Access to file in ro file mount allowed for read request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/fooBaz/',
-                '$isFileMountReadOnly' => true,
-                '$checkWriteAccess' => false,
-                '$expectedResult' => true,
-            ),
-            'Access to file in rw file mount allowed for write request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/fooBaz/',
-                '$isFileMountReadOnly' => false,
-                '$checkWriteAccess' => true,
-                '$expectedResult' => true,
-            ),
-            'Access to file in rw file mount allowed for read request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/fooBaz/',
-                '$isFileMountReadOnly' => false,
-                '$checkWriteAccess' => false,
-                '$expectedResult' => true,
-            ),
-            'Access to file not in file mount denied for write request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/barBaz/',
-                '$isFileMountReadOnly' => false,
-                '$checkWriteAccess' => true,
-                '$expectedResult' => false,
-            ),
-            'Access to file not in file mount denied for read request' => array(
-                '$fileIdentifier' => '/fooBaz/bar.txt',
-                '$fileMountFolderIdentifier' => '/barBaz/',
-                '$isFileMountReadOnly' => false,
-                '$checkWriteAccess' => false,
-                '$expectedResult' => false,
-            ),
-        );
-    }
-
-    /**
-     * @param string $fileIdentifier
-     * @param string $fileMountFolderIdentifier
-     * @param bool $isFileMountReadOnly
-     * @param bool $checkWriteAccess
-     * @param bool $expectedResult
-     * @throws \TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
-     * @test
-     * @dataProvider isWithinFileMountBoundariesDataProvider
-     */
-    public function isWithinFileMountBoundariesRespectsReadOnlyFileMounts($fileIdentifier, $fileMountFolderIdentifier, $isFileMountReadOnly, $checkWriteAccess, $expectedResult)
-    {
-        // @todo mess ahead - rewrite those tests!
-        $connectionProphet = $this->prophesize(Connection::class);
-        $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
-        $connectionProphet->delete(Argument::cetera())->willReturn(0);
-        $connectionProphet->insert(Argument::cetera())->willReturn(0);
-
-        $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
-        $queryBuilderProphet->expr()->willReturn(
-            GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
-        );
-        $queryBuilderProphet->select(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        $queryBuilderProphet->from(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        $queryBuilderProphet->createNamedParameter(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        $queryBuilderProphet->__toString()->willReturn('');
-        $queryBuilderProphet->where(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        $statementProphecy = $this->prophesize(Statement::class);
-        $queryBuilderProphet->execute()->willReturn($statementProphecy->reveal());
-
-        $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
-        $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
-        $connectionPoolProphet->getConnectionForTable(Argument::cetera())->willReturn($connectionProphet->reveal());
-        GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
-
-        /** @var AbstractDriver|\PHPUnit_Framework_MockObject_MockObject $driverMock */
-        $driverMock = $this->getMockForAbstractClass(AbstractDriver::class, array(), '', false);
-        $driverMock->expects($this->any())
-            ->method('getFolderInfoByIdentifier')
-            ->willReturnCallback(function ($identifier) use ($isFileMountReadOnly) {
-                return array(
-                    'identifier' => $identifier,
-                    'name' => trim($identifier, '/'),
-                );
-            });
-        $driverMock->expects($this->any())
-            ->method('isWithin')
-            ->willReturnCallback(function ($folderIdentifier, $fileIdentifier) {
-                if ($fileIdentifier === ResourceStorageInterface::DEFAULT_ProcessingFolder . '/') {
-                    return false;
-                } else {
-                    return strpos($fileIdentifier, $folderIdentifier) === 0;
-                }
-            });
-        $this->prepareSubject(array(), false, $driverMock);
-        $fileMock = $this->getSimpleFileMock($fileIdentifier);
-        $this->subject->setEvaluatePermissions(true);
-        $this->subject->addFileMount('/' . $this->getUniqueId('random') . '/', array('read_only' => false));
-        $this->subject->addFileMount($fileMountFolderIdentifier, array('read_only' => $isFileMountReadOnly));
-        $this->subject->addFileMount('/' . $this->getUniqueId('random') . '/', array('read_only' => false));
-        $this->assertSame($expectedResult, $this->subject->isWithinFileMountBoundaries($fileMock, $checkWriteAccess));
-    }
-
-    /**
-     * @return array
-     */
     public function capabilitiesDataProvider()
     {
         return array(
@@ -872,25 +751,4 @@ class ResourceStorageTest extends BaseTestCase
 
         $this->assertInstanceOf(Folder::class, $processingFolder);
     }
-
-    /**
-     * @test
-     */
-    public function getNestedProcessingFolderTest()
-    {
-        $mockedDriver = $this->createDriverMock(array('basePath' => $this->getMountRootUrl()), null, null);
-        $this->prepareSubject(array(), true, $mockedDriver);
-        $mockedFile = $this->getSimpleFileMock('/someFile');
-
-        $rootProcessingFolder = $this->subject->getProcessingFolder();
-        $processingFolder = $this->subject->getProcessingFolder($mockedFile);
-
-        $this->assertInstanceOf(Folder::class, $processingFolder);
-        $this->assertNotEquals($rootProcessingFolder, $processingFolder);
-
-        for ($i = ResourceStorage::PROCESSING_FOLDER_LEVELS; $i>0; $i--) {
-            $processingFolder = $processingFolder->getParentFolder();
-        }
-        $this->assertEquals($rootProcessingFolder, $processingFolder);
-    }
 }