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