[BUGFIX] Field is not added to TCA if part of fieldname exists
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Collection / RecordCollectionRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Collection;
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 /**
18 * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
19 */
20 class RecordCollectionRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Collection\RecordCollectionRepository
24 */
25 protected $subject;
26
27 /**
28 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Database\DatabaseConnection
29 */
30 protected $databaseMock;
31
32 /**
33 * @var NULL|array
34 */
35 protected $getSingleRowCallbackReturnValue;
36
37 /**
38 * @var NULL|array
39 */
40 protected $getRowsCallbackReturnValue;
41
42 /**
43 * @var string
44 */
45 protected $testTableName;
46
47 /**
48 * Sets up this test case.
49 */
50 protected function setUp()
51 {
52 $this->databaseMock = $this->getMockBuilder(\TYPO3\CMS\Core\Database\DatabaseConnection::class)
53 ->setMethods(array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows', 'fullQuoteStr'))
54 ->getMock();
55 $this->subject = $this->getMockBuilder(\TYPO3\CMS\Core\Collection\RecordCollectionRepository::class)
56 ->setMethods(array('getDatabaseConnection'))
57 ->getMock();
58 $this->subject->expects($this->any())->method('getDatabaseConnection')->will($this->returnValue($this->databaseMock));
59 $this->testTableName = $this->getUniqueId('tx_testtable');
60 }
61
62 /**
63 * @test
64 */
65 public function doesFindByUidReturnNull()
66 {
67 $testUid = rand(1, 1000);
68 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
69 $this->getSingleRowCallbackReturnValue = null;
70 $object = $this->subject->findByUid($testUid);
71 $this->assertNull($object);
72 }
73
74 /**
75 * @test
76 */
77 public function doesFindByUidReturnObject()
78 {
79 $testUid = rand(1, 1000);
80 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
81 $this->getSingleRowCallbackReturnValue = array(
82 'uid' => $testUid,
83 'type' => \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static,
84 'table_name' => $this->testTableName
85 );
86 $object = $this->subject->findByUid($testUid);
87 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $object);
88 }
89
90 /**
91 * @test
92 */
93 public function doesFindByUidThrowException()
94 {
95 $this->expectException(\RuntimeException::class);
96 $this->expectExceptionCode(1328646798);
97
98 $testUid = rand(1, 1000);
99 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
100 $this->getSingleRowCallbackReturnValue = array(
101 'uid' => $testUid,
102 'type' => $this->getUniqueId('unknown')
103 );
104 $object = $this->subject->findByUid($testUid);
105 }
106
107 /**
108 * @test
109 */
110 public function doesFindByTypeReturnNull()
111 {
112 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
113 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
114 $this->getRowsCallbackReturnValue = null;
115 $objects = $this->subject->findByType($type);
116 $this->assertNull($objects);
117 }
118
119 /**
120 * @test
121 */
122 public function doesFindByTypeReturnObjects()
123 {
124 $testUid = rand(1, 1000);
125 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
126 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
127 $this->getRowsCallbackReturnValue = array(
128 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
129 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
130 );
131 $objects = $this->subject->findByType($type);
132 $this->assertEquals(2, count($objects));
133 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
134 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
135 }
136
137 /**
138 * @test
139 */
140 public function doesFindByTableNameReturnNull()
141 {
142 $testTable = $this->getUniqueId('sys_collection_');
143 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
144 $this->getRowsCallbackReturnValue = null;
145 $objects = $this->subject->findByTableName($testTable);
146 $this->assertNull($objects);
147 }
148
149 /**
150 * @test
151 */
152 public function doesFindByTableNameReturnObjects()
153 {
154 $testUid = rand(1, 1000);
155 $testTable = $this->getUniqueId('sys_collection_');
156 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
157 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
158 $this->getRowsCallbackReturnValue = array(
159 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
160 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
161 );
162 $objects = $this->subject->findByTableName($testTable);
163 $this->assertEquals(2, count($objects));
164 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
165 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
166 }
167
168 /**
169 * @test
170 */
171 public function doesFindByTypeAndTableNameReturnNull()
172 {
173 $testTable = $this->getUniqueId('sys_collection_');
174 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
175 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
176 $this->getRowsCallbackReturnValue = null;
177 $objects = $this->subject->findByTypeAndTableName($type, $testTable);
178 $this->assertNull($objects);
179 }
180
181 /**
182 * @test
183 */
184 public function doesFindByTypeAndTableNameReturnObjects()
185 {
186 $testUid = rand(1, 1000);
187 $testTable = $this->getUniqueId('sys_collection_');
188 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
189 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
190 $this->getRowsCallbackReturnValue = array(
191 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
192 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
193 );
194 $objects = $this->subject->findByTypeAndTableName($type, $testTable);
195 $this->assertEquals(2, count($objects));
196 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
197 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
198 }
199
200 /**
201 * Callback for exec_SELECTgetSingleRow
202 *
203 * @param string $fields
204 * @param string $table
205 * @return NULL|array
206 */
207 public function getSingleRowCallback($fields, $table)
208 {
209 if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
210 $returnValue = $this->getSingleRowCallbackReturnValue;
211 } else {
212 $returnValue = $this->limitRecordFields($fields, $this->getSingleRowCallbackReturnValue);
213 }
214 return $returnValue;
215 }
216
217 /**
218 * Callback for exec_SELECTgetRows
219 *
220 * @param string $fields
221 * @param string $table
222 * @return NULL|array
223 */
224 public function getRowsCallback($fields, $table)
225 {
226 if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
227 $returnValue = $this->getRowsCallbackReturnValue;
228 } else {
229 $returnValue = array();
230 foreach ($this->getRowsCallbackReturnValue as $record) {
231 $returnValue[] = $this->limitRecordFields($fields, $record);
232 }
233 }
234 return $returnValue;
235 }
236
237 /**
238 * Limits record fields to a given field list.
239 *
240 * @param string $fields List of fields
241 * @param array $record The database record (or the simulated one)
242 * @return array
243 */
244 protected function limitRecordFields($fields, array $record)
245 {
246 $result = array();
247 foreach ($record as $field => $value) {
248 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
249 $result[$field] = $value;
250 }
251 }
252 return $result;
253 }
254 }