[TASK] EXT:form - Tests for PropertyMappingConfiguration
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Property / PropertyMappingConfigurationTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Property;
4
5 use TYPO3\CMS\Core\Resource\ResourceFactory;
6 use TYPO3\CMS\Core\Utility\GeneralUtility;
7 use TYPO3\CMS\Extbase\Object\ObjectManager;
8 use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration as ExtbasePropertyMappingConfiguration;
9 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
10 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
11 use TYPO3\CMS\Form\Domain\Model\FormDefinition;
12 use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
13 use TYPO3\CMS\Form\Mvc\ProcessingRule;
14 use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
15 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
16 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
17
18 class PropertyMappingConfigurationTest extends UnitTestCase
19 {
20 /** @var PropertyMappingConfiguration */
21 protected $propertyMappingConfiguration;
22
23 /** @var \PHPUnit_Framework_MockObject_MockObject|ExtbasePropertyMappingConfiguration */
24 protected $extbasePropertyMappingConfiguration;
25
26 /** @var \PHPUnit_Framework_MockObject_MockObject|FileUpload */
27 protected $fileUpload;
28
29 /** @var \PHPUnit_Framework_MockObject_MockObject|FormDefinition */
30 protected $rootForm;
31
32 /** @var \PHPUnit_Framework_MockObject_MockObject|ProcessingRule */
33 protected $processingRule;
34
35 protected $singletons = [];
36
37 public function setUp()
38 {
39 $this->singletons = GeneralUtility::getSingletonInstances();
40
41 // Property Mapping Configuration
42 $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
43 ->setMethods(['setTypeConverterOptions'])
44 ->disableOriginalConstructor()
45 ->getMock();
46
47 // Processing Rules
48 $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
49 ->setMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
50 ->disableOriginalConstructor()
51 ->getMock();
52
53 $this->processingRule
54 ->expects($this->any())
55 ->method('getPropertyMappingConfiguration')
56 ->willReturn($this->extbasePropertyMappingConfiguration);
57
58 // Root Form
59 $this->rootForm = $this->getMockBuilder(FormDefinition::class)
60 ->setMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
61 ->disableOriginalConstructor()
62 ->getMock();
63
64 $this->rootForm
65 ->expects($this->any())
66 ->method('getProcessingRule')
67 ->willReturn($this->processingRule);
68
69 // File Upload
70 $this->fileUpload = $this->getMockBuilder(FileUpload::class)
71 ->setMethods(['getProperties', 'getRootForm', 'getIdentifier'])
72 ->disableOriginalConstructor()
73 ->getMock();
74
75 $this->fileUpload
76 ->expects($this->any())
77 ->method('getRootForm')
78 ->willReturn($this->rootForm);
79
80 $this->fileUpload
81 ->expects($this->any())
82 ->method('getIdentifier')
83 ->willReturn('foobar');
84
85 // Property Mapping Configuration
86 $this->propertyMappingConfiguration = new PropertyMappingConfiguration();
87 }
88
89 public function tearDown()
90 {
91 // Remove all singleton instances
92 GeneralUtility::resetSingletonInstances($this->singletons);
93 }
94
95 /**
96 * A bare minimum test that checks if the function maybe potentially works.
97 * @test
98 */
99 public function afterBuildingFinishedAddsFileReferenceConverter()
100 {
101 // Mime Type Validator
102 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
103 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
104
105 // Resource Factory
106 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
107 $resourceFactory = $this->createMock(ResourceFactory::class);
108
109 // Object Manager (in order to return mocked Resource Factory)
110 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
111 $objectManager = $this->getMockBuilder(ObjectManager::class)
112 ->setMethods(['get'])
113 ->disableOriginalConstructor()
114 ->getMock();
115
116 $objectManager
117 ->expects($this->any())
118 ->method('get')
119 ->willReturnMap([
120 [MimeTypeValidator::class, $mimeTypeValidator],
121 [ResourceFactory::class, $resourceFactory]
122 ]);
123
124 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
125
126 // No validators
127 $this->processingRule
128 ->expects($this->any())
129 ->method('getValidators')
130 ->willReturn(new \SplObjectStorage());
131
132 // Mime Types not important
133 $this->fileUpload
134 ->expects($this->any())
135 ->method('getProperties')
136 ->willReturn(['allowedMimeTypes' => []]);
137
138 // Check if the UploadFileReference is included
139 $this->extbasePropertyMappingConfiguration
140 ->expects($this->atLeastOnce())
141 ->method('setTypeConverterOptions')
142 ->with(UploadedFileReferenceConverter::class);
143
144 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
145 }
146
147 /**
148 * @test
149 */
150 public function afterBuildingFinishedAddsMimeTypeConverter()
151 {
152 $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
153
154 // Create a MimeTypeValidator Mock
155 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
156 $mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
157 ->setMethods(['__construct'])
158 ->disableOriginalConstructor()
159 ->getMock();
160
161 // Object Manager to return the MimeTypeValidator
162 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
163 $objectManager = $this->getMockBuilder(ObjectManager::class)
164 ->setMethods(['get'])
165 ->disableOriginalConstructor()
166 ->getMock();
167
168 $objectManager
169 ->expects($this->any())
170 ->method('get')
171 ->with(MimeTypeValidator::class, $mimeTypes)
172 ->willReturn($mimeTypeValidator);
173
174 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
175
176 // Don't add any validators for now
177 $this->processingRule
178 ->expects($this->any())
179 ->method('getValidators')
180 ->willReturn(new \SplObjectStorage());
181
182 // Add some Mime types
183 $this->fileUpload
184 ->expects($this->any())
185 ->method('getProperties')
186 ->willReturn($mimeTypes);
187
188 // Expect the array to contain the MimeTypeValidator
189 $this->extbasePropertyMappingConfiguration
190 ->expects($this->atLeastOnce())
191 ->method('setTypeConverterOptions')
192 ->willReturnCallback(function ($class, $config) {
193 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
194 $validators = $config[UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS];
195
196 $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
197 });
198
199 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
200 }
201
202 /**
203 * @test
204 */
205 public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
206 {
207 // Mime Type Validator
208 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
209 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
210
211 // Resource Factory
212 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
213 $resourceFactory = $this->createMock(ResourceFactory::class);
214
215 // Object Manager (in order to return mocked Resource Factory)
216 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
217 $objectManager = $this->getMockBuilder(ObjectManager::class)
218 ->setMethods(['get'])
219 ->disableOriginalConstructor()
220 ->getMock();
221
222 $objectManager
223 ->expects($this->any())
224 ->method('get')
225 ->willReturnMap([
226 [MimeTypeValidator::class, $mimeTypeValidator],
227 [ResourceFactory::class, $resourceFactory]
228 ]);
229
230 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
231
232 // Don't add any validators for now
233 $this->processingRule
234 ->expects($this->any())
235 ->method('getValidators')
236 ->willReturn(new \SplObjectStorage());
237
238 // Set the file mount
239 $this->fileUpload
240 ->expects($this->any())
241 ->method('getProperties')
242 ->willReturn(['saveToFileMount' => '/tmp']);
243
244 // Expect the array to contain the /tmp upload directory
245 $this->extbasePropertyMappingConfiguration
246 ->expects($this->atLeastOnce())
247 ->method('setTypeConverterOptions')
248 ->willReturnCallback(function ($class, $config) {
249 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
250 $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
251
252 $this->assertSame('/tmp', $folder);
253 });
254
255 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
256 }
257
258 /**
259 * @test
260 */
261 public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
262 {
263 // Mime Type Validator
264 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
265 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
266
267 // Resource Factory
268 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
269 $resourceFactory = $this->createMock(ResourceFactory::class);
270
271 // Object Manager (in order to return mocked Resource Factory)
272 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
273 $objectManager = $this->getMockBuilder(ObjectManager::class)
274 ->setMethods(['get'])
275 ->disableOriginalConstructor()
276 ->getMock();
277
278 $objectManager
279 ->expects($this->any())
280 ->method('get')
281 ->willReturnMap([
282 [MimeTypeValidator::class, $mimeTypeValidator],
283 [ResourceFactory::class, $resourceFactory]
284 ]);
285
286 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
287
288 // Don't add any validators for now
289 $this->processingRule
290 ->expects($this->any())
291 ->method('getValidators')
292 ->willReturn(new \SplObjectStorage());
293
294 $this->rootForm
295 ->expects($this->any())
296 ->method('getPersistenceIdentifier')
297 ->willReturn('/tmp/somefile');
298
299 // Set the file mount
300 $this->fileUpload
301 ->expects($this->any())
302 ->method('getProperties')
303 ->willReturn(['saveToFileMount' => '']);
304
305 // Expect the array to contain the /tmp upload directory
306 $this->extbasePropertyMappingConfiguration
307 ->expects($this->atLeastOnce())
308 ->method('setTypeConverterOptions')
309 ->willReturnCallback(function ($class, $config) {
310 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
311 $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
312
313 $this->assertSame('/tmp', $folder);
314 });
315
316 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
317 }
318
319 /**
320 * @test
321 */
322 public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
323 {
324 // Mime Type Validator
325 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
326 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
327
328 // Resource Factory
329 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
330 $resourceFactory = $this->createMock(ResourceFactory::class);
331
332 // Object Manager (in order to return mocked Resource Factory)
333 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
334 $objectManager = $this->getMockBuilder(ObjectManager::class)
335 ->setMethods(['get'])
336 ->disableOriginalConstructor()
337 ->getMock();
338
339 $objectManager
340 ->expects($this->any())
341 ->method('get')
342 ->willReturnMap([
343 [MimeTypeValidator::class, $mimeTypeValidator],
344 [ResourceFactory::class, $resourceFactory]
345 ]);
346
347 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
348
349 // Don't add any validators for now
350 $this->processingRule
351 ->expects($this->any())
352 ->method('getValidators')
353 ->willReturn(new \SplObjectStorage());
354
355 $this->rootForm
356 ->expects($this->any())
357 ->method('getPersistenceIdentifier')
358 ->willReturn('');
359
360 // Set the file mount
361 $this->fileUpload
362 ->expects($this->any())
363 ->method('getProperties')
364 ->willReturn(['saveToFileMount' => '']);
365
366 // Expect the array to contain the /tmp upload directory
367 $this->extbasePropertyMappingConfiguration
368 ->expects($this->atLeastOnce())
369 ->method('setTypeConverterOptions')
370 ->willReturnCallback(function ($class, $config) {
371 $this->assertArrayNotHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
372 });
373
374 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
375 }
376
377 /**
378 * @test
379 */
380 public function afterBuildingFinishedCopiesValidators()
381 {
382 // Mime Type Validator
383 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
384 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
385
386 // Some other Validator
387 $otherValidator = $this->getMockForAbstractClass(AbstractValidator::class);
388
389 // Resource Factory
390 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
391 $resourceFactory = $this->createMock(ResourceFactory::class);
392
393 // Object Manager (in order to return mocked Resource Factory)
394 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
395 $objectManager = $this->getMockBuilder(ObjectManager::class)
396 ->setMethods(['get'])
397 ->disableOriginalConstructor()
398 ->getMock();
399
400 $objectManager
401 ->expects($this->any())
402 ->method('get')
403 ->willReturnMap([
404 [MimeTypeValidator::class, $mimeTypeValidator],
405 [ResourceFactory::class, $resourceFactory]
406 ]);
407
408 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
409
410 // Don't add any validators for now
411 $validators = new \SplObjectStorage();
412 $validators->attach($otherValidator);
413
414 $this->processingRule
415 ->expects($this->any())
416 ->method('getValidators')
417 ->willReturn($validators);
418
419 // Expect the array to contain the /tmp upload directory
420 $this->extbasePropertyMappingConfiguration
421 ->expects($this->atLeastOnce())
422 ->method('setTypeConverterOptions')
423 ->willReturnCallback(function ($class, $config) use ($otherValidator) {
424 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
425 $validators = $config[UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS];
426
427 self::assertContains($otherValidator, $validators);
428 });
429
430 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
431 }
432
433 /**
434 * @test
435 */
436 public function afterBuildingFinishedDoesNotCopyNotEmptyValidator()
437 {
438 // Mime Type Validator
439 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
440 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
441
442 // Not Empty Validator
443 $notEmptyValidator = $this->getMockForAbstractClass(NotEmptyValidator::class);
444
445 // Resource Factory
446 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
447 $resourceFactory = $this->createMock(ResourceFactory::class);
448
449 // Object Manager (in order to return mocked Resource Factory)
450 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
451 $objectManager = $this->getMockBuilder(ObjectManager::class)
452 ->setMethods(['get'])
453 ->disableOriginalConstructor()
454 ->getMock();
455
456 $objectManager
457 ->expects($this->any())
458 ->method('get')
459 ->willReturnMap([
460 [MimeTypeValidator::class, $mimeTypeValidator],
461 [ResourceFactory::class, $resourceFactory]
462 ]);
463
464 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
465
466 // Don't add any validators for now
467 $validators = new \SplObjectStorage();
468 $validators->attach($notEmptyValidator);
469
470 $this->processingRule
471 ->expects($this->any())
472 ->method('getValidators')
473 ->willReturn($validators);
474
475 // Expect the array to contain the /tmp upload directory
476 $this->extbasePropertyMappingConfiguration
477 ->expects($this->atLeastOnce())
478 ->method('setTypeConverterOptions')
479 ->willReturnCallback(function ($class, $config) use ($notEmptyValidator) {
480 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
481 $validators = $config[UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS];
482
483 self::assertNotContains($notEmptyValidator, $validators);
484 });
485
486 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
487 }
488 }