TYPO3 CMS  TYPO3_8-7
FormManagerControllerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
28 
32 class FormManagerControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
33 {
34 
38  protected $singletonInstances = [];
39 
43  public function setUp()
44  {
45  $this->singletonInstances = GeneralUtility::getSingletonInstances();
46  }
47 
51  public function tearDown()
52  {
53  GeneralUtility::resetSingletonInstances($this->singletonInstances);
54  parent::tearDown();
55  }
56 
61  {
62  $mockController = $this->getAccessibleMock(FormManagerController::class, [
63  'dummy'
64  ], [], '', false);
65 
66  $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
67 
68  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
72 
73  $mockController->_set('formSettings', [
74  'persistenceManager' => [
75  'allowSaveToExtensionPaths' => true,
76  ],
77  ]);
78 
79  $folder1 = new Folder($mockStorage, '/user_upload/', 'user_upload');
80  $folder2 = new Folder($mockStorage, '/forms/', 'forms');
81 
82  $formPersistenceManagerProphecy->getAccessibleFormStorageFolders(Argument::cetera())->willReturn([
83  '1:/user_upload/' => $folder1,
84  '2:/forms/' => $folder2,
85  ]);
86 
87  $formPersistenceManagerProphecy->getAccessibleExtensionFolders(Argument::cetera())->willReturn([
88  'EXT:form/Resources/Forms/' => '/some/path/form/Resources/Forms/',
89  'EXT:form_additions/Resources/Forms/' => '/some/path/form_additions/Resources/Forms/',
90  ]);
91 
92  $expected = [
93  0 => [
94  'label' => 'user_upload',
95  'value' => '1:/user_upload/',
96  ],
97  1 => [
98  'label' => 'forms',
99  'value' => '2:/forms/',
100  ],
101  2 => [
102  'label' => 'EXT:form/Resources/Forms/',
103  'value' => 'EXT:form/Resources/Forms/',
104  ],
105  3 => [
106  'label' => 'EXT:form_additions/Resources/Forms/',
107  'value' => 'EXT:form_additions/Resources/Forms/',
108  ],
109  ];
110 
111  $this->assertSame($expected, $mockController->_call('getAccessibleFormStorageFolders'));
112  }
113 
118  {
119  $objectMangerProphecy = $this->prophesize(ObjectManager::class);
120  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
121 
122  $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
123  'translateValuesRecursive'
124  ], [], '', false);
125 
126  $mockTranslationService
127  ->expects($this->any())
128  ->method('translateValuesRecursive')
129  ->willReturnArgument(0);
130 
131  $objectMangerProphecy
132  ->get(TranslationService::class)
133  ->willReturn($mockTranslationService);
134 
135  $mockController = $this->getAccessibleMock(FormManagerController::class, [
136  'getAccessibleFormStorageFolders'
137  ], [], '', false);
138 
139  $mockUriBuilder = $this->createMock(UriBuilder::class);
140  $mockControllerContext = $this->createMock(ControllerContext::class);
141  $mockControllerContext
142  ->expects($this->any())
143  ->method('getUriBuilder')
144  ->will($this->returnValue($mockUriBuilder));
145 
146  $mockController->_set('controllerContext', $mockControllerContext);
147 
148  $mockController->_set('formSettings', [
149  'formManager' => [
150  'selectablePrototypesConfiguration' => [],
151  ],
152  ]);
153 
154  $mockUriBuilder->expects($this->any())->method('uriFor')->willReturn(
155  '/typo3/index.php?some=param'
156  );
157 
158  $mockController
159  ->expects($this->any())
160  ->method('getAccessibleFormStorageFolders')
161  ->willReturn([
162  0 => [
163  'label' => 'user_upload',
164  'value' => '1:/user_upload/',
165  ],
166  ]);
167 
168  $expected = [
169  'selectablePrototypesConfiguration' => [],
170  'accessibleFormStorageFolders' => [
171  0 => [
172  'label' => 'user_upload',
173  'value' => '1:/user_upload/',
174  ],
175  ],
176  'endpoints' => [
177  'create' => '/typo3/index.php?some=param',
178  'duplicate' => '/typo3/index.php?some=param',
179  'delete' => '/typo3/index.php?some=param',
180  'references' => '/typo3/index.php?some=param',
181  ],
182  ];
183 
184  $this->assertSame(json_encode($expected), $mockController->_call('getFormManagerAppInitialData'));
185  }
186 
191  {
192  $mockController = $this->getAccessibleMock(FormManagerController::class, [
193  'dummy'
194  ], [], '', false);
195 
196  $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
197  $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
198 
199  $databaseService = $this->prophesize(DatabaseService::class);
200  $mockController->_set('databaseService', $databaseService->reveal());
201 
202  $formPersistenceManagerProphecy->listForms(Argument::cetera())->willReturn([
203  0 => [
204  'identifier' => 'ext-form-identifier',
205  'name' => 'some name',
206  'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
207  'readOnly' => false,
208  'removable' => true,
209  'location' => 'storage',
210  'duplicateIdentifier' => false,
211  ],
212  ]);
213 
214  $databaseService->getAllReferencesForFileUid(Argument::cetera())->willReturn([
215  0 => 0,
216  ]);
217 
218  $databaseService->getAllReferencesForPersistenceIdentifier(Argument::cetera())->willReturn([
219  '1:/user_uploads/someFormName.yaml' => 2,
220  ]);
221 
222  $expected = [
223  0 => [
224  'identifier' => 'ext-form-identifier',
225  'name' => 'some name',
226  'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
227  'readOnly' => false,
228  'removable' => true,
229  'location' => 'storage',
230  'duplicateIdentifier' => false,
231  'referenceCount' => 2,
232  ],
233  ];
234 
235  $this->assertSame($expected, $mockController->_call('getAvailableFormDefinitions'));
236  }
237 
242  {
243  $this->expectException(\InvalidArgumentException::class);
244  $this->expectExceptionCode(1477071939);
245 
246  $mockController = $this->getAccessibleMock(FormManagerController::class, [
247  'dummy'
248  ], [], '', false);
249 
250  $mockController->_call('getProcessedReferencesRows', '');
251  }
252 
257  {
258  $mockController = $this->getAccessibleMock(FormManagerController::class, [
259  'getModuleUrl',
260  'getRecord',
261  'getRecordTitle',
262  ], [], '', false);
263 
264  $databaseService = $this->prophesize(DatabaseService::class);
265  $mockController->_set('databaseService', $databaseService->reveal());
266 
267  $databaseService->getReferencesByPersistenceIdentifier(Argument::cetera())->willReturn([
268  0 => [
269  'tablename' => 'tt_content',
270  'recuid' => -1,
271  ],
272  ]);
273 
274  $mockController
275  ->expects($this->any())
276  ->method('getModuleUrl')
277  ->willReturn('/typo3/index.php?some=param');
278 
279  $mockController
280  ->expects($this->any())
281  ->method('getRecord')
282  ->willReturn([ 'uid' => 1, 'pid' => 0 ]);
283 
284  $mockController
285  ->expects($this->any())
286  ->method('getRecordTitle')
287  ->willReturn('record title');
288 
289  $expected = [
290  0 => [
291  'recordPageTitle' => 'record title',
292  'recordTitle' => 'record title',
293  'recordIcon' =>
294 '<span class="t3js-icon icon icon-size-small icon-state-default icon-default-not-found" data-identifier="default-not-found">
295  <span class="icon-markup">
296 <img src="typo3/sysext/core/Resources/Public/Icons/T3Icons/default/default-not-found.svg" width="16" height="16" />
297  </span>
298 
299 </span>',
300  'recordUid' => -1,
301  'recordEditUrl' => '/typo3/index.php?some=param',
302  ],
303  ];
304 
305  $this->assertSame($expected, $mockController->_call('getProcessedReferencesRows', 'fake'));
306  }
307 
312  {
313  $mockController = $this->getAccessibleMock(FormManagerController::class, [
314  'dummy'
315  ], [], '', false);
316 
317  $mockController->_set('formSettings', [
318  'formManager' => [
319  'selectablePrototypesConfiguration' => [
320  0 => [
321  'identifier' => 'standard',
322  'label' => 'some label',
323  'newFormTemplates' => [
324  0 => [
325  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
326  'label' => 'some label',
327  ],
328  1 => [
329  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
330  'label' => 'some other label',
331  ],
332  ],
333  ],
334  ],
335  ],
336  ]);
337 
338  $this->assertTrue($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
339  }
340 
345  {
346  $mockController = $this->getAccessibleMock(FormManagerController::class, [
347  'dummy'
348  ], [], '', false);
349 
350  $mockController->_set('formSettings', [
351  'formManager' => [
352  'selectablePrototypesConfiguration' => [
353  0 => [
354  'identifier' => 'standard',
355  'label' => 'some label',
356  'newFormTemplates' => [
357  0 => [
358  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
359  'label' => 'some label',
360  ],
361  1 => [
362  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
363  'label' => 'some other label',
364  ],
365  ],
366  ],
367  ],
368  ],
369  ]);
370 
371  $this->assertFalse($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/NonExistingForm.yaml'));
372  }
373 
378  {
379  $mockController = $this->getAccessibleMock(FormManagerController::class, [
380  'dummy'
381  ], [], '', false);
382 
383  $mockController->_set('formSettings', [
384  'formManager' => [
385  'selectablePrototypesConfiguration' => [
386  0 => [
387  'identifier' => 'standard',
388  'label' => 'some label',
389  'newFormTemplates' => [
390  0 => [
391  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
392  'label' => 'some label',
393  ],
394  1 => [
395  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
396  'label' => 'some other label',
397  ],
398  ],
399  ],
400  1 => [
401  'identifier' => 'other',
402  'label' => 'some label',
403  'newFormTemplates' => [
404  0 => [
405  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
406  'label' => 'some label',
407  ],
408  ],
409  ],
410  ],
411  ],
412  ]);
413 
414  $this->assertFalse($mockController->_call('isValidTemplatePath', 'other', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
415  }
416 
421  {
422  $mockController = $this->getAccessibleMock(FormManagerController::class, [
423  'dummy'
424  ], [], '', false);
425 
426  $input = 'test form';
427  $expected = 'testform';
428  $this->assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
429  }
430 
435  {
436  $mockController = $this->getAccessibleMock(FormManagerController::class, [
437  'dummy'
438  ], [], '', false);
439 
440  $input = 'test form รค#!_-01';
441  $expected = 'testform_-01';
442  $this->assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
443  }
444 }
static setSingletonInstance($className, SingletonInterface $instance)
static resetSingletonInstances(array $newSingletonInstances)