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\Model; |
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\Knplabs\Article; |
24
|
|
|
use Sonata\TranslationBundle\Tests\Fixtures\Traits\ORM\Knplabs\ArticleTranslation; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Omar Jbara <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class KnplabsOrmTest extends DoctrineOrmTestCase |
30
|
|
|
{ |
31
|
|
|
private const ARTICLE = Article::class; |
32
|
|
|
private const TRANSLATION = ArticleTranslation::class; |
33
|
|
|
|
34
|
|
|
/** @var TranslatableListener */ |
35
|
|
|
private $translatableListener; |
36
|
|
|
|
37
|
|
|
protected function setUp(): void |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
|
41
|
|
|
if (!class_exists(Version::class)) { |
42
|
|
|
$this->markTestSkipped('Doctrine ORM is not available.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$evm = new EventManager(); |
46
|
|
|
$this->translatableListener = new TranslatableListener(); |
47
|
|
|
$this->translatableListener->setTranslatableLocale('en'); |
48
|
|
|
$this->translatableListener->setDefaultLocale('en'); |
49
|
|
|
$evm->addEventSubscriber($this->translatableListener); |
50
|
|
|
|
51
|
|
|
$this->getMockSqliteEntityManager($evm); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testTranslationFieldFilter(): void |
55
|
|
|
{ |
56
|
|
|
$qb = $this->em->createQueryBuilder() |
57
|
|
|
->select('a') |
58
|
|
|
->from(self::ARTICLE, 'a'); |
59
|
|
|
$builder = new ProxyQuery($qb); |
60
|
|
|
|
61
|
|
|
$filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS); |
62
|
|
|
$filter->initialize('title'); |
63
|
|
|
|
64
|
|
|
$filter->filter($builder, 'a', 'title', ['type' => null, 'value' => 'foo']); |
65
|
|
|
$this->assertSame( |
66
|
|
|
'SELECT a FROM '.self::ARTICLE.' a LEFT JOIN a.translations tff' |
67
|
|
|
." WHERE tff.title LIKE '%foo%'", |
68
|
|
|
$builder->getDQL() |
69
|
|
|
); |
70
|
|
|
$this->assertTrue($filter->isActive()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testTranslationFieldFilterWithoutValue(): void |
74
|
|
|
{ |
75
|
|
|
$qb = $this->em->createQueryBuilder() |
76
|
|
|
->select('a') |
77
|
|
|
->from(self::ARTICLE, 'a'); |
78
|
|
|
$builder = new ProxyQuery($qb); |
79
|
|
|
|
80
|
|
|
$filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS); |
81
|
|
|
$filter->initialize('title'); |
82
|
|
|
|
83
|
|
|
$filter->filter($builder, 'a', 'title', ['type' => null, 'value' => null]); |
84
|
|
|
$this->assertSame( |
85
|
|
|
'SELECT a FROM '.self::ARTICLE.' a', |
86
|
|
|
$builder->getDQL() |
87
|
|
|
); |
88
|
|
|
$this->assertFalse($filter->isActive()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testTranslationFieldFilterIfAlreadyJoined(): void |
92
|
|
|
{ |
93
|
|
|
$qb = $this->em->createQueryBuilder() |
94
|
|
|
->select('a') |
95
|
|
|
->from(self::ARTICLE, 'a') |
96
|
|
|
->leftJoin('a.translations', 'tff'); |
97
|
|
|
$builder = new ProxyQuery($qb); |
98
|
|
|
|
99
|
|
|
$filter = new TranslationFieldFilter(TranslationFilterMode::KNPLABS); |
100
|
|
|
$filter->initialize('title'); |
101
|
|
|
|
102
|
|
|
$filter->filter($builder, 'a', 'title', ['type' => null, 'value' => 'foo']); |
103
|
|
|
$this->assertSame( |
104
|
|
|
'SELECT a FROM '.self::ARTICLE.' a LEFT JOIN a.translations tff' |
105
|
|
|
." WHERE tff.title LIKE '%foo%'", |
106
|
|
|
$builder->getDQL() |
107
|
|
|
); |
108
|
|
|
$this->assertTrue($filter->isActive()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getUsedEntityFixtures(): array |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
self::ARTICLE, |
115
|
|
|
self::TRANSLATION, |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|