[BUGFIX] Make BackendUtility::viewOnClick honor doc
[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 * @test
150 */
151 public function getProcessedValueForGroup() {
152 $this->assertSame('1, 2', $this->fixture->getProcessedValue('tt_content', 'multimedia', '1,2'));
153 }
154
155 /**
156 * @test
157 */
158 public function getProcessedValueForGroupWithOneAllowedTable() {
159 /** @var \PHPUnit_Framework_MockObject_MockObject|Utility\BackendUtility $fixture */
160 $fixture = $this->getMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('getRecordWSOL'));
161 $fixture->staticExpects($this->at(0))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 1')));
162 $fixture->staticExpects($this->at(1))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 2')));
163 $this->assertSame('Page 1, Page 2', $fixture->getProcessedValue('tt_content', 'pages', '1,2'));
164 }
165
166 /**
167 * @test
168 */
169 public function getProcessedValueForGroupWithMultipleAllowedTables() {
170 /** @var \PHPUnit_Framework_MockObject_MockObject|Utility\BackendUtility $fixture */
171 $fixture = $this->getMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('getRecordWSOL'));
172 $fixture->staticExpects($this->at(0))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 1')));
173 $fixture->staticExpects($this->at(1))->method('getRecordWSOL')->will($this->returnValue(array('header' => 'Content 2')));
174 $this->assertSame('Page 1, Content 2', $fixture->getProcessedValue('sys_category', 'items', 'pages_1,tt_content_2'));
175 }
176
177 /**
178 * Tests concerning getCommenSelectFields
179 */
180
181 /**
182 * Data provider for getCommonSelectFieldsReturnsCorrectFields
183 *
184 * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
185 */
186 public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider() {
187 return array(
188 'only uid' => array(
189 'table' => 'test_table',
190 'prefix' => '',
191 'presetFields' => array(),
192 'tca' => array(),
193 'expectedFields' => 'uid'
194 ),
195 'label set' => array(
196 'table' => 'test_table',
197 'prefix' => '',
198 'presetFields' => array(),
199 'tca' => array(
200 'ctrl' => array(
201 'label' => 'label'
202 )
203 ),
204 'expectedFields' => 'uid,label'
205 ),
206 'label_alt set' => array(
207 'table' => 'test_table',
208 'prefix' => '',
209 'presetFields' => array(),
210 'tca' => array(
211 'ctrl' => array(
212 'label_alt' => 'label,label2'
213 )
214 ),
215 'expectedFields' => 'uid,label,label2'
216 ),
217 'versioningWS set' => array(
218 'table' => 'test_table',
219 'prefix' => '',
220 'presetFields' => array(),
221 'tca' => array(
222 'ctrl' => array(
223 'versioningWS' => '2'
224 )
225 ),
226 'expectedFields' => 'uid,t3ver_id,t3ver_state,t3ver_wsid,t3ver_count'
227 ),
228 'selicon_field set' => array(
229 'table' => 'test_table',
230 'prefix' => '',
231 'presetFields' => array(),
232 'tca' => array(
233 'ctrl' => array(
234 'selicon_field' => 'field'
235 )
236 ),
237 'expectedFields' => 'uid,field'
238 ),
239 'typeicon_column set' => array(
240 'table' => 'test_table',
241 'prefix' => '',
242 'presetFields' => array(),
243 'tca' => array(
244 'ctrl' => array(
245 'typeicon_column' => 'field'
246 )
247 ),
248 'expectedFields' => 'uid,field'
249 ),
250 'enablecolumns set' => array(
251 'table' => 'test_table',
252 'prefix' => '',
253 'presetFields' => array(),
254 'tca' => array(
255 'ctrl' => array(
256 'enablecolumns' => array(
257 'disabled' => 'hidden',
258 'starttime' => 'start',
259 'endtime' => 'stop',
260 'fe_group' => 'groups'
261 )
262 )
263 ),
264 'expectedFields' => 'uid,hidden,start,stop,groups'
265 ),
266 'label set to uid' => array(
267 'table' => 'test_table',
268 'prefix' => '',
269 'presetFields' => array(),
270 'tca' => array(
271 'ctrl' => array(
272 'label' => 'uid'
273 )
274 ),
275 'expectedFields' => 'uid'
276 )
277 );
278 }
279
280 /**
281 * @test
282 * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
283 */
284 public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix = '', array $presetFields, array $tca, $expectedFields = '') {
285 $tcaBackup = $GLOBALS['TCA'][$table];
286 unset($GLOBALS['TCA'][$table]);
287 $GLOBALS['TCA'][$table] = $tca;
288 $selectFields = $this->fixture->getCommonSelectFields($table, $prefix, $presetFields);
289 unset($GLOBALS['TCA'][$table]);
290 $GLOBALS['TCA'][$table] = $tcaBackup;
291 $this->assertEquals($selectFields, $expectedFields);
292 }
293
294 /**
295 * Tests concerning getLabelFromItemlist
296 */
297
298 /**
299 * Data provider for getLabelFromItemlistReturnsCorrectFields
300 *
301 * @return array The test data with $table, $col, $key, $expectedLabel
302 */
303 public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
304 return array(
305 'item set' => array(
306 'table' => 'tt_content',
307 'col' => 'menu_type',
308 'key' => '1',
309 'tca' => array(
310 'columns' => array(
311 'menu_type' => array(
312 'config' => array(
313 'items' => array(
314 array('Item 1', '0'),
315 array('Item 2', '1'),
316 array('Item 3', '3')
317 )
318 )
319 )
320 )
321 ),
322 'expectedLabel' => 'Item 2'
323 ),
324 'item set twice' => array(
325 'table' => 'tt_content',
326 'col' => 'menu_type',
327 'key' => '1',
328 'tca' => array(
329 'columns' => array(
330 'menu_type' => array(
331 'config' => array(
332 'items' => array(
333 array('Item 1', '0'),
334 array('Item 2a', '1'),
335 array('Item 2b', '1'),
336 array('Item 3', '3')
337 )
338 )
339 )
340 )
341 ),
342 'expectedLabel' => 'Item 2a'
343 ),
344 'item not found' => array(
345 'table' => 'tt_content',
346 'col' => 'menu_type',
347 'key' => '5',
348 'tca' => array(
349 'columns' => array(
350 'menu_type' => array(
351 'config' => array(
352 'items' => array(
353 array('Item 1', '0'),
354 array('Item 2', '1'),
355 array('Item 3', '2')
356 )
357 )
358 )
359 )
360 ),
361 'expectedLabel' => NULL
362 )
363 );
364 }
365
366 /**
367 * @test
368 * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
369 */
370 public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
371 $tcaBackup = $GLOBALS['TCA'][$table];
372 unset($GLOBALS['TCA'][$table]);
373 $GLOBALS['TCA'][$table] = $tca;
374 $label = $this->fixture->getLabelFromItemlist($table, $col, $key);
375 unset($GLOBALS['TCA'][$table]);
376 $GLOBALS['TCA'][$table] = $tcaBackup;
377 $this->assertEquals($label, $expectedLabel);
378 }
379
380 /**
381 * Tests concerning getLabelFromItemListMerged
382 */
383
384 /**
385 * Data provider for getLabelFromItemListMerged
386 *
387 * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
388 */
389 public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
390 return array(
391 'no field found' => array(
392 'pageId' => '123',
393 'table' => 'tt_content',
394 'col' => 'menu_type',
395 'key' => '10',
396 'tca' => array(
397 'columns' => array(
398 'menu_type' => array(
399 'config' => array(
400 'items' => array(
401 array('Item 1', '0'),
402 array('Item 2', '1'),
403 array('Item 3', '3')
404 )
405 )
406 )
407 )
408 ),
409 'expectedLabel' => ''
410 ),
411 'no tsconfig set' => array(
412 'pageId' => '123',
413 'table' => 'tt_content',
414 'col' => 'menu_type',
415 'key' => '1',
416 'tca' => array(
417 'columns' => array(
418 'menu_type' => array(
419 'config' => array(
420 'items' => array(
421 array('Item 1', '0'),
422 array('Item 2', '1'),
423 array('Item 3', '3')
424 )
425 )
426 )
427 )
428 ),
429 'expectedLabel' => 'Item 2'
430 )
431 );
432 }
433
434 /**
435 * @test
436 * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
437 */
438 public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
439 $tcaBackup = $GLOBALS['TCA'][$table];
440 unset($GLOBALS['TCA'][$table]);
441 $GLOBALS['TCA'][$table] = $tca;
442 $label = $this->fixture->getLabelFromItemListMerged($pageId, $table, $column, $key);
443 unset($GLOBALS['TCA'][$table]);
444 $GLOBALS['TCA'][$table] = $tcaBackup;
445 $this->assertEquals($label, $expectedLabel);
446 }
447
448 /**
449 * Tests concerning getFuncCheck
450 */
451
452 /**
453 * @test
454 */
455 public function getFuncCheckReturnsInputTagWithValueAttribute() {
456 $this->assertStringMatchesFormat('<input %Svalue="1"%S/>', Utility\BackendUtility::getFuncCheck('params', 'test', TRUE));
457 }
458
459 /**
460 * Tests concerning getExcludeFields
461 */
462
463 /**
464 * @return array
465 */
466 public function getExcludeFieldsDataProvider() {
467 return array(
468 'getExcludeFields does not return fields not configured as exclude field' => array(
469 array(
470 'tx_foo' => array(
471 'ctrl' => array(
472 'title' => 'foo',
473 ),
474 'columns' => array(
475 'bar' => array(
476 'label' => 'bar',
477 'exclude' => 1
478 ),
479 'baz' => array(
480 'label' => 'bar',
481 ),
482 )
483 )
484 ),
485 array(
486 array(
487 'foo: bar',
488 'tx_foo:bar',
489 ),
490 )
491 ),
492 'getExcludeFields returns fields from root level tables if root level restriction should be ignored' => array(
493 array(
494 'tx_foo' => array(
495 'ctrl' => array(
496 'title' => 'foo',
497 'rootLevel' => TRUE,
498 'security' => array(
499 'ignoreRootLevelRestriction' => TRUE,
500 ),
501 ),
502 'columns' => array(
503 'bar' => array(
504 'label' => 'bar',
505 'exclude' => 1
506 ),
507 )
508 )
509 ),
510 array(
511 array(
512 'foo: bar',
513 'tx_foo:bar',
514 ),
515 )
516 ),
517 'getExcludeFields does not return fields from root level tables' => array(
518 array(
519 'tx_foo' => array(
520 'ctrl' => array(
521 'title' => 'foo',
522 'rootLevel' => TRUE,
523 ),
524 'columns' => array(
525 'bar' => array(
526 'label' => 'bar',
527 'exclude' => 1
528 ),
529 )
530 )
531 ),
532 array()
533 ),
534 'getExcludeFields does not return fields from admin only level tables' => array(
535 array(
536 'tx_foo' => array(
537 'ctrl' => array(
538 'title' => 'foo',
539 'adminOnly' => TRUE,
540 ),
541 'columns' => array(
542 'bar' => array(
543 'label' => 'bar',
544 'exclude' => 1
545 ),
546 )
547 )
548 ),
549 array()
550 ),
551 'getExcludeFields sorts tables and properties with flexform fields properly' => array(
552 array(
553 'tx_foo' => array(
554 'ctrl' => array(
555 'title' => 'foo'
556 ),
557 'columns' => array(
558 'foo' => array(
559 'label' => 'foo',
560 'exclude' => 1
561 ),
562 'bar' => array(
563 'label' => 'bar',
564 'exclude' => 1
565 ),
566 'abarfoo' => array(
567 'label' => 'abarfoo',
568 'config' => array(
569 'type' => 'flex',
570 'ds' => array(
571 '*,dummy' => '<?xml version="1.0" encoding="utf-8"?>
572 <T3DataStructure>
573 <sheets>
574 <sGeneral>
575 <ROOT>
576 <type>array</type>
577 <el>
578 <xmlTitle>
579 <TCEforms>
580 <exclude>1</exclude>
581 <label>The Title:</label>
582 <config>
583 <type>input</type>
584 <size>48</size>
585 </config>
586 </TCEforms>
587 </xmlTitle>
588 </el>
589 </ROOT>
590 </sGeneral>
591 </sheets>
592 </T3DataStructure>'
593 )
594 )
595 )
596 )
597 ),
598 'tx_foobar' => array(
599 'ctrl' => array(
600 'title' => 'foobar'
601 ),
602 'columns' => array(
603 'foo' => array(
604 'label' => 'foo',
605 'exclude' => 1
606 ),
607 'bar' => array(
608 'label' => 'bar',
609 'exclude' => 1
610 )
611 )
612 ),
613 'tx_bar' => array(
614 'ctrl' => array(
615 'title' => 'bar'
616 ),
617 'columns' => array(
618 'foo' => array(
619 'label' => 'foo',
620 'exclude' => 1
621 ),
622 'bar' => array(
623 'label' => 'bar',
624 'exclude' => 1
625 )
626 )
627 )
628 ),
629 array(
630 array(
631 'bar: bar',
632 'tx_bar:bar'
633 ),
634 array(
635 'bar: foo',
636 'tx_bar:foo'
637 ),
638 array(
639 'abarfoo dummy: The Title:',
640 'tx_foo:abarfoo;dummy;sGeneral;xmlTitle'
641 ),
642 array(
643 'foo: bar',
644 'tx_foo:bar'
645 ),
646 array(
647 'foo: foo',
648 'tx_foo:foo'
649 ),
650 array(
651 'foobar: bar',
652 'tx_foobar:bar'
653 ),
654 array(
655 'foobar: foo',
656 'tx_foobar:foo'
657 ),
658 )
659 )
660 );
661 }
662
663 /**
664 * @param $tca
665 * @param $expected
666 *
667 * @test
668 * @dataProvider getExcludeFieldsDataProvider
669 */
670 public function getExcludeFieldsReturnsCorrectFieldList($tca, $expected) {
671 $GLOBALS['TCA'] = $tca;
672 $this->assertSame($expected, \TYPO3\CMS\Backend\Utility\BackendUtility::getExcludeFields());
673 }
674
675 /**
676 * Tests concerning viewOnClick
677 */
678
679 /**
680 * @test
681 */
682 public function viewOnClickReturnsOnClickCodeWithAlternativeUrl() {
683 $alternativeUrl = 'https://typo3.org/about/typo3-the-cms/the-history-of-typo3/#section';
684 $onclickCode = 'var previewWin = window.open(\'' . $alternativeUrl . '\',\'newTYPO3frontendWindow\');';
685 $this->assertStringMatchesFormat($onclickCode, Utility\BackendUtility::viewOnClick(NULL, NULL, NULL, NULL, $alternativeUrl, NULL, FALSE));
686 }
687 }
688
689 ?>