‪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;
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
33 
34 final class ‪TranslationTest extends FunctionalTestCase
35 {
36  protected array ‪$testExtensionsToLoad = [
37  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example',
38  ];
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  $this->importCSVDataSet(__DIR__ . '/Fixtures/TranslationTestImport.csv');
44 
45  $context = $this->get(Context::class);
46  $context->setAspect('language', new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
47  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
48  $frontendTypoScript->setSetupArray([]);
49  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())
50  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE)
51  ->withAttribute('frontend.typoscript', $frontendTypoScript);
52 
53  // ConfigurationManager is used by PersistenceManager to retrieve configuration.
54  // We set a proper extensionName and pluginName for the ConfigurationManager singleton
55  // here, to not run into warnings due to incomplete test setup.
56  $configurationManager = $this->get(ConfigurationManager::class);
57  $configurationManager->setConfiguration([
58  'extensionName' => 'blog_example',
59  'pluginName' => 'test',
60  ]);
61  }
62 
66  #[Test]
67  public function ‪countReturnsCorrectNumberOfPosts(): void
68  {
69  $query = $this->get(PostRepository::class)->createQuery();
70  $querySettings = $query->getQuerySettings();
71  $querySettings->setStoragePageIds([1]);
72  $querySettings->setRespectSysLanguage(true);
73  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
74  $postCount = $query->execute()->count();
75  self::assertSame(4, $postCount);
76  }
77 
81  #[Test]
83  {
84  $query = $this->get(PostRepository::class)->createQuery();
85  $querySettings = $query->getQuerySettings();
86  $querySettings->setStoragePageIds([1]);
87  $querySettings->setRespectSysLanguage(true);
88  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
89  $postCount = $query->execute()->count();
90  self::assertSame(2, $postCount);
91  }
92 
93  #[Test]
95  {
96  $query = $this->get(PostRepository::class)->createQuery();
97  $querySettings = $query->getQuerySettings();
98  $querySettings->setStoragePageIds([1]);
99  $querySettings->setRespectSysLanguage(true);
100  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
101  $postCount = $query->execute()->count();
102  self::assertSame(2, $postCount);
103  }
104 
105  #[Test]
107  {
108  $query = $this->get(PostRepository::class)->createQuery();
109  $querySettings = $query->getQuerySettings();
110  $querySettings->setStoragePageIds([1]);
111  $querySettings->setRespectSysLanguage(true);
112  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_ON));
113  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
115  $posts = $query->execute()->toArray();
116  self::assertCount(2, $posts);
117  self::assertSame('A EN:Post2', $posts[0]->getTitle());
118  self::assertSame('B EN:Post1', $posts[1]->getTitle());
119  }
120 
121  #[Test]
123  {
124  $query = $this->get(PostRepository::class)->createQuery();
125  $querySettings = $query->getQuerySettings();
126  $querySettings->setStoragePageIds([1]);
127  $querySettings->setRespectSysLanguage(false);
128  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_MIXED));
129  $query->matching($query->in('uid', [4]));
131  $posts = $query->execute()->toArray();
132  self::assertCount(1, $posts);
133  self::assertSame('Post2', $posts[0]->getTitle());
134  }
135 
139  #[Test]
141  {
142  $query = $this->get(PostRepository::class)->createQuery();
143  $querySettings = $query->getQuerySettings();
144  $querySettings->setStoragePageIds([1]);
145  $querySettings->setRespectSysLanguage(true);
146  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
147  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
149  $posts = $query->execute()->toArray();
150  self::assertCount(2, $posts);
151  self::assertSame('GR:Post1', $posts[0]->getTitle());
152  self::assertSame('GR:Post11', $posts[1]->getTitle());
153  }
154 
158  #[Test]
160  {
161  $query = $this->get(PostRepository::class)->createQuery();
162  $querySettings = $query->getQuerySettings();
163  $querySettings->setStoragePageIds([1]);
164  $querySettings->setRespectSysLanguage(true);
165  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
166  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
168  $posts = $query->execute()->toArray();
169  self::assertCount(2, $posts);
170  self::assertSame('GR:Post1', $posts[0]->getTitle());
171  self::assertSame('GR:Post11', $posts[1]->getTitle());
172  }
173 
174  public static function ‪fetchingTranslatedPostByUidDataProvider(): array
175  {
176  return [
177  'with one id' => [
178  'input' => [12],
179  'expectedTitles' => ['GR:Post11'],
180  ],
181  'with two ids' => [
182  'input' => [12, 1],
183  'expectedTitles' => ['GR:Post11', 'GR:Post1'],
184  ],
185  ];
186  }
187 
188  #[DataProvider('fetchingTranslatedPostByUidDataProvider')]
189  #[Test]
190  public function ‪fetchingTranslatedPostByInClauseWithStrictLanguageSettings(array $input, array $expectedTitles): void
191  {
192  $query = $this->get(PostRepository::class)->createQuery();
193  $querySettings = $query->getQuerySettings();
194  $querySettings->setStoragePageIds([1]);
195  $querySettings->setRespectSysLanguage(true);
196  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
197  $query->matching($query->in('uid', $input));
199  $posts = $query->execute()->toArray();
200  // @todo: wrong assertion
201  // We're simulating a strict language configuration where a blog post (uid=12 or uid=1) has been translated to another
202  // language. However, Extbase is not able to find the translated record via ->in() and therefore returns an
203  // empty result set. This will be fixed with https://review.typo3.org/c/Packages/TYPO3.CMS/+/67893
204  self::assertCount(0, $posts);
205  // self::assertCount(count($expectedTitles), $posts);
206  // self::assertEqualsCanonicalizing($expectedTitles, array_map(static fn(Post $post): string => $post->getTitle(), $posts));
207  }
208 
209  #[DataProvider('fetchingTranslatedPostByUidDataProvider')]
210  #[Test]
211  public function ‪fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings(array $input, array $expectedTitles): void
212  {
213  $query = $this->get(PostRepository::class)->createQuery();
214  $querySettings = $query->getQuerySettings();
215  $querySettings->setStoragePageIds([1]);
216  $querySettings->setRespectSysLanguage(true);
217  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_ON));
218  $constraints = [];
219  foreach ($input as ‪$uid) {
220  $constraints[] = $query->equals('uid', ‪$uid);
221  }
222  $query->matching($query->logicalOr(...$constraints));
224  $posts = $query->execute()->toArray();
225  // @todo: wrong assertion
226  // We're simulating a strict language configuration where a blog post (uid=12 or uid=1) has been translated to another
227  // language. However, Extbase is not able to find the translated record via ->equals(uid=12 OR uid=1 OR ...) and therefore returns an
228  // empty result set. This will be fixed with https://review.typo3.org/c/Packages/TYPO3.CMS/+/67893
229  self::assertCount(0, $posts);
230  // self::assertCount(count($expectedTitles), $posts);
231  // self::assertEqualsCanonicalizing($expectedTitles, array_map(static fn(Post $post): string => $post->getTitle(), $posts));
232  }
233 
234  #[Test]
236  {
237  $query = $this->get(PostRepository::class)->createQuery();
238  $querySettings = $query->getQuerySettings();
239  $querySettings->setStoragePageIds([1]);
240  $querySettings->setRespectSysLanguage(true);
241  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
242  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
244  $posts = $query->execute()->toArray();
245  self::assertCount(2, $posts);
246  self::assertSame('A EN:Post2', $posts[0]->getTitle());
247  self::assertSame('B EN:Post1', $posts[1]->getTitle());
248  }
249 
254  #[Test]
255  public function ‪orderingByBlogTitle(): void
256  {
257  $query = $this->get(PostRepository::class)->createQuery();
258  $querySettings = $query->getQuerySettings();
259  $querySettings->setStoragePageIds([1]);
260  $querySettings->setRespectSysLanguage(true);
261  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
262  $query->setOrderings([
263  'blog.title' => ‪QueryInterface::ORDER_ASCENDING,
265  ]);
267  $posts = $query->execute()->toArray();
268  self::assertCount(2, $posts);
269  self::assertSame('B EN:Post1', $posts[0]->getTitle());
270  self::assertSame('A EN:Post2', $posts[1]->getTitle());
271  }
272 
278  #[Test]
280  {
281  $query = $this->get(PostRepository::class)->createQuery();
282  $querySettings = $query->getQuerySettings();
283  $querySettings->setStoragePageIds([1]);
284  $querySettings->setRespectSysLanguage(true);
285  $querySettings->setIgnoreEnableFields(true);
286  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_ON));
287  // we need it to have stable results on pgsql
288  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
290  $posts = $query->execute()->toArray();
291  self::assertCount(5, $posts);
292  self::assertSame('Post10', $posts[3]->getTitle());
293  }
294 
299  #[Test]
301  {
302  $query = $this->get(PostRepository::class)->createQuery();
303  $querySettings = $query->getQuerySettings();
304  $querySettings->setStoragePageIds([1]);
305  $querySettings->setRespectSysLanguage(true);
306  $querySettings->setIgnoreEnableFields(true);
307  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
308  // We need it to have stable results on pgsql
309  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
311  $posts = $query->execute()->toArray();
312  self::assertCount(3, $posts);
313  self::assertSame('GR:Post1', $posts[0]->getTitle());
314  self::assertSame('GR:Post10', $posts[1]->getTitle());
315  self::assertSame('GR:Post11', $posts[2]->getTitle());
316  }
317 
325  #[Test]
327  {
328  $query = $this->get(PostRepository::class)->createQuery();
329  $querySettings = $query->getQuerySettings();
330  $querySettings->setStoragePageIds([1]);
331  $querySettings->setRespectSysLanguage(true);
332  $querySettings->setIgnoreEnableFields(true);
333  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_MIXED));
334  //we need it to have stable results on pgsql
335  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
337  $posts = $query->execute()->toArray();
338  self::assertCount(5, $posts);
339  self::assertSame('GR:Post1', $posts[0]->getTitle());
340  self::assertSame('Post2', $posts[1]->getTitle());
341  self::assertSame('Post3', $posts[2]->getTitle());
342  // once the issue is fixed this assertions should be GR:Post10
343  self::assertSame('Post10', $posts[3]->getTitle());
344  self::assertSame('GR:Post11', $posts[4]->getTitle());
345  }
346 
350  #[Test]
351  public function ‪fetchingTranslatedPostByTitle(): void
352  {
353  $query = $this->get(PostRepository::class)->createQuery();
354  $querySettings = $query->getQuerySettings();
355  $querySettings->setStoragePageIds([1]);
356  $querySettings->setRespectSysLanguage(true);
357  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
358  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
359  $query->matching($query->equals('title', 'GR:Post1'));
361  $posts = $query->execute()->toArray();
362  self::assertCount(1, $posts);
363  self::assertSame('GR:Post1', $posts[0]->getTitle());
364  }
365 
370  #[Test]
371  public function ‪fetchingTranslatedPostByBlogTitle(): void
372  {
373  $query = $this->get(PostRepository::class)->createQuery();
374  $querySettings = $query->getQuerySettings();
375  $querySettings->setStoragePageIds([1]);
376  $querySettings->setRespectSysLanguage(true);
377  $querySettings->setLanguageAspect(new ‪LanguageAspect(2, 2, ‪LanguageAspect::OVERLAYS_OFF));
378  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
379  $query->matching($query->equals('blog.title', 'Blog1'));
381  $posts = $query->execute()->toArray();
382  self::assertCount(2, $posts);
383  self::assertSame('GR:Post1', $posts[0]->getTitle());
384  self::assertSame('GR:Post11', $posts[1]->getTitle());
385  }
386 
387  #[Test]
388  public function ‪fetchingPostByTagName(): void
389  {
390  $query = $this->get(PostRepository::class)->createQuery();
391  $querySettings = $query->getQuerySettings();
392  $querySettings->setRespectStoragePage(false);
393  $querySettings->setRespectSysLanguage(true);
394  $querySettings->setLanguageAspect(new ‪LanguageAspect(0, 0, ‪LanguageAspect::OVERLAYS_OFF));
395  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
396  $query->matching($query->equals('tags.name', 'Tag1'));
398  $posts = $query->execute()->toArray();
399  self::assertCount(3, $posts);
400  self::assertSame('Post1', $posts[0]->getTitle());
401  }
402 
403  #[Test]
404  public function ‪fetchingTranslatedPostByTagName(): void
405  {
406  $query = $this->get(PostRepository::class)->createQuery();
407  $querySettings = $query->getQuerySettings();
408  $querySettings->setRespectStoragePage(false);
409  $querySettings->setRespectSysLanguage(true);
410  $querySettings->setLanguageAspect(new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_OFF));
411  $query->setOrderings(['title' => ‪QueryInterface::ORDER_ASCENDING]);
412  $query->matching($query->equals('tags.name', 'Tag1'));
414  $posts = $query->execute()->toArray();
415  self::assertCount(2, $posts);
416  self::assertSame('A EN:Post2', $posts[0]->getTitle());
417  self::assertCount(1, $posts[0]->getTags());
418  self::assertSame('B EN:Post1', $posts[1]->getTitle());
419  self::assertCount(2, $posts[1]->getTags());
420  }
421 }
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostByTagName
‪fetchingPostByTagName()
Definition: TranslationTest.php:388
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsGreekPostsWithHideNonTranslated
‪fetchingPostsReturnsGreekPostsWithHideNonTranslated()
Definition: TranslationTest.php:159
‪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:67
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\countReturnsCorrectNumberOfPostsInEnglishLanguage
‪countReturnsCorrectNumberOfPostsInEnglishLanguage()
Definition: TranslationTest.php:82
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsEnglishPostsWithFallback
‪fetchingPostsReturnsEnglishPostsWithFallback()
Definition: TranslationTest.php:106
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsWithIgnoreEnableField
‪fetchingHiddenPostsWithIgnoreEnableField()
Definition: TranslationTest.php:279
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: TranslationTest.php:36
‪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:351
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingPostsReturnsGreekPostsWithFallback
‪fetchingPostsReturnsGreekPostsWithFallback()
Definition: TranslationTest.php:140
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByBlogTitle
‪fetchingTranslatedPostByBlogTitle()
Definition: TranslationTest.php:371
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\orderingByTitleRespectsEnglishTitles
‪orderingByTitleRespectsEnglishTitles()
Definition: TranslationTest.php:235
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByUidDataProvider
‪static fetchingTranslatedPostByUidDataProvider()
Definition: TranslationTest.php:174
‪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:94
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByTagName
‪fetchingTranslatedPostByTagName()
Definition: TranslationTest.php:404
‪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:190
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest
Definition: TranslationTest.php:35
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsReturnsHiddenOverlay
‪fetchingHiddenPostsReturnsHiddenOverlay()
Definition: TranslationTest.php:300
‪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:255
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_OFF
‪const OVERLAYS_OFF
Definition: LanguageAspect.php:74
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:30
‪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:122
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings
‪fetchingTranslatedPostByEqualsUidClauseWithStrictLanguageSettings(array $input, array $expectedTitles)
Definition: TranslationTest.php:211
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\setUp
‪setUp()
Definition: TranslationTest.php:40
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\TranslationTest\fetchingHiddenPostsReturnsHiddenOverlayOverlayEnabled
‪fetchingHiddenPostsReturnsHiddenOverlayOverlayEnabled()
Definition: TranslationTest.php:326