[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Hooks / DataStructureIdentifierHookTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Hooks;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Object\ObjectManager;
19 use TYPO3\CMS\Form\Hooks\DataStructureIdentifierHook;
20 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
21 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface;
22
23 /**
24 * Test case
25 */
26 class DataStructureIdentifierHookTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
27 {
28 /**
29 * @var array A backup of registered singleton instances
30 */
31 protected $singletonInstances = [];
32
33 /**
34 * Set up
35 */
36 public function setUp()
37 {
38 $this->singletonInstances = GeneralUtility::getSingletonInstances();
39 }
40
41 /**
42 * Tear down
43 */
44 public function tearDown()
45 {
46 GeneralUtility::resetSingletonInstances($this->singletonInstances);
47 parent::tearDown();
48 }
49
50 /**
51 * @test
52 */
53 public function getDataStructureIdentifierPostProcessReturnsIdentifierForNotMatchingScenario()
54 {
55 $givenIdentifier = ['aKey' => 'aValue'];
56 $result = (new DataStructureIdentifierHook())->getDataStructureIdentifierPostProcess(
57 [], 'aTable', 'aField', [], $givenIdentifier
58 );
59 $this->assertEquals($givenIdentifier, $result);
60 }
61
62 /**
63 * @test
64 */
65 public function getDataStructureIdentifierPostProcessAddDefaultValuesForNewRecord()
66 {
67 $result = (new DataStructureIdentifierHook())->getDataStructureIdentifierPostProcess(
68 [], 'tt_content', 'pi_flexform', ['CType' => 'form_formframework'], []
69 );
70 $this->assertEquals(
71 ['ext-form-persistenceIdentifier' => '', 'ext-form-overrideFinishers' => false],
72 $result
73 );
74 }
75
76 /**
77 * @test
78 */
79 public function getDataStructureIdentifierPostProcessAddsGivenPersistenceIdentifier()
80 {
81 $row = [
82 'CType' => 'form_formframework',
83 'pi_flexform' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
84 <T3FlexForms>
85 <data>
86 <sheet index="sDEF">
87 <language index="lDEF">
88 <field index="settings.persistenceIdentifier">
89 <value index="vDEF">1:user_upload/karl.yml</value>
90 </field>
91 </language>
92 </sheet>
93 </data>
94 </T3FlexForms>
95 ',
96 ];
97 $incomingIdentifier = [
98 'aKey' => 'aValue',
99 ];
100 $expected = [
101 'aKey' => 'aValue',
102 'ext-form-persistenceIdentifier' => '1:user_upload/karl.yml',
103 'ext-form-overrideFinishers' => false,
104 ];
105 $result = (new DataStructureIdentifierHook())->getDataStructureIdentifierPostProcess(
106 [], 'tt_content', 'pi_flexform', $row, $incomingIdentifier
107 );
108 $this->assertEquals($expected, $result);
109 }
110
111 /**
112 * @test
113 */
114 public function getDataStructureIdentifierPostProcessAddsOverrideFinisherValue()
115 {
116 $row = [
117 'CType' => 'form_formframework',
118 'pi_flexform' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
119 <T3FlexForms>
120 <data>
121 <sheet index="sDEF">
122 <language index="lDEF">
123 <field index="settings.overrideFinishers">
124 <value index="vDEF">1</value>
125 </field>
126 </language>
127 </sheet>
128 </data>
129 </T3FlexForms>
130 ',
131 ];
132 $expected = [
133 'ext-form-persistenceIdentifier' => '',
134 'ext-form-overrideFinishers' => true,
135 ];
136 $result = (new DataStructureIdentifierHook())->getDataStructureIdentifierPostProcess(
137 [], 'tt_content', 'pi_flexform', $row, []
138 );
139 $this->assertEquals($expected, $result);
140 }
141
142 /**
143 * @test
144 */
145 public function parseDataStructureByIdentifierPostProcessReturnsDataStructureUnchanged()
146 {
147 $dataStructure = ['foo' => 'bar'];
148 $expected = $dataStructure;
149 $result = (new DataStructureIdentifierHook())->parseDataStructureByIdentifierPostProcess(
150 $dataStructure, []
151 );
152 $this->assertEquals($expected, $result);
153 }
154
155 /**
156 * @test
157 */
158 public function parseDataStructureByIdentifierPostProcessAddsExistingFormItems()
159 {
160 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
161 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
162 $formPersistenceManagerProphecy = $this->prophesize(FormPersistenceManager::class);
163 $objectMangerProphecy->get(FormPersistenceManagerInterface::class)
164 ->willReturn($formPersistenceManagerProphecy->reveal());
165
166 $existingForms = [
167 [
168 'persistenceIdentifier' => 'hugo1',
169 'name' => 'myHugo1',
170 ],
171 [
172 'persistenceIdentifier' => 'hugo2',
173 'name' => 'myHugo2',
174 ]
175 ];
176 $formPersistenceManagerProphecy->listForms()->shouldBeCalled()->willReturn($existingForms);
177
178 $incomingDataStructure = [
179 'sheets' => [
180 'sDEF' => [
181 'ROOT' => [
182 'el' => [
183 'settings.persistenceIdentifier' => [
184 'TCEforms' => [
185 'config' => [
186 'items' => [
187 0 => [
188 0 => 'default, no value',
189 1 => '',
190 ],
191 ],
192 ],
193 ],
194 ],
195 ],
196 ],
197 ],
198 ],
199 ];
200
201 $expected = [
202 'sheets' => [
203 'sDEF' => [
204 'ROOT' => [
205 'el' => [
206 'settings.persistenceIdentifier' => [
207 'TCEforms' => [
208 'config' => [
209 'items' => [
210 0 => [
211 0 => 'default, no value',
212 1 => '',
213 ],
214 1 => [
215 0 => 'myHugo1 (hugo1)',
216 1 => 'hugo1',
217 ],
218 2 => [
219 0 => 'myHugo2 (hugo2)',
220 1 => 'hugo2',
221 ],
222 ],
223 ],
224 ],
225 ],
226 ],
227 ],
228 ],
229 ],
230 ];
231
232 $result = (new DataStructureIdentifierHook())->parseDataStructureByIdentifierPostProcess(
233 $incomingDataStructure,
234 ['ext-form-persistenceIdentifier' => '']
235 );
236
237 $this->assertEquals($expected, $result);
238 }
239 }