BUGFIX] Wrong renaming of DBAL dbPostgresqlTest
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / category / Collection / CategoryCollectionTest.php
1 <?php
2 /**
3 * Test case for t3lib_category_CategoryCollection
4 *
5 * @package TYPO3
6 * @subpackage t3lib
7 * @author Fabine Udriot <fabien.udriot@typo3.org>
8 */
9 class t3lib_category_CategoryCollectionTest extends Tx_Phpunit_TestCase {
10
11 /**
12 * @var \TYPO3\CMS\Core\Category\Collection\CategoryCollection
13 */
14 private $fixture;
15
16 /**
17 * @var string
18 */
19 private $tableName = 'tx_foo_5001615c50bed';
20
21 /**
22 * @var array
23 */
24 private $tables = array('sys_category', 'sys_category_record_mm');
25
26 /**
27 * @var int
28 */
29 private $categoryUid = 0;
30
31 /**
32 * @var array
33 */
34 private $collectionRecord = array();
35
36 /**
37 * @var Tx_Phpunit_Framework
38 */
39 private $testingFramework;
40
41 /**
42 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
43 */
44 private $database;
45
46 /**
47 * Sets up this test suite.
48 *
49 * @return void
50 */
51 public function setUp() {
52 $this->database = $GLOBALS['TYPO3_DB'];
53 $this->fixture = new \TYPO3\CMS\Core\Category\Collection\CategoryCollection($this->tableName);
54 $this->collectionRecord = array(
55 'uid' => 0,
56 'title' => uniqid('title'),
57 'description' => uniqid('description'),
58 'table_name' => $this->tableName,
59 );
60 $GLOBALS['TCA'][$this->tableName] = array('ctrl' => array());
61 // prepare environment
62 $this->createDummyTable();
63 $this->testingFramework = new Tx_Phpunit_Framework('sys_category', array('tx_foo'));
64 $this->populateDummyTable();
65 $this->prepareTables();
66 $this->makeRelationBetweenCategoryAndDummyTable();
67 }
68
69 /**
70 * Tears down this test suite.
71 *
72 * @return void
73 */
74 public function tearDown() {
75 $this->testingFramework->cleanUp();
76 // clean up environment
77 $this->dropDummyTable();
78 $this->dropDummyField();
79 unset($this->testingFramework);
80 unset($this->collectionRecord);
81 unset($this->fixture);
82 unset($this->database);
83 }
84
85 /**
86 * @test
87 * @expectedException RuntimeException
88 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::__construct
89 * @return void
90 */
91 public function missingTableNameArgumentForObjectCategoryCollection() {
92 new \TYPO3\CMS\Core\Category\Collection\CategoryCollection();
93 }
94
95 /**
96 * @test
97 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::fromArray
98 * @return void
99 */
100 public function checkIfFromArrayMethodSetCorrectProperties() {
101 $this->fixture->fromArray($this->collectionRecord);
102 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getIdentifier());
103 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getUid());
104 $this->assertEquals($this->collectionRecord['title'], $this->fixture->getTitle());
105 $this->assertEquals($this->collectionRecord['description'], $this->fixture->getDescription());
106 $this->assertEquals($this->collectionRecord['table_name'], $this->fixture->getItemTableName());
107 }
108
109 /**
110 * @test
111 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
112 * @return void
113 */
114 public function canCreateDummyCollection() {
115 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord);
116 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
117 }
118
119 /**
120 * @test
121 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
122 * @return void
123 */
124 public function canCreateDummyCollectionAndFillItems() {
125 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord, TRUE);
126 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
127 }
128
129 /**
130 * @test
131 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getCollectedRecords
132 * @return void
133 */
134 public function getCollectedRecordsReturnsEmptyRecordSet() {
135 $method = new ReflectionMethod('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', 'getCollectedRecords');
136 $method->setAccessible(TRUE);
137 $records = $method->invoke($this->fixture);
138 $this->assertInternalType('array', $records);
139 $this->assertEmpty($records);
140 }
141
142 /**
143 * @test
144 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName
145 * @return void
146 */
147 public function isStorageTableNameEqualsToSysCategory() {
148 $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
149 }
150
151 /**
152 * @test
153 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField
154 * @return void
155 */
156 public function isStorageItemsFieldEqualsToItems() {
157 $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
158 }
159
160 /**
161 * @test
162 * @return void
163 */
164 public function canLoadADummyCollectionFromDatabase() {
165 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
166 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
167 // Check the number of record
168 $this->assertEquals($this->numberOfRecords, $collection->count());
169 // Check that the first record is the one expected
170 $record = $this->database->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=1');
171 $collection->rewind();
172 $this->assertEquals($record, $collection->current());
173 // Add a new record
174 $fakeRecord = array(
175 'uid' => $this->numberOfRecords + 1,
176 'pid' => 0,
177 'title' => uniqid('title'),
178 'categories' => 0
179 );
180 // Check the number of records
181 $collection->add($fakeRecord);
182 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
183 }
184
185 /**
186 * @test
187 * @return void
188 */
189 public function canLoadADummyCollectionFromDatabaseAndAddRecord() {
190 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
191 // Add a new record
192 $fakeRecord = array(
193 'uid' => $this->numberOfRecords + 1,
194 'pid' => 0,
195 'title' => uniqid('title'),
196 'categories' => 0
197 );
198 // Check the number of records
199 $collection->add($fakeRecord);
200 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
201 }
202
203 /**
204 * @test
205 * @return void
206 */
207 public function canLoadADummyCollectionWithoutContentFromDatabase() {
208 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
209 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, FALSE, $this->tableName);
210 // Check the number of record
211 $this->assertEquals(0, $collection->count());
212 }
213
214 /********************/
215 /* INTERNAL METHODS */
216 /********************/
217 /**
218 * Create dummy table for testing purpose
219 *
220 * @return void
221 */
222 private function populateDummyTable() {
223 $this->numberOfRecords = 5;
224 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
225 $values = array(
226 'title' => uniqid('title')
227 );
228 $this->testingFramework->createRecord($this->tableName, $values);
229 }
230 }
231
232 /**
233 * Make relation between tables
234 *
235 * @return void
236 */
237 private function makeRelationBetweenCategoryAndDummyTable() {
238 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
239 $values = array(
240 'uid_local' => $this->categoryUid,
241 'uid_foreign' => $index,
242 'tablenames' => $this->tableName
243 );
244 $this->testingFramework->createRecord('sys_category_record_mm', $values);
245 }
246 }
247
248 /**
249 * Create dummy table for testing purpose
250 *
251 * @return void
252 */
253 private function createDummyTable() {
254 $sql = "CREATE TABLE {$this->tableName} (\n\tuid int(11) auto_increment,\n\tpid int(11) unsigned DEFAULT '0' NOT NULL,\n title tinytext,\n\tcategories int(11) unsigned DEFAULT '0' NOT NULL,\n\tsys_category_is_dummy_record int(11) unsigned DEFAULT '0' NOT NULL,\n\n PRIMARY KEY (uid)\n);";
255 $this->database->sql_query($sql);
256 }
257
258 /**
259 * Drop dummy table
260 *
261 * @return void
262 */
263 private function dropDummyTable() {
264 $sql = ('DROP TABLE ' . $this->tableName) . ';';
265 $this->database->sql_query($sql);
266 }
267
268 /**
269 * Add is_dummy_record record and create dummy record
270 *
271 * @return void
272 */
273 private function prepareTables() {
274 $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
275 foreach ($this->tables as $table) {
276 $_sql = sprintf($sql, $table);
277 $this->database->sql_query($_sql);
278 }
279 $values = array(
280 'title' => uniqid('title'),
281 'is_dummy_record' => 1
282 );
283 $this->categoryUid = $this->testingFramework->createRecord('sys_category', $values);
284 }
285
286 /**
287 * Remove dummy record and drop field
288 *
289 * @return void
290 */
291 private function dropDummyField() {
292 $sql = 'ALTER TABLE %s DROP COLUMN is_dummy_record';
293 foreach ($this->tables as $table) {
294 $_sql = sprintf($sql, $table);
295 $this->database->sql_query($_sql);
296 }
297 }
298
299 }
300
301 ?>