[!!!][TASK] Extract testing framework for TYPO3
[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\CMS\Components\TestingFramework\Core\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(ORIGINAL_ROOT . 'components/testing_framework/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->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
65 $this->postRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PostRepository::class);
66
67 $this->setUpBasicFrontendEnvironment();
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->any())
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 /** @var Post[] $posts */
166 $posts = $query->execute()->toArray();
167
168 $this->assertCount(3, $posts);
169 $this->assertSame('B EN:Post1', $posts[0]->getTitle());
170 $this->assertSame('A EN:Post2', $posts[1]->getTitle());
171 $this->assertSame('Post3', $posts[2]->getTitle());
172 }
173
174 /**
175 * @test
176 */
177 public function fetchingPostsReturnsGreekPostsWithFallback()
178 {
179 $query = $this->postRepository->createQuery();
180
181 $querySettings = $query->getQuerySettings();
182 $querySettings->setStoragePageIds([1]);
183 $querySettings->setRespectSysLanguage(true);
184 $querySettings->setLanguageUid(2);
185
186 /** @var Post[] $posts */
187 $posts = $query->execute()->toArray();
188
189 $this->assertCount(3, $posts);
190 $this->assertSame('GR:Post1', $posts[0]->getTitle());
191 $this->assertSame('Post2', $posts[1]->getTitle());
192 $this->assertSame('Post3', $posts[2]->getTitle());
193 }
194
195 /**
196 * @test
197 */
198 public function orderingByTitleRespectsEnglishTitles()
199 {
200 $query = $this->postRepository->createQuery();
201
202 $querySettings = $query->getQuerySettings();
203 $querySettings->setStoragePageIds([1]);
204 $querySettings->setRespectSysLanguage(true);
205 $querySettings->setLanguageUid(1);
206
207 $query->setOrderings(['title' => QueryInterface::ORDER_ASCENDING]);
208
209 /** @var Post[] $posts */
210 $posts = $query->execute()->toArray();
211
212 $this->assertCount(3, $posts);
213 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
214 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
215 $this->assertSame('Post3', $posts[2]->getTitle());
216 }
217 }