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