77dc94217bb4d5a5d8d910fd545bd866a10e4c9a
[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 $query->matching(
114 $query->equals('usergroup.title', 'Group A')
115 );
116
117 $result = $query->execute()->toArray();
118 $this->assertCount(2, $result);
119 }
120
121 /**
122 * Test ColumnMap::RELATION_HAS_ONE, ColumnMap::ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
123 *
124 * @test
125 */
126 public function queryWithRelationHasOneAndHasAndBelongsToManyWithoutParentKeyFieldNameReturnsExpectedResult()
127 {
128 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
129 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
130 $query = $postRepository->createQuery();
131 $query->matching(
132 $query->equals('author.firstname', 'Author')
133 );
134 $result = $query->execute()->toArray();
135 $this->assertCount(2, $result);
136 }
137
138 /**
139 * @test
140 */
141 public function orReturnsExpectedResult()
142 {
143 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
144 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
145 $query = $postRepository->createQuery();
146 $query->matching(
147 $query->logicalOr(
148 $query->equals('tags.name', 'Tag12'),
149 $query->equals('tags.name', 'Tag11')
150 )
151 );
152 $result = $query->execute()->toArray();
153 $this->assertCount(2, $result);
154 }
155
156 /**
157 * @test
158 */
159 public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForAndQuery()
160 {
161 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
162 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
163 $query = $postRepository->createQuery();
164 $query->matching(
165 $query->logicalAnd(
166 $query->equals('blog', 3),
167 $query->equals('tags.name', 'Tag12'),
168 $query->equals('author.tags.name', 'TagForAuthor1')
169 )
170 );
171 $result = $query->execute()->toArray();
172 $this->assertCount(1, $result);
173 }
174
175 /**
176 * @test
177 */
178 public function queryWithFindInSetReturnsExpectedResult()
179 {
180 /** @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository */
181 $frontendUserRepository = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserRepository');
182 $query = $frontendUserRepository->createQuery();
183
184 $result = $query->matching($query->contains('usergroup', 1))
185 ->execute();
186 $this->assertCount(2, $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->assertCount(3, $post->getCategories());
198 }
199 }