[BUGFIX] Ensure Extbase translation tests fulfill expectations
[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\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
20 use TYPO3\CMS\Frontend\Page\PageRepository;
21
22 class TranslationTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
23 {
24 /**
25 * @var array
26 */
27 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
28
29 /**
30 * @var array
31 */
32 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
36 */
37 protected $objectManager;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Persistence\Repository
41 */
42 protected $postRepository;
43
44 /**
45 * Sets up this test suite.
46 */
47 protected function setUp()
48 {
49 parent::setUp();
50 /*
51 * Posts Dataset for the tests:
52 *
53 * Post1
54 * -> EN: Post1
55 * -> GR: Post1
56 * Post2
57 * -> EN: Post2
58 * Post3
59 */
60 $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
61 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
62 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/translated-posts.xml');
63
64 $this->setUpBasicFrontendEnvironment();
65
66 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
67 $this->postRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PostRepository::class);
68 }
69
70 /**
71 * Minimal frontent environment to satisfy Extbase Typo3DbBackend
72 */
73 protected function setUpBasicFrontendEnvironment()
74 {
75 $environmentServiceMock = $this->createMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class);
76 $environmentServiceMock
77 ->expects($this->atLeast(1))
78 ->method('isEnvironmentInFrontendMode')
79 ->willReturn(true);
80 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, $environmentServiceMock);
81
82 $pageRepositoryFixture = new PageRepository();
83 $frontendControllerMock = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
84 $frontendControllerMock->sys_page = $pageRepositoryFixture;
85 $GLOBALS['TSFE'] = $frontendControllerMock;
86 }
87
88 /**
89 * @test
90 */
91 public function countReturnsCorrectNumberOfPosts()
92 {
93 $query = $this->postRepository->createQuery();
94
95 $querySettings = $query->getQuerySettings();
96 $querySettings->setStoragePageIds([1]);
97 $querySettings->setRespectSysLanguage(true);
98 $querySettings->setLanguageUid(0);
99
100 $postCount = $query->execute()->count();
101 $this->assertSame(3, $postCount);
102 }
103
104 /**
105 * @test
106 */
107 public function countReturnsCorrectNumberOfPostsInEnglishLanguageForStrictMode()
108 {
109 $query = $this->postRepository->createQuery();
110
111 $querySettings = $query->getQuerySettings();
112 $querySettings->setStoragePageIds([1]);
113 $querySettings->setRespectSysLanguage(true);
114 $querySettings->setLanguageUid(1);
115 $querySettings->setLanguageMode('strict');
116
117 $postCount = $query->execute()->count();
118 $this->assertSame(2, $postCount);
119 }
120
121 /**
122 * @test
123 */
124 public function countReturnsCorrectNumberOfPostsInEnglishLanguage()
125 {
126 $query = $this->postRepository->createQuery();
127
128 $querySettings = $query->getQuerySettings();
129 $querySettings->setStoragePageIds([1]);
130 $querySettings->setRespectSysLanguage(true);
131 $querySettings->setLanguageUid(1);
132
133 $postCount = $query->execute()->count();
134 $this->assertSame(3, $postCount);
135 }
136
137 /**
138 * @test
139 */
140 public function countReturnsCorrectNumberOfPostsInGreekLanguage()
141 {
142 $query = $this->postRepository->createQuery();
143
144 $querySettings = $query->getQuerySettings();
145 $querySettings->setStoragePageIds([1]);
146 $querySettings->setRespectSysLanguage(true);
147 $querySettings->setLanguageUid(2);
148 $postCount = $query->execute()->count();
149
150 $this->assertSame(3, $postCount);
151 }
152
153 /**
154 * @test
155 */
156 public function fetchingPostsReturnsEnglishPostsWithFallback()
157 {
158 $query = $this->postRepository->createQuery();
159
160 $querySettings = $query->getQuerySettings();
161 $querySettings->setStoragePageIds([1]);
162 $querySettings->setRespectSysLanguage(true);
163 $querySettings->setLanguageUid(1);
164
165 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
166
167 /** @var Post[] $posts */
168 $posts = $query->execute()->toArray();
169
170 $this->assertCount(3, $posts);
171 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
172 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
173 $this->assertSame('Post3', $posts[2]->getTitle());
174 }
175
176 /**
177 * @test
178 */
179 public function fetchingPostsReturnsGreekPostsWithFallback()
180 {
181 $query = $this->postRepository->createQuery();
182
183 $querySettings = $query->getQuerySettings();
184 $querySettings->setStoragePageIds([1]);
185 $querySettings->setRespectSysLanguage(true);
186 $querySettings->setLanguageUid(2);
187
188 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
189
190 /** @var Post[] $posts */
191 $posts = $query->execute()->toArray();
192
193 $this->assertCount(3, $posts);
194 $this->assertSame('GR:Post1', $posts[0]->getTitle());
195 $this->assertSame('Post2', $posts[1]->getTitle());
196 $this->assertSame('Post3', $posts[2]->getTitle());
197 }
198
199 /**
200 * @test
201 */
202 public function orderingByTitleRespectsEnglishTitles()
203 {
204 $query = $this->postRepository->createQuery();
205
206 $querySettings = $query->getQuerySettings();
207 $querySettings->setStoragePageIds([1]);
208 $querySettings->setRespectSysLanguage(true);
209 $querySettings->setLanguageUid(1);
210
211 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
212
213 /** @var Post[] $posts */
214 $posts = $query->execute()->toArray();
215
216 $this->assertCount(3, $posts);
217 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
218 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
219 $this->assertSame('Post3', $posts[2]->getTitle());
220 }
221 }