TYPO3 CMS  TYPO3_8-7
PropertyMappingConfigurationTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
15 
16 class PropertyMappingConfigurationTest extends UnitTestCase
17 {
20 
23 
25  protected $fileUpload;
26 
28  protected $rootForm;
29 
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  parent::tearDown();
92  }
93 
98  public function afterBuildingFinishedAddsFileReferenceConverter()
99  {
100  // Mime Type Validator
102  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
103 
104  // Resource Factory
106  $resourceFactory = $this->createMock(ResourceFactory::class);
107 
108  // Object Manager (in order to return mocked Resource Factory)
110  $objectManager = $this->getMockBuilder(ObjectManager::class)
111  ->setMethods(['get'])
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $objectManager
116  ->expects($this->any())
117  ->method('get')
118  ->willReturnMap([
119  [MimeTypeValidator::class, $mimeTypeValidator],
120  [ResourceFactory::class, $resourceFactory]
121  ]);
122 
123  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
124 
125  // No validators
126  $this->processingRule
127  ->expects($this->any())
128  ->method('getValidators')
129  ->willReturn(new \SplObjectStorage());
130 
131  // Mime Types not important
132  $this->fileUpload
133  ->expects($this->any())
134  ->method('getProperties')
135  ->willReturn(['allowedMimeTypes' => []]);
136 
137  // Check if the UploadFileReference is included
138  $this->extbasePropertyMappingConfiguration
139  ->expects($this->atLeastOnce())
140  ->method('setTypeConverterOptions')
141  ->with(UploadedFileReferenceConverter::class);
142 
143  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
144  }
145 
149  public function afterBuildingFinishedAddsMimeTypeConverter()
150  {
151  $mimeTypes = ['allowedMimeTypes' => ['text/plain', 'application/x-www-form-urlencoded']];
152 
153  // Create a MimeTypeValidator Mock
155  $mimeTypeValidator = $this->getMockBuilder(MimeTypeValidator::class)
156  ->setMethods(['__construct'])
157  ->disableOriginalConstructor()
158  ->getMock();
159 
160  // Object Manager to return the MimeTypeValidator
162  $objectManager = $this->getMockBuilder(ObjectManager::class)
163  ->setMethods(['get'])
164  ->disableOriginalConstructor()
165  ->getMock();
166 
167  $objectManager
168  ->expects($this->any())
169  ->method('get')
170  ->with(MimeTypeValidator::class, $mimeTypes)
171  ->willReturn($mimeTypeValidator);
172 
173  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
174 
175  // Don't add any validators for now
176  $this->processingRule
177  ->expects($this->any())
178  ->method('getValidators')
179  ->willReturn(new \SplObjectStorage());
180 
181  // Add some Mime types
182  $this->fileUpload
183  ->expects($this->any())
184  ->method('getProperties')
185  ->willReturn($mimeTypes);
186 
187  // Expect the array to contain the MimeTypeValidator
188  $this->extbasePropertyMappingConfiguration
189  ->expects($this->atLeastOnce())
190  ->method('setTypeConverterOptions')
191  ->willReturnCallback(function ($class, $config) {
192  $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS, $config);
194 
195  $this->assertInstanceOf(MimeTypeValidator::class, $validators[0]);
196  });
197 
198  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
199  }
200 
204  public function afterBuildingFinishedSetsUpStoragePathToPropertySaveToFileMountIfItExists()
205  {
206  // Mime Type Validator
208  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
209 
210  // Resource Factory
212  $resourceFactory = $this->createMock(ResourceFactory::class);
213 
214  // Object Manager (in order to return mocked Resource Factory)
216  $objectManager = $this->getMockBuilder(ObjectManager::class)
217  ->setMethods(['get'])
218  ->disableOriginalConstructor()
219  ->getMock();
220 
221  $objectManager
222  ->expects($this->any())
223  ->method('get')
224  ->willReturnMap([
225  [MimeTypeValidator::class, $mimeTypeValidator],
226  [ResourceFactory::class, $resourceFactory]
227  ]);
228 
229  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
230 
231  // Don't add any validators for now
232  $this->processingRule
233  ->expects($this->any())
234  ->method('getValidators')
235  ->willReturn(new \SplObjectStorage());
236 
237  // Set the file mount
238  $this->fileUpload
239  ->expects($this->any())
240  ->method('getProperties')
241  ->willReturn(['saveToFileMount' => '/tmp']);
242 
243  // Expect the array to contain the /tmp upload directory
244  $this->extbasePropertyMappingConfiguration
245  ->expects($this->atLeastOnce())
246  ->method('setTypeConverterOptions')
247  ->willReturnCallback(function ($class, $config) {
248  $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
250 
251  $this->assertSame('/tmp', $folder);
252  });
253 
254  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
255  }
256 
260  public function afterBuildingFinishedSetsUpStoragePathToToFormDefinitionPathIfSaveToFileMountIsNotDefinedAndFormWasNotAddedProgrammatically()
261  {
262  // Mime Type Validator
264  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
265 
266  // Resource Factory
268  $resourceFactory = $this->createMock(ResourceFactory::class);
269 
270  // Object Manager (in order to return mocked Resource Factory)
272  $objectManager = $this->getMockBuilder(ObjectManager::class)
273  ->setMethods(['get'])
274  ->disableOriginalConstructor()
275  ->getMock();
276 
277  $objectManager
278  ->expects($this->any())
279  ->method('get')
280  ->willReturnMap([
281  [MimeTypeValidator::class, $mimeTypeValidator],
282  [ResourceFactory::class, $resourceFactory]
283  ]);
284 
285  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
286 
287  // Don't add any validators for now
288  $this->processingRule
289  ->expects($this->any())
290  ->method('getValidators')
291  ->willReturn(new \SplObjectStorage());
292 
293  $this->rootForm
294  ->expects($this->any())
295  ->method('getPersistenceIdentifier')
296  ->willReturn('/tmp/somefile');
297 
298  // Set the file mount
299  $this->fileUpload
300  ->expects($this->any())
301  ->method('getProperties')
302  ->willReturn(['saveToFileMount' => '']);
303 
304  // Expect the array to contain the /tmp upload directory
305  $this->extbasePropertyMappingConfiguration
306  ->expects($this->atLeastOnce())
307  ->method('setTypeConverterOptions')
308  ->willReturnCallback(function ($class, $config) {
309  $this->assertArrayHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
311 
312  $this->assertSame('/tmp', $folder);
313  });
314 
315  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
316  }
317 
321  public function afterBuildingFinishedSetsStoragePathToUserUploadIfNeitherSaveToFileMountIsSetNorThereIsAFormDefinitionPath()
322  {
323  // Mime Type Validator
325  $mimeTypeValidator = $this->createMock(MimeTypeValidator::class);
326 
327  // Resource Factory
329  $resourceFactory = $this->createMock(ResourceFactory::class);
330 
331  // Object Manager (in order to return mocked Resource Factory)
333  $objectManager = $this->getMockBuilder(ObjectManager::class)
334  ->setMethods(['get'])
335  ->disableOriginalConstructor()
336  ->getMock();
337 
338  $objectManager
339  ->expects($this->any())
340  ->method('get')
341  ->willReturnMap([
342  [MimeTypeValidator::class, $mimeTypeValidator],
343  [ResourceFactory::class, $resourceFactory]
344  ]);
345 
346  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManager);
347 
348  // Don't add any validators for now
349  $this->processingRule
350  ->expects($this->any())
351  ->method('getValidators')
352  ->willReturn(new \SplObjectStorage());
353 
354  $this->rootForm
355  ->expects($this->any())
356  ->method('getPersistenceIdentifier')
357  ->willReturn('');
358 
359  // Set the file mount
360  $this->fileUpload
361  ->expects($this->any())
362  ->method('getProperties')
363  ->willReturn(['saveToFileMount' => '']);
364 
365  // Expect the array to contain the /tmp upload directory
366  $this->extbasePropertyMappingConfiguration
367  ->expects($this->atLeastOnce())
368  ->method('setTypeConverterOptions')
369  ->willReturnCallback(function ($class, $config) {
370  $this->assertArrayNotHasKey(UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER, $config);
371  });
372 
373  $this->propertyMappingConfiguration->afterBuildingFinished($this->fileUpload);
374  }
375 }
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)