TYPO3 CMS  TYPO3_6-2
CategoryCollectionTest.php
Go to the documentation of this file.
1 <?php
3 
17 
24 
28  private $fixture;
29 
33  private $tableName = 'tx_foo_5001615c50bed';
34 
38  private $tables = array('sys_category', 'sys_category_record_mm');
39 
43  private $categoryUid = 0;
44 
48  private $collectionRecord = array();
49 
53  private $numberOfRecords = 5;
54 
58  private $database;
59 
65  public function setUp() {
66  parent::setUp();
67  $this->database = $this->getDatabaseConnection();
68  $this->fixture = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', $this->tableName);
69  $this->collectionRecord = array(
70  'uid' => 0,
71  'title' => $this->getUniqueId('title'),
72  'description' => $this->getUniqueId('description'),
73  'table_name' => $this->tableName,
74  );
75  $GLOBALS['TCA'][$this->tableName] = array('ctrl' => array());
76  // prepare environment
77  $this->createDummyTable();
78  $this->populateDummyTable();
79  $this->prepareTables();
81  }
82 
89  $this->fixture->fromArray($this->collectionRecord);
90  $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getIdentifier());
91  $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getUid());
92  $this->assertEquals($this->collectionRecord['title'], $this->fixture->getTitle());
93  $this->assertEquals($this->collectionRecord['description'], $this->fixture->getDescription());
94  $this->assertEquals($this->collectionRecord['table_name'], $this->fixture->getItemTableName());
95  }
96 
102  public function canCreateDummyCollection() {
103  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord);
104  $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
105  }
106 
113  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord, TRUE);
114  $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
115  }
116 
123  $method = new \ReflectionMethod('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', 'getCollectedRecords');
124  $method->setAccessible(TRUE);
125  $records = $method->invoke($this->fixture);
126  $this->assertInternalType('array', $records);
127  $this->assertEmpty($records);
128  }
129 
136  $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
137  }
138 
145  $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
146  }
147 
152  public function canLoadADummyCollectionFromDatabase() {
154  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
155  // Check the number of record
156  $this->assertEquals($this->numberOfRecords, $collection->count());
157  // Check that the first record is the one expected
158  $record = $this->database->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=1');
159  $collection->rewind();
160  $this->assertEquals($record, $collection->current());
161  // Add a new record
162  $fakeRecord = array(
163  'uid' => $this->numberOfRecords + 1,
164  'pid' => 0,
165  'title' => $this->getUniqueId('title'),
166  'categories' => 0
167  );
168  // Check the number of records
169  $collection->add($fakeRecord);
170  $this->assertEquals($this->numberOfRecords + 1, $collection->count());
171  }
172 
178  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
179  // Add a new record
180  $fakeRecord = array(
181  'uid' => $this->numberOfRecords + 1,
182  'pid' => 0,
183  'title' => $this->getUniqueId('title'),
184  'categories' => 0
185  );
186  // Check the number of records
187  $collection->add($fakeRecord);
188  $this->assertEquals($this->numberOfRecords + 1, $collection->count());
189  }
190 
195  public function canLoadADummyCollectionWithoutContentFromDatabase() {
197  $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, FALSE, $this->tableName);
198  // Check the number of record
199  $this->assertEquals(0, $collection->count());
200  }
201 
202  /********************/
203  /* INTERNAL METHODS */
204  /********************/
210  private function populateDummyTable() {
211  for ($index = 1; $index <= $this->numberOfRecords; $index++) {
212  $values = array(
213  'title' => $this->getUniqueId('title')
214  );
215  $this->database->exec_INSERTquery($this->tableName, $values);
216  }
217  }
218 
225  for ($index = 1; $index <= $this->numberOfRecords; $index++) {
226  $values = array(
227  'uid_local' => $this->categoryUid,
228  'uid_foreign' => $index,
229  'tablenames' => $this->tableName,
230  'fieldname' => 'categories'
231  );
232  $this->database->exec_INSERTquery('sys_category_record_mm', $values);
233  }
234  }
235 
241  private function createDummyTable() {
242  $sql = 'CREATE TABLE ' . $this->tableName . ' (' . LF . TAB .
243  'uid int(11) auto_increment,' . LF . TAB .
244  'pid int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
245  'title tinytext,' . LF . TAB .
246  'tcategories int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
247  'sys_category_is_dummy_record int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . LF . TAB .
248  'PRIMARY KEY (uid)' . LF . ');';
249  $this->database->sql_query($sql);
250  }
251 
257  private function dropDummyTable() {
258  $sql = 'DROP TABLE ' . $this->tableName . ';';
259  $this->database->sql_query($sql);
260  }
261 
267  private function prepareTables() {
268  $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
269  foreach ($this->tables as $table) {
270  $_sql = sprintf($sql, $table);
271  $this->database->sql_query($_sql);
272  }
273  $values = array(
274  'title' => $this->getUniqueId('title'),
275  'is_dummy_record' => 1
276  );
277  $this->database->exec_INSERTquery('sys_category', $values);
278  $this->categoryUid = $this->database->sql_insert_id();
279  }
280 }
$sql
Definition: server.php:82
static load($id, $fillItems=FALSE, $tableName='', $fieldName='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static create(array $collectionRecord, $fillItems=FALSE)