[TASK] EXT:form - Tests for PropertyMappingConfiguration 32/55032/6
authorMarcel Jürgen Falk <marcel.falk@tritum.de>
Mon, 11 Dec 2017 08:04:28 +0000 (09:04 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Wed, 20 Dec 2017 22:14:31 +0000 (23:14 +0100)
Resolves: #83279
Releases: master, 8.7
Change-Id: I40f6ab483289cbf45f132453b0a1e0af4b72b5b5
Reviewed-on: https://review.typo3.org/55032
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/form/Classes/Mvc/Property/PropertyMappingConfiguration.php
typo3/sysext/form/Tests/Unit/Mvc/Property/PropertyMappingConfigurationTest.php [new file with mode: 0644]

index bde3904..9adf724 100644 (file)
@@ -49,18 +49,19 @@ class PropertyMappingConfiguration
      */
     public function afterBuildingFinished(RenderableInterface $renderable)
     {
-        if (get_class($renderable) === FileUpload::class) {
+        if ($renderable instanceof FileUpload) {
             /** @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration */
             $propertyMappingConfiguration = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier())->getPropertyMappingConfiguration();
 
             $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
                 ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $renderable->getProperties()['allowedMimeTypes']]);
+
             $uploadConfiguration = [
                 UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS => [$mimeTypeValidator],
                 UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
             ];
 
-            $saveToFileMountIdentifier = (isset($renderable->getProperties()['saveToFileMount'])) ? $renderable->getProperties()['saveToFileMount'] : null;
+            $saveToFileMountIdentifier = (isset($renderable->getProperties()['saveToFileMount'])) ? $renderable->getProperties()['saveToFileMount'] : '';
             if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
                 $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
             } else {
diff --git a/typo3/sysext/form/Tests/Unit/Mvc/Property/PropertyMappingConfigurationTest.php b/typo3/sysext/form/Tests/Unit/Mvc/Property/PropertyMappingConfigurationTest.php
new file mode 100644 (file)
index 0000000..d1174e3
--- /dev/null
@@ -0,0 +1,374 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Form\Mvc\Property;
+
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
+use TYPO3\CMS\Form\Domain\Model\FormDefinition;
+use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
+use TYPO3\CMS\Form\Mvc\ProcessingRule;
+use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
+use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+class PropertyMappingConfigurationTest extends UnitTestCase
+{
+    /** @var PropertyMappingConfiguration */
+    protected $propertyMappingConfiguration;
+
+    /** @var \PHPUnit_Framework_MockObject_MockObject|ExtbasePropertyMappingConfiguration */
+    protected $extbasePropertyMappingConfiguration;
+
+    /** @var \PHPUnit_Framework_MockObject_MockObject|FileUpload */
+    protected $fileUpload;
+
+    /** @var \PHPUnit_Framework_MockObject_MockObject|FormDefinition */
+    protected $rootForm;
+
+    /** @var \PHPUnit_Framework_MockObject_MockObject|ProcessingRule */
+    protected $processingRule;
+
+    protected $singletons = [];
+
+    public function setUp()
+    {
+        $this->singletons = GeneralUtility::getSingletonInstances();
+
+        // Property Mapping Configuration
+        $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
+            ->setMethods(['setTypeConverterOptions'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        // Processing Rules
+        $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
+            ->setMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getPropertyMappingConfiguration')
+            ->willReturn($this->extbasePropertyMappingConfiguration);
+
+        // Root Form
+        $this->rootForm = $this->getMockBuilder(FormDefinition::class)
+            ->setMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->rootForm
+            ->expects($this->any())
+            ->method('getProcessingRule')
+            ->willReturn($this->processingRule);
+
+        // File Upload
+        $this->fileUpload = $this->getMockBuilder(FileUpload::class)
+            ->setMethods(['getProperties', 'getRootForm', 'getIdentifier'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getRootForm')
+            ->willReturn($this->rootForm);
+
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getIdentifier')
+            ->willReturn('foobar');
+
+        // Property Mapping Configuration
+        $this->propertyMappingConfiguration = new PropertyMappingConfiguration();
+    }
+
+    public function tearDown()
+    {
+        // Remove all singleton instances
+        GeneralUtility::resetSingletonInstances($this->singletons);
+    }
+
+    /**
+     * A bare minimum test that checks if the function maybe potentially works.
+     * @test
+     */
+    public function afterBuildingFinishedAddsFileReferenceConverter()
+    {
+        // Mime Type Validator
+        /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
+        $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
+
+        // Resource Factory
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
+        $resourceFactory = $this->createMock(ResourceFactory::class);
+
+        // Object Manager (in order to return mocked Resource Factory)
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
+        $objectManager = $this->getMockBuilder(ObjectManager::class)
+            ->setMethods(['get'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $objectManager
+            ->expects($this->any())
+            ->method('get')
+            ->willReturnMap([
+                [MimeTypeValidator::class, $mimeTypeValidator],
+                [ResourceFactory::class, $resourceFactory]
+            ]);
+
+        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
+
+        // No validators
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getValidators')
+            ->willReturn(new \SplObjectStorage());
+
+        // Mime Types not important
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getProperties')
+            ->willReturn(['allowedMimeTypes' => []]);
+
+        // Check if the UploadFileReference is included
+        $this->extbasePropertyMappingConfiguration
+            ->expects($this->atLeastOnce())
+            ->method('setTypeConverterOptions')
+            ->with(UploadedFileReferenceConverter::class);
+
+        $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
+    }
+
+    /**
+     * @test
+     */
+    public function afterBuildingFinishedAddsMimeTypeConverter()
+    {
+        $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
+
+        // Create a MimeTypeValidator Mock
+        /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
+        $mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
+            ->setMethods(['__construct'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        // Object Manager to return the MimeTypeValidator
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
+        $objectManager = $this->getMockBuilder(ObjectManager::class)
+            ->setMethods(['get'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $objectManager
+            ->expects($this->any())
+            ->method('get')
+            ->with(MimeTypeValidator::class, $mimeTypes)
+            ->willReturn($mimeTypeValidator);
+
+        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
+
+        // Don't add any validators for now
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getValidators')
+            ->willReturn(new \SplObjectStorage());
+
+        // Add some Mime types
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getProperties')
+            ->willReturn($mimeTypes);
+
+        // Expect the array to contain the MimeTypeValidator
+        $this->extbasePropertyMappingConfiguration
+            ->expects($this->atLeastOnce())
+            ->method('setTypeConverterOptions')
+            ->willReturnCallback(function ($class, $config) {
+                $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
+                $validators = $config[UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS];
+
+                $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
+            });
+
+        $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
+    }
+
+    /**
+     * @test
+     */
+    public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
+    {
+        // Mime Type Validator
+        /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
+        $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
+
+        // Resource Factory
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
+        $resourceFactory = $this->createMock(ResourceFactory::class);
+
+        // Object Manager (in order to return mocked Resource Factory)
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
+        $objectManager = $this->getMockBuilder(ObjectManager::class)
+            ->setMethods(['get'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $objectManager
+            ->expects($this->any())
+            ->method('get')
+            ->willReturnMap([
+                [MimeTypeValidator::class, $mimeTypeValidator],
+                [ResourceFactory::class, $resourceFactory]
+            ]);
+
+        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
+
+        // Don't add any validators for now
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getValidators')
+            ->willReturn(new \SplObjectStorage());
+
+        // Set the file mount
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getProperties')
+            ->willReturn(['saveToFileMount' => '/tmp']);
+
+        // Expect the array to contain the /tmp upload directory
+        $this->extbasePropertyMappingConfiguration
+            ->expects($this->atLeastOnce())
+            ->method('setTypeConverterOptions')
+            ->willReturnCallback(function ($class, $config) {
+                $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
+                $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
+
+                $this->assertSame('/tmp', $folder);
+            });
+
+        $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
+    }
+
+    /**
+     * @test
+     */
+    public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
+    {
+        // Mime Type Validator
+        /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
+        $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
+
+        // Resource Factory
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
+        $resourceFactory = $this->createMock(ResourceFactory::class);
+
+        // Object Manager (in order to return mocked Resource Factory)
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
+        $objectManager = $this->getMockBuilder(ObjectManager::class)
+            ->setMethods(['get'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $objectManager
+            ->expects($this->any())
+            ->method('get')
+            ->willReturnMap([
+                [MimeTypeValidator::class, $mimeTypeValidator],
+                [ResourceFactory::class, $resourceFactory]
+            ]);
+
+        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
+
+        // Don't add any validators for now
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getValidators')
+            ->willReturn(new \SplObjectStorage());
+
+        $this->rootForm
+            ->expects($this->any())
+            ->method('getPersistenceIdentifier')
+            ->willReturn('/tmp/somefile');
+
+        // Set the file mount
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getProperties')
+            ->willReturn(['saveToFileMount' => '']);
+
+        // Expect the array to contain the /tmp upload directory
+        $this->extbasePropertyMappingConfiguration
+            ->expects($this->atLeastOnce())
+            ->method('setTypeConverterOptions')
+            ->willReturnCallback(function ($class, $config) {
+                $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
+                $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
+
+                $this->assertSame('/tmp', $folder);
+            });
+
+        $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
+    }
+
+    /**
+     * @test
+     */
+    public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
+    {
+        // Mime Type Validator
+        /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
+        $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
+
+        // Resource Factory
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
+        $resourceFactory = $this->createMock(ResourceFactory::class);
+
+        // Object Manager (in order to return mocked Resource Factory)
+        /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
+        $objectManager = $this->getMockBuilder(ObjectManager::class)
+            ->setMethods(['get'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $objectManager
+            ->expects($this->any())
+            ->method('get')
+            ->willReturnMap([
+                [MimeTypeValidator::class, $mimeTypeValidator],
+                [ResourceFactory::class, $resourceFactory]
+            ]);
+
+        GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
+
+        // Don't add any validators for now
+        $this->processingRule
+            ->expects($this->any())
+            ->method('getValidators')
+            ->willReturn(new \SplObjectStorage());
+
+        $this->rootForm
+            ->expects($this->any())
+            ->method('getPersistenceIdentifier')
+            ->willReturn('');
+
+        // Set the file mount
+        $this->fileUpload
+            ->expects($this->any())
+            ->method('getProperties')
+            ->willReturn(['saveToFileMount' => '']);
+
+        // Expect the array to contain the /tmp upload directory
+        $this->extbasePropertyMappingConfiguration
+            ->expects($this->atLeastOnce())
+            ->method('setTypeConverterOptions')
+            ->willReturnCallback(function ($class, $config) {
+                $this->assertArrayNotHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
+            });
+
+        $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
+    }
+}