|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Doctrine Behavioral Extensions package. |
| 7 | + * (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Gedmo\Translatable\Issue; |
| 13 | + |
| 14 | +use Doctrine\Common\EventManager; |
| 15 | +use Gedmo\Tests\Tool\BaseTestCaseORM; |
| 16 | +use Gedmo\Tests\Translatable\Fixture\Issue2167\Article; |
| 17 | +use Gedmo\Translatable\Entity\Translation; |
| 18 | +use Gedmo\Translatable\TranslatableListener; |
| 19 | + |
| 20 | +class Issue2167Test extends BaseTestCaseORM |
| 21 | +{ |
| 22 | + private const TRANSLATION = Translation::class; |
| 23 | + private const ENTITY = Article::class; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var TranslatableListener |
| 27 | + */ |
| 28 | + private $translatableListener; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $evm = new EventManager(); |
| 35 | + |
| 36 | + $this->translatableListener = new TranslatableListener(); |
| 37 | + $this->translatableListener->setTranslatableLocale('en'); |
| 38 | + $this->translatableListener->setDefaultLocale('en'); |
| 39 | + $this->translatableListener->setTranslationFallback(false); |
| 40 | + $evm->addEventSubscriber($this->translatableListener); |
| 41 | + |
| 42 | + $this->getDefaultMockSqliteEntityManager($evm); |
| 43 | + } |
| 44 | + |
| 45 | + public function testShouldFindInheritedClassTranslations(): void |
| 46 | + { |
| 47 | + $enTitle = 'My english title'; |
| 48 | + $deTitle = 'My german title'; |
| 49 | + |
| 50 | + // English |
| 51 | + $entity = new Article(); |
| 52 | + $entity->setTitle($enTitle); |
| 53 | + $entity->setLocale('en'); |
| 54 | + $this->em->persist($entity); |
| 55 | + $this->em->flush(); |
| 56 | + |
| 57 | + // German |
| 58 | + $entity->setLocale('de'); |
| 59 | + $entity->setTitle($deTitle); |
| 60 | + $this->em->flush(); |
| 61 | + |
| 62 | + // Find with default translation value as null value (default setting) |
| 63 | + $entityInEn = $this->findUsingQueryBuilder('en'); |
| 64 | + $entityInDe = $this->findUsingQueryBuilder('de'); |
| 65 | + $entityInFr = $this->findUsingQueryBuilder('fr'); |
| 66 | + |
| 67 | + static::assertSame($enTitle, $entityInEn->getTitle()); |
| 68 | + static::assertSame($deTitle, $entityInDe->getTitle()); |
| 69 | + static::assertNull($entityInFr->getTitle()); |
| 70 | + |
| 71 | + // Find with default translation value as empty string |
| 72 | + $this->translatableListener->setDefaultTranslationValue(''); |
| 73 | + |
| 74 | + $entityInEn = $this->findUsingQueryBuilder('en'); |
| 75 | + $entityInDe = $this->findUsingQueryBuilder('de'); |
| 76 | + $entityInFr = $this->findUsingQueryBuilder('fr'); |
| 77 | + |
| 78 | + static::assertSame($enTitle, $entityInEn->getTitle()); |
| 79 | + static::assertSame($deTitle, $entityInDe->getTitle()); |
| 80 | + static::assertSame('', $entityInFr->getTitle()); |
| 81 | + |
| 82 | + // Find with default translation value as not empty string |
| 83 | + $this->translatableListener->setDefaultTranslationValue('no_translated'); |
| 84 | + |
| 85 | + $entityInEn = $this->findUsingQueryBuilder('en'); |
| 86 | + $entityInDe = $this->findUsingQueryBuilder('de'); |
| 87 | + $entityInFr = $this->findUsingQueryBuilder('fr'); |
| 88 | + |
| 89 | + static::assertSame($enTitle, $entityInEn->getTitle()); |
| 90 | + static::assertSame($deTitle, $entityInDe->getTitle()); |
| 91 | + static::assertSame('no_translated', $entityInFr->getTitle()); |
| 92 | + } |
| 93 | + |
| 94 | + protected function getUsedEntityFixtures(): array |
| 95 | + { |
| 96 | + return [ |
| 97 | + self::TRANSLATION, |
| 98 | + self::ENTITY, |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + private function findUsingQueryBuilder(string $locale): ?Article |
| 103 | + { |
| 104 | + $this->em->clear(); |
| 105 | + $this->translatableListener->setTranslatableLocale($locale); |
| 106 | + |
| 107 | + $qb = $this->em->createQueryBuilder()->select('e')->from(self::ENTITY, 'e'); |
| 108 | + |
| 109 | + return $qb->getQuery()->getSingleResult(); |
| 110 | + } |
| 111 | +} |
0 commit comments