‪TYPO3CMS  9.5
CreatableFormElementPropertiesValidatorTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
25 {
30  {
31  $this->expectException(PropertyException::class);
32  $this->expectExceptionCode(1528588035);
33 
34  $validationDto = new ‪ValidationDto('standard', 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 
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 
83  {
84  return [
85  [
86  'input' => 'foo',
87  'allowedValues' => [],
88  'untranslatedAllowedValues' => [],
89  ],
90  [
91  'input' => 'foo',
92  'allowedValues' => ['bar', 'baz'],
93  'untranslatedAllowedValues' => ['bar', 'baz'],
94  ],
95  [
96  'input' => 'foo',
97  'allowedValues' => ['bar', 'baz'],
98  'untranslatedAllowedValues' => ['bar', 'world'],
99  ],
100  [
101  'input' => 1,
102  'allowedValues' => ['1', 2],
103  'untranslatedAllowedValues' => ['1', 2],
104  ],
105  ];
106  }
107 
112  public function ‪validateFormElementValueThrowsExceptionIfValueDoesNotMatch($input, array $allowedValues, array $untranslatedAllowedValues)
113  {
114  $this->expectException(PropertyException::class);
115 
116  $validationDto = new ‪ValidationDto('standard', null, 'test-1', 'label');
117  $validatorMock = $this->getAccessibleMock(
118  CreatableFormElementPropertiesValidator::class,
119  ['getConfigurationService'],
120  [[], '', $validationDto]
121  );
122 
123  $configurationServiceMock = $this->createMock(ConfigurationService::class);
124  $configurationServiceMock->expects(self::any())->method(
125  'getAllowedValuesForFormElementPropertyFromFormEditorSetup'
126  )->willReturnMap([
127  [$validationDto, true, $allowedValues],
128  [$validationDto, false, $untranslatedAllowedValues],
129  ]);
130 
131  $validatorMock->expects(self::any())->method('getConfigurationService')->willReturn($configurationServiceMock);
132 
133  $validatorMock->_call('validateFormElementValue', $input, $validationDto);
134  }
135 
137  {
138  return [
139  [
140  'input' => 'foo',
141  'allowedValues' => ['foo'],
142  'untranslatedAllowedValues' => ['foo'],
143  'allPossibleAllowedValuesTranslations' => [],
144  ],
145  [
146  'input' => 'foo',
147  'allowedValues' => ['bar'],
148  'untranslatedAllowedValues' => ['foo'],
149  'allPossibleAllowedValuesTranslations' => [],
150  ],
151  [
152  'input' => 'foo',
153  'allowedValues' => ['bar'],
154  'untranslatedAllowedValues' => ['baz'],
155  'allPossibleAllowedValuesTranslations' => ['default' => ['foo'], 'de' => ['bar']],
156  ],
157  [
158  'input' => 'foo',
159  'allowedValues' => ['foo', 'baz'],
160  'untranslatedAllowedValues' => ['foo', 'baz'],
161  'allPossibleAllowedValuesTranslations' => [],
162  ],
163  [
164  'input' => 1,
165  'allowedValues' => ['1', 1, 2],
166  'untranslatedAllowedValues' => ['1', 1, 2],
167  'allPossibleAllowedValuesTranslations' => [],
168  ],
169  ];
170  }
171 
176  public function ‪validateFormElementValueThrowsNoExceptionIfValueMatches($input, array $allowedValues, array $untranslatedAllowedValues, array $allPossibleAllowedValuesTranslations)
177  {
178  $validationDto = new ‪ValidationDto('standard', null, 'test-1', 'label');
179  $validatorMock = $this->getAccessibleMock(
180  CreatableFormElementPropertiesValidator::class,
181  ['getConfigurationService'],
182  [[], '', $validationDto]
183  );
184 
185  $configurationServiceMock = $this->createMock(ConfigurationService::class);
186  $configurationServiceMock->expects(self::any())->method(
187  'getAllowedValuesForFormElementPropertyFromFormEditorSetup'
188  )->willReturnMap([
189  [$validationDto, true, $allowedValues],
190  [$validationDto, false, $untranslatedAllowedValues],
191  ]);
192  $configurationServiceMock->expects(self::any())->method('getAllBackendTranslationsForTranslationKeys')->willReturn($allPossibleAllowedValuesTranslations);
193  $validatorMock->expects(self::any())->method('getConfigurationService')->willReturn($configurationServiceMock);
194 
195  $failed = false;
196  try {
197  $validatorMock->_call('validateFormElementValue', $input, $validationDto);
198  } catch (‪PropertyException $e) {
199  $failed = true;
200  }
201  self::assertFalse($failed);
202  }
203 }
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsExceptionIfValueDoesNotMatch
‪validateFormElementValueThrowsExceptionIfValueDoesNotMatch($input, array $allowedValues, array $untranslatedAllowedValues)
Definition: CreatableFormElementPropertiesValidatorTest.php:112
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementPredefinedDefaultValueThrowsNoExceptionIfValueMatches
‪validateFormElementPredefinedDefaultValueThrowsNoExceptionIfValueMatches()
Definition: CreatableFormElementPropertiesValidatorTest.php:56
‪TYPO3\CMS\Extbase\Property\Exception
Definition: DuplicateObjectException.php:2
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ValidationDto
Definition: ValidationDto.php:23
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators
Definition: CreatableFormElementPropertiesValidatorTest.php:3
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator
Definition: CreatableFormElementPropertiesValidator.php:25
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsNoExceptionIfValueMatchesDataProvider
‪validateFormElementValueThrowsNoExceptionIfValueMatchesDataProvider()
Definition: CreatableFormElementPropertiesValidatorTest.php:136
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementPredefinedDefaultValueThrowsExceptionIfValueDoesNotMatch
‪validateFormElementPredefinedDefaultValueThrowsExceptionIfValueDoesNotMatch()
Definition: CreatableFormElementPropertiesValidatorTest.php:29
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest
Definition: CreatableFormElementPropertiesValidatorTest.php:25
‪TYPO3\CMS\Form\Domain\Configuration\ConfigurationService
Definition: ConfigurationService.php:49
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsExceptionIfValueDoesNotMatchDataProvider
‪validateFormElementValueThrowsExceptionIfValueDoesNotMatchDataProvider()
Definition: CreatableFormElementPropertiesValidatorTest.php:82
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:24
‪TYPO3\CMS\Form\Tests\Unit\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidatorTest\validateFormElementValueThrowsNoExceptionIfValueMatches
‪validateFormElementValueThrowsNoExceptionIfValueMatches($input, array $allowedValues, array $untranslatedAllowedValues, array $allPossibleAllowedValuesTranslations)
Definition: CreatableFormElementPropertiesValidatorTest.php:176