‪TYPO3CMS  11.5
PropertyMappingConfigurationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use ‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
36 class ‪PropertyMappingConfigurationTest extends UnitTestCase
37 {
41  protected ‪$resetSingletonInstances = true;
42 
45 
48 
50  protected ‪$fileUpload;
51 
53  protected ‪$rootForm;
54 
56  protected ‪$processingRule;
57 
58  public function ‪setUp(): void
59  {
60  parent::setUp();
61  // Property Mapping Configuration
62  $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
63  ->onlyMethods(['setTypeConverterOptions'])
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  // Processing Rules
68  $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
69  ->onlyMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
70  ->disableOriginalConstructor()
71  ->getMock();
72 
73  $this->processingRule
74  ->method('getPropertyMappingConfiguration')
75  ->willReturn($this->extbasePropertyMappingConfiguration);
76 
77  // Root Form
78  $this->rootForm = $this->getMockBuilder(FormDefinition::class)
79  ->onlyMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
80  ->disableOriginalConstructor()
81  ->getMock();
82 
83  $this->rootForm
84  ->method('getProcessingRule')
85  ->willReturn($this->processingRule);
86 
87  // File Upload
88  $this->fileUpload = $this->getMockBuilder(FileUpload::class)
89  ->onlyMethods(['getProperties', 'getRootForm', 'getIdentifier'])
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $this->fileUpload
94  ->method('getRootForm')
95  ->willReturn($this->rootForm);
96 
97  $this->fileUpload
98  ->method('getIdentifier')
99  ->willReturn('foobar');
100 
101  // Property Mapping Configuration
102  $this->propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
103  }
104 
110  {
111  // No validators
112  $this->processingRule
113  ->method('getValidators')
114  ->willReturn(new \SplObjectStorage());
115 
116  // Mime Types not important
117  $this->fileUpload
118  ->method('getProperties')
119  ->willReturn(['allowedMimeTypes' => []]);
120 
121  // Check if the UploadFileReference is included
122  $this->extbasePropertyMappingConfiguration
123  ->expects(self::atLeastOnce())
124  ->method('setTypeConverterOptions')
125  ->with(UploadedFileReferenceConverter::class);
126 
127  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
128  }
129 
133  public function ‪afterBuildingFinishedAddsMimeTypeConverter(): void
134  {
135  $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
136 
137  // Don't add any validators for now
138  $this->processingRule
139  ->method('getValidators')
140  ->willReturn(new \SplObjectStorage());
141 
142  // Add some Mime types
143  $this->fileUpload
144  ->method('getProperties')
145  ->willReturn($mimeTypes);
146 
147  // Expect the array to contain the MimeTypeValidator
148  $this->extbasePropertyMappingConfiguration
149  ->expects(self::atLeastOnce())
150  ->method('setTypeConverterOptions')
151  ->willReturnCallback(function ($class, $config) {
152  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
154 
155  $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
156  });
157 
158  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
159  }
160 
165  {
166  $resourceFactory = $this->createMock(ResourceFactory::class);
167  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
168 
169  // Don't add any validators for now
170  $this->processingRule
171  ->method('getValidators')
172  ->willReturn(new \SplObjectStorage());
173 
174  // Set the file mount
175  $this->fileUpload
176  ->method('getProperties')
177  ->willReturn(['saveToFileMount' => '/tmp']);
178 
179  // Expect the array to contain the /tmp upload directory
180  $this->extbasePropertyMappingConfiguration
181  ->expects(self::atLeastOnce())
182  ->method('setTypeConverterOptions')
183  ->willReturnCallback(function ($class, $config) {
184  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
186 
187  $this->assertSame('/tmp', $folder);
188  });
189 
190  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
191  }
192 
197  {
198  $resourceFactory = $this->createMock(ResourceFactory::class);
199  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
200 
201  // Don't add any validators for now
202  $this->processingRule
203  ->method('getValidators')
204  ->willReturn(new \SplObjectStorage());
205 
206  $this->rootForm
207  ->method('getPersistenceIdentifier')
208  ->willReturn('/tmp/somefile');
209 
210  // Set the file mount
211  $this->fileUpload
212  ->method('getProperties')
213  ->willReturn(['saveToFileMount' => '']);
214 
215  // Expect the array to contain the /tmp upload directory
216  $this->extbasePropertyMappingConfiguration
217  ->expects(self::atLeastOnce())
218  ->method('setTypeConverterOptions')
219  ->willReturnCallback(function ($class, $config) {
220  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
222 
223  $this->assertSame('/tmp', $folder);
224  });
225 
226  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
227  }
228 
233  {
234  // Don't add any validators for now
235  $this->processingRule
236  ->method('getValidators')
237  ->willReturn(new \SplObjectStorage());
238 
239  $this->rootForm
240  ->method('getPersistenceIdentifier')
241  ->willReturn('');
242 
243  // Set the file mount
244  $this->fileUpload
245  ->method('getProperties')
246  ->willReturn(['saveToFileMount' => '']);
247 
248  // Expect the array to contain the /tmp upload directory
249  $this->extbasePropertyMappingConfiguration
250  ->expects(self::atLeastOnce())
251  ->method('setTypeConverterOptions')
252  ->willReturnCallback(function ($class, $config) {
253  $this->assertArrayNotHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
254  });
255 
256  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
257  }
258 
262  public function ‪afterBuildingFinishedCopiesValidators(): void
263  {
264  // Some other Validator
265  $otherValidator = $this->getMockForAbstractClass(AbstractValidator::class);
266 
267  // Don't add any validators for now
268  $validators = new \SplObjectStorage();
269  $validators->attach($otherValidator);
270 
271  $this->processingRule
272  ->method('getValidators')
273  ->willReturn($validators);
274 
275  // Expect the array to contain the /tmp upload directory
276  $this->extbasePropertyMappingConfiguration
277  ->expects(self::atLeastOnce())
278  ->method('setTypeConverterOptions')
279  ->willReturnCallback(function ($class, $config) use ($otherValidator) {
280  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
282 
283  self::assertContains($otherValidator, $validators);
284  });
285 
286  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
287  }
288 
293  {
294  // Not Empty Validator
295  $notEmptyValidator = $this->getMockForAbstractClass(NotEmptyValidator::class);
296 
297  // Don't add any validators for now
298  $validators = new \SplObjectStorage();
299  $validators->attach($notEmptyValidator);
300 
301  $this->processingRule
302  ->method('getValidators')
303  ->willReturn($validators);
304 
305  // Expect the array to contain the /tmp upload directory
306  $this->extbasePropertyMappingConfiguration
307  ->expects(self::atLeastOnce())
308  ->method('setTypeConverterOptions')
309  ->willReturnCallback(function ($class, $config) use ($notEmptyValidator) {
310  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
312 
313  self::assertNotContains($notEmptyValidator, $validators);
314  });
315 
316  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
317  }
318 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:27
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedDoesNotCopyNotEmptyValidator
‪afterBuildingFinishedDoesNotCopyNotEmptyValidator()
Definition: PropertyMappingConfigurationTest.php:286
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter
Definition: UploadedFileReferenceConverter.php:48
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:37
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsFileReferenceConverter
‪afterBuildingFinishedAddsFileReferenceConverter()
Definition: PropertyMappingConfigurationTest.php:103
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$fileUpload
‪PHPUnit Framework MockObject MockObject FileUpload $fileUpload
Definition: PropertyMappingConfigurationTest.php:46
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$propertyMappingConfiguration
‪PropertyMappingConfiguration $propertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:42
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\setUp
‪setUp()
Definition: PropertyMappingConfigurationTest.php:52
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest
Definition: PropertyMappingConfigurationTest.php:37
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PropertyMappingConfigurationTest.php:40
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_FOLDER
‪const CONFIGURATION_UPLOAD_FOLDER
Definition: UploadedFileReferenceConverter.php:52
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property
Definition: PropertyMappingConfigurationTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedCopiesValidators
‪afterBuildingFinishedCopiesValidators()
Definition: PropertyMappingConfigurationTest.php:256
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath
‪afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
Definition: PropertyMappingConfigurationTest.php:226
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$rootForm
‪PHPUnit Framework MockObject MockObject FormDefinition $rootForm
Definition: PropertyMappingConfigurationTest.php:48
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsMimeTypeConverter
‪afterBuildingFinishedAddsMimeTypeConverter()
Definition: PropertyMappingConfigurationTest.php:127
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically
‪afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
Definition: PropertyMappingConfigurationTest.php:190
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:226
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$processingRule
‪PHPUnit Framework MockObject MockObject ProcessingRule $processingRule
Definition: PropertyMappingConfigurationTest.php:50
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$extbasePropertyMappingConfiguration
‪PHPUnit Framework MockObject MockObject ExtbasePropertyMappingConfiguration $extbasePropertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:44
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_FILE_VALIDATORS
‪const CONFIGURATION_FILE_VALIDATORS
Definition: UploadedFileReferenceConverter.php:67
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:22
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists
‪afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
Definition: PropertyMappingConfigurationTest.php:158
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:28
‪TYPO3\CMS\Form\Mvc\ProcessingRule
Definition: ProcessingRule.php:35