b37e3e1526ae9a2f1068441d2c68ea130105dd3a
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Controller / FormManagerControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Controller;
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
17 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Resource\ResourceStorage;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
22 use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
23 use TYPO3\CMS\Extbase\Object\ObjectManager;
24 use TYPO3\CMS\Form\Controller\FormManagerController;
25 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
26 use TYPO3\CMS\Form\Service\TranslationService;
27
28 /**
29 * Test case
30 */
31 class FormManagerControllerTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
32 {
33
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = [];
38
39 /**
40 * Set up
41 */
42 public function setUp()
43 {
44 $this->singletonInstances = GeneralUtility::getSingletonInstances();
45 }
46
47 /**
48 * Tear down
49 */
50 public function tearDown()
51 {
52 GeneralUtility::resetSingletonInstances($this->singletonInstances);
53 parent::tearDown();
54 }
55
56 /**
57 * @test
58 */
59 public function getAccessibleFormStorageFoldersReturnsProcessedArray()
60 {
61 $mockController = $this->getAccessibleMock(FormManagerController::class, [
62 'dummy'
63 ], [], '', false);
64
65 $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
66
67 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
68 ->disableOriginalConstructor()
69 ->getMock();
70 $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
71
72 $folder1 = new Folder($mockStorage, '/user_upload/', 'user_upload');
73 $folder2 = new Folder($mockStorage, '/forms/', 'forms');
74
75 $formPersistenceManagerProphecy->getAccessibleFormStorageFolders(Argument::cetera())->willReturn([
76 '1:/user_upload/' => $folder1,
77 '2:/forms/' => $folder2,
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 ];
90
91 $this->assertSame($expected, $mockController->_call('getAccessibleFormStorageFolders'));
92 }
93
94 /**
95 * @test
96 */
97 public function getFormManagerAppInitialDataReturnsProcessedArray()
98 {
99 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
100 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
101
102 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
103 'translateValuesRecursive'
104 ], [], '', false);
105
106 $mockTranslationService
107 ->expects($this->any())
108 ->method('translateValuesRecursive')
109 ->willReturnArgument(0);
110
111 $objectMangerProphecy
112 ->get(TranslationService::class)
113 ->willReturn($mockTranslationService);
114
115 $mockController = $this->getAccessibleMock(FormManagerController::class, [
116 'getAccessibleFormStorageFolders'
117 ], [], '', false);
118
119 $mockUriBuilder = $this->createMock(UriBuilder::class);
120 $mockControllerContext = $this->createMock(ControllerContext::class);
121 $mockControllerContext
122 ->expects($this->any())
123 ->method('getUriBuilder')
124 ->will($this->returnValue($mockUriBuilder));
125
126 $mockController->_set('controllerContext', $mockControllerContext);
127
128 $mockController->_set('formSettings', [
129 'formManager' => [
130 'selectablePrototypesConfiguration' => [],
131 ],
132 ]);
133
134 $mockUriBuilder->expects($this->any())->method('uriFor')->willReturn(
135 '/typo3/index.php?some=param'
136 );
137
138 $mockController
139 ->expects($this->any())
140 ->method('getAccessibleFormStorageFolders')
141 ->willReturn([
142 0 => [
143 'label' => 'user_upload',
144 'value' => '1:/user_upload/',
145 ],
146 ]);
147
148 $expected = [
149 'selectablePrototypesConfiguration' => [],
150 'accessibleFormStorageFolders' => [
151 0 => [
152 'label' => 'user_upload',
153 'value' => '1:/user_upload/',
154 ],
155 ],
156 'endpoints' => [
157 'create' => '/typo3/index.php?some=param',
158 'duplicate' => '/typo3/index.php?some=param',
159 'delete' => '/typo3/index.php?some=param',
160 'references' => '/typo3/index.php?some=param',
161 ],
162 ];
163
164 $this->assertSame(json_encode($expected), $mockController->_call('getFormManagerAppInitialData'));
165 }
166
167 /**
168 * @test
169 */
170 public function getAvailableFormDefinitionsReturnsProcessedArray()
171 {
172 $mockController = $this->getAccessibleMock(FormManagerController::class, [
173 'getReferences'
174 ], [], '', false);
175
176 $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
177 $mockController->_set('formPersistenceManager', $formPersistenceManagerProphecy->reveal());
178
179 $formPersistenceManagerProphecy->listForms(Argument::cetera())->willReturn([
180 0 => [
181 'identifier' => 'ext-form-identifier',
182 'name' => 'some name',
183 'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
184 'readOnly' => false,
185 'location' => 'storage',
186 'duplicateIdentifier' => false,
187 ],
188 ]);
189
190 $mockController
191 ->expects($this->any())
192 ->method('getReferences')
193 ->willReturn([
194 'someRow',
195 'anotherRow',
196 ]);
197
198 $expected = [
199 0 => [
200 'identifier' => 'ext-form-identifier',
201 'name' => 'some name',
202 'persistenceIdentifier' => '1:/user_uploads/someFormName.yaml',
203 'readOnly' => false,
204 'location' => 'storage',
205 'duplicateIdentifier' => false,
206 'referenceCount' => 2,
207 ],
208 ];
209
210 $this->assertSame($expected, $mockController->_call('getAvailableFormDefinitions'));
211 }
212
213 /**
214 * @test
215 */
216 public function getProcessedReferencesRowsThrowsExceptionIfPersistenceIdentifierIsEmpty()
217 {
218 $this->expectException(\InvalidArgumentException::class);
219 $this->expectExceptionCode(1477071939);
220
221 $mockController = $this->getAccessibleMock(FormManagerController::class, [
222 'dummy'
223 ], [], '', false);
224
225 $mockController->_call('getProcessedReferencesRows', '');
226 }
227
228 /**
229 * @test
230 */
231 public function getProcessedReferencesRowsReturnsProcessedArray()
232 {
233 $mockController = $this->getAccessibleMock(FormManagerController::class, [
234 'getModuleUrl',
235 'getRecord',
236 'getRecordTitle',
237 'getReferences',
238 ], [], '', false);
239
240 $mockController
241 ->expects($this->any())
242 ->method('getModuleUrl')
243 ->willReturn('/typo3/index.php?some=param');
244
245 $mockController
246 ->expects($this->any())
247 ->method('getRecord')
248 ->willReturn([ 'uid' => 1, 'pid' => 0 ]);
249
250 $mockController
251 ->expects($this->any())
252 ->method('getRecordTitle')
253 ->willReturn('record title');
254
255 $mockController
256 ->expects($this->any())
257 ->method('getReferences')
258 ->willReturn([
259 0 => [
260 'tablename' => 'tt_content',
261 'recuid' => -1,
262 ],
263 ]);
264
265 $expected = [
266 0 => [
267 'recordPageTitle' => 'record title',
268 'recordTitle' => 'record title',
269 'recordIcon' =>
270 '<span class="t3js-icon icon icon-size-small icon-state-default icon-default-not-found" data-identifier="default-not-found">
271 <span class="icon-markup">
272 <img src="typo3/sysext/core/Resources/Public/Icons/T3Icons/default/default-not-found.svg" width="16" height="16" />
273 </span>
274
275 </span>',
276 'recordUid' => -1,
277 'recordEditUrl' => '/typo3/index.php?some=param',
278 ],
279 ];
280
281 $this->assertSame($expected, $mockController->_call('getProcessedReferencesRows', 'fake'));
282 }
283
284 /**
285 * @test
286 */
287 public function isValidTemplatePathReturnsTrueIfTemplateIsDefinedAndExists()
288 {
289 $mockController = $this->getAccessibleMock(FormManagerController::class, [
290 'dummy'
291 ], [], '', false);
292
293 $mockController->_set('formSettings', [
294 'formManager' => [
295 'selectablePrototypesConfiguration' => [
296 0 => [
297 'identifier' => 'standard',
298 'label' => 'some label',
299 'newFormTemplates' => [
300 0 => [
301 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
302 'label' => 'some label',
303 ],
304 1 => [
305 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
306 'label' => 'some other label',
307 ],
308 ],
309 ],
310 ],
311 ],
312 ]);
313
314 $this->assertTrue($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
315 }
316
317 /**
318 * @test
319 */
320 public function isValidTemplatePathReturnsFalseIfTemplateIsDefinedButNotExists()
321 {
322 $mockController = $this->getAccessibleMock(FormManagerController::class, [
323 'dummy'
324 ], [], '', false);
325
326 $mockController->_set('formSettings', [
327 'formManager' => [
328 'selectablePrototypesConfiguration' => [
329 0 => [
330 'identifier' => 'standard',
331 'label' => 'some label',
332 'newFormTemplates' => [
333 0 => [
334 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
335 'label' => 'some label',
336 ],
337 1 => [
338 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
339 'label' => 'some other label',
340 ],
341 ],
342 ],
343 ],
344 ],
345 ]);
346
347 $this->assertFalse($mockController->_call('isValidTemplatePath', 'standard', 'EXT:form/Tests/Unit/Controller/Fixtures/NonExistingForm.yaml'));
348 }
349
350 /**
351 * @test
352 */
353 public function isValidTemplatePathReturnsFalseIfTemplateIsNotDefinedAndExists()
354 {
355 $mockController = $this->getAccessibleMock(FormManagerController::class, [
356 'dummy'
357 ], [], '', false);
358
359 $mockController->_set('formSettings', [
360 'formManager' => [
361 'selectablePrototypesConfiguration' => [
362 0 => [
363 'identifier' => 'standard',
364 'label' => 'some label',
365 'newFormTemplates' => [
366 0 => [
367 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
368 'label' => 'some label',
369 ],
370 1 => [
371 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml',
372 'label' => 'some other label',
373 ],
374 ],
375 ],
376 1 => [
377 'identifier' => 'other',
378 'label' => 'some label',
379 'newFormTemplates' => [
380 0 => [
381 'templatePath' => 'EXT:form/Tests/Unit/Controller/Fixtures/BlankForm.yaml',
382 'label' => 'some label',
383 ],
384 ],
385 ],
386 ],
387 ],
388 ]);
389
390 $this->assertFalse($mockController->_call('isValidTemplatePath', 'other', 'EXT:form/Tests/Unit/Controller/Fixtures/SimpleContactForm.yaml'));
391 }
392
393 /**
394 * @test
395 */
396 public function convertFormNameToIdentifierRemoveSpaces()
397 {
398 $mockController = $this->getAccessibleMock(FormManagerController::class, [
399 'dummy'
400 ], [], '', false);
401
402 $input = 'test form';
403 $expected = 'testform';
404 $this->assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
405 }
406
407 /**
408 * @test
409 */
410 public function convertFormNameToIdentifierRemoveSpecialChars()
411 {
412 $mockController = $this->getAccessibleMock(FormManagerController::class, [
413 'dummy'
414 ], [], '', false);
415
416 $input = 'test form รค#!_-01';
417 $expected = 'testform_-01';
418 $this->assertSame($expected, $mockController->_call('convertFormNameToIdentifier', $input));
419 }
420 }