[!!!][TASK] Move TYPO3_DB to extension
[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\Utility\GeneralUtility;
23
24 /**
25 * Test case
26 */
27 class DatabaseLanguageRowsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
28 {
29 /**
30 * @var DatabaseLanguageRows|\PHPUnit_Framework_MockObject_MockObject
31 */
32 protected $subject;
33
34 /**
35 * @var BackendUserAuthentication | ObjectProphecy
36 */
37 protected $beUserProphecy;
38
39 protected function setUp()
40 {
41 $this->beUserProphecy = $this->prophesize(BackendUserAuthentication::class);
42 $GLOBALS['BE_USER'] = $this->beUserProphecy;
43
44 $this->subject = $this->getMockBuilder(DatabaseLanguageRows::class)
45 ->setMethods(['getRecordWorkspaceOverlay'])
46 ->getMock();
47 }
48
49 /**
50 * @test
51 */
52 public function addDataReturnsUnchangedResultIfTableProvidesNoTranslations()
53 {
54 $input = [
55 'tableName' => 'tt_content',
56 'databaseRow' => [
57 'uid' => 42,
58 'text' => 'bar',
59 ],
60 'processedTca' => [
61 'ctrl' => [],
62 'columns' => [],
63 ],
64 ];
65 $this->assertEquals($input, $this->subject->addData($input));
66 }
67
68 /**
69 * @test
70 */
71 public function addDataThrowsExceptionIfDefaultOfLocalizedRecordIsNotFound()
72 {
73 $input = [
74 'tableName' => 'tt_content',
75 'databaseRow' => [
76 'uid' => 42,
77 'text' => 'localized text',
78 'sys_language_uid' => 2,
79 'l10n_parent' => 23,
80 ],
81 'processedTca' => [
82 'ctrl' => [
83 'languageField' => 'sys_language_uid',
84 'transOrigPointerField' => 'l10n_parent',
85 ],
86 ],
87 ];
88
89 $this->subject->expects($this->once())->method('getRecordWorkspaceOverlay')->willReturn([]);
90
91 $this->expectException(DatabaseDefaultLanguageException::class);
92 $this->expectExceptionCode(1438249426);
93
94 $this->subject->addData($input);
95 }
96
97 /**
98 * @test
99 */
100 public function addDataSetsDefaultLanguageRow()
101 {
102 $input = [
103 'tableName' => 'tt_content',
104 'databaseRow' => [
105 'uid' => 42,
106 'text' => 'localized text',
107 'sys_language_uid' => 2,
108 'l10n_parent' => 23,
109 ],
110 'processedTca' => [
111 'ctrl' => [
112 'languageField' => 'sys_language_uid',
113 'transOrigPointerField' => 'l10n_parent',
114 ],
115 ],
116 ];
117
118 $defaultLanguageRow = [
119 'uid' => 23,
120 'pid' => 123,
121 'text' => 'default language text',
122 'sys_language_uid' => 0,
123 ];
124
125 $this->subject->expects($this->once())->method('getRecordWorkspaceOverlay')->willReturn($defaultLanguageRow);
126
127 $expected = $input;
128 $expected['defaultLanguageRow'] = $defaultLanguageRow;
129
130 $this->assertEquals($expected, $this->subject->addData($input));
131 }
132
133 /**
134 * @test
135 */
136 public function addDataSetsDiffSourceFieldIfGiven()
137 {
138 $diffSource = [
139 'uid' => 42,
140 'text' => 'field content of default lang record when lang overlay was created',
141 ];
142
143 $input = [
144 'tableName' => 'tt_content',
145 'databaseRow' => [
146 'uid' => 42,
147 'text' => 'localized text',
148 'sys_language_uid' => 2,
149 'l10n_parent' => 23,
150 'l10n_diffsource' => serialize($diffSource),
151 ],
152 'processedTca' => [
153 'ctrl' => [
154 'languageField' => 'sys_language_uid',
155 'transOrigPointerField' => 'l10n_parent',
156 'transOrigDiffSourceField' => 'l10n_diffsource',
157 ],
158 ],
159 'defaultLanguageRow' => null,
160 ];
161
162 $defaultLanguageRow = [
163 'uid' => 23,
164 'pid' => 123,
165 'text' => 'default language text',
166 'sys_language_uid' => 0,
167 ];
168
169 $this->subject->expects($this->once())->method('getRecordWorkspaceOverlay')->willReturn($defaultLanguageRow);
170
171 $expected = $input;
172 $expected['defaultLanguageRow'] = $defaultLanguageRow;
173 $expected['defaultLanguageDiffRow']['tt_content:42'] = $diffSource;
174
175 $this->assertEquals($expected, $this->subject->addData($input));
176 }
177
178 /**
179 * @test
180 */
181 public function addDataSetsAdditionalLanguageRowsIfRequestedInUserTypoScript()
182 {
183 $input = [
184 'tableName' => 'tt_content',
185 'databaseRow' => [
186 'uid' => 42,
187 'text' => 'localized text',
188 'sys_language_uid' => 2,
189 'l10n_parent' => 23,
190 ],
191 'processedTca' => [
192 'ctrl' => [
193 'languageField' => 'sys_language_uid',
194 'transOrigPointerField' => 'l10n_parent',
195 ],
196 ],
197 'userTsConfig' => [
198 'options.' => [
199 'additionalPreviewLanguages' => '3',
200 ],
201 ],
202 'systemLanguageRows' => [
203 0 => [
204 'uid' => 0,
205 'title' => 'Default Language',
206 'iso' => 'DEV',
207 ],
208 3 => [
209 'uid' => 3,
210 'title' => 'french',
211 'iso' => 'fr',
212 ],
213 ],
214 'defaultLanguageRow' => null,
215 'additionalLanguageRows' => [],
216 ];
217
218 $translationResult = [
219 'translations' => [
220 3 => [
221 'uid' => 43,
222 'pid' => 32,
223 ],
224 ],
225 ];
226 // For BackendUtility::getRecord()
227 $GLOBALS['TCA']['tt_content'] = ['foo'];
228 $recordWsolResult = [
229 'uid' => 43,
230 'pid' => 32,
231 'text' => 'localized text in french',
232 ];
233
234 $defaultLanguageRow = [
235 'uid' => 23,
236 'pid' => 32,
237 'text' => 'default language text',
238 'sys_language_uid' => 0,
239 ];
240 $this->subject->expects($this->at(0))
241 ->method('getRecordWorkspaceOverlay')
242 ->with('tt_content', 23)
243 ->willReturn($defaultLanguageRow);
244
245 /** @var TranslationConfigurationProvider|ObjectProphecy $translationProphecy */
246 $translationProphecy = $this->prophesize(TranslationConfigurationProvider::class);
247 GeneralUtility::addInstance(TranslationConfigurationProvider::class, $translationProphecy->reveal());
248 $translationProphecy->translationInfo('tt_content', 23, 3)->shouldBeCalled()->willReturn($translationResult);
249
250 // This is the real check: The "additional overlay" should be fetched
251 $this->subject->expects($this->at(1))
252 ->method('getRecordWorkspaceOverlay')
253 ->with('tt_content', 43)
254 ->willReturn($recordWsolResult);
255
256 $expected = $input;
257 $expected['defaultLanguageRow'] = $defaultLanguageRow;
258 $expected['additionalLanguageRows'] = [
259 3 => [
260 'uid' => 43,
261 'pid' => 32,
262 'text' => 'localized text in french',
263 ],
264 ];
265
266 $this->assertEquals($expected, $this->subject->addData($input));
267 }
268
269 /**
270 * @test
271 */
272 public function addDataSetsDoesNotAddHandledRowAsAdditionalLanguageRows()
273 {
274 $input = [
275 'tableName' => 'tt_content',
276 'databaseRow' => [
277 'uid' => 42,
278 'text' => 'localized text',
279 'sys_language_uid' => 2,
280 'l10n_parent' => 23,
281 ],
282 'processedTca' => [
283 'ctrl' => [
284 'languageField' => 'sys_language_uid',
285 'transOrigPointerField' => 'l10n_parent',
286 ],
287 ],
288 'userTsConfig' => [
289 'options.' => [
290 'additionalPreviewLanguages' => '2,3',
291 ],
292 ],
293 'systemLanguageRows' => [
294 0 => [
295 'uid' => 0,
296 'title' => 'Default Language',
297 'iso' => 'DEV',
298 ],
299 2 => [
300 'uid' => 2,
301 'title' => 'dansk',
302 'iso' => 'dk,'
303 ],
304 3 => [
305 'uid' => 3,
306 'title' => 'french',
307 'iso' => 'fr',
308 ],
309 ],
310 'defaultLanguageRow' => null,
311 'additionalLanguageRows' => [],
312 ];
313
314 $translationResult = [
315 'translations' => [
316 3 => [
317 'uid' => 43,
318 ],
319 ],
320 ];
321 // For BackendUtility::getRecord()
322 $GLOBALS['TCA']['tt_content'] = ['foo'];
323 $recordWsolResult = [
324 'uid' => 43,
325 'pid' => 32,
326 'text' => 'localized text in french',
327 ];
328
329 $defaultLanguageRow = [
330 'uid' => 23,
331 'pid' => 32,
332 'text' => 'default language text',
333 'sys_language_uid' => 0,
334 ];
335 $this->subject->expects($this->at(0))
336 ->method('getRecordWorkspaceOverlay')
337 ->with('tt_content', 23)
338 ->willReturn($defaultLanguageRow);
339
340 /** @var TranslationConfigurationProvider|ObjectProphecy $translationProphecy */
341 $translationProphecy = $this->prophesize(TranslationConfigurationProvider::class);
342 GeneralUtility::addInstance(TranslationConfigurationProvider::class, $translationProphecy->reveal());
343 $translationProphecy->translationInfo('tt_content', 23, 3)->shouldBeCalled()->willReturn($translationResult);
344 $translationProphecy->translationInfo('tt_content', 23, 2)->shouldNotBeCalled();
345
346 // This is the real check: The "additional overlay" should be fetched
347 $this->subject->expects($this->at(1))
348 ->method('getRecordWorkspaceOverlay')
349 ->with('tt_content', 43)
350 ->willReturn($recordWsolResult);
351
352 $expected = $input;
353 $expected['defaultLanguageRow'] = $defaultLanguageRow;
354 $expected['additionalLanguageRows'] = [
355 3 => [
356 'uid' => 43,
357 'pid' => 32,
358 'text' => 'localized text in french',
359 ],
360 ];
361
362 $this->assertEquals($expected, $this->subject->addData($input));
363 }
364
365 /**
366 * @test
367 */
368 public function addDataSetsSourceLanguageRow()
369 {
370 $input = [
371 'tableName' => 'tt_content',
372 'databaseRow' => [
373 'uid' => 42,
374 'text' => 'localized text',
375 'sys_language_uid' => 3,
376 'l10n_parent' => 23,
377 'l10n_source' => 24,
378 ],
379 'processedTca' => [
380 'ctrl' => [
381 'languageField' => 'sys_language_uid',
382 'transOrigPointerField' => 'l10n_parent',
383 'translationSource' => 'l10n_source',
384 ],
385 ],
386 'systemLanguageRows' => [
387 0 => [
388 'uid' => 0,
389 'title' => 'Default Language',
390 'iso' => 'DEV',
391 ],
392 2 => [
393 'uid' => 2,
394 'title' => 'dansk',
395 'iso' => 'dk,'
396 ],
397 3 => [
398 'uid' => 3,
399 'title' => 'french',
400 'iso' => 'fr',
401 ],
402 ],
403 'defaultLanguageRow' => null,
404 'sourceLanguageRow' => null,
405 'additionalLanguageRows' => [],
406 ];
407
408 // For BackendUtility::getRecord()
409 $GLOBALS['TCA']['tt_content'] = ['foo'];
410 $sourceLanguageRow = [
411 'uid' => 24,
412 'pid' => 32,
413 'text' => 'localized text in dank',
414 'sys_language_uid' => 2,
415 ];
416 $defaultLanguageRow = [
417 'uid' => 23,
418 'pid' => 32,
419 'text' => 'default language text',
420 'sys_language_uid' => 0,
421 ];
422 $this->subject->expects($this->at(0))
423 ->method('getRecordWorkspaceOverlay')
424 ->with('tt_content', 23)
425 ->willReturn($defaultLanguageRow);
426 $this->subject->expects($this->at(1))
427 ->method('getRecordWorkspaceOverlay')
428 ->with('tt_content', 24)
429 ->willReturn($sourceLanguageRow);
430
431 $expected = $input;
432 $expected['defaultLanguageRow'] = $defaultLanguageRow;
433 $expected['sourceLanguageRow'] = $sourceLanguageRow;
434
435 $this->assertEquals($expected, $this->subject->addData($input));
436 }
437 }