ab9aee2ef62a0fd189fc0c913b54a57a2ffe9048
[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\Core\Tests\FunctionalTestCase {
23
24 /**
25 * @var array
26 */
27 protected $testExtensionsToLoad = array('typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example');
28
29 /**
30 * @var array
31 */
32 protected $coreExtensionsToLoad = array('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 public function setUp() {
48 parent::setUp();
49 /*
50 * Posts Dataset for the tests:
51 *
52 * Post1
53 * -> EN: Post1
54 * -> GR: Post1
55 * Post2
56 * -> EN: Post2
57 * Post3
58 */
59 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
60 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
61 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/translated-posts.xml');
62
63 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
64 $this->postRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\PostRepository::class);
65
66 $this->setUpBasicFrontendEnvironment();
67 }
68
69 /**
70 * Minimal frontent environment to satisfy Extbase Typo3DbBackend
71 */
72 protected function setUpBasicFrontendEnvironment() {
73 $environmentServiceMock = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService');
74 $environmentServiceMock
75 ->expects($this->any())
76 ->method('isEnvironmentInFrontendMode')
77 ->willReturn(TRUE);
78 GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', $environmentServiceMock);
79
80 $pageRepositoryFixture = new PageRepository();
81 $frontendControllerMock = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
82 $frontendControllerMock->sys_page = $pageRepositoryFixture;
83 $GLOBALS['TSFE'] = $frontendControllerMock;
84 }
85
86 /**
87 * @test
88 */
89 public function countReturnsCorrectNumberOfPosts() {
90 $query = $this->postRepository->createQuery();
91
92 $querySettings = $query->getQuerySettings();
93 $querySettings->setStoragePageIds(array(1));
94 $querySettings->setRespectSysLanguage(TRUE);
95 $querySettings->setLanguageUid(0);
96
97 $postCount = $query->execute()->count();
98 $this->assertSame(3, $postCount);
99 }
100
101 /**
102 * @test
103 */
104 public function countReturnsCorrectNumberOfPostsInEnglishLanguage() {
105 $query = $this->postRepository->createQuery();
106
107 $querySettings = $query->getQuerySettings();
108 $querySettings->setStoragePageIds(array(1));
109 $querySettings->setRespectSysLanguage(TRUE);
110 $querySettings->setLanguageUid(1);
111
112 $postCount = $query->execute()->count();
113 $this->assertSame(3, $postCount);
114 }
115
116 /**
117 * @test
118 */
119 public function countReturnsCorrectNumberOfPostsInGreekLanguage() {
120 $query = $this->postRepository->createQuery();
121
122 $querySettings = $query->getQuerySettings();
123 $querySettings->setStoragePageIds(array(1));
124 $querySettings->setRespectSysLanguage(TRUE);
125 $querySettings->setLanguageUid(2);
126 $postCount = $query->execute()->count();
127
128 $this->assertSame(3, $postCount);
129 }
130
131 /**
132 * @test
133 */
134 public function fetchingPostsReturnsEnglishPostsWithFallback() {
135 $query = $this->postRepository->createQuery();
136
137 $querySettings = $query->getQuerySettings();
138 $querySettings->setStoragePageIds(array(1));
139 $querySettings->setRespectSysLanguage(TRUE);
140 $querySettings->setLanguageUid(1);
141
142 /** @var Post[] $posts */
143 $posts = $query->execute()->toArray();
144
145 $this->assertCount(3, $posts);
146 $this->assertSame('B EN:Post1', $posts[0]->getTitle());
147 $this->assertSame('A EN:Post2', $posts[1]->getTitle());
148 $this->assertSame('Post3', $posts[2]->getTitle());
149 }
150
151 /**
152 * @test
153 */
154 public function fetchingPostsReturnsGreekPostsWithFallback() {
155 $query = $this->postRepository->createQuery();
156
157 $querySettings = $query->getQuerySettings();
158 $querySettings->setStoragePageIds(array(1));
159 $querySettings->setRespectSysLanguage(TRUE);
160 $querySettings->setLanguageUid(2);
161
162 /** @var Post[] $posts */
163 $posts = $query->execute()->toArray();
164
165 $this->assertCount(3, $posts);
166 $this->assertSame('GR:Post1', $posts[0]->getTitle());
167 $this->assertSame('Post2', $posts[1]->getTitle());
168 $this->assertSame('Post3', $posts[2]->getTitle());
169 }
170
171 /**
172 * @test
173 */
174 public function orderingByTitleRespectsEnglishTitles() {
175 $query = $this->postRepository->createQuery();
176
177 $querySettings = $query->getQuerySettings();
178 $querySettings->setStoragePageIds(array(1));
179 $querySettings->setRespectSysLanguage(TRUE);
180 $querySettings->setLanguageUid(1);
181
182 $query->setOrderings(array('title' => QueryInterface::ORDER_ASCENDING));
183
184 /** @var Post[] $posts */
185 $posts = $query->execute()->toArray();
186
187 $this->assertCount(3, $posts);
188 $this->assertSame('A EN:Post2', $posts[0]->getTitle());
189 $this->assertSame('B EN:Post1', $posts[1]->getTitle());
190 $this->assertSame('Post3', $posts[2]->getTitle());
191 }
192 }