[TASK] Update to phpunit 4.1
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Utility / BackendUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Oliver Klee (typo3-coding@oliverklee.de)
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
27 use TYPO3\CMS\Backend\Utility\BackendUtility;
28
29 /**
30 * Test case
31 *
32 * @author Oliver Klee <typo3-coding@oliverklee.de>
33 */
34 class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 ///////////////////////////////////////
37 // Tests concerning calcAge
38 ///////////////////////////////////////
39 /**
40 * Data provider for calcAge function
41 *
42 * @return array
43 */
44 public function calcAgeDataProvider() {
45 return array(
46 'Single year' => array(
47 'seconds' => 60 * 60 * 24 * 365,
48 'expectedLabel' => '1 year'
49 ),
50 'Plural years' => array(
51 'seconds' => 60 * 60 * 24 * 365 * 2,
52 'expectedLabel' => '2 yrs'
53 ),
54 'Single negative year' => array(
55 'seconds' => 60 * 60 * 24 * 365 * -1,
56 'expectedLabel' => '-1 year'
57 ),
58 'Plural negative years' => array(
59 'seconds' => 60 * 60 * 24 * 365 * 2 * -1,
60 'expectedLabel' => '-2 yrs'
61 ),
62 'Single day' => array(
63 'seconds' => 60 * 60 * 24,
64 'expectedLabel' => '1 day'
65 ),
66 'Plural days' => array(
67 'seconds' => 60 * 60 * 24 * 2,
68 'expectedLabel' => '2 days'
69 ),
70 'Single negative day' => array(
71 'seconds' => 60 * 60 * 24 * -1,
72 'expectedLabel' => '-1 day'
73 ),
74 'Plural negative days' => array(
75 'seconds' => 60 * 60 * 24 * 2 * -1,
76 'expectedLabel' => '-2 days'
77 ),
78 'Single hour' => array(
79 'seconds' => 60 * 60,
80 'expectedLabel' => '1 hour'
81 ),
82 'Plural hours' => array(
83 'seconds' => 60 * 60 * 2,
84 'expectedLabel' => '2 hrs'
85 ),
86 'Single negative hour' => array(
87 'seconds' => 60 * 60 * -1,
88 'expectedLabel' => '-1 hour'
89 ),
90 'Plural negative hours' => array(
91 'seconds' => 60 * 60 * 2 * -1,
92 'expectedLabel' => '-2 hrs'
93 ),
94 'Single minute' => array(
95 'seconds' => 60,
96 'expectedLabel' => '1 min'
97 ),
98 'Plural minutes' => array(
99 'seconds' => 60 * 2,
100 'expectedLabel' => '2 min'
101 ),
102 'Single negative minute' => array(
103 'seconds' => 60 * -1,
104 'expectedLabel' => '-1 min'
105 ),
106 'Plural negative minutes' => array(
107 'seconds' => 60 * 2 * -1,
108 'expectedLabel' => '-2 min'
109 ),
110 'Zero seconds' => array(
111 'seconds' => 0,
112 'expectedLabel' => '0 min'
113 )
114 );
115 }
116
117 /**
118 * @test
119 * @dataProvider calcAgeDataProvider
120 */
121 public function calcAgeReturnsExpectedValues($seconds, $expectedLabel) {
122 $this->assertSame($expectedLabel, BackendUtility::calcAge($seconds));
123 }
124
125 ///////////////////////////////////////
126 // Tests concerning getProcessedValue
127 ///////////////////////////////////////
128 /**
129 * @test
130 * @see http://forge.typo3.org/issues/20994
131 */
132 public function getProcessedValueForZeroStringIsZero() {
133 $GLOBALS['TCA'] = array(
134 'tt_content' => array(
135 'columns' => array(
136 'header' => array(
137 'config' => array(
138 'type' => 'input',
139 ),
140 ),
141 ),
142 ),
143 );
144 $this->assertEquals('0', BackendUtility::getProcessedValue('tt_content', 'header', '0'));
145 }
146
147 /**
148 * @test
149 */
150 public function getProcessedValueForGroup() {
151 $GLOBALS['TCA'] = array(
152 'tt_content' => array(
153 'columns' => array(
154 'multimedia' => array(
155 'config' => array(
156 'type' => 'group',
157 ),
158 ),
159 ),
160 ),
161 );
162 $this->assertSame('1, 2', BackendUtility::getProcessedValue('tt_content', 'multimedia', '1,2'));
163 }
164
165 /**
166 * @test
167 */
168 public function getProcessedValueForGroupWithOneAllowedTable() {
169 // Disable getRecordWSOL and getRecordTitle dependency by returning stable results
170 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
171 $className = uniqid('BackendUtility');
172 $subject = __NAMESPACE__ . '\\' . $className;
173 eval(
174 'namespace ' . __NAMESPACE__ . ';' .
175 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
176 ' static public function getRecordWSOL() {' .
177 ' static $called = 0;' .
178 ' ++$called;' .
179 ' if ($called === 1) {' .
180 ' return array(\'title\' => \'Page 1\');' .
181 ' }' .
182 ' if ($called === 2) {' .
183 ' return array(\'title\' => \'Page 2\');' .
184 ' }' .
185 ' }' .
186 ' static public function getRecordTitle() {' .
187 ' static $called = 0;' .
188 ' ++$called;' .
189 ' if ($called === 1) {' .
190 ' return \'Page 1\';' .
191 ' }' .
192 ' if ($called === 2) {' .
193 ' return \'Page 2\';' .
194 ' }' .
195 ' }' .
196 '}'
197 );
198
199 $GLOBALS['TCA'] = array(
200 'tt_content' => array(
201 'columns' => array(
202 'pages' => array(
203 'config' => array(
204 'type' => 'group',
205 'allowed' => 'pages',
206 'internal_type' => 'db',
207 'maxitems' => 22,
208 'minitems' => 0,
209 'show_thumbs' => 1,
210 'size' => 3,
211 ),
212 ),
213 ),
214 ),
215 );
216
217 $this->assertSame('Page 1, Page 2', $subject::getProcessedValue('tt_content', 'pages', '1,2'));
218 }
219
220 /**
221 * @test
222 */
223 public function getProcessedValueForGroupWithMultipleAllowedTables() {
224 // Disable getRecordWSOL and getRecordTitle dependency by returning stable results
225 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
226 $className = uniqid('BackendUtility');
227 $subject = __NAMESPACE__ . '\\' . $className;
228 eval(
229 'namespace ' . __NAMESPACE__ . ';' .
230 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
231 ' static public function getRecordWSOL() {' .
232 ' static $called = 0;' .
233 ' ++$called;' .
234 ' if ($called === 1) {' .
235 ' return array(\'title\' => \'Page 1\');' .
236 ' }' .
237 ' if ($called === 2) {' .
238 ' return array(\'header\' => \'Content 2\');' .
239 ' }' .
240 ' }' .
241 ' static public function getRecordTitle() {' .
242 ' static $called = 0;' .
243 ' ++$called;' .
244 ' if ($called === 1) {' .
245 ' return \'Page 1\';' .
246 ' }' .
247 ' if ($called === 2) {' .
248 ' return \'Content 2\';' .
249 ' }' .
250 ' }' .
251 '}'
252 );
253
254 $GLOBALS['TCA'] = array(
255 'sys_category' => array(
256 'columns' => array(
257 'items' => array(
258 'config' => array(
259 'allowed' => '*',
260 'internal_type' => 'db',
261 'MM' => 'sys_category_record_mm',
262 'size' => 10,
263 'type' => 'group',
264 ),
265 ),
266 ),
267 ),
268 );
269
270 $this->assertSame('Page 1, Content 2', $subject::getProcessedValue('sys_category', 'items', 'pages_1,tt_content_2'));
271 }
272
273 /**
274 * Tests concerning getCommonSelectFields
275 */
276
277 /**
278 * Data provider for getCommonSelectFieldsReturnsCorrectFields
279 *
280 * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
281 */
282 public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider() {
283 return array(
284 'only uid' => array(
285 'table' => 'test_table',
286 'prefix' => '',
287 'presetFields' => array(),
288 'tca' => array(),
289 'expectedFields' => 'uid'
290 ),
291 'label set' => array(
292 'table' => 'test_table',
293 'prefix' => '',
294 'presetFields' => array(),
295 'tca' => array(
296 'ctrl' => array(
297 'label' => 'label'
298 )
299 ),
300 'expectedFields' => 'uid,label'
301 ),
302 'label_alt set' => array(
303 'table' => 'test_table',
304 'prefix' => '',
305 'presetFields' => array(),
306 'tca' => array(
307 'ctrl' => array(
308 'label_alt' => 'label,label2'
309 )
310 ),
311 'expectedFields' => 'uid,label,label2'
312 ),
313 'versioningWS set' => array(
314 'table' => 'test_table',
315 'prefix' => '',
316 'presetFields' => array(),
317 'tca' => array(
318 'ctrl' => array(
319 'versioningWS' => '2'
320 )
321 ),
322 'expectedFields' => 'uid,t3ver_id,t3ver_state,t3ver_wsid,t3ver_count'
323 ),
324 'selicon_field set' => array(
325 'table' => 'test_table',
326 'prefix' => '',
327 'presetFields' => array(),
328 'tca' => array(
329 'ctrl' => array(
330 'selicon_field' => 'field'
331 )
332 ),
333 'expectedFields' => 'uid,field'
334 ),
335 'typeicon_column set' => array(
336 'table' => 'test_table',
337 'prefix' => '',
338 'presetFields' => array(),
339 'tca' => array(
340 'ctrl' => array(
341 'typeicon_column' => 'field'
342 )
343 ),
344 'expectedFields' => 'uid,field'
345 ),
346 'enablecolumns set' => array(
347 'table' => 'test_table',
348 'prefix' => '',
349 'presetFields' => array(),
350 'tca' => array(
351 'ctrl' => array(
352 'enablecolumns' => array(
353 'disabled' => 'hidden',
354 'starttime' => 'start',
355 'endtime' => 'stop',
356 'fe_group' => 'groups'
357 )
358 )
359 ),
360 'expectedFields' => 'uid,hidden,start,stop,groups'
361 ),
362 'label set to uid' => array(
363 'table' => 'test_table',
364 'prefix' => '',
365 'presetFields' => array(),
366 'tca' => array(
367 'ctrl' => array(
368 'label' => 'uid'
369 )
370 ),
371 'expectedFields' => 'uid'
372 )
373 );
374 }
375
376 /**
377 * @test
378 * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
379 */
380 public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix = '', array $presetFields, array $tca, $expectedFields = '') {
381 $GLOBALS['TCA'][$table] = $tca;
382 $selectFields = BackendUtility::getCommonSelectFields($table, $prefix, $presetFields);
383 $this->assertEquals($selectFields, $expectedFields);
384 }
385
386 /**
387 * Tests concerning getLabelFromItemlist
388 */
389
390 /**
391 * Data provider for getLabelFromItemlistReturnsCorrectFields
392 *
393 * @return array The test data with $table, $col, $key, $expectedLabel
394 */
395 public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
396 return array(
397 'item set' => array(
398 'table' => 'tt_content',
399 'col' => 'menu_type',
400 'key' => '1',
401 'tca' => array(
402 'columns' => array(
403 'menu_type' => array(
404 'config' => array(
405 'items' => array(
406 array('Item 1', '0'),
407 array('Item 2', '1'),
408 array('Item 3', '3')
409 )
410 )
411 )
412 )
413 ),
414 'expectedLabel' => 'Item 2'
415 ),
416 'item set twice' => array(
417 'table' => 'tt_content',
418 'col' => 'menu_type',
419 'key' => '1',
420 'tca' => array(
421 'columns' => array(
422 'menu_type' => array(
423 'config' => array(
424 'items' => array(
425 array('Item 1', '0'),
426 array('Item 2a', '1'),
427 array('Item 2b', '1'),
428 array('Item 3', '3')
429 )
430 )
431 )
432 )
433 ),
434 'expectedLabel' => 'Item 2a'
435 ),
436 'item not found' => array(
437 'table' => 'tt_content',
438 'col' => 'menu_type',
439 'key' => '5',
440 'tca' => array(
441 'columns' => array(
442 'menu_type' => array(
443 'config' => array(
444 'items' => array(
445 array('Item 1', '0'),
446 array('Item 2', '1'),
447 array('Item 3', '2')
448 )
449 )
450 )
451 )
452 ),
453 'expectedLabel' => NULL
454 )
455 );
456 }
457
458 /**
459 * @test
460 * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
461 */
462 public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
463 $GLOBALS['TCA'][$table] = $tca;
464 $label = BackendUtility::getLabelFromItemlist($table, $col, $key);
465 $this->assertEquals($label, $expectedLabel);
466 }
467
468 /**
469 * Tests concerning getLabelFromItemListMerged
470 */
471
472 /**
473 * Data provider for getLabelFromItemListMerged
474 *
475 * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
476 */
477 public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
478 return array(
479 'no field found' => array(
480 'pageId' => '123',
481 'table' => 'tt_content',
482 'col' => 'menu_type',
483 'key' => '10',
484 'tca' => array(
485 'columns' => array(
486 'menu_type' => array(
487 'config' => array(
488 'items' => array(
489 array('Item 1', '0'),
490 array('Item 2', '1'),
491 array('Item 3', '3')
492 )
493 )
494 )
495 )
496 ),
497 'expectedLabel' => ''
498 ),
499 'no tsconfig set' => array(
500 'pageId' => '123',
501 'table' => 'tt_content',
502 'col' => 'menu_type',
503 'key' => '1',
504 'tca' => array(
505 'columns' => array(
506 'menu_type' => array(
507 'config' => array(
508 'items' => array(
509 array('Item 1', '0'),
510 array('Item 2', '1'),
511 array('Item 3', '3')
512 )
513 )
514 )
515 )
516 ),
517 'expectedLabel' => 'Item 2'
518 )
519 );
520 }
521
522 /**
523 * @test
524 * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
525 */
526 public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
527 // Disable getPagesTSconfig by returning stable result
528 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
529 $className = uniqid('BackendUtility');
530 $subject = __NAMESPACE__ . '\\' . $className;
531 eval(
532 'namespace ' . __NAMESPACE__ . ';' .
533 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
534 ' static public function getPagesTSconfig() {' .
535 ' return array();' .
536 ' }' .
537 '}'
538 );
539
540 $GLOBALS['TCA'][$table] = $tca;
541
542 $this->assertEquals($expectedLabel, $subject::getLabelFromItemListMerged($pageId, $table, $column, $key));
543 }
544
545 /**
546 * Tests concerning getFuncCheck
547 */
548
549 /**
550 * @test
551 */
552 public function getFuncCheckReturnsInputTagWithValueAttribute() {
553 $this->assertStringMatchesFormat('<input %Svalue="1"%S/>', BackendUtility::getFuncCheck('params', 'test', TRUE));
554 }
555
556 /**
557 * Tests concerning getLabelsFromItemsList
558 */
559
560 /**
561 * @test
562 */
563 public function getLabelsFromItemsListReturnsValueIfItemIsFound() {
564 $table = 'foobar';
565 $col = 'someColumn';
566 $tca = array(
567 'columns' => array(
568 'someColumn' => array(
569 'config' => array(
570 'items' => array(
571 '0' => array('aFooLabel', 'foo'),
572 '1' => array('aBarLabel', 'bar')
573 )
574 )
575 )
576 )
577 );
578 // Stub LanguageService and let sL() return the same value that came in again
579 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
580 $GLOBALS['LANG']->expects($this->any())->method('sL')
581 ->will($this->returnCallback(
582 function($name) {
583 return $name;
584 }
585 ));
586
587 $GLOBALS['TCA'][$table] = $tca;
588 $label = BackendUtility::getLabelsFromItemsList($table, $col, 'foo,bar');
589 $this->assertEquals('aFooLabel, aBarLabel', $label);
590 }
591
592 /**
593 * @test
594 */
595 public function getProcessedValueReturnsLabelsForExistingValuesSolely() {
596 $table = 'foobar';
597 $col = 'someColumn';
598 $tca = array(
599 'columns' => array(
600 'someColumn' => array(
601 'config' => array(
602 'type' => 'select',
603 'items' => array(
604 '0' => array('aFooLabel', 'foo'),
605 '1' => array('aBarLabel', 'bar')
606 )
607 )
608 )
609 )
610 );
611 // Stub LanguageService and let sL() return the same value that came in again
612 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
613 $GLOBALS['LANG']->charSet = 'utf-8';
614 $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
615 $GLOBALS['LANG']->expects($this->any())->method('sL')
616 ->will($this->returnCallback(
617 function($name) {
618 return $name;
619 }
620 ));
621 $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
622 ->will($this->returnCallback(
623 function($charset, $string, $len, $crop = '') {
624 return $string;
625 }
626 ));
627
628 $GLOBALS['TCA'][$table] = $tca;
629 $label = BackendUtility::getProcessedValue($table, $col, 'foo,invalidKey,bar');
630 $this->assertEquals('aFooLabel, aBarLabel', $label);
631 }
632
633 /**
634 * @test
635 */
636 public function getProcessedValueReturnsPlainValueIfItemIsNotFound() {
637 $table = 'foobar';
638 $col = 'someColumn';
639 $tca = array(
640 'columns' => array(
641 'someColumn' => array(
642 'config' => array(
643 'type' => 'select',
644 'items' => array(
645 '0' => array('aFooLabel', 'foo')
646 )
647 )
648 )
649 )
650 );
651 // Stub LanguageService and let sL() return the same value that came in again
652 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
653 $GLOBALS['LANG']->charSet = 'utf-8';
654 $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
655 $GLOBALS['LANG']->expects($this->any())->method('sL')
656 ->will($this->returnCallback(
657 function($name) {
658 return $name;
659 }
660 ));
661 $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
662 ->will($this->returnCallback(
663 function($charset, $string, $len, $crop = '') {
664 return $string;
665 }
666 ));
667
668 $GLOBALS['TCA'][$table] = $tca;
669 $label = BackendUtility::getProcessedValue($table, $col, 'invalidKey');
670 $this->assertEquals('invalidKey', $label);
671 }
672
673 /**
674 * Tests concerning getExcludeFields
675 */
676
677 /**
678 * @return array
679 */
680 public function getExcludeFieldsDataProvider() {
681 return array(
682 'getExcludeFields does not return fields not configured as exclude field' => array(
683 array(
684 'tx_foo' => array(
685 'ctrl' => array(
686 'title' => 'foo',
687 ),
688 'columns' => array(
689 'bar' => array(
690 'label' => 'bar',
691 'exclude' => 1
692 ),
693 'baz' => array(
694 'label' => 'bar',
695 ),
696 )
697 )
698 ),
699 array(
700 array(
701 'foo: bar',
702 'tx_foo:bar',
703 ),
704 )
705 ),
706 'getExcludeFields returns fields from root level tables if root level restriction should be ignored' => array(
707 array(
708 'tx_foo' => array(
709 'ctrl' => array(
710 'title' => 'foo',
711 'rootLevel' => TRUE,
712 'security' => array(
713 'ignoreRootLevelRestriction' => TRUE,
714 ),
715 ),
716 'columns' => array(
717 'bar' => array(
718 'label' => 'bar',
719 'exclude' => 1
720 ),
721 )
722 )
723 ),
724 array(
725 array(
726 'foo: bar',
727 'tx_foo:bar',
728 ),
729 )
730 ),
731 'getExcludeFields does not return fields from root level tables' => array(
732 array(
733 'tx_foo' => array(
734 'ctrl' => array(
735 'title' => 'foo',
736 'rootLevel' => TRUE,
737 ),
738 'columns' => array(
739 'bar' => array(
740 'label' => 'bar',
741 'exclude' => 1
742 ),
743 )
744 )
745 ),
746 array()
747 ),
748 'getExcludeFields does not return fields from admin only level tables' => array(
749 array(
750 'tx_foo' => array(
751 'ctrl' => array(
752 'title' => 'foo',
753 'adminOnly' => TRUE,
754 ),
755 'columns' => array(
756 'bar' => array(
757 'label' => 'bar',
758 'exclude' => 1
759 ),
760 )
761 )
762 ),
763 array()
764 ),
765 );
766 }
767
768 /**
769 * @param $tca
770 * @param $expected
771 *
772 * @test
773 * @dataProvider getExcludeFieldsDataProvider
774 */
775 public function getExcludeFieldsReturnsCorrectFieldList($tca, $expected) {
776 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
777 $className = uniqid('BackendUtility');
778 $subject = __NAMESPACE__ . '\\' . $className;
779 eval(
780 'namespace ' . __NAMESPACE__ . ';' .
781 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
782 ' static public function getRegisteredFlexForms() {' .
783 ' return array();' .
784 ' }' .
785 '}'
786 );
787
788 $GLOBALS['TCA'] = $tca;
789
790 // Stub LanguageService and let sL() return the same value that came in again
791 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
792 $GLOBALS['LANG']->expects($this->any())->method('sL')
793 ->will($this->returnCallback(
794 function($name) {
795 return $name;
796 }
797 ));
798
799 $this->assertSame($expected, $subject::getExcludeFields());
800 }
801
802 /**
803 * @test
804 */
805 public function getExcludeFieldsReturnsCorrectListWithFlexFormFields() {
806 $GLOBALS['TCA'] = array(
807 'tx_foo' => array(
808 'ctrl' => array(
809 'title' => 'foo'
810 ),
811 'columns' => array(
812 'foo' => array(
813 'label' => 'foo',
814 'exclude' => 1
815 ),
816 'bar' => array(
817 'label' => 'bar',
818 'exclude' => 1
819 ),
820 'abarfoo' => array(
821 'label' => 'abarfoo',
822 'config' => array(
823 'type' => 'flex',
824 'ds' => array(
825 '*,dummy' => '',
826 )
827 )
828 )
829 )
830 ),
831 'tx_foobar' => array(
832 'ctrl' => array(
833 'title' => 'foobar'
834 ),
835 'columns' => array(
836 'foo' => array(
837 'label' => 'foo',
838 'exclude' => 1
839 ),
840 'bar' => array(
841 'label' => 'bar',
842 'exclude' => 1
843 )
844 )
845 ),
846 'tx_bar' => array(
847 'ctrl' => array(
848 'title' => 'bar'
849 ),
850 'columns' => array(
851 'foo' => array(
852 'label' => 'foo',
853 'exclude' => 1
854 ),
855 'bar' => array(
856 'label' => 'bar',
857 'exclude' => 1
858 )
859 )
860 )
861 );
862
863 $expectedResult = array(
864 array(
865 'bar: bar',
866 'tx_bar:bar'
867 ),
868 array(
869 'bar: foo',
870 'tx_bar:foo'
871 ),
872 array(
873 'abarfoo dummy: The Title:',
874 'tx_foo:abarfoo;dummy;sGeneral;xmlTitle'
875 ),
876 array(
877 'foo: bar',
878 'tx_foo:bar'
879 ),
880 array(
881 'foo: foo',
882 'tx_foo:foo'
883 ),
884 array(
885 'foobar: bar',
886 'tx_foobar:bar'
887 ),
888 array(
889 'foobar: foo',
890 'tx_foobar:foo'
891 ),
892 );
893
894 // Stub LanguageService and let sL() return the same value that came in again
895 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
896 $GLOBALS['LANG']->expects($this->any())->method('sL')
897 ->will($this->returnCallback(
898 function($name) {
899 return $name;
900 }
901 ));
902
903 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
904 $className = uniqid('BackendUtility');
905 $subject = __NAMESPACE__ . '\\' . $className;
906 eval(
907 'namespace ' . __NAMESPACE__ . ';' .
908 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
909 ' static public function getRegisteredFlexForms($table) {' .
910 ' static $called = 0;' .
911 ' ++$called;' .
912 ' if ($called === 1) {' .
913 ' return array();' .
914 ' }' .
915 ' if ($called === 2) {' .
916 ' if ($table !== \'tx_foo\') {' .
917 ' throw new Exception(\'Expected tx_foo as argument on call 2\', 1399638572);' .
918 ' }' .
919 ' $parsedFlexForm = array(' .
920 ' \'abarfoo\' => array(' .
921 ' \'dummy\' => array(' .
922 ' \'title\' => \'dummy\',' .
923 ' \'ds\' => array(' .
924 ' \'sheets\' => array(' .
925 ' \'sGeneral\' => array(' .
926 ' \'ROOT\' => array(' .
927 ' \'type\' => \'array\',' .
928 ' \'el\' => array(' .
929 ' \'xmlTitle\' => array(' .
930 ' \'TCEforms\' => array(' .
931 ' \'exclude\' => 1,' .
932 ' \'label\' => \'The Title:\',' .
933 ' \'config\' => array(' .
934 ' \'type\' => \'input\',' .
935 ' \'size\' => 48,' .
936 ' ),' .
937 ' ),' .
938 ' ),' .
939 ' ),' .
940 ' ),' .
941 ' ),' .
942 ' ),' .
943 ' ),' .
944 ' ),' .
945 ' ),' .
946 ' );' .
947 ' return $parsedFlexForm;' .
948 ' }' .
949 ' if ($called === 3) {' .
950 ' return array();' .
951 ' }' .
952 ' }' .
953 '}'
954 );
955
956 $this->assertSame($expectedResult, $subject::getExcludeFields());
957 }
958
959 /**
960 * Tests concerning viewOnClick
961 */
962
963 /**
964 * @test
965 */
966 public function viewOnClickReturnsOnClickCodeWithAlternativeUrl() {
967 // Make sure the hook inside viewOnClick is not fired. This may be removed if unit tests
968 // bootstrap does not initialize TYPO3_CONF_VARS anymore.
969 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['viewOnClickClass']);
970
971 $alternativeUrl = 'https://typo3.org/about/typo3-the-cms/the-history-of-typo3/#section';
972 $onclickCode = 'var previewWin = window.open(\'' . $alternativeUrl . '\',\'newTYPO3frontendWindow\');';
973 $this->assertStringMatchesFormat(
974 $onclickCode,
975 BackendUtility::viewOnClick(NULL, NULL, NULL, NULL, $alternativeUrl, NULL, FALSE)
976 );
977 }
978
979 /**
980 * Tests concerning replaceMarkersInWhereClause
981 */
982
983 /**
984 * @return array
985 */
986 public function replaceMarkersInWhereClauseDataProvider() {
987 return array(
988 'replaceMarkersInWhereClause replaces record field marker with quoted string' => array(
989 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'',
990 array(
991 '_THIS_ROW' => array(
992 'dummyfield' => 'Hello World'
993 )
994 ),
995 ' AND dummytable.title=\'Hello World\''
996 ),
997 'replaceMarkersInWhereClause replaces record field marker with fullquoted string' => array(
998 ' AND dummytable.title=###REC_FIELD_dummyfield###',
999 array(
1000 '_THIS_ROW' => array(
1001 'dummyfield' => 'Hello World'
1002 )
1003 ),
1004 ' AND dummytable.title=\'Hello World\''
1005 ),
1006 'replaceMarkersInWhereClause replaces multiple record field markers' => array(
1007 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\' AND dummytable.pid=###REC_FIELD_pid###',
1008 array(
1009 '_THIS_ROW' => array(
1010 'dummyfield' => 'Hello World',
1011 'pid' => 42
1012 )
1013 ),
1014 ' AND dummytable.title=\'Hello World\' AND dummytable.pid=\'42\''
1015 ),
1016 'replaceMarkersInWhereClause replaces current pid with integer' => array(
1017 ' AND dummytable.uid=###CURRENT_PID###',
1018 array(
1019 '_CURRENT_PID' => 42
1020 ),
1021 ' AND dummytable.uid=42'
1022 ),
1023 'replaceMarkersInWhereClause replaces current pid with string' => array(
1024 ' AND dummytable.uid=###CURRENT_PID###',
1025 array(
1026 '_CURRENT_PID' => '42string'
1027 ),
1028 ' AND dummytable.uid=42'
1029 ),
1030 'replaceMarkersInWhereClause replaces current record uid with integer' => array(
1031 ' AND dummytable.uid=###THIS_UID###',
1032 array(
1033 '_THIS_UID' => 42
1034 ),
1035 ' AND dummytable.uid=42'
1036 ),
1037 'replaceMarkersInWhereClause replaces current record uid with string' => array(
1038 ' AND dummytable.uid=###THIS_UID###',
1039 array(
1040 '_THIS_UID' => '42string'
1041 ),
1042 ' AND dummytable.uid=42'
1043 ),
1044 'replaceMarkersInWhereClause replaces current record cid with integer' => array(
1045 ' AND dummytable.uid=###THIS_CID###',
1046 array(
1047 '_THIS_CID' => 42
1048 ),
1049 ' AND dummytable.uid=42'
1050 ),
1051 'replaceMarkersInWhereClause replaces current record cid with string' => array(
1052 ' AND dummytable.uid=###THIS_CID###',
1053 array(
1054 '_THIS_CID' => '42string'
1055 ),
1056 ' AND dummytable.uid=42'
1057 ),
1058 'replaceMarkersInWhereClause replaces storage pid with integer' => array(
1059 ' AND dummytable.uid=###STORAGE_PID###',
1060 array(
1061 '_STORAGE_PID' => 42
1062 ),
1063 ' AND dummytable.uid=42'
1064 ),
1065 'replaceMarkersInWhereClause replaces storage pid with string' => array(
1066 ' AND dummytable.uid=###STORAGE_PID###',
1067 array(
1068 '_STORAGE_PID' => '42string'
1069 ),
1070 ' AND dummytable.uid=42'
1071 ),
1072 'replaceMarkersInWhereClause replaces siteroot uid with integer' => array(
1073 ' AND dummytable.uid=###SITEROOT###',
1074 array(
1075 '_SITEROOT' => 42
1076 ),
1077 ' AND dummytable.uid=42'
1078 ),
1079 'replaceMarkersInWhereClause replaces siteroot uid with string' => array(
1080 ' AND dummytable.uid=###SITEROOT###',
1081 array(
1082 '_SITEROOT' => '42string'
1083 ),
1084 ' AND dummytable.uid=42'
1085 ),
1086 'replaceMarkersInWhereClause replaces page tsconfig id with integer' => array(
1087 ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
1088 array(
1089 'dummyfield' => array(
1090 'PAGE_TSCONFIG_ID' => 42
1091 )
1092 ),
1093 ' AND dummytable.uid=42'
1094 ),
1095 'replaceMarkersInWhereClause replaces page tsconfig id with string' => array(
1096 ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
1097 array(
1098 'dummyfield' => array(
1099 'PAGE_TSCONFIG_ID' => '42string'
1100 )
1101 ),
1102 ' AND dummytable.uid=42'
1103 ),
1104 'replaceMarkersInWhereClause replaces page tsconfig string' => array(
1105 ' AND dummytable.title=\'###PAGE_TSCONFIG_STR###\'',
1106 array(
1107 'dummyfield' => array(
1108 'PAGE_TSCONFIG_STR' => '42'
1109 )
1110 ),
1111 ' AND dummytable.title=\'42\''
1112 ),
1113 'replaceMarkersInWhereClause replaces all markers' => array(
1114 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'' .
1115 ' AND dummytable.uid=###REC_FIELD_uid###' .
1116 ' AND dummytable.pid=###CURRENT_PID###' .
1117 ' AND dummytable.l18n_parent=###THIS_UID###' .
1118 ' AND dummytable.cid=###THIS_CID###' .
1119 ' AND dummytable.storage_pid=###STORAGE_PID###' .
1120 ' AND dummytable.siteroot=###SITEROOT###' .
1121 ' AND dummytable.config_uid=###PAGE_TSCONFIG_ID###' .
1122 ' AND dummytable.idlist IN (###PAGE_TSCONFIG_IDLIST###)' .
1123 ' AND dummytable.string=\'###PAGE_TSCONFIG_STR###\'',
1124 array(
1125 '_THIS_ROW' => array(
1126 'dummyfield' => 'Hello World',
1127 'uid' => 42
1128 ),
1129 '_CURRENT_PID' => '1',
1130 '_THIS_UID' => 2,
1131 '_THIS_CID' => 3,
1132 '_STORAGE_PID' => 4,
1133 '_SITEROOT' => 5,
1134 'dummyfield' => array(
1135 'PAGE_TSCONFIG_ID' => 6,
1136 'PAGE_TSCONFIG_IDLIST' => '1,2,3',
1137 'PAGE_TSCONFIG_STR' => 'string'
1138 )
1139 ),
1140 ' AND dummytable.title=\'Hello World\' AND dummytable.uid=\'42\' AND dummytable.pid=1' .
1141 ' AND dummytable.l18n_parent=2 AND dummytable.cid=3 AND dummytable.storage_pid=4' .
1142 ' AND dummytable.siteroot=5 AND dummytable.config_uid=6 AND dummytable.idlist IN (1,2,3)' .
1143 ' AND dummytable.string=\'string\'',
1144 ),
1145 );
1146 }
1147
1148 /**
1149 * @test
1150 * @dataProvider replaceMarkersInWhereClauseDataProvider
1151 */
1152 public function replaceMarkersInWhereClauseReturnsValidWhereClause($whereClause, $tsConfig, $expected) {
1153 // Mock TYPO3_DB and let it return same values that came in
1154 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
1155 $GLOBALS['TYPO3_DB']->expects($this->any())->method('quoteStr')
1156 ->will($this->returnCallback(
1157 function($quoteStr, $table) {
1158 return $quoteStr;
1159 }
1160 ));
1161 $GLOBALS['TYPO3_DB']->expects($this->any())->method('fullQuoteStr')
1162 ->will($this->returnCallback(
1163 function($quoteStr, $table) {
1164 return "'" . $quoteStr . "'";
1165 }
1166 ));
1167 $GLOBALS['TYPO3_DB']->expects($this->any())->method('cleanIntList')
1168 ->will($this->returnCallback(
1169 function($intList) {
1170 return $intList;
1171 }
1172 ));
1173 $this->assertSame($expected, BackendUtility::replaceMarkersInWhereClause($whereClause, 'dummytable', 'dummyfield', $tsConfig));
1174 }
1175
1176 /**
1177 * @test
1178 */
1179 public function replaceMarkersInWhereClauseCleansIdList() {
1180 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
1181 $GLOBALS['TYPO3_DB']->expects($this->once())->method('cleanIntList')->with('1,a,2,b,3,c');
1182 $where = ' AND dummytable.uid IN (###PAGE_TSCONFIG_IDLIST###)';
1183 $tsConfig = array(
1184 'dummyfield' => array(
1185 'PAGE_TSCONFIG_IDLIST' => '1,a,2,b,3,c'
1186 ),
1187 );
1188 BackendUtility::replaceMarkersInWhereClause($where, 'dummytable', 'dummyfield', $tsConfig);
1189 }
1190
1191 /**
1192 * @test
1193 */
1194 public function getModTSconfigIgnoresValuesFromUserTsConfigIfNoSet() {
1195 $completeConfiguration = array(
1196 'value' => 'bar',
1197 'properties' => array(
1198 'permissions.' => array(
1199 'file.' => array(
1200 'default.' => array('readAction' => '1'),
1201 '1.' => array('writeAction' => '1'),
1202 '0.' => array('readAction' => '0'),
1203 ),
1204 )
1205 )
1206 );
1207
1208 $GLOBALS['BE_USER'] = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication', array(), array(), '', FALSE);
1209 $GLOBALS['BE_USER']->expects($this->at(0))->method('getTSConfig')->will($this->returnValue($completeConfiguration));
1210 $GLOBALS['BE_USER']->expects($this->at(1))->method('getTSConfig')->will($this->returnValue(array('value' => NULL, 'properties' => NULL)));
1211
1212 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
1213 $className = uniqid('BackendUtility');
1214 $subject = __NAMESPACE__ . '\\' . $className;
1215 eval(
1216 'namespace ' . __NAMESPACE__ . ';' .
1217 'class ' . $className . ' extends \\TYPO3\\CMS\\Backend\\Utility\\BackendUtility {' .
1218 ' static public function getPagesTSconfig() {' .
1219 ' return array();' .
1220 ' }' .
1221 '}'
1222 );
1223
1224 $this->assertSame($completeConfiguration, $subject::getModTSconfig(42, 'notrelevant'));
1225 }
1226
1227 /**
1228 * Data provider for replaceL10nModeFieldsReplacesFields
1229 * @return array
1230 */
1231 public function replaceL10nModeFieldsReplacesFieldsDataProvider() {
1232 return array(
1233 'same table: mergeIfNotBlank' => array(
1234 'foo',
1235 array(
1236 'origUid' => 1,
1237 'field2' => 'fdas',
1238 'field3' => 'trans',
1239 ),
1240 array(
1241 'foo' => array(
1242 'ctrl' => array(
1243 'transOrigPointerTable' => '',
1244 'transOrigPointerField' => 'origUid'
1245 ),
1246 'columns' => array(
1247 'field2' => array('l10n_mode' => 'mergeIfNotBlank'),
1248 'field3' => array('l10n_mode' => 'mergeIfNotBlank')
1249 )
1250 )
1251 ),
1252 array(
1253 'origUid' => 0,
1254 'field2' => 'basic',
1255 'field3' => '',
1256 ),
1257 array(
1258 'origUid' => 1,
1259 'field2' => 'basic',
1260 'field3' => 'trans',
1261 )
1262 ),
1263 'other table: mergeIfNotBlank' => array(
1264 'foo',
1265 array(
1266 'origUid' => 1,
1267 'field2' => 'fdas',
1268 'field3' => 'trans',
1269 ),
1270 array(
1271 'foo' => array(
1272 'ctrl' => array(
1273 'transOrigPointerTable' => 'bar',
1274 'transOrigPointerField' => 'origUid'
1275 )
1276 ),
1277 'bar' => array(
1278 'columns' => array(
1279 'field2' => array('l10n_mode' => 'mergeIfNotBlank'),
1280 'field3' => array('l10n_mode' => 'mergeIfNotBlank')
1281 )
1282 )
1283 ),
1284 array(
1285 'origUid' => 0,
1286 'field2' => 'basic',
1287 'field3' => '',
1288 ),
1289 array(
1290 'origUid' => 1,
1291 'field2' => 'basic',
1292 'field3' => 'trans',
1293 )
1294 ),
1295 'same table: exclude' => array(
1296 'foo',
1297 array(
1298 'origUid' => 1,
1299 'field2' => 'fdas',
1300 'field3' => 'trans',
1301 ),
1302 array(
1303 'foo' => array(
1304 'ctrl' => array(
1305 'transOrigPointerTable' => '',
1306 'transOrigPointerField' => 'origUid'
1307 ),
1308 'columns' => array(
1309 'field2' => array('l10n_mode' => 'exclude'),
1310 'field3' => array('l10n_mode' => 'exclude')
1311 )
1312 )
1313 ),
1314 array(
1315 'origUid' => 0,
1316 'field2' => 'basic',
1317 'field3' => '',
1318 ),
1319 array(
1320 'origUid' => 1,
1321 'field2' => 'basic',
1322 'field3' => '',
1323 )
1324 ),
1325 'other table: exclude' => array(
1326 'foo',
1327 array(
1328 'origUid' => 1,
1329 'field2' => 'fdas',
1330 'field3' => 'trans',
1331 ),
1332 array(
1333 'foo' => array(
1334 'ctrl' => array(
1335 'transOrigPointerTable' => 'bar',
1336 'transOrigPointerField' => 'origUid'
1337 )
1338 ),
1339 'bar' => array(
1340 'columns' => array(
1341 'field2' => array('l10n_mode' => 'exclude'),
1342 'field3' => array('l10n_mode' => 'exclude')
1343 )
1344 )
1345 ),
1346 array(
1347 'origUid' => 0,
1348 'field2' => 'basic',
1349 'field3' => '',
1350 ),
1351 array(
1352 'origUid' => 1,
1353 'field2' => 'basic',
1354 'field3' => '',
1355 )
1356 ),
1357 );
1358 }
1359
1360 /**
1361 * @test
1362 * @dataProvider replaceL10nModeFieldsReplacesFieldsDataProvider
1363 */
1364 public function replaceL10nModeFieldsReplacesFields($table, $row, $tca, $originalRow, $expected) {
1365 $GLOBALS['TCA'] = $tca;
1366 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
1367 $GLOBALS['TYPO3_DB']->expects($this->any())->method('sql_fetch_assoc')->will($this->returnValue($originalRow));
1368
1369 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
1370 $subject = $this->getAccessibleMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('dummy'));
1371 $this->assertSame($expected, $subject->_call('replaceL10nModeFields', $table, $row));
1372 }
1373 }