[BUGFIX] FormEngine: Fix type=group with internal_type=folder 00/44700/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 13 Nov 2015 09:05:36 +0000 (10:05 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Fri, 13 Nov 2015 19:32:51 +0000 (20:32 +0100)
Restore support for group type fields with internal type of `folder`.

Resolves: #70449
Releases: master
Change-Id: I6cfea6c74c003d64fbbbe7d8904420c757f044d5
Reviewed-on: https://review.typo3.org/44700
Tested-by: Manfred Rutschmann <manfred@dslr-seite.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaGroup.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaGroupTest.php

index 199843f..43c87a2 100644 (file)
@@ -16,6 +16,8 @@ namespace TYPO3\CMS\Backend\Form\FormDataProvider;
 
 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
 use TYPO3\CMS\Core\Database\RelationHandler;
+use TYPO3\CMS\Core\Resource\Folder;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
 
@@ -50,7 +52,7 @@ class TcaGroup implements FormDataProviderInterface
 
             $internalType = $fieldConfig['config']['internal_type'];
             if ($internalType === 'file_reference' || $internalType === 'file') {
-                $files = array();
+                $files = [];
                 // Simple list of files
                 $fileList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
                 foreach ($fileList as $file) {
@@ -72,8 +74,21 @@ class TcaGroup implements FormDataProviderInterface
                 );
                 $relationHandler->getFromDB();
                 $result['databaseRow'][$fieldName] = $relationHandler->readyForInterface();
+            } elseif ($internalType === 'folder') {
+                $folders = [];
+                // Simple list of folders
+                $folderList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
+                foreach ($folderList as $folder) {
+                    if ($folder) {
+                        $folderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($folder);
+                        if ($folderObject instanceof Folder) {
+                            $folderName = PathUtility::basename($folderObject->getIdentifier());
+                            $folders[] = rawurlencode($folder) . '|' . rawurlencode($folderName);
+                        }
+                    }
+                }
+                $result['databaseRow'][$fieldName] = implode(',', $folders);
             } else {
-                // @todo: "folder" is a valid internal_type, too.
                 throw new \UnexpectedValueException(
                     'TCA internal_type of field "' . $fieldName . '" in table ' . $result['tableName']
                     . ' must be set to either "db", "file" or "file_reference"',
index 5b84be1..da2509b 100644 (file)
@@ -17,6 +17,8 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
 
 use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Core\Database\RelationHandler;
+use TYPO3\CMS\Core\Resource\Folder;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Tests\UnitTestCase;
 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -31,9 +33,21 @@ class TcaGroupTest extends UnitTestCase
      */
     protected $subject;
 
+    /**
+     * @var array
+     */
+    protected $singletonInstances;
+
     protected function setUp()
     {
         $this->subject = new TcaGroup();
+        $this->singletonInstances = GeneralUtility::getSingletonInstances();
+    }
+
+    protected function tearDown()
+    {
+        GeneralUtility::resetSingletonInstances($this->singletonInstances);
+        parent::tearDown();
     }
 
     /**
@@ -62,7 +76,7 @@ class TcaGroupTest extends UnitTestCase
     /**
      * @test
      */
-    public function addDataThrowsExceptionWithTypeGroupAndNoValiInternalType()
+    public function addDataThrowsExceptionWithTypeGroupAndNoValidInternalType()
     {
         $input = [
             'processedTca' => [
@@ -108,6 +122,43 @@ class TcaGroupTest extends UnitTestCase
     /**
      * @test
      */
+    public function addDataSetsFolderData()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => '1:/aFolder/anotherFolder/',
+            ],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'group',
+                            'internal_type' => 'folder',
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var Folder|ObjectProphecy $relationHandlerProphecy */
+        $folderProphecy = $this->prophesize(Folder::class);
+        $folderProphecy->getIdentifier()->shouldBeCalled()->willReturn('anotherFolder');
+
+        /** @var ResourceFactory|ObjectProphecy $relationHandlerProphecy */
+        $resourceFactoryProphecy = $this->prophesize(ResourceFactory::class);
+        GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryProphecy->reveal());
+        $resourceFactoryProphecy->retrieveFileOrFolderObject('1:/aFolder/anotherFolder/')
+            ->shouldBeCalled()
+            ->willReturn($folderProphecy->reveal());
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = '1%3A%2FaFolder%2FanotherFolder%2F|anotherFolder';
+        $this->assertSame($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
     public function addDataSetsDatabaseData()
     {
         $aFieldConfig = [