[TASK] Remove @package and @subpackage annotations
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Category / Collection / CategoryCollectionTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Category\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Fabien Udriot <fabien.udriot@typo3.org>
8 * All rights reserved
9 *
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.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
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.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case for t3lib_category_CategoryCollection
29 *
30 * @author Fabien Udriot <fabien.udriot@typo3.org>
31 */
32 class CategoryCollectionTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Core\Category\Collection\CategoryCollection
36 */
37 private $fixture;
38
39 /**
40 * @var string
41 */
42 private $tableName = 'tx_foo_5001615c50bed';
43
44 /**
45 * @var array
46 */
47 private $tables = array('sys_category', 'sys_category_record_mm');
48
49 /**
50 * @var int
51 */
52 private $categoryUid = 0;
53
54 /**
55 * @var array
56 */
57 private $collectionRecord = array();
58
59 /**
60 * @var integer
61 */
62 private $numberOfRecords = 5;
63
64 /**
65 * @var \Tx_Phpunit_Framework
66 */
67 private $testingFramework;
68
69 /**
70 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
71 */
72 private $database;
73
74 /**
75 * Sets up this test suite.
76 *
77 * @return void
78 */
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(
83 'uid' => 0,
84 'title' => uniqid('title'),
85 'description' => uniqid('description'),
86 'table_name' => $this->tableName,
87 );
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();
95 }
96
97 /**
98 * Tears down this test suite.
99 *
100 * @return void
101 */
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);
111 }
112
113 /**
114 * @test
115 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::fromArray
116 * @return void
117 */
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());
125 }
126
127 /**
128 * @test
129 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
130 * @return void
131 */
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);
135 }
136
137 /**
138 * @test
139 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
140 * @return void
141 */
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);
145 }
146
147 /**
148 * @test
149 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getCollectedRecords
150 * @return void
151 */
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);
158 }
159
160 /**
161 * @test
162 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName
163 * @return void
164 */
165 public function isStorageTableNameEqualsToSysCategory() {
166 $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
167 }
168
169 /**
170 * @test
171 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField
172 * @return void
173 */
174 public function isStorageItemsFieldEqualsToItems() {
175 $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
176 }
177
178 /**
179 * @test
180 * @return void
181 */
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());
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 canLoadADummyCollectionFromDatabaseAndAddRecord() {
208 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
209 // Add a new record
210 $fakeRecord = array(
211 'uid' => $this->numberOfRecords + 1,
212 'pid' => 0,
213 'title' => uniqid('title'),
214 'categories' => 0
215 );
216 // Check the number of records
217 $collection->add($fakeRecord);
218 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
219 }
220
221 /**
222 * @test
223 * @return void
224 */
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());
230 }
231
232 /********************/
233 /* INTERNAL METHODS */
234 /********************/
235 /**
236 * Create dummy table for testing purpose
237 *
238 * @return void
239 */
240 private function populateDummyTable() {
241 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
242 $values = array(
243 'title' => uniqid('title')
244 );
245 $this->testingFramework->createRecord($this->tableName, $values);
246 }
247 }
248
249 /**
250 * Make relation between tables
251 *
252 * @return void
253 */
254 private function makeRelationBetweenCategoryAndDummyTable() {
255 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
256 $values = array(
257 'uid_local' => $this->categoryUid,
258 'uid_foreign' => $index,
259 'tablenames' => $this->tableName
260 );
261 $this->testingFramework->createRecord('sys_category_record_mm', $values);
262 }
263 }
264
265 /**
266 * Create dummy table for testing purpose
267 *
268 * @return void
269 */
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);
279 }
280
281 /**
282 * Drop dummy table
283 *
284 * @return void
285 */
286 private function dropDummyTable() {
287 $sql = 'DROP TABLE ' . $this->tableName . ';';
288 $this->database->sql_query($sql);
289 }
290
291 /**
292 * Add is_dummy_record record and create dummy record
293 *
294 * @return void
295 */
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);
301 }
302 $values = array(
303 'title' => uniqid('title'),
304 'is_dummy_record' => 1
305 );
306 $this->categoryUid = $this->testingFramework->createRecord('sys_category', $values);
307 }
308
309 /**
310 * Remove dummy record and drop field
311 *
312 * @return void
313 */
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);
319 }
320 }
321
322 }
323
324 ?>