‪TYPO3CMS  9.5
RecordCollectionRepositoryTest.php
Go to the documentation of this file.
1 <?php
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;
22 
26 class ‪RecordCollectionRepositoryTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
27 {
31  protected ‪$subject;
32 
36  protected ‪$testTableName;
37 
41  protected function ‪setUp()
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()
52  {
53  parent::tearDown();
54 
55  GeneralUtility::makeInstance(ConnectionPool::class)
56  ->getConnectionForTable('sys_collection')
57  ->truncate('sys_collection');
58  }
59 
63  public function ‪doesFindByTypeReturnNull()
64  {
66  $objects = $this->subject->findByType($type);
67  $this->assertNull($objects);
68  }
69 
73  public function ‪doesFindByTypeReturnObjects()
74  {
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 
90  public function ‪doesFindByTableNameReturnNull()
91  {
92  $objects = $this->subject->findByTableName($this->testTableName);
93  $this->assertNull($objects);
94  }
95 
99  public function ‪doesFindByTableNameReturnObjects()
100  {
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 
117  {
119  $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
120 
121  $this->assertNull($objects);
122  }
123 
128  {
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 
145  {
146  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
168  {
169  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
191  {
192  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
227  {
228  $this->subject->method('getEnvironmentMode')->willReturn('FE');
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 
250  {
251  $this->subject->method('getEnvironmentMode')->willReturn('FE');
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 
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 existance, 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 }
‪TYPO3\CMS\Core\Collection\StaticRecordCollection
Definition: StaticRecordCollection.php:24
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\setUp
‪setUp()
Definition: RecordCollectionRepositoryTest.php:39
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidIgnoreOtherEnableFieldsInBackendMode
‪doesFindByUidIgnoreOtherEnableFieldsInBackendMode()
Definition: RecordCollectionRepositoryTest.php:188
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository\TYPE_Static
‪const TYPE_Static
Definition: RecordCollectionRepository.php:30
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeReturnObjects
‪doesFindByTypeReturnObjects()
Definition: RecordCollectionRepositoryTest.php:71
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTableNameReturnNull
‪doesFindByTableNameReturnNull()
Definition: RecordCollectionRepositoryTest.php:88
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeReturnNull
‪doesFindByTypeReturnNull()
Definition: RecordCollectionRepositoryTest.php:61
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeAndTableNameReturnNull
‪doesFindByTypeAndTableNameReturnNull()
Definition: RecordCollectionRepositoryTest.php:114
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest
Definition: RecordCollectionRepositoryTest.php:27
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTypeAndTableNameReturnObjects
‪doesFindByTypeAndTableNameReturnObjects()
Definition: RecordCollectionRepositoryTest.php:125
‪TYPO3\CMS\Core\Tests\Functional\Collection
Definition: RecordCollectionRepositoryTest.php:2
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\insertTestData
‪insertTestData(array $rows)
Definition: RecordCollectionRepositoryTest.php:293
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByTableNameReturnObjects
‪doesFindByTableNameReturnObjects()
Definition: RecordCollectionRepositoryTest.php:97
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\tearDown
‪tearDown()
Definition: RecordCollectionRepositoryTest.php:49
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidReturnAnObjectInFrontendMode
‪doesFindByUidReturnAnObjectInFrontendMode()
Definition: RecordCollectionRepositoryTest.php:224
‪TYPO3\CMS\Core\Collection\RecordCollectionRepository
Definition: RecordCollectionRepository.php:26
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidRespectEnableFieldsInFrontendMode
‪doesFindByUidRespectEnableFieldsInFrontendMode()
Definition: RecordCollectionRepositoryTest.php:247
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidReturnAnObjectInBackendMode
‪doesFindByUidReturnAnObjectInBackendMode()
Definition: RecordCollectionRepositoryTest.php:142
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\doesFindByUidRespectDeletedFieldInBackendMode
‪doesFindByUidRespectDeletedFieldInBackendMode()
Definition: RecordCollectionRepositoryTest.php:165
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\$testTableName
‪string $testTableName
Definition: RecordCollectionRepositoryTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\Collection\RecordCollectionRepositoryTest\$subject
‪RecordCollectionRepository PHPUnit_Framework_MockObject_MockObject $subject
Definition: RecordCollectionRepositoryTest.php:30