[BUGFIX] Show correct label for IRRE records
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseLanguageRowsTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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 Prophecy\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
19 use TYPO3\CMS\Backend\Form\Exception\DatabaseDefaultLanguageException;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseLanguageRows;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Database\DatabaseConnection;
23 use TYPO3\CMS\Core\Tests\UnitTestCase;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Test case
28 */
29 class DatabaseLanguageRowsTest extends UnitTestCase
30 {
31 /**
32 * @var DatabaseLanguageRows
33 */
34 protected $subject;
35
36 /**
37 * @var DatabaseConnection | ObjectProphecy
38 */
39 protected $dbProphecy;
40
41 /**
42 * @var BackendUserAuthentication | ObjectProphecy
43 */
44 protected $beUserProphecy;
45
46 protected function setUp()
47 {
48 $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
49 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
50
51 $this->beUserProphecy = $this->prophesize(BackendUserAuthentication::class);
52 $GLOBALS['BE_USER'] = $this->beUserProphecy;
53
54 $this->subject = new DatabaseLanguageRows();
55 }
56
57 /**
58 * @test
59 */
60 public function addDataReturnsUnchangedResultIfTableProvidesNoTranslations()
61 {
62 $input = [
63 'tableName' => 'tt_content',
64 'databaseRow' => [
65 'uid' => 42,
66 'text' => 'bar',
67 ],
68 'processedTca' => [
69 'ctrl' => array(),
70 'columns' => array(),
71 ],
72 ];
73 $this->assertEquals($input, $this->subject->addData($input));
74 }
75
76 /**
77 * @test
78 */
79 public function addDataThrowsExceptionIfDefaultOfLocalizedRecordIsNotFound()
80 {
81 $input = [
82 'tableName' => 'tt_content',
83 'databaseRow' => [
84 'uid' => 42,
85 'text' => 'localized text',
86 'sys_language_uid' => 2,
87 'l10n_parent' => 23,
88 ],
89 'processedTca' => [
90 'ctrl' => array(
91 'languageField' => 'sys_language_uid',
92 'transOrigPointerField' => 'l10n_parent',
93 ),
94 ],
95 ];
96
97 // Needed for BackendUtility::getRecord
98 $GLOBALS['TCA']['tt_content'] = array('foo');
99 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=23')->shouldBeCalled()->willReturn(null);
100
101 $this->setExpectedException(DatabaseDefaultLanguageException::class, $this->anything(), 1438249426);
102
103 $this->subject->addData($input);
104 }
105
106 /**
107 * @test
108 */
109 public function addDataSetsDefaultLanguageRow()
110 {
111 $input = [
112 'tableName' => 'tt_content',
113 'databaseRow' => [
114 'uid' => 42,
115 'text' => 'localized text',
116 'sys_language_uid' => 2,
117 'l10n_parent' => 23,
118 ],
119 'processedTca' => [
120 'ctrl' => array(
121 'languageField' => 'sys_language_uid',
122 'transOrigPointerField' => 'l10n_parent',
123 ),
124 ],
125 ];
126
127 $defaultLanguageRow = [
128 'uid' => 23,
129 'pid' => 123,
130 'text' => 'default language text',
131 'sys_language_uid' => 0,
132 ];
133 // Needed for BackendUtility::getRecord
134 $GLOBALS['TCA']['tt_content'] = array('foo');
135 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=23')->shouldBeCalled()->willReturn($defaultLanguageRow);
136
137 $expected = $input;
138 $expected['defaultLanguageRow'] = $defaultLanguageRow;
139
140 $this->assertEquals($expected, $this->subject->addData($input));
141 }
142
143 /**
144 * @test
145 */
146 public function addDataSetsDiffSourceFieldIfGiven()
147 {
148 $diffSource = [
149 'uid' => 42,
150 'text' => 'field content of default lang record when lang overlay was created',
151 ];
152
153 $input = [
154 'tableName' => 'tt_content',
155 'databaseRow' => [
156 'uid' => 42,
157 'text' => 'localized text',
158 'sys_language_uid' => 2,
159 'l10n_parent' => 23,
160 'l10n_diffsource' => serialize($diffSource),
161 ],
162 'processedTca' => [
163 'ctrl' => [
164 'languageField' => 'sys_language_uid',
165 'transOrigPointerField' => 'l10n_parent',
166 'transOrigDiffSourceField' => 'l10n_diffsource',
167 ],
168 ],
169 'defaultLanguageRow' => null,
170 ];
171
172 $defaultLanguageRow = [
173 'uid' => 23,
174 'pid' => 123,
175 'text' => 'default language text',
176 'sys_language_uid' => 0,
177 ];
178 // Needed for BackendUtility::getRecord
179 $GLOBALS['TCA']['tt_content'] = array('foo');
180 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=23')->shouldBeCalled()->willReturn($defaultLanguageRow);
181
182 $expected = $input;
183 $expected['defaultLanguageRow'] = $defaultLanguageRow;
184 $expected['defaultLanguageDiffRow'] = $diffSource;
185
186 $this->assertEquals($expected, $this->subject->addData($input));
187 }
188
189 /**
190 * @test
191 */
192 public function addDataSetsAdditionalLanguageRowsIfRequestedInUserTypoScript()
193 {
194 $input = [
195 'tableName' => 'tt_content',
196 'databaseRow' => [
197 'uid' => 42,
198 'text' => 'localized text',
199 'sys_language_uid' => 2,
200 'l10n_parent' => 23,
201 ],
202 'processedTca' => [
203 'ctrl' => [
204 'languageField' => 'sys_language_uid',
205 'transOrigPointerField' => 'l10n_parent',
206 ],
207 ],
208 'userTsConfig' => [
209 'options.' => [
210 'additionalPreviewLanguages' => '3',
211 ],
212 ],
213 'systemLanguageRows' => [
214 0 => [
215 'uid' => 0,
216 'title' => 'Default Language',
217 'iso' => 'DEV',
218 ],
219 3 => [
220 'uid' => 3,
221 'title' => 'french',
222 'iso' => 'fr',
223 ],
224 ],
225 'defaultLanguageRow' => null,
226 'additionalLanguageRows' => [],
227 ];
228
229 $translationResult = [
230 'translations' => [
231 3 => [
232 'uid' => 43,
233 'pid' => 32,
234 ],
235 ],
236 ];
237 // For BackendUtility::getRecord()
238 $GLOBALS['TCA']['tt_content'] = array('foo');
239 $recordWsolResult = [
240 'uid' => 43,
241 'pid' => 32,
242 'text' => 'localized text in french',
243 ];
244
245 $defaultLanguageRow = [
246 'uid' => 23,
247 'pid' => 32,
248 'text' => 'default language text',
249 'sys_language_uid' => 0,
250 ];
251 // Needed for BackendUtility::getRecord
252 $GLOBALS['TCA']['tt_content'] = array('foo');
253 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=23')->shouldBeCalled()->willReturn($defaultLanguageRow);
254
255 /** @var TranslationConfigurationProvider|ObjectProphecy $translationProphecy */
256 $translationProphecy = $this->prophesize(TranslationConfigurationProvider::class);
257 GeneralUtility::addInstance(TranslationConfigurationProvider::class, $translationProphecy->reveal());
258 $translationProphecy->translationInfo('tt_content', 23, 3)->shouldBeCalled()->willReturn($translationResult);
259
260 // This is the real check: The "additional overlay" should be fetched
261 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=43')->shouldBeCalled()->willReturn($recordWsolResult);
262
263 $expected = $input;
264 $expected['defaultLanguageRow'] = $defaultLanguageRow;
265 $expected['additionalLanguageRows'] = [
266 3 => [
267 'uid' => 43,
268 'pid' => 32,
269 'text' => 'localized text in french',
270 ],
271 ];
272
273 $this->assertEquals($expected, $this->subject->addData($input));
274 }
275
276 /**
277 * @test
278 */
279 public function addDataSetsDoesNotAddHandledRowAsAdditionalLanguageRows()
280 {
281 $input = [
282 'tableName' => 'tt_content',
283 'databaseRow' => [
284 'uid' => 42,
285 'text' => 'localized text',
286 'sys_language_uid' => 2,
287 'l10n_parent' => 23,
288 ],
289 'processedTca' => [
290 'ctrl' => [
291 'languageField' => 'sys_language_uid',
292 'transOrigPointerField' => 'l10n_parent',
293 ],
294 ],
295 'userTsConfig' => [
296 'options.' => [
297 'additionalPreviewLanguages' => '2,3',
298 ],
299 ],
300 'systemLanguageRows' => [
301 0 => [
302 'uid' => 0,
303 'title' => 'Default Language',
304 'iso' => 'DEV',
305 ],
306 2 => [
307 'uid' => 2,
308 'title' => 'dansk',
309 'iso' => 'dk,'
310 ],
311 3 => [
312 'uid' => 3,
313 'title' => 'french',
314 'iso' => 'fr',
315 ],
316 ],
317 'defaultLanguageRow' => null,
318 'additionalLanguageRows' => [],
319 ];
320
321 $translationResult = [
322 'translations' => [
323 3 => [
324 'uid' => 43,
325 ],
326 ],
327 ];
328 // For BackendUtility::getRecord()
329 $GLOBALS['TCA']['tt_content'] = array('foo');
330 $recordWsolResult = [
331 'uid' => 43,
332 'pid' => 32,
333 'text' => 'localized text in french',
334 ];
335
336 $defaultLanguageRow = [
337 'uid' => 23,
338 'pid' => 32,
339 'text' => 'default language text',
340 'sys_language_uid' => 0,
341 ];
342 // Needed for BackendUtility::getRecord
343 $GLOBALS['TCA']['tt_content'] = array('foo');
344 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=23')->shouldBeCalled()->willReturn($defaultLanguageRow);
345
346 /** @var TranslationConfigurationProvider|ObjectProphecy $translationProphecy */
347 $translationProphecy = $this->prophesize(TranslationConfigurationProvider::class);
348 GeneralUtility::addInstance(TranslationConfigurationProvider::class, $translationProphecy->reveal());
349 $translationProphecy->translationInfo('tt_content', 23, 3)->shouldBeCalled()->willReturn($translationResult);
350 $translationProphecy->translationInfo('tt_content', 23, 2)->shouldNotBeCalled();
351
352 // This is the real check: The "additional overlay" should be fetched
353 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=43')->shouldBeCalled()->willReturn($recordWsolResult);
354
355 $expected = $input;
356 $expected['defaultLanguageRow'] = $defaultLanguageRow;
357 $expected['additionalLanguageRows'] = [
358 3 => [
359 'uid' => 43,
360 'pid' => 32,
361 'text' => 'localized text in french',
362 ],
363 ];
364
365 $this->assertEquals($expected, $this->subject->addData($input));
366 }
367 }