[SECURITY] Filter disallowed properties in form editor
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Configuration / FormDefinition / Validators / CreatableFormElementPropertiesValidatorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators;
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 TYPO3\CMS\Form\Domain\Configuration\ConfigurationService;
19 use TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException;
20 use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator;
21 use TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 class CreatableFormElementPropertiesValidatorTest extends UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function validateFormElementPredefinedDefaultValueThrowsExceptionIfValueDoesNotMatch()
30 {
31 $this->expectException(PropertyException::class);
32 $this->expectExceptionCode(1528588035);
33
34 $validationDto = new ValidationDto(null, null, 'test-1', 'label');
35 $input = 'xxx';
36 $typeConverter = $this->getAccessibleMock(
37 CreatableFormElementPropertiesValidator::class,
38 ['getConfigurationService'],
39 [[], '', $validationDto]
40 );
41 $configurationService = $this->createMock(ConfigurationService::class);
42 $configurationService->expects($this->any())
43 ->method('getFormElementPredefinedDefaultValueFromFormEditorSetup')
44 ->willReturn('default');
45 $configurationService->expects($this->any())
46 ->method('isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup')
47 ->willReturn(true);
48 $typeConverter->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
49
50 $typeConverter($input, '');
51 }
52
53 /**
54 * @test
55 */
56 public function validateFormElementPredefinedDefaultValueThrowsNoExceptionIfValueMatches()
57 {
58 $validationDto = new ValidationDto(null, null, 'test-1', 'label');
59 $typeConverter = $this->getAccessibleMock(
60 CreatableFormElementPropertiesValidator::class,
61 ['getConfigurationService'],
62 [[], '', $validationDto]
63 );
64 $configurationService = $this->createMock(ConfigurationService::class);
65 $configurationService->expects($this->any())
66 ->method('getFormElementPredefinedDefaultValueFromFormEditorSetup')
67 ->willReturn('default');
68 $configurationService->expects($this->any())
69 ->method('isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup')
70 ->willReturn(true);
71 $typeConverter->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
72
73 $failed = false;
74 try {
75 $typeConverter('', 'default');
76 } catch (PropertyException $e) {
77 $failed = true;
78 }
79 $this->assertFalse($failed);
80 }
81 }