23a66b831582d517d591c4a271c1a0bc2fdc019c
[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 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 * @expectedException RuntimeException
118 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::__construct
119 * @return void
120 */
121 public function missingTableNameArgumentForObjectCategoryCollection() {
122 new \TYPO3\CMS\Core\Category\Collection\CategoryCollection();
123 }
124
125 /**
126 * @test
127 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::fromArray
128 * @return void
129 */
130 public function checkIfFromArrayMethodSetCorrectProperties() {
131 $this->fixture->fromArray($this->collectionRecord);
132 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getIdentifier());
133 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getUid());
134 $this->assertEquals($this->collectionRecord['title'], $this->fixture->getTitle());
135 $this->assertEquals($this->collectionRecord['description'], $this->fixture->getDescription());
136 $this->assertEquals($this->collectionRecord['table_name'], $this->fixture->getItemTableName());
137 }
138
139 /**
140 * @test
141 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
142 * @return void
143 */
144 public function canCreateDummyCollection() {
145 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord);
146 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
147 }
148
149 /**
150 * @test
151 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
152 * @return void
153 */
154 public function canCreateDummyCollectionAndFillItems() {
155 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord, TRUE);
156 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
157 }
158
159 /**
160 * @test
161 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getCollectedRecords
162 * @return void
163 */
164 public function getCollectedRecordsReturnsEmptyRecordSet() {
165 $method = new \ReflectionMethod('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', 'getCollectedRecords');
166 $method->setAccessible(TRUE);
167 $records = $method->invoke($this->fixture);
168 $this->assertInternalType('array', $records);
169 $this->assertEmpty($records);
170 }
171
172 /**
173 * @test
174 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName
175 * @return void
176 */
177 public function isStorageTableNameEqualsToSysCategory() {
178 $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
179 }
180
181 /**
182 * @test
183 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField
184 * @return void
185 */
186 public function isStorageItemsFieldEqualsToItems() {
187 $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
188 }
189
190 /**
191 * @test
192 * @return void
193 */
194 public function canLoadADummyCollectionFromDatabase() {
195 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
196 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
197 // Check the number of record
198 $this->assertEquals($this->numberOfRecords, $collection->count());
199 // Check that the first record is the one expected
200 $record = $this->database->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=1');
201 $collection->rewind();
202 $this->assertEquals($record, $collection->current());
203 // Add a new record
204 $fakeRecord = array(
205 'uid' => $this->numberOfRecords + 1,
206 'pid' => 0,
207 'title' => uniqid('title'),
208 'categories' => 0
209 );
210 // Check the number of records
211 $collection->add($fakeRecord);
212 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
213 }
214
215 /**
216 * @test
217 * @return void
218 */
219 public function canLoadADummyCollectionFromDatabaseAndAddRecord() {
220 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
221 // Add a new record
222 $fakeRecord = array(
223 'uid' => $this->numberOfRecords + 1,
224 'pid' => 0,
225 'title' => uniqid('title'),
226 'categories' => 0
227 );
228 // Check the number of records
229 $collection->add($fakeRecord);
230 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
231 }
232
233 /**
234 * @test
235 * @return void
236 */
237 public function canLoadADummyCollectionWithoutContentFromDatabase() {
238 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
239 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, FALSE, $this->tableName);
240 // Check the number of record
241 $this->assertEquals(0, $collection->count());
242 }
243
244 /********************/
245 /* INTERNAL METHODS */
246 /********************/
247 /**
248 * Create dummy table for testing purpose
249 *
250 * @return void
251 */
252 private function populateDummyTable() {
253 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
254 $values = array(
255 'title' => uniqid('title')
256 );
257 $this->testingFramework->createRecord($this->tableName, $values);
258 }
259 }
260
261 /**
262 * Make relation between tables
263 *
264 * @return void
265 */
266 private function makeRelationBetweenCategoryAndDummyTable() {
267 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
268 $values = array(
269 'uid_local' => $this->categoryUid,
270 'uid_foreign' => $index,
271 'tablenames' => $this->tableName
272 );
273 $this->testingFramework->createRecord('sys_category_record_mm', $values);
274 }
275 }
276
277 /**
278 * Create dummy table for testing purpose
279 *
280 * @return void
281 */
282 private function createDummyTable() {
283 $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);";
284 $this->database->sql_query($sql);
285 }
286
287 /**
288 * Drop dummy table
289 *
290 * @return void
291 */
292 private function dropDummyTable() {
293 $sql = ('DROP TABLE ' . $this->tableName) . ';';
294 $this->database->sql_query($sql);
295 }
296
297 /**
298 * Add is_dummy_record record and create dummy record
299 *
300 * @return void
301 */
302 private function prepareTables() {
303 $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
304 foreach ($this->tables as $table) {
305 $_sql = sprintf($sql, $table);
306 $this->database->sql_query($_sql);
307 }
308 $values = array(
309 'title' => uniqid('title'),
310 'is_dummy_record' => 1
311 );
312 $this->categoryUid = $this->testingFramework->createRecord('sys_category', $values);
313 }
314
315 /**
316 * Remove dummy record and drop field
317 *
318 * @return void
319 */
320 private function dropDummyField() {
321 $sql = 'ALTER TABLE %s DROP COLUMN is_dummy_record';
322 foreach ($this->tables as $table) {
323 $_sql = sprintf($sql, $table);
324 $this->database->sql_query($_sql);
325 }
326 }
327
328 }
329
330 ?>