dd6f9d5c22d18800d5fb8953fb5c6d9b9ea40ae1
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Utility / BackendUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Utility;
3 use TYPO3\CMS\Backend\Utility;
4
5 /***************************************************************
6 * Copyright notice
7 *
8 * (c) 2009-2013 Oliver Klee (typo3-coding@oliverklee.de)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for \TYPO3\CMS\Core\Utility\BackendUtility
30 *
31 * @author Oliver Klee <typo3-coding@oliverklee.de>
32 */
33 class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * @var \TYPO3\CMS\Backend\Utility\BackendUtility
37 */
38 private $fixture;
39
40 public function setUp() {
41 $this->fixture = new Utility\BackendUtility();
42 }
43
44 public function tearDown() {
45 unset($this->fixture);
46 }
47
48 ///////////////////////////////////////
49 // Tests concerning getProcessedValue
50 ///////////////////////////////////////
51 /**
52 * @test
53 * @see http://bugs.typo3.org/view.php?id=11875
54 */
55 public function getProcessedValueForZeroStringIsZero() {
56 $this->assertEquals('0', $this->fixture->getProcessedValue('tt_content', 'header', '0'));
57 }
58
59 /**
60 * Tests concerning getCommenSelectFields
61 */
62
63 /**
64 * Data provider for getCommonSelectFieldsReturnsCorrectFields
65 *
66 * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
67 */
68 public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider() {
69 return array(
70 'only uid' => array(
71 'table' => 'test_table',
72 'prefix' => '',
73 'presetFields' => array(),
74 'tca' => array(),
75 'expectedFields' => 'uid'
76 ),
77 'label set' => array(
78 'table' => 'test_table',
79 'prefix' => '',
80 'presetFields' => array(),
81 'tca' => array(
82 'ctrl' => array(
83 'label' => 'label'
84 )
85 ),
86 'expectedFields' => 'uid,label'
87 ),
88 'label_alt set' => array(
89 'table' => 'test_table',
90 'prefix' => '',
91 'presetFields' => array(),
92 'tca' => array(
93 'ctrl' => array(
94 'label_alt' => 'label,label2'
95 )
96 ),
97 'expectedFields' => 'uid,label,label2'
98 ),
99 'versioningWS set' => array(
100 'table' => 'test_table',
101 'prefix' => '',
102 'presetFields' => array(),
103 'tca' => array(
104 'ctrl' => array(
105 'versioningWS' => '2'
106 )
107 ),
108 'expectedFields' => 'uid,t3ver_id,t3ver_state,t3ver_wsid,t3ver_count'
109 ),
110 'selicon_field set' => array(
111 'table' => 'test_table',
112 'prefix' => '',
113 'presetFields' => array(),
114 'tca' => array(
115 'ctrl' => array(
116 'selicon_field' => 'field'
117 )
118 ),
119 'expectedFields' => 'uid,field'
120 ),
121 'typeicon_column set' => array(
122 'table' => 'test_table',
123 'prefix' => '',
124 'presetFields' => array(),
125 'tca' => array(
126 'ctrl' => array(
127 'typeicon_column' => 'field'
128 )
129 ),
130 'expectedFields' => 'uid,field'
131 ),
132 'enablecolumns set' => array(
133 'table' => 'test_table',
134 'prefix' => '',
135 'presetFields' => array(),
136 'tca' => array(
137 'ctrl' => array(
138 'enablecolumns' => array(
139 'disabled' => 'hidden',
140 'starttime' => 'start',
141 'endtime' => 'stop',
142 'fe_group' => 'groups'
143 )
144 )
145 ),
146 'expectedFields' => 'uid,hidden,start,stop,groups'
147 ),
148 'label set to uid' => array(
149 'table' => 'test_table',
150 'prefix' => '',
151 'presetFields' => array(),
152 'tca' => array(
153 'ctrl' => array(
154 'label' => 'uid'
155 )
156 ),
157 'expectedFields' => 'uid'
158 )
159 );
160 }
161
162 /**
163 * @test
164 * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
165 */
166 public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix = '', array $presetFields, array $tca, $expectedFields = '') {
167 $tcaBackup = $GLOBALS['TCA'][$table];
168 unset($GLOBALS['TCA'][$table]);
169 $GLOBALS['TCA'][$table] = $tca;
170 $selectFields = $this->fixture->getCommonSelectFields($table, $prefix, $presetFields);
171 unset($GLOBALS['TCA'][$table]);
172 $GLOBALS['TCA'][$table] = $tcaBackup;
173 $this->assertEquals($selectFields, $expectedFields);
174 }
175
176 /**
177 * Tests concerning getLabelFromItemlist
178 */
179
180 /**
181 * Data provider for getLabelFromItemlistReturnsCorrectFields
182 *
183 * @return array The test data with $table, $col, $key, $expectedLabel
184 */
185 public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
186 return array(
187 'item set' => array(
188 'table' => 'tt_content',
189 'col' => 'menu_type',
190 'key' => '1',
191 'tca' => array(
192 'columns' => array(
193 'menu_type' => array(
194 'config' => array(
195 'items' => array(
196 array('Item 1', '0'),
197 array('Item 2', '1'),
198 array('Item 3', '3')
199 )
200 )
201 )
202 )
203 ),
204 'expectedLabel' => 'Item 2'
205 ),
206 'item set twice' => array(
207 'table' => 'tt_content',
208 'col' => 'menu_type',
209 'key' => '1',
210 'tca' => array(
211 'columns' => array(
212 'menu_type' => array(
213 'config' => array(
214 'items' => array(
215 array('Item 1', '0'),
216 array('Item 2a', '1'),
217 array('Item 2b', '1'),
218 array('Item 3', '3')
219 )
220 )
221 )
222 )
223 ),
224 'expectedLabel' => 'Item 2a'
225 ),
226 'item not found' => array(
227 'table' => 'tt_content',
228 'col' => 'menu_type',
229 'key' => '5',
230 'tca' => array(
231 'columns' => array(
232 'menu_type' => array(
233 'config' => array(
234 'items' => array(
235 array('Item 1', '0'),
236 array('Item 2', '1'),
237 array('Item 3', '2')
238 )
239 )
240 )
241 )
242 ),
243 'expectedLabel' => NULL
244 )
245 );
246 }
247
248 /**
249 * @test
250 * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
251 */
252 public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
253 $tcaBackup = $GLOBALS['TCA'][$table];
254 unset($GLOBALS['TCA'][$table]);
255 $GLOBALS['TCA'][$table] = $tca;
256 $label = $this->fixture->getLabelFromItemlist($table, $col, $key);
257 unset($GLOBALS['TCA'][$table]);
258 $GLOBALS['TCA'][$table] = $tcaBackup;
259 $this->assertEquals($label, $expectedLabel);
260 }
261
262 /**
263 * Tests concerning getLabelFromItemListMerged
264 */
265
266 /**
267 * Data provider for getLabelFromItemListMerged
268 *
269 * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
270 */
271 public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
272 return array(
273 'no field found' => array(
274 'pageId' => '123',
275 'table' => 'tt_content',
276 'col' => 'menu_type',
277 'key' => '10',
278 'tca' => array(
279 'columns' => array(
280 'menu_type' => array(
281 'config' => array(
282 'items' => array(
283 array('Item 1', '0'),
284 array('Item 2', '1'),
285 array('Item 3', '3')
286 )
287 )
288 )
289 )
290 ),
291 'expectedLabel' => ''
292 ),
293 'no tsconfig set' => array(
294 'pageId' => '123',
295 'table' => 'tt_content',
296 'col' => 'menu_type',
297 'key' => '1',
298 'tca' => array(
299 'columns' => array(
300 'menu_type' => array(
301 'config' => array(
302 'items' => array(
303 array('Item 1', '0'),
304 array('Item 2', '1'),
305 array('Item 3', '3')
306 )
307 )
308 )
309 )
310 ),
311 'expectedLabel' => 'Item 2'
312 )
313 );
314 }
315
316 /**
317 * @test
318 * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
319 */
320 public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
321 $tcaBackup = $GLOBALS['TCA'][$table];
322 unset($GLOBALS['TCA'][$table]);
323 $GLOBALS['TCA'][$table] = $tca;
324 $label = $this->fixture->getLabelFromItemListMerged($pageId, $table, $column, $key);
325 unset($GLOBALS['TCA'][$table]);
326 $GLOBALS['TCA'][$table] = $tcaBackup;
327 $this->assertEquals($label, $expectedLabel);
328 }
329
330 /**
331 * Tests concerning getFuncCheck
332 */
333
334 /**
335 * @test
336 */
337 public function getFuncCheckReturnsInputTagWithValueAttribute() {
338 $this->assertStringMatchesFormat('<input %Svalue="1"%S/>', Utility\BackendUtility::getFuncCheck('params', 'test', TRUE));
339 }
340
341 /**
342 * Tests concerning getExcludeFields
343 */
344
345 /**
346 * @return array
347 */
348 public function getExcludeFieldsDataProvider() {
349 return array(
350 'getExcludeFields does not return fields not configured as exclude field' => array(
351 array(
352 'tx_foo' => array(
353 'ctrl' => array(
354 'title' => 'foo',
355 ),
356 'columns' => array(
357 'bar' => array(
358 'label' => 'bar',
359 'exclude' => 1
360 ),
361 'baz' => array(
362 'label' => 'bar',
363 ),
364 )
365 )
366 ),
367 array(
368 array(
369 'foo: bar',
370 'tx_foo:bar',
371 ),
372 )
373 ),
374 'getExcludeFields returns fields from root level tables if root level restriction should be ignored' => array(
375 array(
376 'tx_foo' => array(
377 'ctrl' => array(
378 'title' => 'foo',
379 'rootLevel' => TRUE,
380 'security' => array(
381 'ignoreRootLevelRestriction' => TRUE,
382 ),
383 ),
384 'columns' => array(
385 'bar' => array(
386 'label' => 'bar',
387 'exclude' => 1
388 ),
389 )
390 )
391 ),
392 array(
393 array(
394 'foo: bar',
395 'tx_foo:bar',
396 ),
397 )
398 ),
399 'getExcludeFields does not return fields from root level tables' => array(
400 array(
401 'tx_foo' => array(
402 'ctrl' => array(
403 'title' => 'foo',
404 'rootLevel' => TRUE,
405 ),
406 'columns' => array(
407 'bar' => array(
408 'label' => 'bar',
409 'exclude' => 1
410 ),
411 )
412 )
413 ),
414 array()
415 ),
416 'getExcludeFields does not return fields from admin only level tables' => array(
417 array(
418 'tx_foo' => array(
419 'ctrl' => array(
420 'title' => 'foo',
421 'adminOnly' => TRUE,
422 ),
423 'columns' => array(
424 'bar' => array(
425 'label' => 'bar',
426 'exclude' => 1
427 ),
428 )
429 )
430 ),
431 array()
432 ),
433 );
434 }
435
436 /**
437 * @param $tca
438 * @param $expected
439 *
440 * @test
441 * @dataProvider getExcludeFieldsDataProvider
442 */
443 public function getExcludeFieldsReturnsCorrectFieldList($tca, $expected) {
444 $GLOBALS['TCA'] = $tca;
445 $this->assertSame($expected, \TYPO3\CMS\Backend\Utility\BackendUtility::getExcludeFields());
446 }
447 }
448
449 ?>