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