1652424a31c2f5d669745724f2db5b11052498e2
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / QueryParserTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 class QueryParserTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
20 {
21
22 /**
23 * @var array
24 */
25 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
26
27 /**
28 * @var array
29 */
30 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
34 */
35 protected $objectManager;
36
37 /**
38 * @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository
39 */
40 protected $blogRepository;
41
42 /**
43 * Sets up this test suite.
44 */
45 protected function setUp()
46 {
47 parent::setUp();
48
49 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/categories.xml');
50 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
51 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
52 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags-mm.xml');
53 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/persons.xml');
54 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
55 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
56 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/category-mm.xml');
57 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/fe_users.xml');
58 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/fe_groups.xml');
59
60 $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
61 $this->blogRepository = $this->objectManager->get(\ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository::class);
62 }
63
64 /**
65 * @test
66 */
67 public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForOrQuery()
68 {
69 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
70 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
71 $query = $postRepository->createQuery();
72 $query->matching(
73 $query->logicalAnd(
74 $query->equals('blog', 3),
75 $query->logicalOr(
76 $query->equals('tags.name', 'Tag12'),
77 $query->equals('author.tags.name', 'TagForAuthor1')
78 )
79 )
80 );
81
82 $result = $query->execute()->toArray();
83 $this->assertEquals(3, count($result));
84 }
85
86 /**
87 * Test ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
88 *
89 * @test
90 */
91 public function queryWithRelationHasAndBelongsToManyReturnsExpectedResult()
92 {
93 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
94 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
95 $query = $postRepository->createQuery();
96 $query->matching(
97 $query->equals('tags.name', 'Tag12')
98 );
99 $result = $query->execute()->toArray();
100 $this->assertEquals(2, count($result));
101 }
102
103 /**
104 * Test ColumnMap::RELATION_HAS_MANY
105 *
106 * @test
107 */
108 public function queryWithRelationHasManyWithoutParentKeyFieldNameReturnsExpectedResult()
109 {
110 /** @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository */
111 $frontendUserRepository = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserRepository');
112 $query = $frontendUserRepository->createQuery();
113
114 $result = $query->matching(
115 $query->equals('usergroup.title', 'Group A')
116 )->execute();
117 $this->assertSame(2, count($result));
118 }
119
120 /**
121 * Test ColumnMap::RELATION_HAS_ONE, ColumnMap::ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
122 *
123 * @test
124 */
125 public function queryWithRelationHasOneAndHasAndBelongsToManyWithoutParentKeyFieldNameReturnsExpectedResult()
126 {
127 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
128 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
129 $query = $postRepository->createQuery();
130 $query->matching(
131 $query->equals('author.firstname', 'Author')
132 );
133 $result = $query->execute()->toArray();
134 $this->assertEquals(2, count($result));
135 }
136
137 /**
138 * @test
139 */
140 public function orReturnsExpectedResult()
141 {
142 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
143 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
144 $query = $postRepository->createQuery();
145 $query->matching(
146 $query->logicalOr(
147 $query->equals('tags.name', 'Tag12'),
148 $query->equals('tags.name', 'Tag11')
149 )
150 );
151 $result = $query->execute()->toArray();
152 $this->assertEquals(2, count($result));
153 }
154
155 /**
156 * @test
157 */
158 public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForAndQuery()
159 {
160 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
161 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
162 $query = $postRepository->createQuery();
163 $query->matching(
164 $query->logicalAnd(
165 $query->equals('blog', 3),
166 $query->equals('tags.name', 'Tag12'),
167 $query->equals('author.tags.name', 'TagForAuthor1')
168 )
169 );
170 $result = $query->execute()->toArray();
171 $this->assertEquals(1, count($result));
172 }
173
174 /**
175 * @test
176 */
177 public function queryWithFindInSetReturnsExpectedResult()
178 {
179 /** @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository */
180 $frontendUserRepository = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserRepository');
181 $query = $frontendUserRepository->createQuery();
182
183 $result = $query->matching(
184 $query->contains('usergroup', 1)
185 )->execute();
186 $this->assertSame(2, count($result));
187 }
188
189 /**
190 * @test
191 */
192 public function queryForPostWithCategoriesReturnsPostWithCategories()
193 {
194 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
195 $query = $postRepository->createQuery();
196 $post = $query->matching($query->equals('uid', 1))->execute()->current();
197 $this->assertSame(3, count($post->getCategories()));
198 }
199 }