‪TYPO3CMS  10.4
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 
23 use ‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪PropertyMappingConfigurationTest extends UnitTestCase
38 {
42  protected ‪$resetSingletonInstances = true;
43 
46 
49 
51  protected ‪$fileUpload;
52 
54  protected ‪$rootForm;
55 
57  protected ‪$processingRule;
58 
59  public function ‪setUp(): void
60  {
61  parent::setUp();
62  // Property Mapping Configuration
63  $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
64  ->setMethods(['setTypeConverterOptions'])
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  // Processing Rules
69  $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
70  ->setMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->processingRule
75  ->expects(self::any())
76  ->method('getPropertyMappingConfiguration')
77  ->willReturn($this->extbasePropertyMappingConfiguration);
78 
79  // Root Form
80  $this->rootForm = $this->getMockBuilder(FormDefinition::class)
81  ->setMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
82  ->disableOriginalConstructor()
83  ->getMock();
84 
85  $this->rootForm
86  ->expects(self::any())
87  ->method('getProcessingRule')
88  ->willReturn($this->processingRule);
89 
90  // File Upload
91  $this->fileUpload = $this->getMockBuilder(FileUpload::class)
92  ->setMethods(['getProperties', 'getRootForm', 'getIdentifier'])
93  ->disableOriginalConstructor()
94  ->getMock();
95 
96  $this->fileUpload
97  ->expects(self::any())
98  ->method('getRootForm')
99  ->willReturn($this->rootForm);
100 
101  $this->fileUpload
102  ->expects(self::any())
103  ->method('getIdentifier')
104  ->willReturn('foobar');
105 
106  // Property Mapping Configuration
107  $this->propertyMappingConfiguration = new ‪PropertyMappingConfiguration();
108  }
109 
115  {
116  // Mime Type Validator
118  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
119 
120  // Resource Factory
122  $resourceFactory = $this->createMock(ResourceFactory::class);
124  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
125 
126  // Object Manager (in order to return mocked Resource Factory)
127  $objectManager = $this->prophesize(ObjectManager::class);
128  $objectManager->get(UploadedFileReferenceConverter::class)->willReturn($typeConverter);
129  $objectManager->get(MimeTypeValidator::class)->willReturn($mimeTypeValidator);
130  $objectManager->get(ResourceFactory::class)->willReturn($resourceFactory);
131  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
132 
133  // No validators
134  $this->processingRule
135  ->expects(self::any())
136  ->method('getValidators')
137  ->willReturn(new \SplObjectStorage());
138 
139  // Mime Types not important
140  $this->fileUpload
141  ->expects(self::any())
142  ->method('getProperties')
143  ->willReturn(['allowedMimeTypes' => []]);
144 
145  // Check if the UploadFileReference is included
146  $this->extbasePropertyMappingConfiguration
147  ->expects(self::atLeastOnce())
148  ->method('setTypeConverterOptions')
149  ->with(UploadedFileReferenceConverter::class);
150 
151  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
152  }
153 
157  public function ‪afterBuildingFinishedAddsMimeTypeConverter(): void
158  {
159  $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
160 
161  // Create a MimeTypeValidator Mock
163  $mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
164  ->setMethods(['__construct'])
165  ->disableOriginalConstructor()
166  ->getMock();
168  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
169 
170  // Object Manager to return the MimeTypeValidator
171  $objectManager = $this->prophesize(ObjectManager::class);
172  $objectManager->get(UploadedFileReferenceConverter::class)->willReturn($typeConverter);
173  $objectManager->get(MimeTypeValidator::class, $mimeTypes)->willReturn($mimeTypeValidator);
174  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager->reveal());
175 
176  // Don't add any validators for now
177  $this->processingRule
178  ->expects(self::any())
179  ->method('getValidators')
180  ->willReturn(new \SplObjectStorage());
181 
182  // Add some Mime types
183  $this->fileUpload
184  ->expects(self::any())
185  ->method('getProperties')
186  ->willReturn($mimeTypes);
187 
188  // Expect the array to contain the MimeTypeValidator
189  $this->extbasePropertyMappingConfiguration
190  ->expects(self::atLeastOnce())
191  ->method('setTypeConverterOptions')
192  ->willReturnCallback(function ($class, $config) {
193  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
195 
196  $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
197  });
198 
199  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
200  }
201 
206  {
207  // Mime Type Validator
209  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
210 
211  // Resource Factory
213  $resourceFactory = $this->createMock(ResourceFactory::class);
215  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
216 
217  // Object Manager (in order to return mocked Resource Factory)
219  $objectManager = $this->getMockBuilder(ObjectManager::class)
220  ->setMethods(['get'])
221  ->disableOriginalConstructor()
222  ->getMock();
223 
224  $objectManager
225  ->expects(self::any())
226  ->method('get')
227  ->willReturnMap([
228  [UploadedFileReferenceConverter::class, $typeConverter],
229  [MimeTypeValidator::class, $mimeTypeValidator],
230  [ResourceFactory::class, $resourceFactory]
231  ]);
232 
233  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
234 
235  // Don't add any validators for now
236  $this->processingRule
237  ->expects(self::any())
238  ->method('getValidators')
239  ->willReturn(new \SplObjectStorage());
240 
241  // Set the file mount
242  $this->fileUpload
243  ->expects(self::any())
244  ->method('getProperties')
245  ->willReturn(['saveToFileMount' => '/tmp']);
246 
247  // Expect the array to contain the /tmp upload directory
248  $this->extbasePropertyMappingConfiguration
249  ->expects(self::atLeastOnce())
250  ->method('setTypeConverterOptions')
251  ->willReturnCallback(function ($class, $config) {
252  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
254 
255  $this->assertSame('/tmp', $folder);
256  });
257 
258  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
259  }
260 
265  {
266  // Mime Type Validator
268  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
269 
270  // Resource Factory
272  $resourceFactory = $this->createMock(ResourceFactory::class);
274  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
275 
276  // Object Manager (in order to return mocked Resource Factory)
278  $objectManager = $this->getMockBuilder(ObjectManager::class)
279  ->setMethods(['get'])
280  ->disableOriginalConstructor()
281  ->getMock();
282 
283  $objectManager
284  ->expects(self::any())
285  ->method('get')
286  ->willReturnMap([
287  [UploadedFileReferenceConverter::class, $typeConverter],
288  [MimeTypeValidator::class, $mimeTypeValidator],
289  [ResourceFactory::class, $resourceFactory]
290  ]);
291 
292  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
293 
294  // Don't add any validators for now
295  $this->processingRule
296  ->expects(self::any())
297  ->method('getValidators')
298  ->willReturn(new \SplObjectStorage());
299 
300  $this->rootForm
301  ->expects(self::any())
302  ->method('getPersistenceIdentifier')
303  ->willReturn('/tmp/somefile');
304 
305  // Set the file mount
306  $this->fileUpload
307  ->expects(self::any())
308  ->method('getProperties')
309  ->willReturn(['saveToFileMount' => '']);
310 
311  // Expect the array to contain the /tmp upload directory
312  $this->extbasePropertyMappingConfiguration
313  ->expects(self::atLeastOnce())
314  ->method('setTypeConverterOptions')
315  ->willReturnCallback(function ($class, $config) {
316  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
318 
319  $this->assertSame('/tmp', $folder);
320  });
321 
322  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
323  }
324 
329  {
330  // Mime Type Validator
332  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
333 
334  // Resource Factory
336  $resourceFactory = $this->createMock(ResourceFactory::class);
338  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
339 
340  // Object Manager (in order to return mocked Resource Factory)
342  $objectManager = $this->getMockBuilder(ObjectManager::class)
343  ->setMethods(['get'])
344  ->disableOriginalConstructor()
345  ->getMock();
346 
347  $objectManager
348  ->expects(self::any())
349  ->method('get')
350  ->willReturnMap([
351  [UploadedFileReferenceConverter::class, $typeConverter],
352  [MimeTypeValidator::class, $mimeTypeValidator],
353  [ResourceFactory::class, $resourceFactory]
354  ]);
355 
356  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
357 
358  // Don't add any validators for now
359  $this->processingRule
360  ->expects(self::any())
361  ->method('getValidators')
362  ->willReturn(new \SplObjectStorage());
363 
364  $this->rootForm
365  ->expects(self::any())
366  ->method('getPersistenceIdentifier')
367  ->willReturn('');
368 
369  // Set the file mount
370  $this->fileUpload
371  ->expects(self::any())
372  ->method('getProperties')
373  ->willReturn(['saveToFileMount' => '']);
374 
375  // Expect the array to contain the /tmp upload directory
376  $this->extbasePropertyMappingConfiguration
377  ->expects(self::atLeastOnce())
378  ->method('setTypeConverterOptions')
379  ->willReturnCallback(function ($class, $config) {
380  $this->assertArrayNotHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
381  });
382 
383  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
384  }
385 
389  public function ‪afterBuildingFinishedCopiesValidators(): void
390  {
391  // Mime Type Validator
393  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
394 
395  // Some other Validator
396  $otherValidator = $this->getMockForAbstractClass(AbstractValidator::class);
397 
398  // Resource Factory
400  $resourceFactory = $this->createMock(ResourceFactory::class);
402  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
403 
404  // Object Manager (in order to return mocked Resource Factory)
406  $objectManager = $this->getMockBuilder(ObjectManager::class)
407  ->setMethods(['get'])
408  ->disableOriginalConstructor()
409  ->getMock();
410 
411  $objectManager
412  ->expects(self::any())
413  ->method('get')
414  ->willReturnMap([
415  [UploadedFileReferenceConverter::class, $typeConverter],
416  [MimeTypeValidator::class, $mimeTypeValidator],
417  [ResourceFactory::class, $resourceFactory]
418  ]);
419 
420  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
421 
422  // Don't add any validators for now
423  $validators = new \SplObjectStorage();
424  $validators->attach($otherValidator);
425 
426  $this->processingRule
427  ->expects(self::any())
428  ->method('getValidators')
429  ->willReturn($validators);
430 
431  // Expect the array to contain the /tmp upload directory
432  $this->extbasePropertyMappingConfiguration
433  ->expects(self::atLeastOnce())
434  ->method('setTypeConverterOptions')
435  ->willReturnCallback(function ($class, $config) use ($otherValidator) {
436  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
438 
439  self::assertContains($otherValidator, $validators);
440  });
441 
442  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
443  }
444 
449  {
450  // Mime Type Validator
452  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
453 
454  // Not Empty Validator
455  $notEmptyValidator = $this->getMockForAbstractClass(NotEmptyValidator::class);
456 
457  // Resource Factory
459  $resourceFactory = $this->createMock(ResourceFactory::class);
461  $typeConverter = $this->createMock(UploadedFileReferenceConverter::class);
462 
463  // Object Manager (in order to return mocked Resource Factory)
465  $objectManager = $this->getMockBuilder(ObjectManager::class)
466  ->setMethods(['get'])
467  ->disableOriginalConstructor()
468  ->getMock();
469 
470  $objectManager
471  ->expects(self::any())
472  ->method('get')
473  ->willReturnMap([
474  [UploadedFileReferenceConverter::class, $typeConverter],
475  [MimeTypeValidator::class, $mimeTypeValidator],
476  [ResourceFactory::class, $resourceFactory]
477  ]);
478 
479  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
480 
481  // Don't add any validators for now
482  $validators = new \SplObjectStorage();
483  $validators->attach($notEmptyValidator);
484 
485  $this->processingRule
486  ->expects(self::any())
487  ->method('getValidators')
488  ->willReturn($validators);
489 
490  // Expect the array to contain the /tmp upload directory
491  $this->extbasePropertyMappingConfiguration
492  ->expects(self::atLeastOnce())
493  ->method('setTypeConverterOptions')
494  ->willReturnCallback(function ($class, $config) use ($notEmptyValidator) {
495  $this->assertArrayHasKey(‪UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
497 
498  self::assertNotContains($notEmptyValidator, $validators);
499  });
500 
501  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
502  }
503 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:27
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedDoesNotCopyNotEmptyValidator
‪afterBuildingFinishedDoesNotCopyNotEmptyValidator()
Definition: PropertyMappingConfigurationTest.php:442
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter
Definition: UploadedFileReferenceConverter.php:48
‪TYPO3\CMS\Form\Mvc\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:38
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsFileReferenceConverter
‪afterBuildingFinishedAddsFileReferenceConverter()
Definition: PropertyMappingConfigurationTest.php:108
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$fileUpload
‪PHPUnit Framework MockObject MockObject FileUpload $fileUpload
Definition: PropertyMappingConfigurationTest.php:47
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$propertyMappingConfiguration
‪PropertyMappingConfiguration $propertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:43
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\setUp
‪setUp()
Definition: PropertyMappingConfigurationTest.php:53
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest
Definition: PropertyMappingConfigurationTest.php:38
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PropertyMappingConfigurationTest.php:41
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_UPLOAD_FOLDER
‪const CONFIGURATION_UPLOAD_FOLDER
Definition: UploadedFileReferenceConverter.php:53
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property
Definition: PropertyMappingConfigurationTest.php:18
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedCopiesValidators
‪afterBuildingFinishedCopiesValidators()
Definition: PropertyMappingConfigurationTest.php:383
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath
‪afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
Definition: PropertyMappingConfigurationTest.php:322
‪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:49
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedAddsMimeTypeConverter
‪afterBuildingFinishedAddsMimeTypeConverter()
Definition: PropertyMappingConfigurationTest.php:151
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically
‪afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
Definition: PropertyMappingConfigurationTest.php:258
‪TYPO3\CMS\Form\Domain\Model\FormDefinition
Definition: FormDefinition.php:223
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$processingRule
‪PHPUnit Framework MockObject MockObject ProcessingRule $processingRule
Definition: PropertyMappingConfigurationTest.php:51
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\$extbasePropertyMappingConfiguration
‪PHPUnit Framework MockObject MockObject ExtbasePropertyMappingConfiguration $extbasePropertyMappingConfiguration
Definition: PropertyMappingConfigurationTest.php:45
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter\CONFIGURATION_FILE_VALIDATORS
‪const CONFIGURATION_FILE_VALIDATORS
Definition: UploadedFileReferenceConverter.php:68
‪TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator
Definition: NotEmptyValidator.php:22
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\PropertyMappingConfigurationTest\afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists
‪afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
Definition: PropertyMappingConfigurationTest.php:199
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:28
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Form\Mvc\ProcessingRule
Definition: ProcessingRule.php:36