TYPO3 CMS  TYPO3_8-7
QueryParserTest.php
Go to the documentation of this file.
1 <?php
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 
18 
19 class QueryParserTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
20 {
21 
25  protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
26 
30  protected $coreExtensionsToLoad = ['extbase', 'fluid'];
31 
35  protected $objectManager;
36 
40  protected $blogRepository;
41 
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 
67  public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForOrQuery()
68  {
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 
91  public function queryWithRelationHasAndBelongsToManyReturnsExpectedResult()
92  {
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 
109  public function queryWithRelationHasManyWithoutParentKeyFieldNameReturnsExpectedResult()
110  {
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 
127  public function queryWithRelationHasOneAndHasAndBelongsToManyWithoutParentKeyFieldNameReturnsExpectedResult()
128  {
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 
143  public function orReturnsExpectedResult()
144  {
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 
161  public function queryWithMultipleRelationsToIdenticalTablesReturnsExpectedResultForAndQuery()
162  {
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 
180  public function queryWithFindInSetReturnsExpectedResult()
181  {
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 
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 }
static makeInstance($className,... $constructorArguments)