[TASK] Make FormManagerControllerTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Controller / FormManagerControllerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Tests\Unit\Controller;
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
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\Resource\Folder;
22 use TYPO3\CMS\Core\Resource\ResourceStorage;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
25 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
26 use TYPO3\CMS\Extbase\Object\ObjectManager;
27 use TYPO3\CMS\Form\Controller\FormManagerController;
28 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
29 use TYPO3\CMS\Form\Service\TranslationService;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31
32 /**
33 * Test case
34 */
35 class FormManagerControllerTest extends UnitTestCase
36 {
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = [];
41
42 /**
43 * Set up
44 */
45 public function setUp()
46 {
47 $this->singletonInstances = GeneralUtility::getSingletonInstances();
48 }
49
50 /**
51 * Tear down
52 */
53 public function tearDown(): void
54 {
55 GeneralUtility::resetSingletonInstances($this->singletonInstances);
56 parent::tearDown();
57 }
58
59 /**
60 * @test
61 */
62 public function getAccessibleFormStorageFoldersReturnsProcessedArray(): void
63 {
64 $mockController = $this->getAccessibleMock(FormManagerController::class, [
65 'dummy'
66 ], [], '', false);
67
68 $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
69
70 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
71 ->disableOriginalConstructor()
72 ->getMock();
73 $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
74
75 $mockController->_set('formSettings', [
76 'persistenceManager' => [
77 'allowSaveToExtensionPaths' => true,
78 ],
79 ]);
80
81 $folder1 = new Folder($mockStorage, '/user_upload/', 'user_upload');
82 $folder2 = new Folder($mockStorage, '/forms/', 'forms');
83
84 $formPersistenceManagerProphecy->getAccessibleFormStorageFolders(Argument::cetera())->willReturn([
85 '1:/user_upload/' => $folder1,
86 '2:/forms/' => $folder2,
87 ]);
88
89 $formPersistenceManagerProphecy->getAccessibleExtensionFolders(Argument::cetera())->willReturn([
90 'EXT:form/Resources/Forms/' => '/some/path/form/Resources/Forms/',
91 'EXT:form_additions/Resources/Forms/' => '/some/path/form_additions/Resources/Forms/',
92 ]);
93
94 $expected = [
95 0 => [
96 'label' => 'user_upload',
97 'value' => '1:/user_upload/',
98 ],
99 1 => [
100 'label' => 'forms',
101 'value' => '2:/forms/',
102 ],
103 2 => [
104 'label' => 'EXT:form/Resources/Forms/',
105 'value' => 'EXT:form/Resources/Forms/',
106 ],
107 3 => [
108 'label' => 'EXT:form_additions/Resources/Forms/',
109 'value' => 'EXT:form_additions/Resources/Forms/',
110 ],
111 ];
112
113 $this->assertSame($expected, $mockController->_call('getAccessibleFormStorageFolders'));
114 }
115
116 /**
117 * @test
118 */
119 public function getFormManagerAppInitialDataReturnsProcessedArray(): void
120 {
121 $objectManagerProphecy = $this->prophesize(ObjectManager::class);
122 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
123
124 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
125 'translateValuesRecursive'
126 ], [], '', false);
127
128 $mockTranslationService
129 ->expects($this->any())
130 ->method('translateValuesRecursive')
131 ->willReturnArgument(0);
132
133 $objectManagerProphecy
134 ->get(TranslationService::class)
135 ->willReturn($mockTranslationService);
136
137 $mockController = $this->getAccessibleMock(FormManagerController::class, [
138 'getAccessibleFormStorageFolders'
139 ], [], '', false);
140
141 $mockUriBuilder = $this->createMock(UriBuilder::class);
142 $mockControllerContext = $this->createMock(ControllerContext::class);
143 $mockControllerContext
144 ->expects($this->any())
145 ->method('getUriBuilder')
146 ->will($this->returnValue($mockUriBuilder));
147
148 $mockController->_set('controllerContext', $mockControllerContext);
149
150 $mockController->_set('formSettings', [
151 'formManager' => [
152 'selectablePrototypesConfiguration' => [],
153 ],
154 ]);
155
156 $mockUriBuilder->expects($this->any())->method('uriFor')->willReturn(
157 '/typo3/index.php?some=param'
158 );
159
160 $mockController
161 ->expects($this->any())
162 ->method('getAccessibleFormStorageFolders')
163 ->willReturn([
164 0 => [
165 'label' => 'user_upload',
166 'value' => '1:/user_upload/',
167 ],
168 ]);
169
170 $expected = [
171 'selectablePrototypesConfiguration' => [],
172 'accessibleFormStorageFolders' => [
173 0 => [
174 'label' => 'user_upload',
175 'value' => '1:/user_upload/',
176 ],
177 ],
178 'endpoints' => [
179 'create' => '/typo3/index.php?some=param',
180 'duplicate' => '/typo3/index.php?some=param',
181 'delete' => '/typo3/index.php?some=param',
182 'references' => '/typo3/index.php?some=param',
183 ],
184 ];
185
186 $this->assertSame(json_encode($expected), $mockController->_call('getFormManagerAppInitialData'));
187 }
188
189 /**
190 * @test
191 */
192 public function getAvailableFormDefinitionsReturnsProcessedArray(): void
193 {
194 $mockController = $this->getAccessibleMock(FormManagerController::class, [
195 'getReferences'
196 ], [], '', false);
197
198 $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
199 $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
200
201 $formPersistenceManagerProphecy->listForms(Argument::cetera())->willReturn([
202 0 => [
203 'identifier' => 'ext-form-identifier',
204 'name' => 'some name',
205 'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
206 'readOnly' => false,
207 'removable' => true,
208 'location' => 'storage',
209 'duplicateIdentifier' => false,
210 ],
211 ]);
212
213 $mockController
214 ->expects($this->any())
215 ->method('getReferences')
216 ->willReturn([
217 'someRow',
218 'anotherRow',
219 ]);
220
221 $expected = [
222 0 => [
223 'identifier' => 'ext-form-identifier',
224 'name' => 'some name',
225 'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
226 'readOnly' => false,
227 'removable' => true,
228 'location' => 'storage',
229 'duplicateIdentifier' => false,
230 'referenceCount' => 2,
231 ],
232 ];
233
234 $this->assertSame($expected, $mockController->_call('getAvailableFormDefinitions'));
235 }
236
237 /**
238 * @test
239 */
240 public function getProcessedReferencesRowsThrowsExceptionIfPersistenceIdentifierIsEmpty(): void
241 {
242 $this->expectException(\InvalidArgumentException::class);
243 $this->expectExceptionCode(1477071939);
244
245 $mockController = $this->getAccessibleMock(FormManagerController::class, [
246 'dummy'
247 ], [], '', false);
248
249 $mockController->_call('getProcessedReferencesRows', '');
250 }
251
252 /**
253 * @test
254 */
255 public function getProcessedReferencesRowsReturnsProcessedArray()
256 {
257 $iconFactoryProphecy = $this->prophesize(IconFactory::class);
258 GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
259 $iconProphecy = $this->prophesize(Icon::class);
260 $iconFactoryProphecy->getIconForRecord(Argument::cetera())->willReturn($iconProphecy->reveal());
261 $iconProphecy->render()->shouldBeCalled()->willReturn('');
262
263 $mockController = $this->getAccessibleMock(FormManagerController::class, [
264 'getModuleUrl',
265 'getRecord',
266 'getRecordTitle',
267 'getReferences',
268 ], [], '', false);
269
270 $mockController
271 ->expects($this->any())
272 ->method('getModuleUrl')
273 ->willReturn('/typo3/index.php?some=param');
274
275 $mockController
276 ->expects($this->any())
277 ->method('getRecord')
278 ->willReturn([ 'uid' => 1, 'pid' => 0 ]);
279
280 $mockController
281 ->expects($this->any())
282 ->method('getRecordTitle')
283 ->willReturn('record title');
284
285 $mockController
286 ->expects($this->any())
287 ->method('getReferences')
288 ->willReturn([
289 0 => [
290 'tablename' => 'tt_content',
291 'recuid' => -1,
292 ],
293 ]);
294
295 $expected = [
296 0 => [
297 'recordPageTitle' => 'record title',
298 'recordTitle' => 'record title',
299 'recordIcon' => '',
300 'recordUid' => -1,
301 'recordEditUrl' => '/typo3/index.php?some=param',
302 ],
303 ];
304
305 $this->assertSame($expected, $mockController->_call('getProcessedReferencesRows', 'fake'));
306 }
307
308 /**
309 * @test
310 */
311 public function isValidTemplatePathReturnsTrueIfTemplateIsDefinedAndExists(): void
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
341 /**
342 * @test
343 */
344 public function isValidTemplatePathReturnsFalseIfTemplateIsDefinedButNotExists(): void
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
374 /**
375 * @test
376 */
377 public function isValidTemplatePathReturnsFalseIfTemplateIsNotDefinedAndExists(): void
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
417 /**
418 * @test
419 */
420 public function convertFormNameToIdentifierRemoveSpaces(): void
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
431 /**
432 * @test
433 */
434 public function convertFormNameToIdentifierRemoveSpecialChars(): void
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 }