[BUGFIX] BackendUtility::calcAge returns negative value for 0
[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 calcAge
50 ///////////////////////////////////////
51 /**
52 * Data provider for calcAge function
53 *
54 * @return array
55 */
56 public function calcAgeDataProvider() {
57 return array(
58 'Single year' => array(
59 'seconds' => 60 * 60 * 24 * 365,
60 'expectedLabel' => '1 year'
61 ),
62 'Plural years' => array(
63 'seconds' => 60 * 60 * 24 * 365 * 2,
64 'expectedLabel' => '2 yrs'
65 ),
66 'Single negative year' => array(
67 'seconds' => 60 * 60 * 24 * 365 * -1,
68 'expectedLabel' => '-1 year'
69 ),
70 'Plural negative years' => array(
71 'seconds' => 60 * 60 * 24 * 365 * 2 * -1,
72 'expectedLabel' => '-2 yrs'
73 ),
74 'Single day' => array(
75 'seconds' => 60 * 60 * 24,
76 'expectedLabel' => '1 day'
77 ),
78 'Plural days' => array(
79 'seconds' => 60 * 60 * 24 * 2,
80 'expectedLabel' => '2 days'
81 ),
82 'Single negative day' => array(
83 'seconds' => 60 * 60 * 24 * -1,
84 'expectedLabel' => '-1 day'
85 ),
86 'Plural negative days' => array(
87 'seconds' => 60 * 60 * 24 * 2 * -1,
88 'expectedLabel' => '-2 days'
89 ),
90 'Single hour' => array(
91 'seconds' => 60 * 60,
92 'expectedLabel' => '1 hour'
93 ),
94 'Plural hours' => array(
95 'seconds' => 60 * 60 * 2,
96 'expectedLabel' => '2 hrs'
97 ),
98 'Single negative hour' => array(
99 'seconds' => 60 * 60 * -1,
100 'expectedLabel' => '-1 hour'
101 ),
102 'Plural negative hours' => array(
103 'seconds' => 60 * 60 * 2 * -1,
104 'expectedLabel' => '-2 hrs'
105 ),
106 'Single minute' => array(
107 'seconds' => 60,
108 'expectedLabel' => '1 min'
109 ),
110 'Plural minutes' => array(
111 'seconds' => 60 * 2,
112 'expectedLabel' => '2 min'
113 ),
114 'Single negative minute' => array(
115 'seconds' => 60 * -1,
116 'expectedLabel' => '-1 min'
117 ),
118 'Plural negative minutes' => array(
119 'seconds' => 60 * 2 * -1,
120 'expectedLabel' => '-2 min'
121 ),
122 'Zero seconds' => array(
123 'seconds' => 0,
124 'expectedLabel' => '0 min'
125 )
126 );
127 }
128
129 /**
130 * @test
131 * @dataProvider calcAgeDataProvider
132 */
133 public function calcAgeReturnsExpectedValues($seconds, $expectedLabel) {
134 $this->assertSame($expectedLabel, $this->fixture->calcAge($seconds));
135 }
136
137 ///////////////////////////////////////
138 // Tests concerning getProcessedValue
139 ///////////////////////////////////////
140 /**
141 * @test
142 * @see http://bugs.typo3.org/view.php?id=11875
143 */
144 public function getProcessedValueForZeroStringIsZero() {
145 $this->assertEquals('0', $this->fixture->getProcessedValue('tt_content', 'header', '0'));
146 }
147
148 /**
149 * Tests concerning getCommenSelectFields
150 */
151
152 /**
153 * Data provider for getCommonSelectFieldsReturnsCorrectFields
154 *
155 * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
156 */
157 public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider() {
158 return array(
159 'only uid' => array(
160 'table' => 'test_table',
161 'prefix' => '',
162 'presetFields' => array(),
163 'tca' => array(),
164 'expectedFields' => 'uid'
165 ),
166 'label set' => array(
167 'table' => 'test_table',
168 'prefix' => '',
169 'presetFields' => array(),
170 'tca' => array(
171 'ctrl' => array(
172 'label' => 'label'
173 )
174 ),
175 'expectedFields' => 'uid,label'
176 ),
177 'label_alt set' => array(
178 'table' => 'test_table',
179 'prefix' => '',
180 'presetFields' => array(),
181 'tca' => array(
182 'ctrl' => array(
183 'label_alt' => 'label,label2'
184 )
185 ),
186 'expectedFields' => 'uid,label,label2'
187 ),
188 'versioningWS set' => array(
189 'table' => 'test_table',
190 'prefix' => '',
191 'presetFields' => array(),
192 'tca' => array(
193 'ctrl' => array(
194 'versioningWS' => '2'
195 )
196 ),
197 'expectedFields' => 'uid,t3ver_id,t3ver_state,t3ver_wsid,t3ver_count'
198 ),
199 'selicon_field set' => array(
200 'table' => 'test_table',
201 'prefix' => '',
202 'presetFields' => array(),
203 'tca' => array(
204 'ctrl' => array(
205 'selicon_field' => 'field'
206 )
207 ),
208 'expectedFields' => 'uid,field'
209 ),
210 'typeicon_column set' => array(
211 'table' => 'test_table',
212 'prefix' => '',
213 'presetFields' => array(),
214 'tca' => array(
215 'ctrl' => array(
216 'typeicon_column' => 'field'
217 )
218 ),
219 'expectedFields' => 'uid,field'
220 ),
221 'enablecolumns set' => array(
222 'table' => 'test_table',
223 'prefix' => '',
224 'presetFields' => array(),
225 'tca' => array(
226 'ctrl' => array(
227 'enablecolumns' => array(
228 'disabled' => 'hidden',
229 'starttime' => 'start',
230 'endtime' => 'stop',
231 'fe_group' => 'groups'
232 )
233 )
234 ),
235 'expectedFields' => 'uid,hidden,start,stop,groups'
236 ),
237 'label set to uid' => array(
238 'table' => 'test_table',
239 'prefix' => '',
240 'presetFields' => array(),
241 'tca' => array(
242 'ctrl' => array(
243 'label' => 'uid'
244 )
245 ),
246 'expectedFields' => 'uid'
247 )
248 );
249 }
250
251 /**
252 * @test
253 * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
254 */
255 public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix = '', array $presetFields, array $tca, $expectedFields = '') {
256 $tcaBackup = $GLOBALS['TCA'][$table];
257 unset($GLOBALS['TCA'][$table]);
258 $GLOBALS['TCA'][$table] = $tca;
259 $selectFields = $this->fixture->getCommonSelectFields($table, $prefix, $presetFields);
260 unset($GLOBALS['TCA'][$table]);
261 $GLOBALS['TCA'][$table] = $tcaBackup;
262 $this->assertEquals($selectFields, $expectedFields);
263 }
264
265 /**
266 * Tests concerning getLabelFromItemlist
267 */
268
269 /**
270 * Data provider for getLabelFromItemlistReturnsCorrectFields
271 *
272 * @return array The test data with $table, $col, $key, $expectedLabel
273 */
274 public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
275 return array(
276 'item set' => array(
277 'table' => 'tt_content',
278 'col' => 'menu_type',
279 'key' => '1',
280 'tca' => array(
281 'columns' => array(
282 'menu_type' => array(
283 'config' => array(
284 'items' => array(
285 array('Item 1', '0'),
286 array('Item 2', '1'),
287 array('Item 3', '3')
288 )
289 )
290 )
291 )
292 ),
293 'expectedLabel' => 'Item 2'
294 ),
295 'item set twice' => array(
296 'table' => 'tt_content',
297 'col' => 'menu_type',
298 'key' => '1',
299 'tca' => array(
300 'columns' => array(
301 'menu_type' => array(
302 'config' => array(
303 'items' => array(
304 array('Item 1', '0'),
305 array('Item 2a', '1'),
306 array('Item 2b', '1'),
307 array('Item 3', '3')
308 )
309 )
310 )
311 )
312 ),
313 'expectedLabel' => 'Item 2a'
314 ),
315 'item not found' => array(
316 'table' => 'tt_content',
317 'col' => 'menu_type',
318 'key' => '5',
319 'tca' => array(
320 'columns' => array(
321 'menu_type' => array(
322 'config' => array(
323 'items' => array(
324 array('Item 1', '0'),
325 array('Item 2', '1'),
326 array('Item 3', '2')
327 )
328 )
329 )
330 )
331 ),
332 'expectedLabel' => NULL
333 )
334 );
335 }
336
337 /**
338 * @test
339 * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
340 */
341 public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
342 $tcaBackup = $GLOBALS['TCA'][$table];
343 unset($GLOBALS['TCA'][$table]);
344 $GLOBALS['TCA'][$table] = $tca;
345 $label = $this->fixture->getLabelFromItemlist($table, $col, $key);
346 unset($GLOBALS['TCA'][$table]);
347 $GLOBALS['TCA'][$table] = $tcaBackup;
348 $this->assertEquals($label, $expectedLabel);
349 }
350
351 /**
352 * Tests concerning getLabelFromItemListMerged
353 */
354
355 /**
356 * Data provider for getLabelFromItemListMerged
357 *
358 * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
359 */
360 public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
361 return array(
362 'no field found' => array(
363 'pageId' => '123',
364 'table' => 'tt_content',
365 'col' => 'menu_type',
366 'key' => '10',
367 'tca' => array(
368 'columns' => array(
369 'menu_type' => array(
370 'config' => array(
371 'items' => array(
372 array('Item 1', '0'),
373 array('Item 2', '1'),
374 array('Item 3', '3')
375 )
376 )
377 )
378 )
379 ),
380 'expectedLabel' => ''
381 ),
382 'no tsconfig set' => array(
383 'pageId' => '123',
384 'table' => 'tt_content',
385 'col' => 'menu_type',
386 'key' => '1',
387 'tca' => array(
388 'columns' => array(
389 'menu_type' => array(
390 'config' => array(
391 'items' => array(
392 array('Item 1', '0'),
393 array('Item 2', '1'),
394 array('Item 3', '3')
395 )
396 )
397 )
398 )
399 ),
400 'expectedLabel' => 'Item 2'
401 )
402 );
403 }
404
405 /**
406 * @test
407 * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
408 */
409 public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
410 $tcaBackup = $GLOBALS['TCA'][$table];
411 unset($GLOBALS['TCA'][$table]);
412 $GLOBALS['TCA'][$table] = $tca;
413 $label = $this->fixture->getLabelFromItemListMerged($pageId, $table, $column, $key);
414 unset($GLOBALS['TCA'][$table]);
415 $GLOBALS['TCA'][$table] = $tcaBackup;
416 $this->assertEquals($label, $expectedLabel);
417 }
418
419 /**
420 * Tests concerning getFuncCheck
421 */
422
423 /**
424 * @test
425 */
426 public function getFuncCheckReturnsInputTagWithValueAttribute() {
427 $this->assertStringMatchesFormat('<input %Svalue="1"%S/>', Utility\BackendUtility::getFuncCheck('params', 'test', TRUE));
428 }
429
430 /**
431 * Tests concerning getExcludeFields
432 */
433
434 /**
435 * @return array
436 */
437 public function getExcludeFieldsDataProvider() {
438 return array(
439 'getExcludeFields does not return fields not configured as exclude field' => array(
440 array(
441 'tx_foo' => array(
442 'ctrl' => array(
443 'title' => 'foo',
444 ),
445 'columns' => array(
446 'bar' => array(
447 'label' => 'bar',
448 'exclude' => 1
449 ),
450 'baz' => array(
451 'label' => 'bar',
452 ),
453 )
454 )
455 ),
456 array(
457 array(
458 'foo: bar',
459 'tx_foo:bar',
460 ),
461 )
462 ),
463 'getExcludeFields returns fields from root level tables if root level restriction should be ignored' => array(
464 array(
465 'tx_foo' => array(
466 'ctrl' => array(
467 'title' => 'foo',
468 'rootLevel' => TRUE,
469 'security' => array(
470 'ignoreRootLevelRestriction' => TRUE,
471 ),
472 ),
473 'columns' => array(
474 'bar' => array(
475 'label' => 'bar',
476 'exclude' => 1
477 ),
478 )
479 )
480 ),
481 array(
482 array(
483 'foo: bar',
484 'tx_foo:bar',
485 ),
486 )
487 ),
488 'getExcludeFields does not return fields from root level tables' => array(
489 array(
490 'tx_foo' => array(
491 'ctrl' => array(
492 'title' => 'foo',
493 'rootLevel' => TRUE,
494 ),
495 'columns' => array(
496 'bar' => array(
497 'label' => 'bar',
498 'exclude' => 1
499 ),
500 )
501 )
502 ),
503 array()
504 ),
505 'getExcludeFields does not return fields from admin only level tables' => array(
506 array(
507 'tx_foo' => array(
508 'ctrl' => array(
509 'title' => 'foo',
510 'adminOnly' => TRUE,
511 ),
512 'columns' => array(
513 'bar' => array(
514 'label' => 'bar',
515 'exclude' => 1
516 ),
517 )
518 )
519 ),
520 array()
521 ),
522 );
523 }
524
525 /**
526 * @param $tca
527 * @param $expected
528 *
529 * @test
530 * @dataProvider getExcludeFieldsDataProvider
531 */
532 public function getExcludeFieldsReturnsCorrectFieldList($tca, $expected) {
533 $GLOBALS['TCA'] = $tca;
534 $this->assertSame($expected, \TYPO3\CMS\Backend\Utility\BackendUtility::getExcludeFields());
535 }
536 }
537
538 ?>