[BUGFIX] Consider property clean if lazy loaded proxy is untouched
[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 * @group not-mssql
108 */
109 public function queryWithRelationHasManyWithoutParentKeyFieldNameReturnsExpectedResult()
110 {
111 /** @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository */
112 $frontendUserRepository = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserRepository');
113 $query = $frontendUserRepository->createQuery();
114 $query->matching(
115 $query->equals('usergroup.title', 'Group A')
116 );
117
118 $result = $query->execute()->toArray();
119 $this->assertCount(3, $result);
120 }
121
122 /**
123 * Test ColumnMap::RELATION_HAS_ONE, ColumnMap::ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY
124 *
125 * @test
126 */
127 public function queryWithRelationHasOneAndHasAndBelongsToManyWithoutParentKeyFieldNameReturnsExpectedResult()
128 {
129 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
130 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
131 $query = $postRepository->createQuery();
132 $query->matching(
133 $query->equals('author.firstname', 'Author')
134 );
135 $result = $query->execute()->toArray();
136 // there are 16 post in total, 2 without author, 1 hidden, 1 deleted => 12 posts
137 $this->assertCount(12, $result);
138 }
139
140 /**
141 * @test
142 */
143 public function orReturnsExpectedResult()
144 {
145 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
146 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
147 $query = $postRepository->createQuery();
148 $query->matching(
149 $query->logicalOr(
150 $query->equals('tags.name', 'Tag12'),
151 $query->equals('tags.name', 'Tag11')
152 )
153 );
154 $result = $query->execute()->toArray();
155 $this->assertCount(2, $result);
156 }
157
158 /**
159 * @test
160 */
161 public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForAndQuery()
162 {
163 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
164 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
165 $query = $postRepository->createQuery();
166 $query->matching(
167 $query->logicalAnd(
168 $query->equals('blog', 3),
169 $query->equals('tags.name', 'Tag12'),
170 $query->equals('author.tags.name', 'TagForAuthor1')
171 )
172 );
173 $result = $query->execute()->toArray();
174 $this->assertCount(1, $result);
175 }
176
177 /**
178 * @test
179 */
180 public function queryWithFindInSetReturnsExpectedResult()
181 {
182 /** @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository */
183 $frontendUserRepository = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Repository\\FrontendUserRepository');
184 $query = $frontendUserRepository->createQuery();
185
186 $result = $query->matching($query->contains('usergroup', 1))
187 ->execute();
188 $this->assertCount(3, $result);
189 }
190
191 /**
192 * @test
193 */
194 public function queryForPostWithCategoriesReturnsPostWithCategories()
195 {
196 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
197 $query = $postRepository->createQuery();
198 $post = $query->matching($query->equals('uid', 1))->execute()->current();
199 $this->assertCount(3, $post->getCategories());
200 }
201 }