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