[BUGFIX] Pass language mode to QuerySettings in default language too
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / TranslationTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
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 ExtbaseTeam\BlogExample\Domain\Model\Post;
18 use TYPO3\CMS\Core\Context\Context;
19 use TYPO3\CMS\Core\Context\LanguageAspect;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
22 use TYPO3\CMS\Frontend\Page\PageRepository;
23
24 class TranslationTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
25 {
26 /**
27 * @var array
28 */
29 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
30
31 /**
32 * @var array
33 */
34 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
38 */
39 protected $objectManager;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Persistence\Repository
43 */
44 protected $postRepository;
45
46 /**
47 * Sets up this test suite.
48 */
49 protected function setUp()
50 {
51 parent::setUp();
52 /*
53 * Posts Dataset for the tests:
54 *
55 * Post1
56 * -> EN: Post1
57 * -> GR: Post1
58 * Post2
59 * -> EN: Post2
60 * Post3
61 * Post10 [hidden]
62 * -> GR: Post10 [hidden]
63 */
64 $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
65 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
66 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/translated-posts.xml');
67
68 $this->setUpBasicFrontendEnvironment();
69
70 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
71 $this->postRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PostRepository::class);
72 }
73
74 /**
75 * Minimal frontend environment to satisfy Extbase Typo3DbBackend
76 */
77 protected function setUpBasicFrontendEnvironment()
78 {
79 // in v9 overlay and language mode has different default values, so we need to set them here explicitely
80 // to match v8 behaviour
81 $context = GeneralUtility::makeInstance(Context::class);
82 $context->setAspect('language', new LanguageAspect(0, 0, LanguageAspect::OVERLAYS_OFF, ['off']));
83
84 $environmentServiceMock = $this->createMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class);
85 $environmentServiceMock
86 ->expects($this->atLeast(1))
87 ->method('isEnvironmentInFrontendMode')
88 ->willReturn(true);
89 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, $environmentServiceMock);
90
91 $pageRepositoryFixture = new PageRepository();
92 $frontendControllerMock = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
93 $frontendControllerMock->sys_page = $pageRepositoryFixture;
94 $GLOBALS['TSFE'] = $frontendControllerMock;
95 }
96
97 /**
98 * @test
99 */
100 public function countReturnsCorrectNumberOfPosts()
101 {
102 $query = $this->postRepository->createQuery();
103
104 $querySettings = $query->getQuerySettings();
105 $querySettings->setStoragePageIds([1]);
106 $querySettings->setRespectSysLanguage(true);
107 $querySettings->setLanguageUid(0);
108
109 $postCount = $query->execute()->count();
110 $this->assertSame(3, $postCount);
111 }
112
113 /**
114 * @test
115 */
116 public function countReturnsCorrectNumberOfPostsInEnglishLanguageForStrictMode()
117 {
118 $query = $this->postRepository->createQuery();
119
120 $querySettings = $query->getQuerySettings();
121 $querySettings->setStoragePageIds([1]);
122 $querySettings->setRespectSysLanguage(true);
123 $querySettings->setLanguageUid(1);
124 $querySettings->setLanguageMode('strict');
125
126 $postCount = $query->execute()->count();
127 $this->assertSame(2, $postCount);
128 }
129
130 /**
131 * @test
132 */
133 public function countReturnsCorrectNumberOfPostsInEnglishLanguage()
134 {
135 $query = $this->postRepository->createQuery();
136
137 $querySettings = $query->getQuerySettings();
138 $querySettings->setStoragePageIds([1]);
139 $querySettings->setRespectSysLanguage(true);
140 $querySettings->setLanguageUid(1);
141
142 $postCount = $query->execute()->count();
143 $this->assertSame(3, $postCount);
144 }
145
146 /**
147 * @test
148 */
149 public function countReturnsCorrectNumberOfPostsInGreekLanguage()
150 {
151 $query = $this->postRepository->createQuery();
152
153 $querySettings = $query->getQuerySettings();
154 $querySettings->setStoragePageIds([1]);
155 $querySettings->setRespectSysLanguage(true);
156 $querySettings->setLanguageUid(2);
157 $postCount = $query->execute()->count();
158
159 $this->assertSame(3, $postCount);
160 }
161
162 /**
163 * @test
164 */
165 public function fetchingPostsReturnsEnglishPostsWithFallback()
166 {
167 $query = $this->postRepository->createQuery();
168
169 $querySettings = $query->getQuerySettings();
170 $querySettings->setStoragePageIds([1]);
171 $querySettings->setRespectSysLanguage(true);
172 $querySettings->setLanguageUid(1);
173
174 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
175
176 /** @var Post[] $posts */
177 $posts = $query->execute()->toArray();
178
179 $this->assertCount(3, $posts);
180 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
181 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
182 $this->assertSame('Post3', $posts[2]->getTitle());
183 }
184
185 /**
186 * @test
187 */
188 public function fetchingPostsReturnsGreekPostsWithFallback()
189 {
190 $query = $this->postRepository->createQuery();
191
192 $querySettings = $query->getQuerySettings();
193 $querySettings->setStoragePageIds([1]);
194 $querySettings->setRespectSysLanguage(true);
195 $querySettings->setLanguageUid(2);
196
197 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
198
199 /** @var Post[] $posts */
200 $posts = $query->execute()->toArray();
201
202 $this->assertCount(3, $posts);
203 $this->assertSame('GR:Post1', $posts[0]->getTitle());
204 $this->assertSame('Post2', $posts[1]->getTitle());
205 $this->assertSame('Post3', $posts[2]->getTitle());
206 }
207
208 /**
209 * @test
210 */
211 public function orderingByTitleRespectsEnglishTitles()
212 {
213 $query = $this->postRepository->createQuery();
214
215 $querySettings = $query->getQuerySettings();
216 $querySettings->setStoragePageIds([1]);
217 $querySettings->setRespectSysLanguage(true);
218 $querySettings->setLanguageUid(1);
219
220 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
221
222 /** @var Post[] $posts */
223 $posts = $query->execute()->toArray();
224
225 $this->assertCount(3, $posts);
226 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
227 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
228 $this->assertSame('Post3', $posts[2]->getTitle());
229 }
230
231 /**
232 * This test checks whether setIgnoreEnableFields(true) affects the query
233 * It's expected that when ignoring enable fields, the hidden record is also returned.
234 * This is related to https://forge.typo3.org/issues/68672
235 *
236 * @test
237 */
238 public function fetchingHiddenPostsWithIgnoreEnableField()
239 {
240 $query = $this->postRepository->createQuery();
241
242 $querySettings = $query->getQuerySettings();
243 $querySettings->setStoragePageIds([1]);
244 $querySettings->setRespectSysLanguage(true);
245 $querySettings->setIgnoreEnableFields(true);
246 $querySettings->setLanguageUid(0);
247
248 /** @var Post[] $posts */
249 $posts = $query->execute()->toArray();
250
251 $this->assertCount(4, $posts);
252 $this->assertSame('Post10', $posts[3]->getTitle());
253 }
254
255 /**
256 * This test checks whether setIgnoreEnableFields(true) affects translated record too.
257 * It's expected that when ignoring enable fields, the hidden translated record is shown.
258 * This is related to https://forge.typo3.org/issues/68672
259 *
260 * This tests documents current, buggy behaviour!
261 *
262 * @test
263 */
264 public function fetchingHiddenPostsReturnsHiddenOverlay()
265 {
266 $query = $this->postRepository->createQuery();
267
268 $querySettings = $query->getQuerySettings();
269 $querySettings->setStoragePageIds([1]);
270 $querySettings->setRespectSysLanguage(true);
271 $querySettings->setIgnoreEnableFields(true);
272 $querySettings->setLanguageUid(2);
273
274 /** @var Post[] $posts */
275 $posts = $query->execute()->toArray();
276
277 $this->assertCount(4, $posts);
278 //This assertion is wrong, once the issue https://forge.typo3.org/issues/68672
279 //is fixed, the following assertion should be used.
280 $this->assertSame('Post10', $posts[3]->getTitle());
281 //$this->assertSame('GR:Post10', $posts[3]->getTitle());
282 }
283 }