[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Collection / RecordCollectionRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\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 use Doctrine\DBAL\Platforms\SQLServerPlatform;
18 use TYPO3\CMS\Core\Collection\RecordCollectionRepository;
19 use TYPO3\CMS\Core\Collection\StaticRecordCollection;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
25 */
26 class RecordCollectionRepositoryTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
27 {
28 /**
29 * @var RecordCollectionRepository|\PHPUnit_Framework_MockObject_MockObject
30 */
31 protected $subject;
32
33 /**
34 * @var string
35 */
36 protected $testTableName;
37
38 /**
39 * Sets up this test case.
40 */
41 protected function setUp(): void
42 {
43 parent::setUp();
44
45 $this->subject = $this->getMockBuilder(RecordCollectionRepository::class)
46 ->setMethods(['getEnvironmentMode'])
47 ->getMock();
48 $this->testTableName = $this->getUniqueId('tx_testtable');
49 }
50
51 protected function tearDown(): void
52 {
53 parent::tearDown();
54
55 GeneralUtility::makeInstance(ConnectionPool::class)
56 ->getConnectionForTable('sys_collection')
57 ->truncate('sys_collection');
58 }
59
60 /**
61 * @test
62 */
63 public function doesFindByTypeReturnNull()
64 {
65 $type = RecordCollectionRepository::TYPE_Static;
66 $objects = $this->subject->findByType($type);
67 $this->assertNull($objects);
68 }
69
70 /**
71 * @test
72 */
73 public function doesFindByTypeReturnObjects()
74 {
75 $type = RecordCollectionRepository::TYPE_Static;
76 $this->insertTestData([
77 ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
78 ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
79 ]);
80
81 $objects = $this->subject->findByType($type);
82 $this->assertCount(2, $objects);
83 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
84 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
85 }
86
87 /**
88 * @test
89 */
90 public function doesFindByTableNameReturnNull()
91 {
92 $objects = $this->subject->findByTableName($this->testTableName);
93 $this->assertNull($objects);
94 }
95
96 /**
97 * @test
98 */
99 public function doesFindByTableNameReturnObjects()
100 {
101 $type = RecordCollectionRepository::TYPE_Static;
102 $this->insertTestData([
103 ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
104 ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
105 ]);
106 $objects = $this->subject->findByTableName($this->testTableName);
107
108 $this->assertCount(2, $objects);
109 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
110 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
111 }
112
113 /**
114 * @test
115 */
116 public function doesFindByTypeAndTableNameReturnNull()
117 {
118 $type = RecordCollectionRepository::TYPE_Static;
119 $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
120
121 $this->assertNull($objects);
122 }
123
124 /**
125 * @test
126 */
127 public function doesFindByTypeAndTableNameReturnObjects()
128 {
129 $type = RecordCollectionRepository::TYPE_Static;
130 $this->insertTestData([
131 ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
132 ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
133 ]);
134 $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
135
136 $this->assertCount(2, $objects);
137 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
138 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
139 }
140
141 /**
142 * @test
143 */
144 public function doesFindByUidReturnAnObjectInBackendMode()
145 {
146 $this->subject->method('getEnvironmentMode')->willReturn('BE');
147 $type = RecordCollectionRepository::TYPE_Static;
148 $this->insertTestData([
149 [
150 'uid' => 1,
151 'type' => $type,
152 'table_name' => $this->testTableName,
153 'deleted' => 0,
154 'hidden' => 0,
155 'starttime' => 0,
156 'endtime' => 0
157 ]
158 ]);
159 $object = $this->subject->findByUid(1);
160
161 $this->assertInstanceOf(StaticRecordCollection::class, $object);
162 }
163
164 /**
165 * @test
166 */
167 public function doesFindByUidRespectDeletedFieldInBackendMode()
168 {
169 $this->subject->method('getEnvironmentMode')->willReturn('BE');
170 $type = RecordCollectionRepository::TYPE_Static;
171 $this->insertTestData([
172 [
173 'uid' => 1,
174 'type' => $type,
175 'table_name' => $this->testTableName,
176 'deleted' => 1,
177 'hidden' => 0,
178 'starttime' => 0,
179 'endtime' => 0
180 ]
181 ]);
182 $object = $this->subject->findByUid(1);
183
184 $this->assertNull($object);
185 }
186
187 /**
188 * @test
189 */
190 public function doesFindByUidIgnoreOtherEnableFieldsInBackendMode()
191 {
192 $this->subject->method('getEnvironmentMode')->willReturn('BE');
193 $type = RecordCollectionRepository::TYPE_Static;
194 $this->insertTestData([
195 [
196 'uid' => 1,
197 'type' => $type,
198 'table_name' => $this->testTableName,
199 'hidden' => 1,
200 ],
201 [
202 'uid' => 2,
203 'type' => $type,
204 'table_name' => $this->testTableName,
205 'starttime' => time() + 99999,
206 ],
207 [
208 'uid' => 3,
209 'type' => $type,
210 'table_name' => $this->testTableName,
211 'endtime' => time() - 99999
212 ]
213 ]);
214 $hiddenObject = $this->subject->findByUid(1);
215 $futureObject = $this->subject->findByUid(2);
216 $expiredObject = $this->subject->findByUid(3);
217
218 $this->assertInstanceOf(StaticRecordCollection::class, $hiddenObject);
219 $this->assertInstanceOf(StaticRecordCollection::class, $futureObject);
220 $this->assertInstanceOf(StaticRecordCollection::class, $expiredObject);
221 }
222
223 /**
224 * @test
225 */
226 public function doesFindByUidReturnAnObjectInFrontendMode()
227 {
228 $this->subject->method('getEnvironmentMode')->willReturn('FE');
229 $type = RecordCollectionRepository::TYPE_Static;
230 $this->insertTestData([
231 [
232 'uid' => 1,
233 'type' => $type,
234 'table_name' => $this->testTableName,
235 'deleted' => 0,
236 'hidden' => 0,
237 'starttime' => 0,
238 'endtime' => 0
239 ]
240 ]);
241 $object = $this->subject->findByUid(1);
242
243 $this->assertInstanceOf(StaticRecordCollection::class, $object);
244 }
245
246 /**
247 * @test
248 */
249 public function doesFindByUidRespectEnableFieldsInFrontendMode()
250 {
251 $this->subject->method('getEnvironmentMode')->willReturn('FE');
252 $type = RecordCollectionRepository::TYPE_Static;
253 $this->insertTestData([
254 [
255 'uid' => 1,
256 'type' => $type,
257 'table_name' => $this->testTableName,
258 'deleted' => 1,
259 ],
260 [
261 'uid' => 2,
262 'type' => $type,
263 'table_name' => $this->testTableName,
264 'hidden' => 1,
265 ],
266 [
267 'uid' => 3,
268 'type' => $type,
269 'table_name' => $this->testTableName,
270 'starttime' => time() + 99999,
271 ],
272 [
273 'uid' => 4,
274 'type' => $type,
275 'table_name' => $this->testTableName,
276 'endtime' => time() - 99999
277 ]
278 ]);
279 $deletedObject = $this->subject->findByUid(1);
280 $hiddenObject = $this->subject->findByUid(2);
281 $futureObject = $this->subject->findByUid(3);
282 $expiredObject = $this->subject->findByUid(4);
283
284 $this->assertNull($deletedObject);
285 $this->assertNull($hiddenObject);
286 $this->assertNull($futureObject);
287 $this->assertNull($expiredObject);
288 }
289
290 /**
291 * Insert test rows into the sys_collection table
292 *
293 * @param array $rows
294 */
295 protected function insertTestData(array $rows)
296 {
297 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_collection');
298 $platform = $connection->getDatabasePlatform();
299 $sqlServerIdentityDisabled = false;
300 if ($platform instanceof SQLServerPlatform) {
301 try {
302 $connection->exec('SET IDENTITY_INSERT sys_collection ON');
303 $sqlServerIdentityDisabled = true;
304 } catch (\Doctrine\DBAL\DBALException $e) {
305 // Some tables like sys_refindex don't have an auto-increment uid field and thus no
306 // IDENTITY column. Instead of testing existence, we just try to set IDENTITY ON
307 // and catch the possible error that occurs.
308 }
309 }
310
311 $types = [];
312 $tableDetails = $connection->getSchemaManager()->listTableDetails('sys_collection');
313 foreach ($rows as $row) {
314 foreach ($row as $columnName => $columnValue) {
315 $types[] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
316 }
317 break;
318 }
319
320 foreach ($rows as $row) {
321 $connection->insert('sys_collection', $row, $types);
322 }
323
324 if ($sqlServerIdentityDisabled) {
325 // Reset identity if it has been changed
326 $connection->exec('SET IDENTITY_INSERT sys_collection OFF');
327 }
328 }
329 }