[BUGFIX] Localization State does not properly sanitize and enrich
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / DataHandling / Localization / StateTest.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Tests\Unit\DataHandler\Localization;
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
18 use TYPO3\CMS\Core\DataHandling\Localization\State;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 /**
22 * Test case
23 */
24 class StateTest extends UnitTestCase
25 {
26 const TABLE_NAME = 'tx_test_table';
27
28 /**
29 * Set up the tests
30 */
31 protected function setUp()
32 {
33 $GLOBALS['TCA'] = [];
34 }
35
36 /**
37 * @param string $tableName
38 * @param array $states
39 *
40 * @test
41 * @dataProvider stateObjectCanBeCreatedDataProvider
42 */
43 public function stateObjectCanBeCreated(string $tableName, array $states)
44 {
45 $subject = new State($tableName, $states);
46
47 $this->assertInstanceOf(State::class, $subject);
48 }
49
50 /**
51 * @return array
52 */
53 public function stateObjectCanBeCreatedDataProvider(): array
54 {
55 return [
56 'without states' => [
57 static::TABLE_NAME,
58 [],
59 ],
60 'with states' => [
61 static::TABLE_NAME,
62 ['nonExistingField' => 'invalidState'],
63 ],
64 ];
65 }
66
67 /**
68 * @param array $states
69 * @param array $expected
70 *
71 * @test
72 * @dataProvider statesAreEnrichedAndSanitizedOnObjectCreationDataProvider
73 */
74 public function statesAreEnrichedAndSanitizedOnObjectCreation(
75 array $states,
76 array $expected
77 ) {
78 $GLOBALS['TCA'] = $this->provideTableConfiguration(
79 'first_field',
80 'second_field'
81 );
82
83 $subject = new State(static::TABLE_NAME, $states);
84
85 $this->assertSame(
86 $expected,
87 $subject->toArray()
88 );
89 }
90
91 /**
92 * @return array
93 */
94 public function statesAreEnrichedAndSanitizedOnObjectCreationDataProvider(): array
95 {
96 return [
97 'empty' => [
98 [],
99 [
100 'first_field' => 'parent',
101 'second_field' => 'parent',
102 ],
103 ],
104 'invalid field only' => [
105 [
106 'invalid_field' => 'invalidState',
107 ],
108 [
109 'first_field' => 'parent',
110 'second_field' => 'parent',
111 ],
112 ],
113 'first_field only, valid state' => [
114 [
115 'first_field' => 'custom',
116 ],
117 [
118 'first_field' => 'custom',
119 'second_field' => 'parent',
120 ],
121 ],
122 'first_field only, invalid state' => [
123 [
124 'first_field' => 'invalidState',
125 ],
126 [
127 'first_field' => 'parent',
128 'second_field' => 'parent',
129 ],
130 ],
131 'all valid fields, valid states' => [
132 [
133 'first_field' => 'custom',
134 'second_field' => 'parent',
135 ],
136 [
137 'first_field' => 'custom',
138 'second_field' => 'parent',
139 ],
140 ],
141 'all valid fields, invalid states' => [
142 [
143 'first_field' => 'invalidState',
144 'second_field' => 'invalidState',
145 ],
146 [
147 'first_field' => 'parent',
148 'second_field' => 'parent',
149 ],
150 ],
151 'all valid fields, valid states and invalid field' => [
152 [
153 'invalid_field' => 'invalidState',
154 'first_field' => 'custom',
155 'second_field' => 'parent',
156 ],
157 [
158 'first_field' => 'custom',
159 'second_field' => 'parent',
160 ],
161 ],
162 'all valid fields, invalid states and invalid field' => [
163 [
164 'invalid_field' => 'invalidState',
165 'first_field' => 'invalidState',
166 'second_field' => 'invalidState',
167 ],
168 [
169 'first_field' => 'parent',
170 'second_field' => 'parent',
171 ],
172 ],
173 ];
174 }
175
176 /**
177 * @param string[] ...$fieldNames
178 *
179 * @return array
180 */
181 private function provideTableConfiguration(string ...$fieldNames): array
182 {
183 $columnsConfiguration = [];
184 foreach ($fieldNames as $fieldName) {
185 $columnsConfiguration[$fieldName]['config']['behaviour']['allowLanguageSynchronization'] = true;
186 }
187 return [
188 static::TABLE_NAME => [
189 'columns' => $columnsConfiguration,
190 ],
191 ];
192 }
193 }