2 namespace TYPO3\CMS\Core\Tests\Functional\Category\Collection
;
4 /***************************************************************
7 * (c) 2012 Fabien Udriot <fabien.udriot@typo3.org>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
28 * Test case for t3lib_category_CategoryCollection
30 * @author Fabien Udriot <fabien.udriot@typo3.org>
32 class CategoryCollectionTest
extends \TYPO3\CMS\Core\Tests\UnitTestCase
{
35 * @var \TYPO3\CMS\Core\Category\Collection\CategoryCollection
42 private $tableName = 'tx_foo_5001615c50bed';
47 private $tables = array('sys_category', 'sys_category_record_mm');
52 private $categoryUid = 0;
57 private $collectionRecord = array();
62 private $numberOfRecords = 5;
65 * @var \Tx_Phpunit_Framework
67 private $testingFramework;
70 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
75 * Sets up this test suite.
79 public function setUp() {
80 $this->database
= $GLOBALS['TYPO3_DB'];
81 $this->fixture
= new \TYPO3\CMS\Core\Category\Collection\
CategoryCollection($this->tableName
);
82 $this->collectionRecord
= array(
84 'title' => uniqid('title'),
85 'description' => uniqid('description'),
86 'table_name' => $this->tableName
,
88 $GLOBALS['TCA'][$this->tableName
] = array('ctrl' => array());
89 // prepare environment
90 $this->createDummyTable();
91 $this->testingFramework
= new \
Tx_Phpunit_Framework('sys_category', array('tx_foo'));
92 $this->populateDummyTable();
93 $this->prepareTables();
94 $this->makeRelationBetweenCategoryAndDummyTable();
98 * Tears down this test suite.
102 public function tearDown() {
103 $this->testingFramework
->cleanUp();
104 // clean up environment
105 $this->dropDummyTable();
106 $this->dropDummyField();
107 unset($this->testingFramework
);
108 unset($this->collectionRecord
);
109 unset($this->fixture
);
110 unset($this->database
);
115 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::fromArray
118 public function checkIfFromArrayMethodSetCorrectProperties() {
119 $this->fixture
->fromArray($this->collectionRecord
);
120 $this->assertEquals($this->collectionRecord
['uid'], $this->fixture
->getIdentifier());
121 $this->assertEquals($this->collectionRecord
['uid'], $this->fixture
->getUid());
122 $this->assertEquals($this->collectionRecord
['title'], $this->fixture
->getTitle());
123 $this->assertEquals($this->collectionRecord
['description'], $this->fixture
->getDescription());
124 $this->assertEquals($this->collectionRecord
['table_name'], $this->fixture
->getItemTableName());
129 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
132 public function canCreateDummyCollection() {
133 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::create($this->collectionRecord
);
134 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
139 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
142 public function canCreateDummyCollectionAndFillItems() {
143 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::create($this->collectionRecord
, TRUE);
144 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
149 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getCollectedRecords
152 public function getCollectedRecordsReturnsEmptyRecordSet() {
153 $method = new \
ReflectionMethod('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', 'getCollectedRecords');
154 $method->setAccessible(TRUE);
155 $records = $method->invoke($this->fixture
);
156 $this->assertInternalType('array', $records);
157 $this->assertEmpty($records);
162 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName
165 public function isStorageTableNameEqualsToSysCategory() {
166 $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::getStorageTableName());
171 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField
174 public function isStorageItemsFieldEqualsToItems() {
175 $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::getStorageItemsField());
182 public function canLoadADummyCollectionFromDatabase() {
183 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
184 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::load($this->categoryUid
, TRUE, $this->tableName
);
185 // Check the number of record
186 $this->assertEquals($this->numberOfRecords
, $collection->count());
187 // Check that the first record is the one expected
188 $record = $this->database
->exec_SELECTgetSingleRow('*', $this->tableName
, 'uid=1');
189 $collection->rewind();
190 $this->assertEquals($record, $collection->current());
193 'uid' => $this->numberOfRecords +
1,
195 'title' => uniqid('title'),
198 // Check the number of records
199 $collection->add($fakeRecord);
200 $this->assertEquals($this->numberOfRecords +
1, $collection->count());
207 public function canLoadADummyCollectionFromDatabaseAndAddRecord() {
208 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::load($this->categoryUid
, TRUE, $this->tableName
);
211 'uid' => $this->numberOfRecords +
1,
213 'title' => uniqid('title'),
216 // Check the number of records
217 $collection->add($fakeRecord);
218 $this->assertEquals($this->numberOfRecords +
1, $collection->count());
225 public function canLoadADummyCollectionWithoutContentFromDatabase() {
226 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
227 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection
::load($this->categoryUid
, FALSE, $this->tableName
);
228 // Check the number of record
229 $this->assertEquals(0, $collection->count());
232 /********************/
233 /* INTERNAL METHODS */
234 /********************/
236 * Create dummy table for testing purpose
240 private function populateDummyTable() {
241 for ($index = 1; $index <= $this->numberOfRecords
; $index++
) {
243 'title' => uniqid('title')
245 $this->testingFramework
->createRecord($this->tableName
, $values);
250 * Make relation between tables
254 private function makeRelationBetweenCategoryAndDummyTable() {
255 for ($index = 1; $index <= $this->numberOfRecords
; $index++
) {
257 'uid_local' => $this->categoryUid
,
258 'uid_foreign' => $index,
259 'tablenames' => $this->tableName
261 $this->testingFramework
->createRecord('sys_category_record_mm', $values);
266 * Create dummy table for testing purpose
270 private function createDummyTable() {
271 $sql = 'CREATE TABLE ' . $this->tableName
. ' (' . LF
. TAB
.
272 'uid int(11) auto_increment,' . LF
. TAB
.
273 'pid int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF
. TAB
.
274 'title tinytext,' . LF
. TAB
.
275 'tcategories int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF
. TAB
.
276 'sys_category_is_dummy_record int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF
. LF
. TAB
.
277 'PRIMARY KEY (uid)' . LF
. ');';
278 $this->database
->sql_query($sql);
286 private function dropDummyTable() {
287 $sql = 'DROP TABLE ' . $this->tableName
. ';';
288 $this->database
->sql_query($sql);
292 * Add is_dummy_record record and create dummy record
296 private function prepareTables() {
297 $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
298 foreach ($this->tables
as $table) {
299 $_sql = sprintf($sql, $table);
300 $this->database
->sql_query($_sql);
303 'title' => uniqid('title'),
304 'is_dummy_record' => 1
306 $this->categoryUid
= $this->testingFramework
->createRecord('sys_category', $values);
310 * Remove dummy record and drop field
314 private function dropDummyField() {
315 $sql = 'ALTER TABLE %s DROP COLUMN is_dummy_record';
316 foreach ($this->tables
as $table) {
317 $_sql = sprintf($sql, $table);
318 $this->database
->sql_query($_sql);