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