[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\Form\Domain\Model\FormDefinition;
10 use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
11 use TYPO3\CMS\Form\Mvc\ProcessingRule;
12 use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
13 use TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator;
14 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
15
16 class PropertyMappingConfigurationTest extends UnitTestCase
17 {
18 /** @var PropertyMappingConfiguration */
19 protected $propertyMappingConfiguration;
20
21 /** @var \PHPUnit_Framework_MockObject_MockObject|ExtbasePropertyMappingConfiguration */
22 protected $extbasePropertyMappingConfiguration;
23
24 /** @var \PHPUnit_Framework_MockObject_MockObject|FileUpload */
25 protected $fileUpload;
26
27 /** @var \PHPUnit_Framework_MockObject_MockObject|FormDefinition */
28 protected $rootForm;
29
30 /** @var \PHPUnit_Framework_MockObject_MockObject|ProcessingRule */
31 protected $processingRule;
32
33 protected $singletons = [];
34
35 public function setUp()
36 {
37 $this->singletons = GeneralUtility::getSingletonInstances();
38
39 // Property Mapping Configuration
40 $this->extbasePropertyMappingConfiguration = $this->getMockBuilder(ExtbasePropertyMappingConfiguration::class)
41 ->setMethods(['setTypeConverterOptions'])
42 ->disableOriginalConstructor()
43 ->getMock();
44
45 // Processing Rules
46 $this->processingRule = $this->getMockBuilder(ProcessingRule::class)
47 ->setMethods(['getValidators', 'removeValidator', 'getPropertyMappingConfiguration'])
48 ->disableOriginalConstructor()
49 ->getMock();
50
51 $this->processingRule
52 ->expects($this->any())
53 ->method('getPropertyMappingConfiguration')
54 ->willReturn($this->extbasePropertyMappingConfiguration);
55
56 // Root Form
57 $this->rootForm = $this->getMockBuilder(FormDefinition::class)
58 ->setMethods(['getProcessingRule', 'getPersistenceIdentifier', 'getIdentifier'])
59 ->disableOriginalConstructor()
60 ->getMock();
61
62 $this->rootForm
63 ->expects($this->any())
64 ->method('getProcessingRule')
65 ->willReturn($this->processingRule);
66
67 // File Upload
68 $this->fileUpload = $this->getMockBuilder(FileUpload::class)
69 ->setMethods(['getProperties', 'getRootForm', 'getIdentifier'])
70 ->disableOriginalConstructor()
71 ->getMock();
72
73 $this->fileUpload
74 ->expects($this->any())
75 ->method('getRootForm')
76 ->willReturn($this->rootForm);
77
78 $this->fileUpload
79 ->expects($this->any())
80 ->method('getIdentifier')
81 ->willReturn('foobar');
82
83 // Property Mapping Configuration
84 $this->propertyMappingConfiguration = new PropertyMappingConfiguration();
85 }
86
87 public function tearDown()
88 {
89 // Remove all singleton instances
90 GeneralUtility::resetSingletonInstances($this->singletons);
91 }
92
93 /**
94 * A bare minimum test that checks if the function maybe potentially works.
95 * @test
96 */
97 public function afterBuildingFinishedAddsFileReferenceConverter()
98 {
99 // Mime Type Validator
100 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
101 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
102
103 // Resource Factory
104 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
105 $resourceFactory = $this->createMock(ResourceFactory::class);
106
107 // Object Manager (in order to return mocked Resource Factory)
108 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
109 $objectManager = $this->getMockBuilder(ObjectManager::class)
110 ->setMethods(['get'])
111 ->disableOriginalConstructor()
112 ->getMock();
113
114 $objectManager
115 ->expects($this->any())
116 ->method('get')
117 ->willReturnMap([
118 [MimeTypeValidator::class, $mimeTypeValidator],
119 [ResourceFactory::class, $resourceFactory]
120 ]);
121
122 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
123
124 // No validators
125 $this->processingRule
126 ->expects($this->any())
127 ->method('getValidators')
128 ->willReturn(new \SplObjectStorage());
129
130 // Mime Types not important
131 $this->fileUpload
132 ->expects($this->any())
133 ->method('getProperties')
134 ->willReturn(['allowedMimeTypes' => []]);
135
136 // Check if the UploadFileReference is included
137 $this->extbasePropertyMappingConfiguration
138 ->expects($this->atLeastOnce())
139 ->method('setTypeConverterOptions')
140 ->with(UploadedFileReferenceConverter::class);
141
142 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
143 }
144
145 /**
146 * @test
147 */
148 public function afterBuildingFinishedAddsMimeTypeConverter()
149 {
150 $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
151
152 // Create a MimeTypeValidator Mock
153 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
154 $mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
155 ->setMethods(['__construct'])
156 ->disableOriginalConstructor()
157 ->getMock();
158
159 // Object Manager to return the MimeTypeValidator
160 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
161 $objectManager = $this->getMockBuilder(ObjectManager::class)
162 ->setMethods(['get'])
163 ->disableOriginalConstructor()
164 ->getMock();
165
166 $objectManager
167 ->expects($this->any())
168 ->method('get')
169 ->with(MimeTypeValidator::class, $mimeTypes)
170 ->willReturn($mimeTypeValidator);
171
172 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
173
174 // Don't add any validators for now
175 $this->processingRule
176 ->expects($this->any())
177 ->method('getValidators')
178 ->willReturn(new \SplObjectStorage());
179
180 // Add some Mime types
181 $this->fileUpload
182 ->expects($this->any())
183 ->method('getProperties')
184 ->willReturn($mimeTypes);
185
186 // Expect the array to contain the MimeTypeValidator
187 $this->extbasePropertyMappingConfiguration
188 ->expects($this->atLeastOnce())
189 ->method('setTypeConverterOptions')
190 ->willReturnCallback(function ($class, $config) {
191 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
192 $validators = $config[UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS];
193
194 $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
195 });
196
197 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
198 }
199
200 /**
201 * @test
202 */
203 public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
204 {
205 // Mime Type Validator
206 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
207 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
208
209 // Resource Factory
210 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
211 $resourceFactory = $this->createMock(ResourceFactory::class);
212
213 // Object Manager (in order to return mocked Resource Factory)
214 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
215 $objectManager = $this->getMockBuilder(ObjectManager::class)
216 ->setMethods(['get'])
217 ->disableOriginalConstructor()
218 ->getMock();
219
220 $objectManager
221 ->expects($this->any())
222 ->method('get')
223 ->willReturnMap([
224 [MimeTypeValidator::class, $mimeTypeValidator],
225 [ResourceFactory::class, $resourceFactory]
226 ]);
227
228 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
229
230 // Don't add any validators for now
231 $this->processingRule
232 ->expects($this->any())
233 ->method('getValidators')
234 ->willReturn(new \SplObjectStorage());
235
236 // Set the file mount
237 $this->fileUpload
238 ->expects($this->any())
239 ->method('getProperties')
240 ->willReturn(['saveToFileMount' => '/tmp']);
241
242 // Expect the array to contain the /tmp upload directory
243 $this->extbasePropertyMappingConfiguration
244 ->expects($this->atLeastOnce())
245 ->method('setTypeConverterOptions')
246 ->willReturnCallback(function ($class, $config) {
247 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
248 $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
249
250 $this->assertSame('/tmp', $folder);
251 });
252
253 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
254 }
255
256 /**
257 * @test
258 */
259 public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
260 {
261 // Mime Type Validator
262 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
263 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
264
265 // Resource Factory
266 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
267 $resourceFactory = $this->createMock(ResourceFactory::class);
268
269 // Object Manager (in order to return mocked Resource Factory)
270 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
271 $objectManager = $this->getMockBuilder(ObjectManager::class)
272 ->setMethods(['get'])
273 ->disableOriginalConstructor()
274 ->getMock();
275
276 $objectManager
277 ->expects($this->any())
278 ->method('get')
279 ->willReturnMap([
280 [MimeTypeValidator::class, $mimeTypeValidator],
281 [ResourceFactory::class, $resourceFactory]
282 ]);
283
284 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
285
286 // Don't add any validators for now
287 $this->processingRule
288 ->expects($this->any())
289 ->method('getValidators')
290 ->willReturn(new \SplObjectStorage());
291
292 $this->rootForm
293 ->expects($this->any())
294 ->method('getPersistenceIdentifier')
295 ->willReturn('/tmp/somefile');
296
297 // Set the file mount
298 $this->fileUpload
299 ->expects($this->any())
300 ->method('getProperties')
301 ->willReturn(['saveToFileMount' => '']);
302
303 // Expect the array to contain the /tmp upload directory
304 $this->extbasePropertyMappingConfiguration
305 ->expects($this->atLeastOnce())
306 ->method('setTypeConverterOptions')
307 ->willReturnCallback(function ($class, $config) {
308 $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
309 $folder = $config[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER];
310
311 $this->assertSame('/tmp', $folder);
312 });
313
314 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
315 }
316
317 /**
318 * @test
319 */
320 public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
321 {
322 // Mime Type Validator
323 /** @var \PHPUnit_Framework_MockObject_MockObject|MimeTypeValidator $mimeTypeValidator */
324 $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
325
326 // Resource Factory
327 /** @var \PHPUnit_Framework_MockObject_MockObject|ResourceFactory $resourceFactory */
328 $resourceFactory = $this->createMock(ResourceFactory::class);
329
330 // Object Manager (in order to return mocked Resource Factory)
331 /** @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager $objectManager */
332 $objectManager = $this->getMockBuilder(ObjectManager::class)
333 ->setMethods(['get'])
334 ->disableOriginalConstructor()
335 ->getMock();
336
337 $objectManager
338 ->expects($this->any())
339 ->method('get')
340 ->willReturnMap([
341 [MimeTypeValidator::class, $mimeTypeValidator],
342 [ResourceFactory::class, $resourceFactory]
343 ]);
344
345 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
346
347 // Don't add any validators for now
348 $this->processingRule
349 ->expects($this->any())
350 ->method('getValidators')
351 ->willReturn(new \SplObjectStorage());
352
353 $this->rootForm
354 ->expects($this->any())
355 ->method('getPersistenceIdentifier')
356 ->willReturn('');
357
358 // Set the file mount
359 $this->fileUpload
360 ->expects($this->any())
361 ->method('getProperties')
362 ->willReturn(['saveToFileMount' => '']);
363
364 // Expect the array to contain the /tmp upload directory
365 $this->extbasePropertyMappingConfiguration
366 ->expects($this->atLeastOnce())
367 ->method('setTypeConverterOptions')
368 ->willReturnCallback(function ($class, $config) {
369 $this->assertArrayNotHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
370 });
371
372 $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
373 }
374 }