‪TYPO3CMS  10.4
FormDefinitionArrayConverterTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪FormDefinitionArrayConverterTest extends UnitTestCase
30 {
34  protected ‪$resetSingletonInstances = true;
35 
39  public function ‪setUp(): void
40  {
41  parent::setUp();
42  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
43  }
44 
49  {
50  $sessionToken = '123';
51 
52  $data = [
53  'prototypeName' => 'standard',
54  'identifier' => 'test',
55  'type' => 'Text',
56  'enabled' => false,
57  'properties' => [
58  'options' => [
59  [
60  '_label' => 'label',
61  '_value' => 'value',
62  ],
63  ],
64  ],
65  '_orig_prototypeName' => [
66  'value' => 'standard',
67  'hmac' => GeneralUtility::hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
68  ],
69  '_orig_identifier' => [
70  'value' => 'test',
71  'hmac' => GeneralUtility::hmac(serialize(['test', 'identifier', 'test']), $sessionToken),
72  ],
73  ];
74 
75  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['getFormDefinitionValidationService', 'retrieveSessionToken'], [], '', false);
76  $formDefinitionValidationService = $this->prophesize(FormDefinitionValidationService::class);
77  $formDefinitionValidationService->validateFormDefinitionProperties(Argument::cetera())->shouldBeCalled();
78  $formDefinitionValidationService->isPropertyValueEqualToHistoricalValue(Argument::cetera())->willReturn(true);
79 
80  $typeConverter->expects(self::any())->method(
81  'retrieveSessionToken'
82  )->willReturn($sessionToken);
83 
84  $typeConverter->expects(self::any())->method(
85  'getFormDefinitionValidationService'
86  )->willReturn($formDefinitionValidationService->reveal());
87 
88  $input = json_encode($data);
89  $expected = [
90  'prototypeName' => 'standard',
91  'identifier' => 'test',
92  'type' => 'Text',
93  'enabled' => false,
94  'properties' => [
95  'options' => [
96  'value' => 'label',
97  ],
98  ],
99  ];
100  $result = $typeConverter->convertFrom($input, FormDefinitionArray::class);
101 
102  self::assertInstanceOf(FormDefinitionArray::class, $result);
103  self::assertSame($expected, $result->getArrayCopy());
104  }
105 
110  {
111  $this->expectException(PropertyException::class);
112  $this->expectExceptionCode(1512578002);
113 
114  $typeConverter = new ‪FormDefinitionArrayConverter();
115  $input = '{"francine":"stan",';
116 
117  $typeConverter->convertFrom($input, FormDefinitionArray::class);
118  }
119 
124  {
125  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['dummy'], [], '', false);
126 
127  $input = [
128  'foo1' => 'bar',
129  'foo2' => [
130  'foo3' => [
131  [
132  '_label' => 'xxx1',
133  '_value' => 'yyy1',
134  ],
135  [
136  '_label' => 'xxx2',
137  '_value' => 'yyy2',
138  ],
139  [
140  '_label' => 'xxx3',
141  '_value' => 'yyy2',
142  ],
143  ],
144  '_label' => 'xxx',
145  '_value' => 'yyy',
146  ],
147  '_label' => 'xxx',
148  '_value' => 'yyy',
149  ];
150 
151  $expected = [
152  'foo1' => 'bar',
153  'foo2' => [
154  'foo3' => [
155  'yyy1' => 'xxx1',
156  'yyy2' => 'xxx3',
157  ],
158  '_label' => 'xxx',
159  '_value' => 'yyy',
160  ],
161  '_label' => 'xxx',
162  '_value' => 'yyy',
163  ];
164 
165  self::assertSame($expected, $typeConverter->_call('transformMultiValueElementsForFormFramework', $input));
166  }
167 
172  {
173  $this->expectException(PropertyException::class);
174  $this->expectExceptionCode(1528538322);
175 
176  $sessionToken = '123';
177  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['retrieveSessionToken'], [], '', false);
178 
179  $typeConverter->expects(self::any())->method(
180  'retrieveSessionToken'
181  )->willReturn($sessionToken);
182 
183  $input = [
184  'prototypeName' => 'foo',
185  'identifier' => 'test',
186  '_orig_prototypeName' => [
187  'value' => 'standard',
188  'hmac' => GeneralUtility::hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
189  ],
190  '_orig_identifier' => [
191  'value' => 'test',
192  'hmac' => GeneralUtility::hmac(serialize(['test', 'identifier', 'test']), $sessionToken),
193  ],
194  ];
195 
196  $typeConverter->convertFrom(json_encode($input), FormDefinitionArray::class);
197  }
198 
203  {
204  $this->expectException(PropertyException::class);
205  $this->expectExceptionCode(1528538322);
206 
207  $sessionToken = '123';
208  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['retrieveSessionToken'], [], '', false);
209 
210  $typeConverter->expects(self::any())->method(
211  'retrieveSessionToken'
212  )->willReturn($sessionToken);
213 
214  $input = [
215  'prototypeName' => 'standard',
216  'identifier' => 'xxx',
217  '_orig_prototypeName' => [
218  'value' => 'standard',
219  'hmac' => GeneralUtility::hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
220  ],
221  '_orig_identifier' => [
222  'value' => 'test',
223  'hmac' => GeneralUtility::hmac(serialize(['test', 'prototypeName', 'test']), $sessionToken),
224  ],
225  ];
226 
227  $typeConverter->convertFrom(json_encode($input), FormDefinitionArray::class);
228  }
229 }
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertsJsonStringToFormDefinitionArray
‪convertsJsonStringToFormDefinitionArray()
Definition: FormDefinitionArrayConverterTest.php:47
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\setUp
‪setUp()
Definition: FormDefinitionArrayConverterTest.php:38
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertFromThrowsExceptionIfIdentifierWasChanged
‪convertFromThrowsExceptionIfIdentifierWasChanged()
Definition: FormDefinitionArrayConverterTest.php:201
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService
Definition: FormDefinitionValidationService.php:36
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\transformMultiValueElementsForFormFrameworkTransformValues
‪transformMultiValueElementsForFormFrameworkTransformValues()
Definition: FormDefinitionArrayConverterTest.php:122
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter
Definition: FormDefinitionArrayConverterTest.php:16
‪TYPO3\CMS\Form\Type\FormDefinitionArray
Definition: FormDefinitionArray.php:25
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertFromThrowsExceptionIfJsonIsInvalid
‪convertFromThrowsExceptionIfJsonIsInvalid()
Definition: FormDefinitionArrayConverterTest.php:108
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest
Definition: FormDefinitionArrayConverterTest.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\FormDefinitionArrayConverter
Definition: FormDefinitionArrayConverter.php:37
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertFromThrowsExceptionIfPrototypeNameWasChanged
‪convertFromThrowsExceptionIfPrototypeNameWasChanged()
Definition: FormDefinitionArrayConverterTest.php:170
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:26
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FormDefinitionArrayConverterTest.php:33