‪TYPO3CMS  ‪main
TranslationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
34 
35 final class ‪TranslationTest extends FunctionalTestCase
36 {
37  protected array ‪$testExtensionsToLoad = [
38  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example',
39  ];
40 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
44  $this->importCSVDataSet(__DIR__ . '/Fixtures/TranslationTestImport.csv');
45 
46  $context = GeneralUtility::makeInstance(Context::class);
47  $context->setAspect('language', new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
48  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), []);
49  $frontendTypoScript->setSetupArray([]);
50  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
51  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE)
52  ->withAttribute('frontend.typoscript', $frontendTypoScript);
53 
54  // ConfigurationManager is used by PersistenceManager to retrieve configuration.
55  // We set a proper extensionName and pluginName for the ConfigurationManager singleton
56  // here, to not run into warnings due to incomplete test setup.
57  $configurationManager = $this->get(ConfigurationManager::class);
58  $configurationManager->setConfiguration([
59  'extensionName' => 'blog_example',
60  'pluginName' => 'test',
61  ]);
62  }
63 
67  #[Test]
68  public function ‪countReturnsCorrectNumberOfPosts(): void
69  {
70  $query = $this->get(PostRepository::class)->createQuery();
71  $querySettings = $query->getQuerySettings();
72  $querySettings->setStoragePageIds([1]);
73  $querySettings->setRespectSysLanguage(true);
74  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
75  $postCount = $query->execute()->count();
76  self::assertSame(4, $postCount);
77  }
78 
82  #[Test]
84  {
85  $query = $this->get(PostRepository::class)->createQuery();
86  $querySettings = $query->getQuerySettings();
87  $querySettings->setStoragePageIds([1]);
88  $querySettings->setRespectSysLanguage(true);
89  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
90  $postCount = $query->execute()->count();
91  self::assertSame(2, $postCount);
92  }
93 
94  #[Test]
96  {
97  $query = $this->get(PostRepository::class)->createQuery();
98  $querySettings = $query->getQuerySettings();
99  $querySettings->setStoragePageIds([1]);
100  $querySettings->setRespectSysLanguage(true);
101  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
102  $postCount = $query->execute()->count();
103  self::assertSame(2, $postCount);
104  }
105 
106  #[Test]
108  {
109  $query = $this->get(PostRepository::class)->createQuery();
110  $querySettings = $query->getQuerySettings();
111  $querySettings->setStoragePageIds([1]);
112  $querySettings->setRespectSysLanguage(true);
113  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_ON));
114  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
116  $posts = $query->execute()->toArray();
117  self::assertCount(2, $posts);
118  self::assertSame('A EN:Post2', $posts[0]->getTitle());
119  self::assertSame('B EN:Post1', $posts[1]->getTitle());
120  }
121 
122  #[Test]
124  {
125  $query = $this->get(PostRepository::class)->createQuery();
126  $querySettings = $query->getQuerySettings();
127  $querySettings->setStoragePageIds([1]);
128  $querySettings->setRespectSysLanguage(false);
129  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_MIXED));
130  $query->matching($query->in('uid', [4]));
132  $posts = $query->execute()->toArray();
133  self::assertCount(1, $posts);
134  self::assertSame('Post2', $posts[0]->getTitle());
135  }
136 
140  #[Test]
142  {
143  $query = $this->get(PostRepository::class)->createQuery();
144  $querySettings = $query->getQuerySettings();
145  $querySettings->setStoragePageIds([1]);
146  $querySettings->setRespectSysLanguage(true);
147  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
148  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
150  $posts = $query->execute()->toArray();
151  self::assertCount(2, $posts);
152  self::assertSame('GR:Post1', $posts[0]->getTitle());
153  self::assertSame('GR:Post11', $posts[1]->getTitle());
154  }
155 
159  #[Test]
161  {
162  $query = $this->get(PostRepository::class)->createQuery();
163  $querySettings = $query->getQuerySettings();
164  $querySettings->setStoragePageIds([1]);
165  $querySettings->setRespectSysLanguage(true);
166  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
167  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
169  $posts = $query->execute()->toArray();
170  self::assertCount(2, $posts);
171  self::assertSame('GR:Post1', $posts[0]->getTitle());
172  self::assertSame('GR:Post11', $posts[1]->getTitle());
173  }
174 
175  public static function ‪fetchingTranslatedPostByUidDataProvider(): array
176  {
177  return [
178  'with one id' => [
179  'input' => [12],
180  'expectedTitles' => ['GR:Post11'],
181  ],
182  'with two ids' => [
183  'input' => [12, 1],
184  'expectedTitles' => ['GR:Post11', 'GR:Post1'],
185  ],
186  ];
187  }
188 
189  #[DataProvider('fetchingTranslatedPostByUidDataProvider')]
190  #[Test]
191  public function ‪fetchingTranslatedPostByInClauseWithStrictLanguageSettings(array $input, array $expectedTitles): void
192  {
193  $query = $this->get(PostRepository::class)->createQuery();
194  $querySettings = $query->getQuerySettings();
195  $querySettings->setStoragePageIds([1]);
196  $querySettings->setRespectSysLanguage(true);
197  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
198  $query->matching($query->in('uid', $input));
200  $posts = $query->execute()->toArray();
201  // @todo: wrong assertion
202  // We're simulating a strict language configuration where a blog post (uid=12 or uid=1) has been translated to another
203  // language. However, Extbase is not able to find the translated record via ->in() and therefore returns an
204  // empty result set. This will be fixed with https://review.typo3.org/c/Packages/TYPO3.CMS/+/67893
205  self::assertCount(0, $posts);
206  // self::assertCount(count($expectedTitles), $posts);
207  // self::assertEqualsCanonicalizing($expectedTitles, array_map(static fn(Post $post): string => $post->getTitle(), $posts));
208  }
209 
210  #[DataProvider('fetchingTranslatedPostByUidDataProvider')]
211  #[Test]
212  public function ‪fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings(array $input, array $expectedTitles): void
213  {
214  $query = $this->get(PostRepository::class)->createQuery();
215  $querySettings = $query->getQuerySettings();
216  $querySettings->setStoragePageIds([1]);
217  $querySettings->setRespectSysLanguage(true);
218  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
219  $constraints = [];
220  foreach ($input as ‪$uid) {
221  $constraints[] = $query->equals('uid', ‪$uid);
222  }
223  $query->matching($query->logicalOr(...$constraints));
225  $posts = $query->execute()->toArray();
226  // @todo: wrong assertion
227  // We're simulating a strict language configuration where a blog post (uid=12 or uid=1) has been translated to another
228  // language. However, Extbase is not able to find the translated record via ->equals(uid=12 OR uid=1 OR ...) and therefore returns an
229  // empty result set. This will be fixed with https://review.typo3.org/c/Packages/TYPO3.CMS/+/67893
230  self::assertCount(0, $posts);
231  // self::assertCount(count($expectedTitles), $posts);
232  // self::assertEqualsCanonicalizing($expectedTitles, array_map(static fn(Post $post): string => $post->getTitle(), $posts));
233  }
234 
235  #[Test]
237  {
238  $query = $this->get(PostRepository::class)->createQuery();
239  $querySettings = $query->getQuerySettings();
240  $querySettings->setStoragePageIds([1]);
241  $querySettings->setRespectSysLanguage(true);
242  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
243  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
245  $posts = $query->execute()->toArray();
246  self::assertCount(2, $posts);
247  self::assertSame('A EN:Post2', $posts[0]->getTitle());
248  self::assertSame('B EN:Post1', $posts[1]->getTitle());
249  }
250 
255  #[Test]
256  public function ‪orderingByBlogTitle(): void
257  {
258  $query = $this->get(PostRepository::class)->createQuery();
259  $querySettings = $query->getQuerySettings();
260  $querySettings->setStoragePageIds([1]);
261  $querySettings->setRespectSysLanguage(true);
262  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
263  $query->setOrderings([
264  'blog.title' => ‪QueryInterface::ORDER_ASCENDING,
266  ]);
268  $posts = $query->execute()->toArray();
269  self::assertCount(2, $posts);
270  self::assertSame('B EN:Post1', $posts[0]->getTitle());
271  self::assertSame('A EN:Post2', $posts[1]->getTitle());
272  }
273 
279  #[Test]
281  {
282  $query = $this->get(PostRepository::class)->createQuery();
283  $querySettings = $query->getQuerySettings();
284  $querySettings->setStoragePageIds([1]);
285  $querySettings->setRespectSysLanguage(true);
286  $querySettings->setIgnoreEnableFields(true);
287  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_ON));
288  // we need it to have stable results on pgsql
289  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
291  $posts = $query->execute()->toArray();
292  self::assertCount(5, $posts);
293  self::assertSame('Post10', $posts[3]->getTitle());
294  }
295 
300  #[Test]
302  {
303  $query = $this->get(PostRepository::class)->createQuery();
304  $querySettings = $query->getQuerySettings();
305  $querySettings->setStoragePageIds([1]);
306  $querySettings->setRespectSysLanguage(true);
307  $querySettings->setIgnoreEnableFields(true);
308  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
309  // We need it to have stable results on pgsql
310  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
312  $posts = $query->execute()->toArray();
313  self::assertCount(3, $posts);
314  self::assertSame('GR:Post1', $posts[0]->getTitle());
315  self::assertSame('GR:Post10', $posts[1]->getTitle());
316  self::assertSame('GR:Post11', $posts[2]->getTitle());
317  }
318 
326  #[Test]
328  {
329  $query = $this->get(PostRepository::class)->createQuery();
330  $querySettings = $query->getQuerySettings();
331  $querySettings->setStoragePageIds([1]);
332  $querySettings->setRespectSysLanguage(true);
333  $querySettings->setIgnoreEnableFields(true);
334  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_MIXED));
335  //we need it to have stable results on pgsql
336  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
338  $posts = $query->execute()->toArray();
339  self::assertCount(5, $posts);
340  self::assertSame('GR:Post1', $posts[0]->getTitle());
341  self::assertSame('Post2', $posts[1]->getTitle());
342  self::assertSame('Post3', $posts[2]->getTitle());
343  // once the issue is fixed this assertions should be GR:Post10
344  self::assertSame('Post10', $posts[3]->getTitle());
345  self::assertSame('GR:Post11', $posts[4]->getTitle());
346  }
347 
351  #[Test]
352  public function ‪fetchingTranslatedPostByTitle(): void
353  {
354  $query = $this->get(PostRepository::class)->createQuery();
355  $querySettings = $query->getQuerySettings();
356  $querySettings->setStoragePageIds([1]);
357  $querySettings->setRespectSysLanguage(true);
358  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
359  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
360  $query->matching($query->equals('title', 'GR:Post1'));
362  $posts = $query->execute()->toArray();
363  self::assertCount(1, $posts);
364  self::assertSame('GR:Post1', $posts[0]->getTitle());
365  }
366 
371  #[Test]
372  public function ‪fetchingTranslatedPostByBlogTitle(): void
373  {
374  $query = $this->get(PostRepository::class)->createQuery();
375  $querySettings = $query->getQuerySettings();
376  $querySettings->setStoragePageIds([1]);
377  $querySettings->setRespectSysLanguage(true);
378  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
379  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
380  $query->matching($query->equals('blog.title', 'Blog1'));
382  $posts = $query->execute()->toArray();
383  self::assertCount(2, $posts);
384  self::assertSame('GR:Post1', $posts[0]->getTitle());
385  self::assertSame('GR:Post11', $posts[1]->getTitle());
386  }
387 
388  #[Test]
389  public function ‪fetchingPostByTagName(): void
390  {
391  $query = $this->get(PostRepository::class)->createQuery();
392  $querySettings = $query->getQuerySettings();
393  $querySettings->setRespectStoragePage(false);
394  $querySettings->setRespectSysLanguage(true);
395  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
396  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
397  $query->matching($query->equals('tags.name', 'Tag1'));
399  $posts = $query->execute()->toArray();
400  self::assertCount(3, $posts);
401  self::assertSame('Post1', $posts[0]->getTitle());
402  }
403 
404  #[Test]
405  public function ‪fetchingTranslatedPostByTagName(): void
406  {
407  $query = $this->get(PostRepository::class)->createQuery();
408  $querySettings = $query->getQuerySettings();
409  $querySettings->setRespectStoragePage(false);
410  $querySettings->setRespectSysLanguage(true);
411  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
412  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
413  $query->matching($query->equals('tags.name', 'Tag1'));
415  $posts = $query->execute()->toArray();
416  self::assertCount(2, $posts);
417  self::assertSame('A EN:Post2', $posts[0]->getTitle());
418  self::assertCount(1, $posts[0]->getTags());
419  self::assertSame('B EN:Post1', $posts[1]->getTitle());
420  self::assertCount(2, $posts[1]->getTags());
421  }
422 }
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostByTagName
‪fetchingPostByTagName()
Definition: TranslationTest.php:389
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsGreekPostsWithHideNonTranslated
‪fetchingPostsReturnsGreekPostsWithHideNonTranslated()
Definition: TranslationTest.php:160
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_MIXED
‪const OVERLAYS_MIXED
Definition: LanguageAspect.php:75
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\countReturnsCorrectNumberOfPosts
‪countReturnsCorrectNumberOfPosts()
Definition: TranslationTest.php:68
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\countReturnsCorrectNumberOfPostsInEnglishLanguage
‪countReturnsCorrectNumberOfPostsInEnglishLanguage()
Definition: TranslationTest.php:83
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsEnglishPostsWithFallback
‪fetchingPostsReturnsEnglishPostsWithFallback()
Definition: TranslationTest.php:107
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsWithIgnoreEnableField
‪fetchingHiddenPostsWithIgnoreEnableField()
Definition: TranslationTest.php:280
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: TranslationTest.php:37
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_ON
‪const OVERLAYS_ON
Definition: LanguageAspect.php:76
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByTitle
‪fetchingTranslatedPostByTitle()
Definition: TranslationTest.php:352
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsGreekPostsWithFallback
‪fetchingPostsReturnsGreekPostsWithFallback()
Definition: TranslationTest.php:141
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByBlogTitle
‪fetchingTranslatedPostByBlogTitle()
Definition: TranslationTest.php:372
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\orderingByTitleRespectsEnglishTitles
‪orderingByTitleRespectsEnglishTitles()
Definition: TranslationTest.php:236
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByUidDataProvider
‪static fetchingTranslatedPostByUidDataProvider()
Definition: TranslationTest.php:175
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: QueryInterface.php:99
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\countReturnsCorrectNumberOfPostsInGreekLanguage
‪countReturnsCorrectNumberOfPostsInGreekLanguage()
Definition: TranslationTest.php:95
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByTagName
‪fetchingTranslatedPostByTagName()
Definition: TranslationTest.php:405
‪TYPO3Tests\BlogExample\Domain\Model\Post
Definition: Post.php:28
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence
Definition: AddTest.php:18
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByInClauseWithStrictLanguageSettings
‪fetchingTranslatedPostByInClauseWithStrictLanguageSettings(array $input, array $expectedTitles)
Definition: TranslationTest.php:191
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest
Definition: TranslationTest.php:36
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsReturnsHiddenOverlay
‪fetchingHiddenPostsReturnsHiddenOverlay()
Definition: TranslationTest.php:301
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\orderingByBlogTitle
‪orderingByBlogTitle()
Definition: TranslationTest.php:256
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_OFF
‪const OVERLAYS_OFF
Definition: LanguageAspect.php:74
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3Tests\BlogExample\Domain\Repository\PostRepository
Definition: PostRepository.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsByInClauseReturnsDefaultPostsWithFallback
‪fetchingPostsByInClauseReturnsDefaultPostsWithFallback()
Definition: TranslationTest.php:123
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings
‪fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings(array $input, array $expectedTitles)
Definition: TranslationTest.php:212
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\setUp
‪setUp()
Definition: TranslationTest.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsReturnsHiddenOverlayOverlayEnabled
‪fetchingHiddenPostsReturnsHiddenOverlayOverlayEnabled()
Definition: TranslationTest.php:327