TYPO3 CMS  TYPO3_7-6
CategoryCollectionTest.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 
18 
23 {
27  private $subject;
28 
32  private $tableName = 'tx_foo_5001615c50bed';
33 
37  private $tables = ['sys_category', 'sys_category_record_mm'];
38 
42  private $categoryUid = 0;
43 
47  private $collectionRecord = [];
48 
52  private $numberOfRecords = 5;
53 
57  private $database;
58 
64  protected function setUp()
65  {
66  parent::setUp();
67  $this->database = $this->getDatabaseConnection();
68  $this->subject = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Category\Collection\CategoryCollection::class, $this->tableName);
69  $this->collectionRecord = [
70  'uid' => 0,
71  'title' => $this->getUniqueId('title'),
72  'description' => $this->getUniqueId('description'),
73  'table_name' => $this->tableName,
74  ];
75  $GLOBALS['TCA'][$this->tableName] = ['ctrl' => []];
76  // prepare environment
77  $this->createDummyTable();
78  $this->populateDummyTable();
79  $this->prepareTables();
81  }
82 
89  {
90  $this->subject->fromArray($this->collectionRecord);
91  $this->assertEquals($this->collectionRecord['uid'], $this->subject->getIdentifier());
92  $this->assertEquals($this->collectionRecord['uid'], $this->subject->getUid());
93  $this->assertEquals($this->collectionRecord['title'], $this->subject->getTitle());
94  $this->assertEquals($this->collectionRecord['description'], $this->subject->getDescription());
95  $this->assertEquals($this->collectionRecord['table_name'], $this->subject->getItemTableName());
96  }
97 
103  public function canCreateDummyCollection()
104  {
105  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord);
106  $this->assertInstanceOf(\TYPO3\CMS\Core\Category\Collection\CategoryCollection::class, $collection);
107  }
108 
115  {
116  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord, true);
117  $this->assertInstanceOf(\TYPO3\CMS\Core\Category\Collection\CategoryCollection::class, $collection);
118  }
119 
126  {
127  $method = new \ReflectionMethod(\TYPO3\CMS\Core\Category\Collection\CategoryCollection::class, 'getCollectedRecords');
128  $method->setAccessible(true);
129  $records = $method->invoke($this->subject);
130  $this->assertInternalType('array', $records);
131  $this->assertEmpty($records);
132  }
133 
140  {
141  $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
142  }
143 
150  {
151  $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
152  }
153 
158  public function canLoadADummyCollectionFromDatabase()
159  {
161  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, true, $this->tableName);
162  // Check the number of record
163  $this->assertEquals($this->numberOfRecords, $collection->count());
164  // Check that the first record is the one expected
165  $record = $this->database->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=1');
166  $collection->rewind();
167  $this->assertEquals($record, $collection->current());
168  // Add a new record
169  $fakeRecord = [
170  'uid' => $this->numberOfRecords + 1,
171  'pid' => 0,
172  'title' => $this->getUniqueId('title'),
173  'categories' => 0
174  ];
175  // Check the number of records
176  $collection->add($fakeRecord);
177  $this->assertEquals($this->numberOfRecords + 1, $collection->count());
178  }
179 
185  {
186  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, true, $this->tableName);
187  // Add a new record
188  $fakeRecord = [
189  'uid' => $this->numberOfRecords + 1,
190  'pid' => 0,
191  'title' => $this->getUniqueId('title'),
192  'categories' => 0
193  ];
194  // Check the number of records
195  $collection->add($fakeRecord);
196  $this->assertEquals($this->numberOfRecords + 1, $collection->count());
197  }
198 
203  public function canLoadADummyCollectionWithoutContentFromDatabase()
204  {
206  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, false, $this->tableName);
207  // Check the number of record
208  $this->assertEquals(0, $collection->count());
209  }
210 
216  {
217  // Remove one relation
218  $fakeName = [
219  'tablenames' => $this->getUniqueId('name')
220  ];
221  $this->database->exec_UPDATEquery(
222  'sys_category_record_mm',
223  'uid_foreign = 1',
224  $fakeName
225  );
226  // Check the number of records
227  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, true, $this->tableName);
228  $this->assertEquals($this->numberOfRecords - 1, $collection->count());
229  }
230 
231  /********************/
232  /* INTERNAL METHODS */
233  /********************/
239  private function populateDummyTable()
240  {
241  for ($index = 1; $index <= $this->numberOfRecords; $index++) {
242  $values = [
243  'title' => $this->getUniqueId('title')
244  ];
245  $this->database->exec_INSERTquery($this->tableName, $values);
246  }
247  }
248 
255  {
256  for ($index = 1; $index <= $this->numberOfRecords; $index++) {
257  $values = [
258  'uid_local' => $this->categoryUid,
259  'uid_foreign' => $index,
260  'tablenames' => $this->tableName,
261  'fieldname' => 'categories'
262  ];
263  $this->database->exec_INSERTquery('sys_category_record_mm', $values);
264  }
265  }
266 
272  private function createDummyTable()
273  {
274  $sql = 'CREATE TABLE ' . $this->tableName . ' (' . LF . TAB .
275  'uid int(11) auto_increment,' . LF . TAB .
276  'pid int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
277  'title tinytext,' . LF . TAB .
278  'tcategories int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
279  'sys_category_is_dummy_record int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . LF . TAB .
280  'PRIMARY KEY (uid)' . LF . ');';
281  $this->database->sql_query($sql);
282  }
283 
289  private function dropDummyTable()
290  {
291  $sql = 'DROP TABLE ' . $this->tableName . ';';
292  $this->database->sql_query($sql);
293  }
294 
300  private function prepareTables()
301  {
302  $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
303  foreach ($this->tables as $table) {
304  $_sql = sprintf($sql, $table);
305  $this->database->sql_query($_sql);
306  }
307  $values = [
308  'title' => $this->getUniqueId('title'),
309  'l10n_diffsource' => '',
310  'description' => '',
311  'is_dummy_record' => 1
312  ];
313  $this->database->exec_INSERTquery('sys_category', $values);
314  $this->categoryUid = $this->database->sql_insert_id();
315  }
316 }
static load($id, $fillItems=false, $tableName='', $fieldName='')
static create(array $collectionRecord, $fillItems=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
$sql
Definition: server.php:84