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