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