LocalizationControllerTest.php 9.57 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4

5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Backend\Tests\Functional\Controller\Page;

20
21
use TYPO3\CMS\Backend\Controller\Page\LocalizationController;
use TYPO3\CMS\Core\DataHandling\DataHandler;
22
use TYPO3\CMS\Core\Http\ServerRequest;
23
use TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase;
24
25
26
27

/**
 * Test case for TYPO3\CMS\Backend\Controller\Page\LocalizationController
 */
28
class LocalizationControllerTest extends AbstractDataHandlerActionTestCase
29
30
{
    /**
31
32
33
34
35
     * @var string
     */
    protected $assertionDataSetDirectory = 'typo3/sysext/backend/Tests/Functional/Controller/Page/Localization/CSV/DataSet/';

    /**
36
     * @var LocalizationController|\PHPUnit\Framework\MockObject\MockObject
37
38
39
     */
    protected $subject;

40
41
42
43
44
45
46
47
48
49
    /**
     * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
     */
    protected $backendUser;

    /**
     * @var array
     */
    protected $coreExtensionsToLoad = ['workspaces'];

50
51
52
    /**
     * Sets up this test case.
     */
53
    protected function setUp(): void
54
55
56
    {
        parent::setUp();

57
        $this->importDataSet(__DIR__ . '/Fixtures/pages.xml');
58
        $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/sys_language.xml');
59
        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-default-language.xml');
60
61
        $this->setUpFrontendRootPage(1);
        $this->setUpFrontendSite(1, $this->siteLanguageConfiguration);
62

63
        $this->subject = $this->getAccessibleMock(LocalizationController::class, ['getPageColumns']);
64
65
66
67
    }

    /**
     * @test
68
     * see DataSet/TranslatedFromDefault.csv
69
     */
70
    public function recordsGetTranslatedFromDefaultLanguage(): void
71
72
73
74
75
76
77
78
    {
        $params = [
            'pageId' => 1,
            'srcLanguageId' => 0,
            'destLanguageId' => 1,
            'uidList' => [1, 2, 3],
            'action' => LocalizationController::ACTION_LOCALIZE,
        ];
79
        $this->subject->_call('process', $params);
80
        $this->assertAssertionDataSet('TranslatedFromDefault');
81
82
83
84
85
    }

    /**
     * @test
     */
86
    public function recordsGetTranslatedFromDifferentTranslation(): void
87
88
89
90
91
92
93
94
95
96
    {
        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml');

        $params = [
            'pageId' => 1,
            'srcLanguageId' => 1,
            'destLanguageId' => 2,
            'uidList' => [4, 5, 6], // uids of tt_content-danish-language
            'action' => LocalizationController::ACTION_LOCALIZE,
        ];
97
        $this->subject->_call('process', $params);
98
        $this->assertAssertionDataSet('TranslatedFromTranslation');
99
100
101
102
103
    }

    /**
     * @test
     */
104
    public function recordsGetCopiedFromDefaultLanguage(): void
105
106
107
108
109
110
111
112
    {
        $params = [
            'pageId' => 1,
            'srcLanguageId' => 0,
            'destLanguageId' => 2,
            'uidList' => [1, 2, 3],
            'action' => LocalizationController::ACTION_COPY,
        ];
113
        $this->subject->_call('process', $params);
114
        $this->assertAssertionDataSet('CopiedFromDefault');
115
116
117
118
119
    }

    /**
     * @test
     */
120
    public function recordsGetCopiedFromAnotherLanguage(): void
121
122
123
124
125
126
127
128
129
130
    {
        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml');

        $params = [
            'pageId' => 1,
            'srcLanguageId' => 1,
            'destLanguageId' => 2,
            'uidList' => [4, 5, 6], // uids of tt_content-danish-language
            'action' => LocalizationController::ACTION_COPY,
        ];
131
        $this->subject->_call('process', $params);
132
        $this->assertAssertionDataSet('CopiedFromTranslation');
133
134
135
    }

    /**
136
137
138
139
140
141
142
143
144
     * This test:
     * - copies default language records 1,2,3, into language 1 ("free mode translation")
     * - creates new CE in default language after record 2, called 'Test content 2.5'
     * - copies into language record 9 ('Test content 2.5')
     * - checks if translated/copied record "[Translate to Dansk:] Test content 2.5" has sorting value after
     *   "[Translate to Dansk:] Test content 1", which is the previous record in the colpos.
     *
     * For detail about the sorting algorithm when translating records, see DataHandler->getPreviousLocalizedRecordUid
     *
145
146
     * @test
     */
147
    public function copyingNewContentFromLanguageIntoExistingLocalizationHasSameOrdering(): void
148
149
150
151
152
153
154
155
    {
        $params = [
            'pageId' => 1,
            'srcLanguageId' => 0,
            'destLanguageId' => 1,
            'uidList' => [1, 2, 3],
            'action' => LocalizationController::ACTION_COPY,
        ];
156
        $this->subject->_call('process', $params);
157
158
159
160
161
162
163
164

        // Create another content element in default language
        $data = [
            'tt_content' => [
                'NEW123456' => [
                    'sys_language_uid' => 0,
                    'header' => 'Test content 2.5',
                    'pid' => -2,
165
                    'colPos' => 0,
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
                ],
            ],
        ];
        $dataHandler = new DataHandler();
        $dataHandler->start($data, []);
        $dataHandler->process_datamap();
        $dataHandler->process_cmdmap();
        $newContentElementUid = $dataHandler->substNEWwithIDs['NEW123456'];

        // Copy the new content element
        $params = [
            'pageId' => 1,
            'srcLanguageId' => 0,
            'destLanguageId' => 1,
            'uidList' => [$newContentElementUid],
            'action' => LocalizationController::ACTION_COPY,
        ];
183
        $this->subject->_call('process', $params);
184
        $this->assertAssertionDataSet('CreatedElementOrdering');
185
    }
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    /**
     * @test
     */
    public function defaultLanguageIsFoundAsOriginLanguage(): void
    {
        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml');

        // Create another content element in default language
        $data = [
            'tt_content' => [
                'NEW123456' => [
                    'sys_language_uid' => 0,
                    'header' => 'New content element',
                    'pid' => 1,
                    'colPos' => 0,
                ],
            ],
        ];
        $dataHandler = new DataHandler();
        $dataHandler->start($data, []);
        $dataHandler->process_datamap();
        $dataHandler->process_cmdmap();

        $request = (new ServerRequest())->withQueryParams([
            'pageId'         => 1, // page uid, the records are stored on
212
            'languageId'     => 1,  // current language id
213
214
215
        ]);

        $usedLanguages = (string)$this->subject->getUsedLanguagesInPage($request)->getBody();
216
        self::assertThat($usedLanguages, self::stringContains('"uid":0'));
217
218
    }

219
220
221
    /**
     * @test
     */
222
    public function recordLocalizeSummaryRespectsWorkspaceEncapsulationForDeletedRecords(): void
223
224
225
226
227
    {
        // Delete record 2 within workspace 1
        $this->backendUser->workspace = 1;
        $this->actionService->deleteRecord('tt_content', 2);

228
229
230
231
232
233
234
        $expectedRecords = [
            '0' => [
                ['uid' => 1],
            ],
            '1' => [
                ['uid' => 3],
            ],
235
236
        ];

237
238
        $localizeSummary = $this->getReducedRecordLocalizeSummary();

239
        self::assertEquals($expectedRecords, $localizeSummary);
240
241
242
243
244
    }

    /**
     * @test
     */
245
    public function recordLocalizeSummaryRespectsWorkspaceEncapsulationForMovedRecords(): void
246
247
248
249
250
    {
        // Move record 2 to page 2 within workspace 1
        $this->backendUser->workspace = 1;
        $this->actionService->moveRecord('tt_content', 2, 2);

251
252
253
254
255
256
257
        $expectedRecords = [
            '0' => [
                ['uid' => 1],
            ],
            '1' => [
                ['uid' => 3],
            ],
258
259
        ];

260
261
        $localizeSummary = $this->getReducedRecordLocalizeSummary();

262
        self::assertEquals($expectedRecords, $localizeSummary);
263
264
265
266
267
268
269
    }

    /**
     * Get record localized summary list reduced to list of uids
     *
     * @return array
     */
270
    protected function getReducedRecordLocalizeSummary(): array
271
272
273
274
    {
        $request = (new ServerRequest())->withQueryParams([
            'pageId'         => 1, // page uid, the records are stored on
            'destLanguageId' => 1, // destination language uid
275
            'languageId'     => 0,  // source language uid
276
277
        ]);

278
279
280
281
282
283
        $this->subject->method('getPageColumns')->willReturn([
            0 => 'Column 0',
            1 => 'Column 1',
        ]);

        $recordLocalizeSummaryResponse = $this->subject->getRecordLocalizeSummary($request);
284
285

        // Reduce the fetched record summary to list of uids
286
        if ($recordLocalizeSummary = json_decode((string)$recordLocalizeSummaryResponse->getBody(), true)) {
287
288
289
            foreach ($recordLocalizeSummary['records'] as $colPos => $records) {
                foreach ($records as $key => $record) {
                    $recordLocalizeSummary['records'][$colPos][$key] = array_intersect_key($record, ['uid' => '']);
290
291
292
293
                }
            }
        }

294
295
        return $recordLocalizeSummary['records'];
    }
296
}