[!!!][TASK] Move TYPO3_DB to extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Tree / TableConfiguration / DatabaseTreeDataProviderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Tree\TableConfiguration;
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 Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
19 use TYPO3\CMS\Backend\Tree\TreeNode;
20 use TYPO3\CMS\Backend\Tree\TreeNodeCollection;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionContainerInterface;
25 use TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * Testcase for \TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
30 */
31 class DatabaseTreeDataProviderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
32 {
33 /**
34 * @var \PHPUnit_Framework_MockObject_MockObject|DatabaseTreeDataProvider|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
35 */
36 protected $subject;
37
38 /**
39 * @var TreeNode
40 */
41 protected $treeData;
42
43 /**
44 * Set up
45 */
46 protected function setUp()
47 {
48 $this->treeData = new TreeNode();
49 }
50
51 /**
52 * Setup prophecies for database stack
53 *
54 * @param int $instanceCount Number of instances of ConnectionPool::class to register
55 * @return \Prophecy\Prophecy\ObjectProphecy|\TYPO3\CMS\Core\Database\Query\QueryBuilder
56 */
57 protected function setupDatabaseMock(int $instanceCount = 1)
58 {
59 // Prophecies and revelations for a lot of the database stack classes
60 $connectionPoolProphecy = $this->prophesize(ConnectionPool::class);
61 $queryRestrictionProphecy = $this->prophesize(QueryRestrictionContainerInterface::class);
62 $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
63 $expressionBuilderProphecy = $this->prophesize(ExpressionBuilder::class);
64 $statementProphecy = $this->prophesize(Statement::class);
65
66 $expressionBuilderProphecy->eq(Argument::cetera())->willReturn('1=1');
67
68 // Simulate method call flow on database objects and verify correct query is built
69 $connectionPoolProphecy->getQueryBuilderForTable(Argument::cetera())
70 ->shouldBeCalled()
71 ->willReturn($queryBuilderProphecy->reveal());
72 $queryRestrictionProphecy->removeAll()
73 ->shouldBeCalled()
74 ->willReturn($queryRestrictionProphecy->reveal());
75 $queryBuilderProphecy->getRestrictions()
76 ->shouldBeCalled()
77 ->willReturn($queryRestrictionProphecy->reveal());
78 $queryBuilderProphecy->expr()
79 ->shouldBeCalled()
80 ->willReturn($expressionBuilderProphecy->reveal());
81 $queryBuilderProphecy->execute()
82 ->shouldBeCalled()
83 ->willReturn($statementProphecy->reveal());
84
85 $queryBuilderProphecy->select(Argument::cetera())
86 ->shouldBeCalled()
87 ->willReturn($queryBuilderProphecy->reveal());
88 $queryBuilderProphecy->from(Argument::cetera())
89 ->shouldBeCalled()
90 ->willReturn($queryBuilderProphecy->reveal());
91 $queryBuilderProphecy->createNamedParameter(Argument::cetera())
92 ->shouldBeCalled()
93 ->willReturnArgument(0);
94 $queryBuilderProphecy->where(Argument::cetera())
95 ->shouldBeCalled()
96 ->willReturn($queryBuilderProphecy->reveal());
97 $queryBuilderProphecy->setMaxResults(Argument::cetera())
98 ->shouldBeCalled()
99 ->willReturn($queryBuilderProphecy->reveal());
100
101 $statementProphecy->fetch()
102 ->shouldBeCalled()
103 ->willReturn(['uid' => 0, 'parent' => '']);
104
105 // Register connection pool revelation in framework, this is the entry point used by system unter test
106 for ($i = 1; $i <= $instanceCount; $i++) {
107 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphecy->reveal());
108 }
109
110 return $queryBuilderProphecy;
111 }
112
113 /**
114 * @param array $mockMethods
115 */
116 protected function initializeSubjectMock(array $mockMethods)
117 {
118 $this->subject = $this->getAccessibleMock(DatabaseTreeDataProvider::class, $mockMethods, [], '', false);
119 $this->subject->expects($this->any())->method('getRootUid')->will($this->returnValue(0));
120 $this->subject->_set('treeData', $this->treeData);
121 }
122
123 /**
124 * @test
125 */
126 public function loadTreeDataLevelMaximumSetToZeroWorks()
127 {
128 $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
129 $this->subject->_set('levelMaximum', 0);
130 $this->subject->expects($this->never())->method('getChildrenOf');
131 $this->subject->_call('loadTreeData');
132 }
133
134 /**
135 * @test
136 */
137 public function loadTreeDataLevelMaximumSetToOneWorks()
138 {
139 $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid', 'getChildrenOf']);
140 $this->subject->_set('levelMaximum', 1);
141 $this->subject->expects($this->once())->method('getChildrenOf')->with($this->treeData, 1);
142 $this->subject->_call('loadTreeData');
143 }
144
145 /**
146 * @test
147 */
148 public function getChildrenOfLevelMaximumSetToOneWorks()
149 {
150 $this->setupDatabaseMock();
151
152 $expectedTreeNode = new TreeNode();
153 $expectedTreeNode->setId(1);
154 $expectedStorage = new TreeNodeCollection();
155 $expectedStorage->append($expectedTreeNode);
156
157 $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
158 $this->subject->_set('levelMaximum', 1);
159 $this->subject->expects($this->once())->method('getRelatedRecords')->will($this->returnValue([1]));
160 $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
161
162 $this->assertEquals($expectedStorage, $storage);
163 }
164
165 /**
166 * @test
167 */
168 public function getChildrenOfLevelMaximumSetToTwoWorks()
169 {
170 $this->setupDatabaseMock(2);
171
172 $expectedStorage = new TreeNodeCollection();
173
174 $expectedFirstLevelTreeNode = new TreeNode();
175 $expectedFirstLevelTreeNode->setId(1);
176
177 $expectedSecondLevelTreeNode = new TreeNode();
178 $expectedSecondLevelTreeNode->setId(2);
179
180 $expectedStorageOfSecondLevelChildren = new TreeNodeCollection();
181 $expectedStorageOfSecondLevelChildren->append($expectedSecondLevelTreeNode);
182
183 $expectedFirstLevelTreeNode->setChildNodes($expectedStorageOfSecondLevelChildren);
184 $expectedStorage->append($expectedFirstLevelTreeNode);
185
186 $this->initializeSubjectMock(['getRelatedRecords', 'getRootUid']);
187 $this->subject->_set('levelMaximum', 2);
188 $this->subject->expects($this->at(0))->method('getRelatedRecords')->will($this->returnValue([1]));
189 $this->subject->expects($this->at(1))->method('getRelatedRecords')->will($this->returnValue([2]));
190 $storage = $this->subject->_call('getChildrenOf', $this->treeData, 1);
191
192 $this->assertEquals($expectedStorage, $storage);
193 }
194 }