‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
24 use ‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
36 final class ‪PropertyMappingConfigurationTest extends UnitTestCase
37 {
38  protected bool ‪$resetSingletonInstances = true;
39 
41  protected ExtbasePropertyMappingConfiguration&MockObject ‪$extbasePropertyMappingConfiguration;
42  protected ‪FileUpload&MockObject ‪$fileUpload;
43  protected ‪FormDefinition&MockObject ‪$rootForm;
44  protected ‪ProcessingRule&MockObject ‪$processingRule;
45 
46  public function ‪setUp(): void
47  {
48  parent::setUp();
49  // Property Mapping Configuration
50  $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
51  ->onlyMethods(['setTypeConverterOptions'])
52  ->disableOriginalConstructor()
53  ->getMock();
54 
55  // Processing Rules
56  $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
57  ->onlyMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
58  ->disableOriginalConstructor()
59  ->getMock();
60 
61  $this->processingRule
62  ->method('getPropertyMappingConfiguration')
63  ->willReturn($this->extbasePropertyMappingConfiguration);
64 
65  // Root Form
66  $this->rootForm = $this->getMockBuilder(FormDefinition::class)
67  ->onlyMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $this->rootForm
72  ->method('getProcessingRule')
73  ->willReturn($this->processingRule);
74 
75  // File Upload
76  $this->fileUpload = $this->getMockBuilder(FileUpload::class)
77  ->onlyMethods(['getProperties', 'getRootForm', 'getIdentifier'])
78  ->disableOriginalConstructor()
79  ->getMock();
80 
81  $this->fileUpload
82  ->method('getRootForm')
83  ->willReturn($this->rootForm);
84 
85  $this->fileUpload
86  ->method('getIdentifier')
87  ->willReturn('foobar');
88 
89  $this->propertyMappingConfiguration = new ‪PropertyMappingConfiguration($this->createMock(ValidatorResolver::class));
90  }
91 
95  #[Test]
97  {
98  // No validators
99  $this->processingRule
100  ->method('getValidators')
101  ->willReturn(new \SplObjectStorage());
102 
103  // Mime Types not important
104  $this->fileUpload
105  ->method('getProperties')
106  ->willReturn(['allowedMimeTypes' => []]);
107 
108  // Check if the UploadFileReference is included
109  $this->extbasePropertyMappingConfiguration
110  ->expects(self::atLeastOnce())
111  ->method('setTypeConverterOptions')
112  ->with(UploadedFileReferenceConverter::class);
113 
114  // Property Mapping Configuration
115  ‪$propertyMappingConfiguration = new ‪PropertyMappingConfiguration($this->createMock(ValidatorResolver::class));
117  }
118 
119  #[Test]
121  {
122  $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
123 
124  // Don't add any validators for now
125  $this->processingRule
126  ->method('getValidators')
127  ->willReturn(new \SplObjectStorage());
128 
129  // Add some Mime types
130  $this->fileUpload
131  ->method('getProperties')
132  ->willReturn($mimeTypes);
133 
134  // Expect the array to contain the MimeTypeValidator
135  $this->extbasePropertyMappingConfiguration
136  ->expects(self::atLeastOnce())
137  ->method('setTypeConverterOptions')
138  ->willReturnCallback(function (string $typeConverter, array $options): ExtbasePropertyMappingConfiguration {
139  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $options);
141 
142  $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
143 
145  });
146 
147  $mimeTypeValidator = new ‪MimeTypeValidator();
148  $mimeTypeValidator->setOptions(['allowedMimeTypes' => []]);
149  $validatorResolver = $this->createMock(ValidatorResolver::class);
150  $validatorResolver->method('createValidator')->with(
151  MimeTypeValidator::class,
152  ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']]
153  )->willReturn($mimeTypeValidator);
156  }
157 
158  #[Test]
160  {
161  $resourceFactory = $this->createMock(ResourceFactory::class);
162  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
163 
164  // Don't add any validators for now
165  $this->processingRule
166  ->method('getValidators')
167  ->willReturn(new \SplObjectStorage());
168 
169  // Set the file mount
170  $this->fileUpload
171  ->method('getProperties')
172  ->willReturn(['saveToFileMount' => '/tmp']);
173 
174  // Expect the array to contain the /tmp upload directory
175  $this->extbasePropertyMappingConfiguration
176  ->expects(self::atLeastOnce())
177  ->method('setTypeConverterOptions')
178  ->willReturnCallback(function (string $typeConverter, array $options): ExtbasePropertyMappingConfiguration {
179  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $options);
181 
182  $this->assertSame('/tmp', $folder);
183 
185  });
186 
187  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
188  }
189 
190  #[Test]
192  {
193  $resourceFactory = $this->createMock(ResourceFactory::class);
194  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory);
195 
196  // Don't add any validators for now
197  $this->processingRule
198  ->method('getValidators')
199  ->willReturn(new \SplObjectStorage());
200 
201  $this->rootForm
202  ->method('getPersistenceIdentifier')
203  ->willReturn('/tmp/somefile');
204 
205  // Set the file mount
206  $this->fileUpload
207  ->method('getProperties')
208  ->willReturn(['saveToFileMount' => '']);
209 
210  // Expect the array to contain the /tmp upload directory
211  $this->extbasePropertyMappingConfiguration
212  ->expects(self::atLeastOnce())
213  ->method('setTypeConverterOptions')
214  ->willReturnCallback(function (string $typeConverter, array $options): ExtbasePropertyMappingConfiguration {
215  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $options);
217 
218  $this->assertSame('/tmp', $folder);
219 
221  });
222 
223  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
224  }
225 
226  #[Test]
228  {
229  // Don't add any validators for now
230  $this->processingRule
231  ->method('getValidators')
232  ->willReturn(new \SplObjectStorage());
233 
234  $this->rootForm
235  ->method('getPersistenceIdentifier')
236  ->willReturn('');
237 
238  // Set the file mount
239  $this->fileUpload
240  ->method('getProperties')
241  ->willReturn(['saveToFileMount' => '']);
242 
243  // Expect the array to contain the /tmp upload directory
244  $this->extbasePropertyMappingConfiguration
245  ->expects(self::atLeastOnce())
246  ->method('setTypeConverterOptions')
247  ->willReturnCallback(function (string $typeConverter, array $options): ExtbasePropertyMappingConfiguration {
248  $this->assertArrayNotHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $options);
249 
251  });
252 
253  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
254  }
255 
256  #[Test]
258  {
259  // Some other Validator
260  $otherValidator = $this->createMock(ValidatorInterface::class);
261 
262  // Don't add any validators for now
263  $validators = new \SplObjectStorage();
264  $validators->attach($otherValidator);
265 
266  $this->processingRule
267  ->method('getValidators')
268  ->willReturn($validators);
269 
270  // Expect the array to contain the /tmp upload directory
271  $this->extbasePropertyMappingConfiguration
272  ->expects(self::atLeastOnce())
273  ->method('setTypeConverterOptions')
274  ->willReturnCallback(function (string $typeConverter, array $options) use ($otherValidator): ExtbasePropertyMappingConfiguration {
275  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $options);
277 
278  self::assertContains($otherValidator, $validators);
279 
281  });
282 
283  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
284  }
285 
286  #[Test]
288  {
289  // Not Empty Validator
290  $notEmptyValidator = new ‪NotEmptyValidator();
291  $notEmptyValidator->setOptions([]);
292 
293  // Don't add any validators for now
294  $validators = new \SplObjectStorage();
295  $validators->attach($notEmptyValidator);
296 
297  $this->processingRule
298  ->method('getValidators')
299  ->willReturn($validators);
300 
301  // Expect the array to contain the /tmp upload directory
302  $this->extbasePropertyMappingConfiguration
303  ->expects(self::atLeastOnce())
304  ->method('setTypeConverterOptions')
305  ->willReturnCallback(function (string $typeConverter, array $options) use ($notEmptyValidator): ExtbasePropertyMappingConfiguration {
306  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $options);
308 
309  self::assertNotContains($notEmptyValidator, $validators);
310 
312  });
313 
314  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
315  }
316 }
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$processingRule
‪ProcessingRule &MockObject $processingRule
Definition: PropertyMappingConfigurationTest.php:44
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedDoesNotCopyNotEmptyValidator
‪afterBuildingFinishedDoesNotCopyNotEmptyValidator()
Definition: PropertyMappingConfigurationTest.php:287
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter
Definition: UploadedFileReferenceConverter.php:50
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:38
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsFileReferenceConverter
‪afterBuildingFinishedAddsFileReferenceConverter()
Definition: PropertyMappingConfigurationTest.php:96
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$propertyMappingConfiguration
‪PropertyMappingConfiguration $propertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:40
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:38
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\setUp
‪setUp()
Definition: PropertyMappingConfigurationTest.php:46
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration\afterBuildingFinished
‪afterBuildingFinished(RenderableInterface $renderable)
Definition: PropertyMappingConfiguration.php:51
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest
Definition: PropertyMappingConfigurationTest.php:37
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$rootForm
‪FormDefinition &MockObject $rootForm
Definition: PropertyMappingConfigurationTest.php:43
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PropertyMappingConfigurationTest.php:38
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_FOLDER
‪const CONFIGURATION_UPLOAD_FOLDER
Definition: UploadedFileReferenceConverter.php:54
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property
Definition: PropertyMappingConfigurationTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedCopiesValidators
‪afterBuildingFinishedCopiesValidators()
Definition: PropertyMappingConfigurationTest.php:257
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath
‪afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
Definition: PropertyMappingConfigurationTest.php:227
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsMimeTypeConverter
‪afterBuildingFinishedAddsMimeTypeConverter()
Definition: PropertyMappingConfigurationTest.php:120
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically
‪afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
Definition: PropertyMappingConfigurationTest.php:191
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:224
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$extbasePropertyMappingConfiguration
‪ExtbasePropertyMappingConfiguration &MockObject $extbasePropertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:41
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$fileUpload
‪FileUpload &MockObject $fileUpload
Definition: PropertyMappingConfigurationTest.php:42
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_FILE_VALIDATORS
‪const CONFIGURATION_FILE_VALIDATORS
Definition: UploadedFileReferenceConverter.php:69
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:24
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists
‪afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
Definition: PropertyMappingConfigurationTest.php:159
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:28
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:26
‪TYPO3\CMS\Form\Mvc\ProcessingRule
Definition: ProcessingRule.php:36