GedmoOrmTest::testPersonalTranslatableEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Tests\Traits;
15
16
use Doctrine\Common\EventManager;
17
use Doctrine\ORM\Version;
18
use Gedmo\Translatable\TranslatableListener;
19
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
20
use Sonata\TranslationBundle\Enum\TranslationFilterMode;
21
use Sonata\TranslationBundle\Filter\TranslationFieldFilter;
22
use Sonata\TranslationBundle\Test\DoctrineOrmTestCase;
23
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslatable;
24
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\ArticlePersonalTranslation;
25
26
final class GedmoOrmTest extends DoctrineOrmTestCase
27
{
28
    public const ARTICLE = ArticlePersonalTranslatable::class;
29
    public const TRANSLATION = ArticlePersonalTranslation::class;
30
31
    /** @var TranslatableListener */
32
    private $translatableListener;
33
34
    protected function setUp(): void
35
    {
36
        parent::setUp();
37
38
        if (!class_exists(Version::class)) {
39
            $this->markTestSkipped('Doctrine ORM is not available.');
40
        }
41
42
        $evm = new EventManager();
43
        $this->translatableListener = new TranslatableListener();
44
        $this->translatableListener->setTranslatableLocale('en');
45
        $this->translatableListener->setDefaultLocale('en');
46
        $evm->addEventSubscriber($this->translatableListener);
47
48
        $this->getMockSqliteEntityManager($evm);
49
    }
50
51
    public function testPersonalTranslatableEntity(): void
52
    {
53
        $article = new ArticlePersonalTranslatable();
54
        $article->setTitle('en');
55
56
        $this->em->persist($article);
57
        $this->em->flush();
58
59
        $this->translatableListener->setTranslatableLocale('de');
60
        $article->setTitle('de');
61
62
        $ltTranslation = new ArticlePersonalTranslation();
63
        $ltTranslation
64
            ->setField('title')
65
            ->setContent('lt')
66
            ->setObject($article)
67
            ->setLocale('lt')
68
        ;
69
        $this->em->persist($ltTranslation);
70
        $this->em->persist($article);
71
        $this->em->flush();
72
73
        // Tests if Gedmo\Locale annotation exists
74
        $article->setLocale('pl');
75
        $article->setTitle('pl');
76
        $this->em->persist($article);
77
        $this->em->flush();
78
79
        $this->em->clear();
80
81
        $article = $this->em->find(self::ARTICLE, ['id' => 1]);
82
        $translations = $article->getTranslations();
83
        $this->assertCount(3, $translations);
84
    }
85
86
    public function testTranslationFieldFilter(): void
87
    {
88
        $qb = $this->em->createQueryBuilder()
89
                       ->select('o')
90
                       ->from(self::ARTICLE, 'o');
91
        $builder = new ProxyQuery($qb);
92
93
        $filter = new TranslationFieldFilter(TranslationFilterMode::GEDMO);
94
        $filter->initialize('title');
95
96
        $filter->filter($builder, 'o', 'title', ['type' => null, 'value' => 'foo']);
97
        $this->assertSame(
98
            'SELECT o FROM '.self::ARTICLE.' o LEFT JOIN o.translations tff'
99
            ." WHERE (tff.field = 'title' AND tff.content LIKE '%foo%') OR o.title LIKE '%foo%'",
100
            $builder->getDQL()
101
        );
102
        $this->assertTrue($filter->isActive());
103
    }
104
105
    public function testTranslationFieldFilterWithoutValue(): void
106
    {
107
        $qb = $this->em->createQueryBuilder()
108
                       ->select('o')
109
                       ->from(self::ARTICLE, 'o');
110
        $builder = new ProxyQuery($qb);
111
112
        $filter = new TranslationFieldFilter(TranslationFilterMode::GEDMO);
113
        $filter->initialize('title');
114
115
        $filter->filter($builder, 'o', 'title', ['type' => null, 'value' => null]);
116
        $this->assertSame(
117
            'SELECT o FROM '.self::ARTICLE.' o',
118
            $builder->getDQL()
119
        );
120
        $this->assertFalse($filter->isActive());
121
    }
122
123
    public function testTranslationFieldFilterIfAlreadyJoined(): void
124
    {
125
        $qb = $this->em->createQueryBuilder()
126
                       ->select('o')
127
                       ->from(self::ARTICLE, 'o')
128
                       ->leftJoin('o.translations', 'tff');
129
        $builder = new ProxyQuery($qb);
130
131
        $filter = new TranslationFieldFilter(TranslationFilterMode::GEDMO);
132
        $filter->initialize('title');
133
134
        $filter->filter($builder, 'o', 'title', ['type' => null, 'value' => 'foo']);
135
        $this->assertSame(
136
            'SELECT o FROM '.self::ARTICLE.' o LEFT JOIN o.translations tff'
137
            ." WHERE (tff.field = 'title' AND tff.content LIKE '%foo%') OR o.title LIKE '%foo%'",
138
            $builder->getDQL()
139
        );
140
        $this->assertTrue($filter->isActive());
141
    }
142
143
    protected function getUsedEntityFixtures(): array
144
    {
145
        return [
146
            self::ARTICLE,
147
            self::TRANSLATION,
148
        ];
149
    }
150
}
151