‪TYPO3CMS  11.5
CreatableFormElementPropertiesValidatorTest.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 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
27 {
32  {
33  $this->expectException(PropertyException::class);
34  $this->expectExceptionCode(1528588035);
35 
36  $validationDto = new ‪ValidationDto('standard', null, 'test-1', 'label');
37  $input = 'xxx';
38  $typeConverter = $this->getAccessibleMock(
39  CreatableFormElementPropertiesValidator::class,
40  ['getConfigurationService'],
41  [[], '', $validationDto]
42  );
43  $configurationService = $this->createMock(ConfigurationService::class);
44  $configurationService
45  ->method('getFormElementPredefinedDefaultValueFromFormEditorSetup')
46  ->willReturn('default');
47  $configurationService
48  ->method('isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup')
49  ->willReturn(true);
50  $typeConverter->method('getConfigurationService')->willReturn($configurationService);
51 
52  $typeConverter($input, '');
53  }
54 
59  {
60  $validationDto = new ‪ValidationDto(null, null, 'test-1', 'label');
61  $typeConverter = $this->getAccessibleMock(
62  CreatableFormElementPropertiesValidator::class,
63  ['getConfigurationService'],
64  [[], '', $validationDto]
65  );
66  $configurationService = $this->createMock(ConfigurationService::class);
67  $configurationService
68  ->method('getFormElementPredefinedDefaultValueFromFormEditorSetup')
69  ->willReturn('default');
70  $configurationService
71  ->method('isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup')
72  ->willReturn(true);
73  $typeConverter->method('getConfigurationService')->willReturn($configurationService);
74 
75  $failed = false;
76  try {
77  $typeConverter('', 'default');
78  } catch (‪PropertyException $e) {
79  $failed = true;
80  }
81  self::assertFalse($failed);
82  }
83 
85  {
86  return [
87  [
88  'input' => 'foo',
89  'allowedValues' => [],
90  'untranslatedAllowedValues' => [],
91  ],
92  [
93  'input' => 'foo',
94  'allowedValues' => ['bar', 'baz'],
95  'untranslatedAllowedValues' => ['bar', 'baz'],
96  ],
97  [
98  'input' => 'foo',
99  'allowedValues' => ['bar', 'baz'],
100  'untranslatedAllowedValues' => ['bar', 'world'],
101  ],
102  [
103  'input' => 1,
104  'allowedValues' => ['1', 2],
105  'untranslatedAllowedValues' => ['1', 2],
106  ],
107  ];
108  }
109 
114  public function ‪validateFormElementValueThrowsExceptionIfValueDoesNotMatch($input, array $allowedValues, array $untranslatedAllowedValues): void
115  {
116  $this->expectException(PropertyException::class);
117 
118  $validationDto = new ‪ValidationDto('standard', null, 'test-1', 'label');
119  $validatorMock = $this->getAccessibleMock(
120  CreatableFormElementPropertiesValidator::class,
121  ['getConfigurationService'],
122  [[], '', $validationDto]
123  );
124 
125  $configurationServiceMock = $this->createMock(ConfigurationService::class);
126  $configurationServiceMock->method(
127  'getAllowedValuesForFormElementPropertyFromFormEditorSetup'
128  )->willReturnMap([
129  [$validationDto, true, $allowedValues],
130  [$validationDto, false, $untranslatedAllowedValues],
131  ]);
132 
133  $validatorMock->method('getConfigurationService')->willReturn($configurationServiceMock);
134 
135  $validatorMock->_call('validateFormElementValue', $input, $validationDto);
136  }
137 
139  {
140  return [
141  [
142  'input' => 'foo',
143  'allowedValues' => ['foo'],
144  'untranslatedAllowedValues' => ['foo'],
145  'allPossibleAllowedValuesTranslations' => [],
146  ],
147  [
148  'input' => 'foo',
149  'allowedValues' => ['bar'],
150  'untranslatedAllowedValues' => ['foo'],
151  'allPossibleAllowedValuesTranslations' => [],
152  ],
153  [
154  'input' => 'foo',
155  'allowedValues' => ['bar'],
156  'untranslatedAllowedValues' => ['baz'],
157  'allPossibleAllowedValuesTranslations' => ['default' => ['foo'], 'de' => ['bar']],
158  ],
159  [
160  'input' => 'foo',
161  'allowedValues' => ['foo', 'baz'],
162  'untranslatedAllowedValues' => ['foo', 'baz'],
163  'allPossibleAllowedValuesTranslations' => [],
164  ],
165  [
166  'input' => 1,
167  'allowedValues' => ['1', 1, 2],
168  'untranslatedAllowedValues' => ['1', 1, 2],
169  'allPossibleAllowedValuesTranslations' => [],
170  ],
171  ];
172  }
173 
178  public function ‪validateFormElementValueThrowsNoExceptionIfValueMatches($input, array $allowedValues, array $untranslatedAllowedValues, array $allPossibleAllowedValuesTranslations): void
179  {
180  $validationDto = new ‪ValidationDto('standard', null, 'test-1', 'label');
181  $validatorMock = $this->getAccessibleMock(
182  CreatableFormElementPropertiesValidator::class,
183  ['getConfigurationService'],
184  [[], '', $validationDto]
185  );
186 
187  $configurationServiceMock = $this->createMock(ConfigurationService::class);
188  $configurationServiceMock->method(
189  'getAllowedValuesForFormElementPropertyFromFormEditorSetup'
190  )->willReturnMap([
191  [$validationDto, true, $allowedValues],
192  [$validationDto, false, $untranslatedAllowedValues],
193  ]);
194  $configurationServiceMock->method('getAllBackendTranslationsForTranslationKeys')->willReturn($allPossibleAllowedValuesTranslations);
195  $validatorMock->method('getConfigurationService')->willReturn($configurationServiceMock);
196 
197  $failed = false;
198  try {
199  $validatorMock->_call('validateFormElementValue', $input, $validationDto);
200  } catch (‪PropertyException $e) {
201  $failed = true;
202  }
203  self::assertFalse($failed);
204  }
205 }
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsExceptionIfValueDoesNotMatch
‪validateFormElementValueThrowsExceptionIfValueDoesNotMatch($input, array $allowedValues, array $untranslatedAllowedValues)
Definition: CreatableFormElementPropertiesValidatorTest.php:114
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementPredefinedDefaultValueThrowsNoExceptionIfValueMatches
‪validateFormElementPredefinedDefaultValueThrowsNoExceptionIfValueMatches()
Definition: CreatableFormElementPropertiesValidatorTest.php:58
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:18
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto
Definition: ValidationDto.php:23
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators
Definition: CreatableFormElementPropertiesValidatorTest.php:18
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator
Definition: CreatableFormElementPropertiesValidator.php:27
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsNoExceptionIfValueMatchesDataProvider
‪validateFormElementValueThrowsNoExceptionIfValueMatchesDataProvider()
Definition: CreatableFormElementPropertiesValidatorTest.php:138
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementPredefinedDefaultValueThrowsExceptionIfValueDoesNotMatch
‪validateFormElementPredefinedDefaultValueThrowsExceptionIfValueDoesNotMatch()
Definition: CreatableFormElementPropertiesValidatorTest.php:31
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest
Definition: CreatableFormElementPropertiesValidatorTest.php:27
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:50
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsExceptionIfValueDoesNotMatchDataProvider
‪validateFormElementValueThrowsExceptionIfValueDoesNotMatchDataProvider()
Definition: CreatableFormElementPropertiesValidatorTest.php:84
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:25
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsNoExceptionIfValueMatches
‪validateFormElementValueThrowsNoExceptionIfValueMatches($input, array $allowedValues, array $untranslatedAllowedValues, array $allPossibleAllowedValuesTranslations)
Definition: CreatableFormElementPropertiesValidatorTest.php:178