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