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