‪TYPO3CMS  10.4
RecordCollectionRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\DBALException;
19 use Doctrine\DBAL\Platforms\SQLServerPlatform;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
30 class ‪RecordCollectionRepositoryTest extends FunctionalTestCase
31 {
35  protected ‪$subject;
36 
40  protected ‪$testTableName;
41 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48 
49  $this->subject = $this->getMockBuilder(RecordCollectionRepository::class)
50  ->setMethods(['getEnvironmentMode'])
51  ->getMock();
52  $this->testTableName = ‪StringUtility::getUniqueId('tx_testtable');
53  }
54 
55  protected function ‪tearDown(): void
56  {
57  parent::tearDown();
58 
59  GeneralUtility::makeInstance(ConnectionPool::class)
60  ->getConnectionForTable('sys_collection')
61  ->truncate('sys_collection');
62  }
63 
67  public function ‪doesFindByTypeReturnNull()
68  {
70  $objects = $this->subject->findByType($type);
71  self::assertNull($objects);
72  }
73 
77  public function ‪doesFindByTypeReturnObjects()
78  {
80  $this->‪insertTestData([
81  ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
82  ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
83  ]);
84 
85  $objects = $this->subject->findByType($type);
86  self::assertCount(2, $objects);
87  self::assertInstanceOf(StaticRecordCollection::class, $objects[0]);
88  self::assertInstanceOf(StaticRecordCollection::class, $objects[1]);
89  }
90 
94  public function ‪doesFindByTableNameReturnNull()
95  {
96  $objects = $this->subject->findByTableName($this->testTableName);
97  self::assertNull($objects);
98  }
99 
103  public function ‪doesFindByTableNameReturnObjects()
104  {
106  $this->‪insertTestData([
107  ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
108  ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
109  ]);
110  $objects = $this->subject->findByTableName($this->testTableName);
111 
112  self::assertCount(2, $objects);
113  self::assertInstanceOf(StaticRecordCollection::class, $objects[0]);
114  self::assertInstanceOf(StaticRecordCollection::class, $objects[1]);
115  }
116 
121  {
123  $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
124 
125  self::assertNull($objects);
126  }
127 
132  {
134  $this->‪insertTestData([
135  ['uid' => 1, 'type' => $type, 'table_name' => $this->testTableName],
136  ['uid' => 2, 'type' => $type, 'table_name' => $this->testTableName]
137  ]);
138  $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
139 
140  self::assertCount(2, $objects);
141  self::assertInstanceOf(StaticRecordCollection::class, $objects[0]);
142  self::assertInstanceOf(StaticRecordCollection::class, $objects[1]);
143  }
144 
149  {
150  $this->subject->method('getEnvironmentMode')->willReturn('BE');
152  $this->‪insertTestData([
153  [
154  'uid' => 1,
155  'type' => $type,
156  'table_name' => $this->testTableName,
157  'deleted' => 0,
158  'hidden' => 0,
159  'starttime' => 0,
160  'endtime' => 0
161  ]
162  ]);
163  $object = $this->subject->findByUid(1);
164 
165  self::assertInstanceOf(StaticRecordCollection::class, $object);
166  }
167 
172  {
173  $this->subject->method('getEnvironmentMode')->willReturn('BE');
175  $this->‪insertTestData([
176  [
177  'uid' => 1,
178  'type' => $type,
179  'table_name' => $this->testTableName,
180  'deleted' => 1,
181  'hidden' => 0,
182  'starttime' => 0,
183  'endtime' => 0
184  ]
185  ]);
186  $object = $this->subject->findByUid(1);
187 
188  self::assertNull($object);
189  }
190 
195  {
196  $this->subject->method('getEnvironmentMode')->willReturn('BE');
198  $this->‪insertTestData([
199  [
200  'uid' => 1,
201  'type' => $type,
202  'table_name' => $this->testTableName,
203  'hidden' => 1,
204  ],
205  [
206  'uid' => 2,
207  'type' => $type,
208  'table_name' => $this->testTableName,
209  'starttime' => time() + 99999,
210  ],
211  [
212  'uid' => 3,
213  'type' => $type,
214  'table_name' => $this->testTableName,
215  'endtime' => time() - 99999
216  ]
217  ]);
218  $hiddenObject = $this->subject->findByUid(1);
219  $futureObject = $this->subject->findByUid(2);
220  $expiredObject = $this->subject->findByUid(3);
221 
222  self::assertInstanceOf(StaticRecordCollection::class, $hiddenObject);
223  self::assertInstanceOf(StaticRecordCollection::class, $futureObject);
224  self::assertInstanceOf(StaticRecordCollection::class, $expiredObject);
225  }
226 
231  {
232  $this->subject->method('getEnvironmentMode')->willReturn('FE');
234  $this->‪insertTestData([
235  [
236  'uid' => 1,
237  'type' => $type,
238  'table_name' => $this->testTableName,
239  'deleted' => 0,
240  'hidden' => 0,
241  'starttime' => 0,
242  'endtime' => 0
243  ]
244  ]);
245  $object = $this->subject->findByUid(1);
246 
247  self::assertInstanceOf(StaticRecordCollection::class, $object);
248  }
249 
254  {
255  $this->subject->method('getEnvironmentMode')->willReturn('FE');
257  $this->‪insertTestData([
258  [
259  'uid' => 1,
260  'type' => $type,
261  'table_name' => $this->testTableName,
262  'deleted' => 1,
263  ],
264  [
265  'uid' => 2,
266  'type' => $type,
267  'table_name' => $this->testTableName,
268  'hidden' => 1,
269  ],
270  [
271  'uid' => 3,
272  'type' => $type,
273  'table_name' => $this->testTableName,
274  'starttime' => time() + 99999,
275  ],
276  [
277  'uid' => 4,
278  'type' => $type,
279  'table_name' => $this->testTableName,
280  'endtime' => time() - 99999
281  ]
282  ]);
283  $deletedObject = $this->subject->findByUid(1);
284  $hiddenObject = $this->subject->findByUid(2);
285  $futureObject = $this->subject->findByUid(3);
286  $expiredObject = $this->subject->findByUid(4);
287 
288  self::assertNull($deletedObject);
289  self::assertNull($hiddenObject);
290  self::assertNull($futureObject);
291  self::assertNull($expiredObject);
292  }
293 
299  protected function ‪insertTestData(array $rows)
300  {
301  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_collection');
302  $platform = $connection->getDatabasePlatform();
303  $sqlServerIdentityDisabled = false;
304  if ($platform instanceof SQLServerPlatform) {
305  try {
306  $connection->exec('SET IDENTITY_INSERT sys_collection ON');
307  $sqlServerIdentityDisabled = true;
308  } catch (DBALException $e) {
309  // Some tables like sys_refindex don't have an auto-increment uid field and thus no
310  // IDENTITY column. Instead of testing existence, we just try to set IDENTITY ON
311  // and catch the possible error that occurs.
312  }
313  }
314 
315  $types = [];
316  $tableDetails = $connection->getSchemaManager()->listTableDetails('sys_collection');
317  foreach ($rows as $row) {
318  foreach ($row as $columnName => $columnValue) {
319  $types[] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
320  }
321  break;
322  }
323 
324  foreach ($rows as $row) {
325  $connection->insert('sys_collection', $row, $types);
326  }
327 
328  if ($sqlServerIdentityDisabled) {
329  // Reset identity if it has been changed
330  $connection->exec('SET IDENTITY_INSERT sys_collection OFF');
331  }
332  }
333 }
‪TYPO3\CMS\Core\Collection\StaticRecordCollection
Definition: StaticRecordCollection.php:25
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\setUp
‪setUp()
Definition: RecordCollectionRepositoryTest.php:43
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidIgnoreOtherEnableFieldsInBackendMode
‪doesFindByUidIgnoreOtherEnableFieldsInBackendMode()
Definition: RecordCollectionRepositoryTest.php:192
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\TYPE_Static
‪const TYPE_Static
Definition: RecordCollectionRepository.php:32
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeReturnObjects
‪doesFindByTypeReturnObjects()
Definition: RecordCollectionRepositoryTest.php:75
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTableNameReturnNull
‪doesFindByTableNameReturnNull()
Definition: RecordCollectionRepositoryTest.php:92
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeReturnNull
‪doesFindByTypeReturnNull()
Definition: RecordCollectionRepositoryTest.php:65
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeAndTableNameReturnNull
‪doesFindByTypeAndTableNameReturnNull()
Definition: RecordCollectionRepositoryTest.php:118
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest
Definition: RecordCollectionRepositoryTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeAndTableNameReturnObjects
‪doesFindByTypeAndTableNameReturnObjects()
Definition: RecordCollectionRepositoryTest.php:129
‪TYPO3\CMS\Core\Tests\Functional\Collection
Definition: RecordCollectionRepositoryTest.php:16
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\insertTestData
‪insertTestData(array $rows)
Definition: RecordCollectionRepositoryTest.php:297
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTableNameReturnObjects
‪doesFindByTableNameReturnObjects()
Definition: RecordCollectionRepositoryTest.php:101
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\tearDown
‪tearDown()
Definition: RecordCollectionRepositoryTest.php:53
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidReturnAnObjectInFrontendMode
‪doesFindByUidReturnAnObjectInFrontendMode()
Definition: RecordCollectionRepositoryTest.php:228
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\$subject
‪RecordCollectionRepository PHPUnit Framework MockObject MockObject $subject
Definition: RecordCollectionRepositoryTest.php:34
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository
Definition: RecordCollectionRepository.php:28
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidRespectEnableFieldsInFrontendMode
‪doesFindByUidRespectEnableFieldsInFrontendMode()
Definition: RecordCollectionRepositoryTest.php:251
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidReturnAnObjectInBackendMode
‪doesFindByUidReturnAnObjectInBackendMode()
Definition: RecordCollectionRepositoryTest.php:146
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidRespectDeletedFieldInBackendMode
‪doesFindByUidRespectDeletedFieldInBackendMode()
Definition: RecordCollectionRepositoryTest.php:169
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\$testTableName
‪string $testTableName
Definition: RecordCollectionRepositoryTest.php:38
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22