‪TYPO3CMS  10.4
FormManagerControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Prophecy\Argument;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
38 class ‪FormManagerControllerTest extends UnitTestCase
39 {
43  protected ‪$resetSingletonInstances = true;
44 
49  {
50  $mockController = $this->getAccessibleMock(FormManagerController::class, [
51  'dummy'
52  ], [], '', false);
53 
54  $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
55 
56  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59  $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
60 
61  $mockController->_set('formSettings', [
62  'persistenceManager' => [
63  'allowSaveToExtensionPaths' => true,
64  ],
65  ]);
66 
67  $folder1 = new ‪Folder($mockStorage, '/user_upload/', 'user_upload');
68  $folder2 = new ‪Folder($mockStorage, '/forms/', 'forms');
69 
70  $formPersistenceManagerProphecy->getAccessibleFormStorageFolders(Argument::cetera())->willReturn([
71  '1:/user_upload/' => $folder1,
72  '2:/forms/' => $folder2,
73  ]);
74 
75  $formPersistenceManagerProphecy->getAccessibleExtensionFolders(Argument::cetera())->willReturn([
76  'EXT:form/Resources/Forms/' => '/some/path/form/Resources/Forms/',
77  'EXT:form_additions/Resources/Forms/' => '/some/path/form_additions/Resources/Forms/',
78  ]);
79 
80  $expected = [
81  0 => [
82  'label' => 'user_upload',
83  'value' => '1:/user_upload/',
84  ],
85  1 => [
86  'label' => 'forms',
87  'value' => '2:/forms/',
88  ],
89  2 => [
90  'label' => 'EXT:form/Resources/Forms/',
91  'value' => 'EXT:form/Resources/Forms/',
92  ],
93  3 => [
94  'label' => 'EXT:form_additions/Resources/Forms/',
95  'value' => 'EXT:form_additions/Resources/Forms/',
96  ],
97  ];
98 
99  self::assertSame($expected, $mockController->_call('getAccessibleFormStorageFolders'));
100  }
101 
106  {
107  $objectManagerProphecy = $this->prophesize(ObjectManager::class);
108  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
109 
110  $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
111  'translateValuesRecursive'
112  ], [], '', false);
113 
114  $mockTranslationService
115  ->expects(self::any())
116  ->method('translateValuesRecursive')
117  ->willReturnArgument(0);
118 
119  $objectManagerProphecy
120  ->get(TranslationService::class)
121  ->willReturn($mockTranslationService);
122 
123  $mockController = $this->getAccessibleMock(FormManagerController::class, [
124  'getAccessibleFormStorageFolders'
125  ], [], '', false);
126 
127  $mockUriBuilder = $this->createMock(UriBuilder::class);
128  $mockControllerContext = $this->createMock(ControllerContext::class);
129  $mockControllerContext
130  ->expects(self::any())
131  ->method('getUriBuilder')
132  ->willReturn($mockUriBuilder);
133 
134  $mockController->_set('controllerContext', $mockControllerContext);
135 
136  $mockController->_set('formSettings', [
137  'formManager' => [
138  'selectablePrototypesConfiguration' => [],
139  ],
140  ]);
141 
142  $mockUriBuilder->expects(self::any())->method('uriFor')->willReturn(
143  '/typo3/index.php?some=param'
144  );
145 
146  $mockController
147  ->expects(self::any())
148  ->method('getAccessibleFormStorageFolders')
149  ->willReturn([
150  0 => [
151  'label' => 'user_upload',
152  'value' => '1:/user_upload/',
153  ],
154  ]);
155 
156  $expected = [
157  'selectablePrototypesConfiguration' => [],
158  'accessibleFormStorageFolders' => [
159  0 => [
160  'label' => 'user_upload',
161  'value' => '1:/user_upload/',
162  ],
163  ],
164  'endpoints' => [
165  'create' => '/typo3/index.php?some=param',
166  'duplicate' => '/typo3/index.php?some=param',
167  'delete' => '/typo3/index.php?some=param',
168  'references' => '/typo3/index.php?some=param',
169  ],
170  ];
171 
172  self::assertSame(json_encode($expected), $mockController->_call('getFormManagerAppInitialData'));
173  }
174 
179  {
180  $mockController = $this->getAccessibleMock(FormManagerController::class, [
181  'dummy'
182  ], [], '', false);
183 
184  $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
185  $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
186 
187  $databaseService = $this->prophesize(DatabaseService::class);
188  $mockController->_set('databaseService', $databaseService->reveal());
189 
190  $formPersistenceManagerProphecy->listForms(Argument::cetera())->willReturn([
191  0 => [
192  'identifier' => 'ext-form-identifier',
193  'name' => 'some name',
194  'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
195  'readOnly' => false,
196  'removable' => true,
197  'location' => 'storage',
198  'duplicateIdentifier' => false,
199  ],
200  ]);
201 
202  $databaseService->getAllReferencesForFileUid(Argument::cetera())->willReturn([
203  0 => 0,
204  ]);
205 
206  $databaseService->getAllReferencesForPersistenceIdentifier(Argument::cetera())->willReturn([
207  '1:/user_uploads/someFormName.yaml' => 2,
208  ]);
209 
210  $expected = [
211  0 => [
212  'identifier' => 'ext-form-identifier',
213  'name' => 'some name',
214  'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
215  'readOnly' => false,
216  'removable' => true,
217  'location' => 'storage',
218  'duplicateIdentifier' => false,
219  'referenceCount' => 2,
220  ],
221  ];
222 
223  self::assertSame($expected, $mockController->_call('getAvailableFormDefinitions'));
224  }
225 
230  {
231  $this->expectException(\InvalidArgumentException::class);
232  $this->expectExceptionCode(1477071939);
233 
234  $mockController = $this->getAccessibleMock(FormManagerController::class, [
235  'dummy'
236  ], [], '', false);
237 
238  $mockController->_call('getProcessedReferencesRows', '');
239  }
240 
245  {
246  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
247  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
248  $iconProphecy = $this->prophesize(Icon::class);
249  $iconFactoryProphecy->getIconForRecord(Argument::cetera())->willReturn($iconProphecy->reveal());
250  $iconProphecy->render()->shouldBeCalled()->willReturn('');
251 
252  $mockController = $this->getAccessibleMock(FormManagerController::class, [
253  'getModuleUrl',
254  'getRecord',
255  'getRecordTitle',
256  ], [], '', false);
257 
258  $databaseService = $this->prophesize(DatabaseService::class);
259  $mockController->_set('databaseService', $databaseService->reveal());
260 
261  $databaseService->getReferencesByPersistenceIdentifier(Argument::cetera())->willReturn([
262  0 => [
263  'tablename' => 'tt_content',
264  'recuid' => -1,
265  ],
266  ]);
267 
268  $mockController
269  ->expects(self::any())
270  ->method('getModuleUrl')
271  ->willReturn('/typo3/index.php?some=param');
272 
273  $mockController
274  ->expects(self::any())
275  ->method('getRecord')
276  ->willReturn([ 'uid' => 1, 'pid' => 0 ]);
277 
278  $mockController
279  ->expects(self::any())
280  ->method('getRecordTitle')
281  ->willReturn('record title');
282 
283  $expected = [
284  0 => [
285  'recordPageTitle' => 'record title',
286  'recordTitle' => 'record title',
287  'recordIcon' => '',
288  'recordUid' => -1,
289  'recordEditUrl' => '/typo3/index.php?some=param',
290  ],
291  ];
292 
293  self::assertSame($expected, $mockController->_call('getProcessedReferencesRows', 'fake'));
294  }
295 
300  {
301  $mockController = $this->getAccessibleMock(FormManagerController::class, [
302  'dummy'
303  ], [], '', false);
304 
305  $mockController->_set('formSettings', [
306  'formManager' => [
307  'selectablePrototypesConfiguration' => [
308  0 => [
309  'identifier' => 'standard',
310  'label' => 'some label',
311  'newFormTemplates' => [
312  0 => [
313  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
314  'label' => 'some label',
315  ],
316  1 => [
317  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
318  'label' => 'some other label',
319  ],
320  ],
321  ],
322  ],
323  ],
324  ]);
325 
326  self::assertTrue($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
327  }
328 
333  {
334  $mockController = $this->getAccessibleMock(FormManagerController::class, [
335  'dummy'
336  ], [], '', false);
337 
338  $mockController->_set('formSettings', [
339  'formManager' => [
340  'selectablePrototypesConfiguration' => [
341  0 => [
342  'identifier' => 'standard',
343  'label' => 'some label',
344  'newFormTemplates' => [
345  0 => [
346  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
347  'label' => 'some label',
348  ],
349  1 => [
350  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
351  'label' => 'some other label',
352  ],
353  ],
354  ],
355  ],
356  ],
357  ]);
358 
359  self::assertFalse($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/NonExistingForm.yaml'));
360  }
361 
366  {
367  $mockController = $this->getAccessibleMock(FormManagerController::class, [
368  'dummy'
369  ], [], '', false);
370 
371  $mockController->_set('formSettings', [
372  'formManager' => [
373  'selectablePrototypesConfiguration' => [
374  0 => [
375  'identifier' => 'standard',
376  'label' => 'some label',
377  'newFormTemplates' => [
378  0 => [
379  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
380  'label' => 'some label',
381  ],
382  1 => [
383  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
384  'label' => 'some other label',
385  ],
386  ],
387  ],
388  1 => [
389  'identifier' => 'other',
390  'label' => 'some label',
391  'newFormTemplates' => [
392  0 => [
393  'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
394  'label' => 'some label',
395  ],
396  ],
397  ],
398  ],
399  ],
400  ]);
401 
402  self::assertFalse($mockController->_call('isValidTemplatePath', 'other', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
403  }
404 
408  public function ‪convertFormNameToIdentifierRemoveSpaces(): void
409  {
410  $mockController = $this->getAccessibleMock(FormManagerController::class, [
411  'dummy'
412  ], [], '', false);
413 
414  $input = 'test form';
415  $expected = 'testform';
416  self::assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
417  }
418 
423  {
424  $mockController = $this->getAccessibleMock(FormManagerController::class, [
425  'dummy'
426  ], [], '', false);
427 
428  $input = 'téstform';
429  $expected = 'testform';
430  self::assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
431  }
432 
437  {
438  $mockController = $this->getAccessibleMock(FormManagerController::class, [
439  'dummy'
440  ], [], '', false);
441 
442  $input = 'test form ä#!_-01';
443  $expected = 'testformae_-01';
444  self::assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
445  }
446 }
‪TYPO3\CMS\Form\Service\DatabaseService
Definition: DatabaseService.php:32
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\isValidTemplatePathReturnsFalseIfTemplateIsNotDefinedAndExists
‪isValidTemplatePathReturnsFalseIfTemplateIsNotDefinedAndExists()
Definition: FormManagerControllerTest.php:364
‪TYPO3\CMS\Form\Service\TranslationService
Definition: TranslationService.php:44
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:39
‪TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext
Definition: ControllerContext.php:28
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\getAccessibleFormStorageFoldersReturnsProcessedArray
‪getAccessibleFormStorageFoldersReturnsProcessedArray()
Definition: FormManagerControllerTest.php:47
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\isValidTemplatePathReturnsTrueIfTemplateIsDefinedAndExists
‪isValidTemplatePathReturnsTrueIfTemplateIsDefinedAndExists()
Definition: FormManagerControllerTest.php:298
‪TYPO3\CMS\Form\Controller\FormManagerController
Definition: FormManagerController.php:47
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\convertFormNameToIdentifierRemoveSpecialChars
‪convertFormNameToIdentifierRemoveSpecialChars()
Definition: FormManagerControllerTest.php:435
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\getFormManagerAppInitialDataReturnsProcessedArray
‪getFormManagerAppInitialDataReturnsProcessedArray()
Definition: FormManagerControllerTest.php:104
‪TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager
Definition: FormPersistenceManager.php:54
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\getAvailableFormDefinitionsReturnsProcessedArray
‪getAvailableFormDefinitionsReturnsProcessedArray()
Definition: FormManagerControllerTest.php:177
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\isValidTemplatePathReturnsFalseIfTemplateIsDefinedButNotExists
‪isValidTemplatePathReturnsFalseIfTemplateIsDefinedButNotExists()
Definition: FormManagerControllerTest.php:331
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\getProcessedReferencesRowsThrowsExceptionIfPersistenceIdentifierIsEmpty
‪getProcessedReferencesRowsThrowsExceptionIfPersistenceIdentifierIsEmpty()
Definition: FormManagerControllerTest.php:228
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\getProcessedReferencesRowsReturnsProcessedArray
‪getProcessedReferencesRowsReturnsProcessedArray()
Definition: FormManagerControllerTest.php:243
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\convertFormNameToIdentifierRemoveSpaces
‪convertFormNameToIdentifierRemoveSpaces()
Definition: FormManagerControllerTest.php:407
‪TYPO3\CMS\Form\Tests\Unit\Controller
Definition: AbstractBackendControllerTest.php:16
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\convertFormNameToIdentifierConvertAccentedCharacters
‪convertFormNameToIdentifierConvertAccentedCharacters()
Definition: FormManagerControllerTest.php:421
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest
Definition: FormManagerControllerTest.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Tests\Unit\Controller\FormManagerControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FormManagerControllerTest.php:42
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28