595fbe7953f6153b48f9bc76d7f5e365c688267c
[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 TYPO3\CMS\Core\Collection\RecordCollectionRepository;
18 use TYPO3\CMS\Core\Collection\StaticRecordCollection;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
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()
42 {
43 parent::setUp();
44
45 $this->subject = $this->getMockBuilder(RecordCollectionRepository::class)
46 ->setMethods(['getEnvironmentMode'])
47 ->getMock();
48
49 $this->testTableName = $this->getUniqueId('tx_testtable');
50
51 $typoScriptFrontendController = GeneralUtility::makeInstance(
52 TypoScriptFrontendController::class,
53 null,
54 1,
55 0
56 );
57 $typoScriptFrontendController->showHiddenRecords = false;
58 $GLOBALS['TSFE'] = $typoScriptFrontendController;
59 }
60
61 protected function tearDown()
62 {
63 parent::tearDown();
64
65 GeneralUtility::makeInstance(ConnectionPool::class)
66 ->getConnectionForTable('sys_collection')
67 ->truncate('sys_collection');
68 }
69
70 /**
71 * @test
72 */
73 public function doesFindByTypeReturnNull()
74 {
75 $type = RecordCollectionRepository::TYPE_Static;
76 $objects = $this->subject->findByType($type);
77 $this->assertNull($objects);
78 }
79
80 /**
81 * @test
82 */
83 public function doesFindByTypeReturnObjects()
84 {
85 $type = RecordCollectionRepository::TYPE_Static;
86 $this->insertTestData([
87 ['type' => $type, 'table_name' => $this->testTableName],
88 ['type' => $type, 'table_name' => $this->testTableName]
89 ]);
90
91 $objects = $this->subject->findByType($type);
92 $this->assertCount(2, $objects);
93 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
94 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
95 }
96
97 /**
98 * @test
99 */
100 public function doesFindByTableNameReturnNull()
101 {
102 $objects = $this->subject->findByTableName($this->testTableName);
103 $this->assertNull($objects);
104 }
105
106 /**
107 * @test
108 */
109 public function doesFindByTableNameReturnObjects()
110 {
111 $type = RecordCollectionRepository::TYPE_Static;
112 $this->insertTestData([
113 ['type' => $type, 'table_name' => $this->testTableName],
114 ['type' => $type, 'table_name' => $this->testTableName]
115 ]);
116 $objects = $this->subject->findByTableName($this->testTableName);
117
118 $this->assertCount(2, $objects);
119 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
120 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
121 }
122
123 /**
124 * @test
125 */
126 public function doesFindByTypeAndTableNameReturnNull()
127 {
128 $type = RecordCollectionRepository::TYPE_Static;
129 $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
130
131 $this->assertNull($objects);
132 }
133
134 /**
135 * @test
136 */
137 public function doesFindByTypeAndTableNameReturnObjects()
138 {
139 $type = RecordCollectionRepository::TYPE_Static;
140 $this->insertTestData([
141 ['type' => $type, 'table_name' => $this->testTableName],
142 ['type' => $type, 'table_name' => $this->testTableName]
143 ]);
144 $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
145
146 $this->assertCount(2, $objects);
147 $this->assertInstanceOf(StaticRecordCollection::class, $objects[0]);
148 $this->assertInstanceOf(StaticRecordCollection::class, $objects[1]);
149 }
150
151 /**
152 * @test
153 */
154 public function doesFindByUidReturnAnObjectInBackendMode()
155 {
156 $this->subject->method('getEnvironmentMode')->willReturn('BE');
157 $type = RecordCollectionRepository::TYPE_Static;
158 $this->insertTestData([
159 [
160 'uid' => 1,
161 'type' => $type,
162 'table_name' => $this->testTableName,
163 'deleted' => 0,
164 'hidden' => 0,
165 'starttime' => 0,
166 'endtime' => 0
167 ]
168 ]);
169 $object = $this->subject->findByUid(1);
170
171 $this->assertInstanceOf(StaticRecordCollection::class, $object);
172 }
173
174 /**
175 * @test
176 */
177 public function doesFindByUidRespectDeletedFieldInBackendMode()
178 {
179 $this->subject->method('getEnvironmentMode')->willReturn('BE');
180 $type = RecordCollectionRepository::TYPE_Static;
181 $this->insertTestData([
182 [
183 'uid' => 1,
184 'type' => $type,
185 'table_name' => $this->testTableName,
186 'deleted' => 1,
187 'hidden' => 0,
188 'starttime' => 0,
189 'endtime' => 0
190 ]
191 ]);
192 $object = $this->subject->findByUid(1);
193
194 $this->assertNull($object);
195 }
196
197 /**
198 * @test
199 */
200 public function doesFindByUidIgnoreOtherEnableFieldsInBackendMode()
201 {
202 $this->subject->method('getEnvironmentMode')->willReturn('BE');
203 $type = RecordCollectionRepository::TYPE_Static;
204 $this->insertTestData([
205 [
206 'uid' => 1,
207 'type' => $type,
208 'table_name' => $this->testTableName,
209 'hidden' => 1,
210 ],
211 [
212 'uid' => 2,
213 'type' => $type,
214 'table_name' => $this->testTableName,
215 'starttime' => time() + 99999,
216 ],
217 [
218 'uid' => 3,
219 'type' => $type,
220 'table_name' => $this->testTableName,
221 'endtime' => time() - 99999
222 ]
223 ]);
224 $hiddenObject = $this->subject->findByUid(1);
225 $futureObject = $this->subject->findByUid(2);
226 $expiredObject = $this->subject->findByUid(3);
227
228 $this->assertInstanceOf(StaticRecordCollection::class, $hiddenObject);
229 $this->assertInstanceOf(StaticRecordCollection::class, $futureObject);
230 $this->assertInstanceOf(StaticRecordCollection::class, $expiredObject);
231 }
232
233 /**
234 * @test
235 */
236 public function doesFindByUidReturnAnObjectInFrontendMode()
237 {
238 $this->subject->method('getEnvironmentMode')->willReturn('FE');
239 $type = RecordCollectionRepository::TYPE_Static;
240 $this->insertTestData([
241 [
242 'uid' => 1,
243 'type' => $type,
244 'table_name' => $this->testTableName,
245 'deleted' => 0,
246 'hidden' => 0,
247 'starttime' => 0,
248 'endtime' => 0
249 ]
250 ]);
251 $object = $this->subject->findByUid(1);
252
253 $this->assertInstanceOf(StaticRecordCollection::class, $object);
254 }
255
256 /**
257 * @test
258 */
259 public function doesFindByUidRespectEnableFieldsInFrontendMode()
260 {
261 $this->subject->method('getEnvironmentMode')->willReturn('FE');
262 $type = RecordCollectionRepository::TYPE_Static;
263 $this->insertTestData([
264 [
265 'uid' => 1,
266 'type' => $type,
267 'table_name' => $this->testTableName,
268 'deleted' => 1,
269 ],
270 [
271 'uid' => 2,
272 'type' => $type,
273 'table_name' => $this->testTableName,
274 'hidden' => 1,
275 ],
276 [
277 'uid' => 3,
278 'type' => $type,
279 'table_name' => $this->testTableName,
280 'starttime' => time() + 99999,
281 ],
282 [
283 'uid' => 4,
284 'type' => $type,
285 'table_name' => $this->testTableName,
286 'endtime' => time() - 99999
287 ]
288 ]);
289 $deletedObject = $this->subject->findByUid(1);
290 $hiddenObject = $this->subject->findByUid(2);
291 $futureObject = $this->subject->findByUid(3);
292 $expiredObject = $this->subject->findByUid(4);
293
294 $this->assertNull($deletedObject);
295 $this->assertNull($hiddenObject);
296 $this->assertNull($futureObject);
297 $this->assertNull($expiredObject);
298 }
299
300 /**
301 * Insert test rows into the sys_collection table
302 *
303 * @param array $rows
304 */
305 protected function insertTestData(array $rows)
306 {
307 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_collection');
308
309 foreach ($rows as $row) {
310 $connection->insert('sys_collection', $row);
311 }
312 }
313 }