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