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