TYPO3 CMS  TYPO3_8-7
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 
23 
27 class RecordCollectionRepositoryTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
28 {
32  protected $subject;
33 
37  protected $testTableName;
38 
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 
74  public function doesFindByTypeReturnNull()
75  {
77  $objects = $this->subject->findByType($type);
78  $this->assertNull($objects);
79  }
80 
84  public function doesFindByTypeReturnObjects()
85  {
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 
102  {
103  $objects = $this->subject->findByTableName($this->testTableName);
104  $this->assertNull($objects);
105  }
106 
111  {
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 
128  {
130  $objects = $this->subject->findByTypeAndTableName($type, $this->testTableName);
131 
132  $this->assertNull($objects);
133  }
134 
139  {
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 
156  {
157  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
179  {
180  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
202  {
203  $this->subject->method('getEnvironmentMode')->willReturn('BE');
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 
238  {
239  $this->subject->method('getEnvironmentMode')->willReturn('FE');
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 
261  {
262  $this->subject->method('getEnvironmentMode')->willReturn('FE');
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 
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 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']