[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * Test case for \TYPO3\CMS\Core\Category\Collection\CategoryCollection
20 *
21 * @author Fabien Udriot <fabien.udriot@typo3.org>
22 */
23 class CategoryCollectionTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
24
25 /**
26 * @var \TYPO3\CMS\Core\Category\Collection\CategoryCollection
27 */
28 private $fixture;
29
30 /**
31 * @var string
32 */
33 private $tableName = 'tx_foo_5001615c50bed';
34
35 /**
36 * @var array
37 */
38 private $tables = array('sys_category', 'sys_category_record_mm');
39
40 /**
41 * @var int
42 */
43 private $categoryUid = 0;
44
45 /**
46 * @var array
47 */
48 private $collectionRecord = array();
49
50 /**
51 * @var integer
52 */
53 private $numberOfRecords = 5;
54
55 /**
56 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
57 */
58 private $database;
59
60 /**
61 * Sets up this test suite.
62 *
63 * @return void
64 */
65 public function setUp() {
66 parent::setUp();
67 $this->database = $this->getDatabaseConnection();
68 $this->fixture = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', $this->tableName);
69 $this->collectionRecord = array(
70 'uid' => 0,
71 'title' => uniqid('title'),
72 'description' => uniqid('description'),
73 'table_name' => $this->tableName,
74 );
75 $GLOBALS['TCA'][$this->tableName] = array('ctrl' => array());
76 // prepare environment
77 $this->createDummyTable();
78 $this->populateDummyTable();
79 $this->prepareTables();
80 $this->makeRelationBetweenCategoryAndDummyTable();
81 }
82
83 /**
84 * @test
85 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::fromArray
86 * @return void
87 */
88 public function checkIfFromArrayMethodSetCorrectProperties() {
89 $this->fixture->fromArray($this->collectionRecord);
90 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getIdentifier());
91 $this->assertEquals($this->collectionRecord['uid'], $this->fixture->getUid());
92 $this->assertEquals($this->collectionRecord['title'], $this->fixture->getTitle());
93 $this->assertEquals($this->collectionRecord['description'], $this->fixture->getDescription());
94 $this->assertEquals($this->collectionRecord['table_name'], $this->fixture->getItemTableName());
95 }
96
97 /**
98 * @test
99 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
100 * @return void
101 */
102 public function canCreateDummyCollection() {
103 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord);
104 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
105 }
106
107 /**
108 * @test
109 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create
110 * @return void
111 */
112 public function canCreateDummyCollectionAndFillItems() {
113 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::create($this->collectionRecord, TRUE);
114 $this->assertInstanceOf('\TYPO3\CMS\Core\Category\Collection\CategoryCollection', $collection);
115 }
116
117 /**
118 * @test
119 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getCollectedRecords
120 * @return void
121 */
122 public function getCollectedRecordsReturnsEmptyRecordSet() {
123 $method = new \ReflectionMethod('TYPO3\\CMS\\Core\\Category\\Collection\\CategoryCollection', 'getCollectedRecords');
124 $method->setAccessible(TRUE);
125 $records = $method->invoke($this->fixture);
126 $this->assertInternalType('array', $records);
127 $this->assertEmpty($records);
128 }
129
130 /**
131 * @test
132 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName
133 * @return void
134 */
135 public function isStorageTableNameEqualsToSysCategory() {
136 $this->assertEquals('sys_category', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageTableName());
137 }
138
139 /**
140 * @test
141 * @covers \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField
142 * @return void
143 */
144 public function isStorageItemsFieldEqualsToItems() {
145 $this->assertEquals('items', \TYPO3\CMS\Core\Category\Collection\CategoryCollection::getStorageItemsField());
146 }
147
148 /**
149 * @test
150 * @return void
151 */
152 public function canLoadADummyCollectionFromDatabase() {
153 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
154 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
155 // Check the number of record
156 $this->assertEquals($this->numberOfRecords, $collection->count());
157 // Check that the first record is the one expected
158 $record = $this->database->exec_SELECTgetSingleRow('*', $this->tableName, 'uid=1');
159 $collection->rewind();
160 $this->assertEquals($record, $collection->current());
161 // Add a new record
162 $fakeRecord = array(
163 'uid' => $this->numberOfRecords + 1,
164 'pid' => 0,
165 'title' => uniqid('title'),
166 'categories' => 0
167 );
168 // Check the number of records
169 $collection->add($fakeRecord);
170 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
171 }
172
173 /**
174 * @test
175 * @return void
176 */
177 public function canLoadADummyCollectionFromDatabaseAndAddRecord() {
178 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, TRUE, $this->tableName);
179 // Add a new record
180 $fakeRecord = array(
181 'uid' => $this->numberOfRecords + 1,
182 'pid' => 0,
183 'title' => uniqid('title'),
184 'categories' => 0
185 );
186 // Check the number of records
187 $collection->add($fakeRecord);
188 $this->assertEquals($this->numberOfRecords + 1, $collection->count());
189 }
190
191 /**
192 * @test
193 * @return void
194 */
195 public function canLoadADummyCollectionWithoutContentFromDatabase() {
196 /** @var $collection \TYPO3\CMS\Core\Category\Collection\CategoryCollection */
197 $collection = \TYPO3\CMS\Core\Category\Collection\CategoryCollection::load($this->categoryUid, FALSE, $this->tableName);
198 // Check the number of record
199 $this->assertEquals(0, $collection->count());
200 }
201
202 /********************/
203 /* INTERNAL METHODS */
204 /********************/
205 /**
206 * Create dummy table for testing purpose
207 *
208 * @return void
209 */
210 private function populateDummyTable() {
211 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
212 $values = array(
213 'title' => uniqid('title')
214 );
215 $this->database->exec_INSERTquery($this->tableName, $values);
216 }
217 }
218
219 /**
220 * Make relation between tables
221 *
222 * @return void
223 */
224 private function makeRelationBetweenCategoryAndDummyTable() {
225 for ($index = 1; $index <= $this->numberOfRecords; $index++) {
226 $values = array(
227 'uid_local' => $this->categoryUid,
228 'uid_foreign' => $index,
229 'tablenames' => $this->tableName,
230 'fieldname' => 'categories'
231 );
232 $this->database->exec_INSERTquery('sys_category_record_mm', $values);
233 }
234 }
235
236 /**
237 * Create dummy table for testing purpose
238 *
239 * @return void
240 */
241 private function createDummyTable() {
242 $sql = 'CREATE TABLE ' . $this->tableName . ' (' . LF . TAB .
243 'uid int(11) auto_increment,' . LF . TAB .
244 'pid int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
245 'title tinytext,' . LF . TAB .
246 'tcategories int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . TAB .
247 'sys_category_is_dummy_record int(11) unsigned DEFAULT \'0\' NOT NULL,' . LF . LF . TAB .
248 'PRIMARY KEY (uid)' . LF . ');';
249 $this->database->sql_query($sql);
250 }
251
252 /**
253 * Drop dummy table
254 *
255 * @return void
256 */
257 private function dropDummyTable() {
258 $sql = 'DROP TABLE ' . $this->tableName . ';';
259 $this->database->sql_query($sql);
260 }
261
262 /**
263 * Add is_dummy_record record and create dummy record
264 *
265 * @return void
266 */
267 private function prepareTables() {
268 $sql = 'ALTER TABLE %s ADD is_dummy_record tinyint(1) unsigned DEFAULT \'0\' NOT NULL';
269 foreach ($this->tables as $table) {
270 $_sql = sprintf($sql, $table);
271 $this->database->sql_query($_sql);
272 }
273 $values = array(
274 'title' => uniqid('title'),
275 'is_dummy_record' => 1
276 );
277 $this->database->exec_INSERTquery('sys_category', $values);
278 $this->categoryUid = $this->database->sql_insert_id();
279 }
280 }