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