‪TYPO3CMS  ‪main
FormDefinitionArrayConverterTest.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 
20 use PHPUnit\Framework\Attributes\Test;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪FormDefinitionArrayConverterTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
32 
33  public function ‪setUp(): void
34  {
35  parent::setUp();
36  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
37  $this->hashService = new ‪HashService();
38  }
39 
40  #[Test]
42  {
43  $sessionToken = '123';
44 
45  $data = [
46  'prototypeName' => 'standard',
47  'identifier' => 'test',
48  'type' => 'Text',
49  'enabled' => false,
50  'properties' => [
51  'options' => [
52  [
53  '_label' => 'label',
54  '_value' => 'value',
55  ],
56  ],
57  ],
58  '_orig_prototypeName' => [
59  'value' => 'standard',
60  'hmac' => $this->hashService->hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
61  ],
62  '_orig_identifier' => [
63  'value' => 'test',
64  'hmac' => $this->hashService->hmac(serialize(['test', 'identifier', 'test']), $sessionToken),
65  ],
66  ];
67 
68  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['getFormDefinitionValidationService', 'retrieveSessionToken'], callOriginalConstructor: false);
69  $formDefinitionValidationServiceMock = $this->createMock(FormDefinitionValidationService::class);
70  $formDefinitionValidationServiceMock->expects(self::atLeastOnce())->method('validateFormDefinitionProperties')->with(self::anything());
71  $formDefinitionValidationServiceMock->method('isPropertyValueEqualToHistoricalValue')->with(self::anything())->willReturn(true);
72 
73  $typeConverter->method(
74  'retrieveSessionToken'
75  )->willReturn($sessionToken);
76 
77  $typeConverter->method(
78  'getFormDefinitionValidationService'
79  )->willReturn($formDefinitionValidationServiceMock);
80 
81  $input = json_encode($data);
82  $expected = [
83  'prototypeName' => 'standard',
84  'identifier' => 'test',
85  'type' => 'Text',
86  'enabled' => false,
87  'properties' => [
88  'options' => [
89  'value' => 'label',
90  ],
91  ],
92  ];
93  $result = $typeConverter->convertFrom($input, FormDefinitionArray::class);
94 
95  self::assertInstanceOf(FormDefinitionArray::class, $result);
96  self::assertSame($expected, $result->getArrayCopy());
97  }
98 
99  #[Test]
101  {
102  $this->expectException(PropertyException::class);
103  $this->expectExceptionCode(1512578002);
104 
105  $typeConverter = new ‪FormDefinitionArrayConverter();
106  $input = '{"francine":"stan",';
107 
108  $typeConverter->convertFrom($input, FormDefinitionArray::class);
109  }
110 
111  #[Test]
113  {
114  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, null, [], '', false);
115 
116  $input = [
117  'foo1' => 'bar',
118  'foo2' => [
119  'foo3' => [
120  [
121  '_label' => 'xxx1',
122  '_value' => 'yyy1',
123  ],
124  [
125  '_label' => 'xxx2',
126  '_value' => 'yyy2',
127  ],
128  [
129  '_label' => 'xxx3',
130  '_value' => 'yyy2',
131  ],
132  ],
133  '_label' => 'xxx',
134  '_value' => 'yyy',
135  ],
136  '_label' => 'xxx',
137  '_value' => 'yyy',
138  ];
139 
140  $expected = [
141  'foo1' => 'bar',
142  'foo2' => [
143  'foo3' => [
144  'yyy1' => 'xxx1',
145  'yyy2' => 'xxx3',
146  ],
147  '_label' => 'xxx',
148  '_value' => 'yyy',
149  ],
150  '_label' => 'xxx',
151  '_value' => 'yyy',
152  ];
153 
154  self::assertSame($expected, $typeConverter->_call('transformMultiValueElementsForFormFramework', $input));
155  }
156 
157  #[Test]
159  {
160  $this->expectException(PropertyException::class);
161  $this->expectExceptionCode(1528538322);
162 
163  $sessionToken = '123';
164  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['retrieveSessionToken'], [], '', false);
165 
166  $typeConverter->method(
167  'retrieveSessionToken'
168  )->willReturn($sessionToken);
169 
170  $input = [
171  'prototypeName' => 'foo',
172  'identifier' => 'test',
173  '_orig_prototypeName' => [
174  'value' => 'standard',
175  'hmac' => $this->hashService->hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
176  ],
177  '_orig_identifier' => [
178  'value' => 'test',
179  'hmac' => $this->hashService->hmac(serialize(['test', 'identifier', 'test']), $sessionToken),
180  ],
181  ];
182 
183  $typeConverter->convertFrom(json_encode($input), FormDefinitionArray::class);
184  }
185 
186  #[Test]
188  {
189  $this->expectException(PropertyException::class);
190  $this->expectExceptionCode(1528538322);
191 
192  $sessionToken = '123';
193  $typeConverter = $this->getAccessibleMock(FormDefinitionArrayConverter::class, ['retrieveSessionToken'], [], '', false);
194 
195  $typeConverter->method(
196  'retrieveSessionToken'
197  )->willReturn($sessionToken);
198 
199  $input = [
200  'prototypeName' => 'standard',
201  'identifier' => 'xxx',
202  '_orig_prototypeName' => [
203  'value' => 'standard',
204  'hmac' => $this->hashService->hmac(serialize(['test', 'prototypeName', 'standard']), $sessionToken),
205  ],
206  '_orig_identifier' => [
207  'value' => 'test',
208  'hmac' => $this->hashService->hmac(serialize(['test', 'prototypeName', 'test']), $sessionToken),
209  ],
210  ];
211 
212  $typeConverter->convertFrom(json_encode($input), FormDefinitionArray::class);
213  }
214 }
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertsJsonStringToFormDefinitionArray
‪convertsJsonStringToFormDefinitionArray()
Definition: FormDefinitionArrayConverterTest.php:41
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\setUp
‪setUp()
Definition: FormDefinitionArrayConverterTest.php:33
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertFromThrowsExceptionIfIdentifierWasChanged
‪convertFromThrowsExceptionIfIdentifierWasChanged()
Definition: FormDefinitionArrayConverterTest.php:187
‪TYPO3\CMS\Form\Domain\Configuration\FormDefinitionValidationService
Definition: FormDefinitionValidationService.php:36
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\$hashService
‪HashService $hashService
Definition: FormDefinitionArrayConverterTest.php:31
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\transformMultiValueElementsForFormFrameworkTransformValues
‪transformMultiValueElementsForFormFrameworkTransformValues()
Definition: FormDefinitionArrayConverterTest.php:112
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter
Definition: FormDefinitionArrayConverterTest.php:18
‪TYPO3\CMS\Form\Type\FormDefinitionArray
Definition: FormDefinitionArray.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\convertFromThrowsExceptionIfJsonIsInvalid
‪convertFromThrowsExceptionIfJsonIsInvalid()
Definition: FormDefinitionArrayConverterTest.php:100
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest
Definition: FormDefinitionArrayConverterTest.php:29
‪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:158
‪TYPO3\CMS\Form\Domain\Configuration\Exception\PropertyException
Definition: PropertyException.php:25
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Form\Tests\Unit\Mvc\Property\TypeConverter\FormDefinitionArrayConverterTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FormDefinitionArrayConverterTest.php:30