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