‪TYPO3CMS  ‪main
LocalizationControllerTest.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;
26 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
27 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
28 
29 final class ‪LocalizationControllerTest extends FunctionalTestCase
30 {
32 
33  protected array ‪$coreExtensionsToLoad = ['workspaces'];
34 
35  protected const ‪LANGUAGE_PRESETS = [
36  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
37  'DA' => ['id' => 1, 'title' => 'Dansk', 'locale' => 'da_DK.UTF8'],
38  'DE' => ['id' => 2, 'title' => 'Deutsch', 'locale' => 'de_DE.UTF-8'],
39  ];
40 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
44  $this->importCSVDataSet(__DIR__ . '/Fixtures/be_users.csv');
45  $this->importCSVDataSet(__DIR__ . '/Fixtures/pages.csv');
46  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-default-language.csv');
48  'test',
49  $this->‪buildSiteConfiguration(1),
50  [
51  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
52  $this->‪buildLanguageConfiguration('DA', '/da/'),
53  $this->‪buildLanguageConfiguration('DE', '/de'),
54  ]
55  );
56  }
57 
58  #[Test]
59  public function ‪recordsGetTranslatedFromDefaultLanguage(): void
60  {
61  $backendUser = $this->setUpBackendUser(1);
62  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
63  $params = [
64  'pageId' => 1,
65  'srcLanguageId' => 0,
66  'destLanguageId' => 1,
67  'uidList' => [1, 2, 3],
69  ];
70  $subject = $this->getAccessibleMock(LocalizationController::class, null);
71  $subject->_call('process', $params);
72  $this->assertCSVDataSet(__DIR__ . '/Localization/CSV/DataSet/TranslatedFromDefault.csv');
73  }
74 
75  #[Test]
77  {
78  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-danish-language.csv');
79  $backendUser = $this->setUpBackendUser(1);
80  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
81  $params = [
82  'pageId' => 1,
83  'srcLanguageId' => 1,
84  'destLanguageId' => 2,
85  'uidList' => [4, 5, 6], // uids of tt_content-danish-language
87  ];
88  $subject = $this->getAccessibleMock(LocalizationController::class, null);
89  $subject->_call('process', $params);
90  $this->assertCSVDataSet(__DIR__ . '/Localization/CSV/DataSet/TranslatedFromTranslation.csv');
91  }
92 
93  #[Test]
94  public function ‪recordsGetCopiedFromDefaultLanguage(): void
95  {
96  $backendUser = $this->setUpBackendUser(1);
97  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
98  $params = [
99  'pageId' => 1,
100  'srcLanguageId' => 0,
101  'destLanguageId' => 2,
102  'uidList' => [1, 2, 3],
104  ];
105  $subject = $this->getAccessibleMock(LocalizationController::class, null);
106  $subject->_call('process', $params);
107  $this->assertCSVDataSet(__DIR__ . '/Localization/CSV/DataSet/CopiedFromDefault.csv');
108  }
109 
110  #[Test]
111  public function ‪recordsGetCopiedFromAnotherLanguage(): void
112  {
113  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-danish-language.csv');
114  $backendUser = $this->setUpBackendUser(1);
115  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
116  $params = [
117  'pageId' => 1,
118  'srcLanguageId' => 1,
119  'destLanguageId' => 2,
120  'uidList' => [4, 5, 6], // uids of tt_content-danish-language
122  ];
123  $subject = $this->getAccessibleMock(LocalizationController::class, null);
124  $subject->_call('process', $params);
125  $this->assertCSVDataSet(__DIR__ . '/Localization/CSV/DataSet/CopiedFromTranslation.csv');
126  }
127 
138  #[Test]
140  {
141  $backendUser = $this->setUpBackendUser(1);
142  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
143  $params = [
144  'pageId' => 1,
145  'srcLanguageId' => 0,
146  'destLanguageId' => 1,
147  'uidList' => [1, 2, 3],
149  ];
150  $subject = $this->getAccessibleMock(LocalizationController::class, null);
151  $subject->_call('process', $params);
152  // Create another content element in default language
153  $data = [
154  'tt_content' => [
155  'NEW123456' => [
156  'sys_language_uid' => 0,
157  'header' => 'Test content 2.5',
158  'pid' => -2,
159  'colPos' => 0,
160  ],
161  ],
162  ];
163  $dataHandler = new ‪DataHandler();
164  $dataHandler->start($data, []);
165  $dataHandler->process_datamap();
166  $dataHandler->process_cmdmap();
167  $newContentElementUid = $dataHandler->substNEWwithIDs['NEW123456'];
168  // Copy the new content element
169  $params = [
170  'pageId' => 1,
171  'srcLanguageId' => 0,
172  'destLanguageId' => 1,
173  'uidList' => [$newContentElementUid],
175  ];
176  $subject->_call('process', $params);
177  $this->assertCSVDataSet(__DIR__ . '/Localization/CSV/DataSet/CreatedElementOrdering.csv');
178  }
179 
180  #[Test]
181  public function ‪defaultLanguageIsFoundAsOriginLanguage(): void
182  {
183  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-danish-language.csv');
184  $backendUser = $this->setUpBackendUser(1);
185  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
186  // Create another content element in default language
187  $data = [
188  'tt_content' => [
189  'NEW123456' => [
190  'sys_language_uid' => 0,
191  'header' => 'New content element',
192  'pid' => 1,
193  'colPos' => 0,
194  ],
195  ],
196  ];
197  $dataHandler = new ‪DataHandler();
198  $dataHandler->start($data, []);
199  $dataHandler->process_datamap();
200  $dataHandler->process_cmdmap();
201  $request = (new ‪ServerRequest())->withQueryParams([
202  'pageId' => 1, // page uid, the records are stored on
203  'languageId' => 1, // current language id
204  ]);
205  $subject = $this->getAccessibleMock(LocalizationController::class, null);
206  $usedLanguages = (string)$subject->getUsedLanguagesInPage($request)->getBody();
207  self::assertThat($usedLanguages, self::stringContains('"uid":0'));
208  }
209  #[Test]
211  {
212  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-default-language-deleted-element.csv');
213  $this->importCSVDataSet(__DIR__ . '/Fixtures/tt_content-danish-language-deleted-source.csv');
214  $backendUser = $this->setUpBackendUser(1);
215  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
216  $request = (new ‪ServerRequest())->withQueryParams([
217  'pageId' => 2, // page uid, the records are stored on
218  'languageId' => 1, // current language id
219  ]);
220  $subject = $this->getAccessibleMock(LocalizationController::class, null);
221  $usedLanguages = (string)$subject->getUsedLanguagesInPage($request)->getBody();
222  self::assertThat($usedLanguages, self::stringContains('"uid":0'));
223  }
224 
225  #[Test]
227  {
228  // Delete record 2 within workspace 1
229  $backendUser = $this->setUpBackendUser(1);
230  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
231  $backendUser->workspace = 1;
232  $actionService = new ActionService();
233  $actionService->deleteRecord('tt_content', 2);
234  $expectedRecords = [
235  '0' => [
236  ['uid' => 1],
237  ],
238  '1' => [
239  ['uid' => 3],
240  ],
241  ];
242  $request = (new ‪ServerRequest())->withQueryParams([
243  'pageId' => 1, // page uid, the records are stored on
244  'destLanguageId' => 1, // destination language uid
245  'languageId' => 0, // source language uid
246  ]);
247  $subject = $this->getAccessibleMock(LocalizationController::class, ['getPageColumns']);
248  $subject->method('getPageColumns')->willReturn([
249  0 => 'Column 0',
250  1 => 'Column 1',
251  ]);
252  $recordLocalizeSummaryResponse = $subject->getRecordLocalizeSummary($request);
253  // Reduce the fetched record summary to list of uids
254  if ($recordLocalizeSummary = json_decode((string)$recordLocalizeSummaryResponse->getBody(), true)) {
255  foreach ($recordLocalizeSummary['records'] as $colPos => $records) {
256  foreach ($records as $key => ‪$record) {
257  $recordLocalizeSummary['records'][$colPos][$key] = array_intersect_key(‪$record, ['uid' => '']);
258  }
259  }
260  }
261  $localizeSummary = $recordLocalizeSummary['records'];
262  self::assertEquals($expectedRecords, $localizeSummary);
263  }
264 
265  #[Test]
267  {
268  // Move record 2 to page 2 within workspace 1
269  $backendUser = $this->setUpBackendUser(1);
270  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
271  $backendUser->workspace = 1;
272  $actionService = new ActionService();
273  $actionService->moveRecord('tt_content', 2, 2);
274  $expectedRecords = [
275  '0' => [
276  ['uid' => 1],
277  ],
278  '1' => [
279  ['uid' => 3],
280  ],
281  ];
282  $request = (new ‪ServerRequest())->withQueryParams([
283  'pageId' => 1, // page uid, the records are stored on
284  'destLanguageId' => 1, // destination language uid
285  'languageId' => 0, // source language uid
286  ]);
287  $subject = $this->getAccessibleMock(LocalizationController::class, ['getPageColumns']);
288  $subject->method('getPageColumns')->willReturn([
289  0 => 'Column 0',
290  1 => 'Column 1',
291  ]);
292  $recordLocalizeSummaryResponse = $subject->getRecordLocalizeSummary($request);
293  // Reduce the fetched record summary to list of uids
294  if ($recordLocalizeSummary = json_decode((string)$recordLocalizeSummaryResponse->getBody(), true)) {
295  foreach ($recordLocalizeSummary['records'] as $colPos => $records) {
296  foreach ($records as $key => ‪$record) {
297  $recordLocalizeSummary['records'][$colPos][$key] = array_intersect_key(‪$record, ['uid' => '']);
298  }
299  }
300  }
301  $localizeSummary = $recordLocalizeSummary['records'];
302  self::assertEquals($expectedRecords, $localizeSummary);
303  }
304 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:108
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\setUp
‪setUp()
Definition: LocalizationControllerTest.php:40
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: LocalizationControllerTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\copyingNewContentFromLanguageIntoExistingLocalizationHasSameOrdering
‪copyingNewContentFromLanguageIntoExistingLocalizationHasSameOrdering()
Definition: LocalizationControllerTest.php:138
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordLocalizeSummaryRespectsWorkspaceEncapsulationForDeletedRecords
‪recordLocalizeSummaryRespectsWorkspaceEncapsulationForDeletedRecords()
Definition: LocalizationControllerTest.php:225
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\defaultLanguageIsFoundAsOriginLanguage
‪defaultLanguageIsFoundAsOriginLanguage()
Definition: LocalizationControllerTest.php:180
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest
Definition: LocalizationControllerTest.php:30
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController
Definition: LocalizationController.php:43
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\deletedDefaultLanguageItemIsHandledAsIfNoRecordsExistAndReturnsAllOriginLanguages
‪deletedDefaultLanguageItemIsHandledAsIfNoRecordsExistAndReturnsAllOriginLanguages()
Definition: LocalizationControllerTest.php:209
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_LOCALIZE
‪const ACTION_LOCALIZE
Definition: LocalizationController.php:52
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: LocalizationControllerTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordsGetTranslatedFromDifferentTranslation
‪recordsGetTranslatedFromDifferentTranslation()
Definition: LocalizationControllerTest.php:75
‪TYPO3\CMS\Backend\Controller\Page\LocalizationController\ACTION_COPY
‪const ACTION_COPY
Definition: LocalizationController.php:47
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordsGetCopiedFromAnotherLanguage
‪recordsGetCopiedFromAnotherLanguage()
Definition: LocalizationControllerTest.php:110
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordsGetCopiedFromDefaultLanguage
‪recordsGetCopiedFromDefaultLanguage()
Definition: LocalizationControllerTest.php:93
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page
Definition: LocalizationControllerTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordsGetTranslatedFromDefaultLanguage
‪recordsGetTranslatedFromDefaultLanguage()
Definition: LocalizationControllerTest.php:58
‪TYPO3\CMS\Backend\Tests\Functional\Controller\Page\LocalizationControllerTest\recordLocalizeSummaryRespectsWorkspaceEncapsulationForMovedRecords
‪recordLocalizeSummaryRespectsWorkspaceEncapsulationForMovedRecords()
Definition: LocalizationControllerTest.php:265