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