[BUGFIX] Tests: Improve TCA mocking in BackendUtility
[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 * @var \TYPO3\CMS\Backend\Utility\BackendUtility
38 */
39 protected $subject;
40
41 public function setUp() {
42 $this->subject = new BackendUtility();
43 }
44
45 ///////////////////////////////////////
46 // Tests concerning calcAge
47 ///////////////////////////////////////
48 /**
49 * Data provider for calcAge function
50 *
51 * @return array
52 */
53 public function calcAgeDataProvider() {
54 return array(
55 'Single year' => array(
56 'seconds' => 60 * 60 * 24 * 365,
57 'expectedLabel' => '1 year'
58 ),
59 'Plural years' => array(
60 'seconds' => 60 * 60 * 24 * 365 * 2,
61 'expectedLabel' => '2 yrs'
62 ),
63 'Single negative year' => array(
64 'seconds' => 60 * 60 * 24 * 365 * -1,
65 'expectedLabel' => '-1 year'
66 ),
67 'Plural negative years' => array(
68 'seconds' => 60 * 60 * 24 * 365 * 2 * -1,
69 'expectedLabel' => '-2 yrs'
70 ),
71 'Single day' => array(
72 'seconds' => 60 * 60 * 24,
73 'expectedLabel' => '1 day'
74 ),
75 'Plural days' => array(
76 'seconds' => 60 * 60 * 24 * 2,
77 'expectedLabel' => '2 days'
78 ),
79 'Single negative day' => array(
80 'seconds' => 60 * 60 * 24 * -1,
81 'expectedLabel' => '-1 day'
82 ),
83 'Plural negative days' => array(
84 'seconds' => 60 * 60 * 24 * 2 * -1,
85 'expectedLabel' => '-2 days'
86 ),
87 'Single hour' => array(
88 'seconds' => 60 * 60,
89 'expectedLabel' => '1 hour'
90 ),
91 'Plural hours' => array(
92 'seconds' => 60 * 60 * 2,
93 'expectedLabel' => '2 hrs'
94 ),
95 'Single negative hour' => array(
96 'seconds' => 60 * 60 * -1,
97 'expectedLabel' => '-1 hour'
98 ),
99 'Plural negative hours' => array(
100 'seconds' => 60 * 60 * 2 * -1,
101 'expectedLabel' => '-2 hrs'
102 ),
103 'Single minute' => array(
104 'seconds' => 60,
105 'expectedLabel' => '1 min'
106 ),
107 'Plural minutes' => array(
108 'seconds' => 60 * 2,
109 'expectedLabel' => '2 min'
110 ),
111 'Single negative minute' => array(
112 'seconds' => 60 * -1,
113 'expectedLabel' => '-1 min'
114 ),
115 'Plural negative minutes' => array(
116 'seconds' => 60 * 2 * -1,
117 'expectedLabel' => '-2 min'
118 ),
119 'Zero seconds' => array(
120 'seconds' => 0,
121 'expectedLabel' => '0 min'
122 )
123 );
124 }
125
126 /**
127 * @test
128 * @dataProvider calcAgeDataProvider
129 */
130 public function calcAgeReturnsExpectedValues($seconds, $expectedLabel) {
131 $this->assertSame($expectedLabel, $this->subject->calcAge($seconds));
132 }
133
134 ///////////////////////////////////////
135 // Tests concerning getProcessedValue
136 ///////////////////////////////////////
137 /**
138 * @test
139 * @see http://forge.typo3.org/issues/20994
140 */
141 public function getProcessedValueForZeroStringIsZero() {
142 $GLOBALS['TCA'] = array(
143 'tt_content' => array(
144 'columns' => array(
145 'header' => array(
146 'config' => array(
147 'type' => 'input',
148 ),
149 ),
150 ),
151 ),
152 );
153 $this->assertEquals('0', $this->subject->getProcessedValue('tt_content', 'header', '0'));
154 }
155
156 /**
157 * @test
158 */
159 public function getProcessedValueForGroup() {
160 $GLOBALS['TCA'] = array(
161 'tt_content' => array(
162 'columns' => array(
163 'multimedia' => array(
164 'config' => array(
165 'type' => 'group',
166 ),
167 ),
168 ),
169 ),
170 );
171 $this->assertSame('1, 2', $this->subject->getProcessedValue('tt_content', 'multimedia', '1,2'));
172 }
173
174 /**
175 * @test
176 */
177 public function getProcessedValueForGroupWithOneAllowedTable() {
178 $GLOBALS['TCA'] = array(
179 'tt_content' => array(
180 'columns' => array(
181 'pages' => array(
182 'config' => array(
183 'type' => 'group',
184 'allowed' => 'pages',
185 'internal_type' => 'db',
186 'maxitems' => 22,
187 'minitems' => 0,
188 'show_thumbs' => 1,
189 'size' => 3,
190 ),
191 ),
192 ),
193 ),
194 );
195 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
196 $subject = $this->getMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('getRecordWSOL', 'getRecordTitle'));
197 $subject->staticExpects($this->at(0))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 1')));
198 $subject->staticExpects($this->at(1))->method('getRecordTitle')->will($this->returnValue('Page 1'));
199 $subject->staticExpects($this->at(2))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 2')));
200 $subject->staticExpects($this->at(3))->method('getRecordTitle')->will($this->returnValue('Page 2'));
201 $this->assertSame('Page 1, Page 2', $subject->getProcessedValue('tt_content', 'pages', '1,2'));
202 }
203
204 /**
205 * @test
206 */
207 public function getProcessedValueForGroupWithMultipleAllowedTables() {
208 $GLOBALS['TCA'] = array(
209 'sys_category' => array(
210 'columns' => array(
211 'items' => array(
212 'config' => array(
213 'allowed' => '*',
214 'internal_type' => 'db',
215 'MM' => 'sys_category_record_mm',
216 'size' => 10,
217 'type' => 'group',
218 ),
219 ),
220 ),
221 ),
222 );
223 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $subject */
224 $subject = $this->getMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('getRecordWSOL', 'getRecordTitle'));
225 $subject->staticExpects($this->at(0))->method('getRecordWSOL')->will($this->returnValue(array('title' => 'Page 1')));
226 $subject->staticExpects($this->at(1))->method('getRecordTitle')->will($this->returnValue('Page 1'));
227 $subject->staticExpects($this->at(2))->method('getRecordWSOL')->will($this->returnValue(array('header' => 'Content 2')));
228 $subject->staticExpects($this->at(3))->method('getRecordTitle')->will($this->returnValue('Content 2'));
229 $this->assertSame('Page 1, Content 2', $subject->getProcessedValue('sys_category', 'items', 'pages_1,tt_content_2'));
230 }
231
232 /**
233 * Tests concerning getCommonSelectFields
234 */
235
236 /**
237 * Data provider for getCommonSelectFieldsReturnsCorrectFields
238 *
239 * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
240 */
241 public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider() {
242 return array(
243 'only uid' => array(
244 'table' => 'test_table',
245 'prefix' => '',
246 'presetFields' => array(),
247 'tca' => array(),
248 'expectedFields' => 'uid'
249 ),
250 'label set' => array(
251 'table' => 'test_table',
252 'prefix' => '',
253 'presetFields' => array(),
254 'tca' => array(
255 'ctrl' => array(
256 'label' => 'label'
257 )
258 ),
259 'expectedFields' => 'uid,label'
260 ),
261 'label_alt set' => array(
262 'table' => 'test_table',
263 'prefix' => '',
264 'presetFields' => array(),
265 'tca' => array(
266 'ctrl' => array(
267 'label_alt' => 'label,label2'
268 )
269 ),
270 'expectedFields' => 'uid,label,label2'
271 ),
272 'versioningWS set' => array(
273 'table' => 'test_table',
274 'prefix' => '',
275 'presetFields' => array(),
276 'tca' => array(
277 'ctrl' => array(
278 'versioningWS' => '2'
279 )
280 ),
281 'expectedFields' => 'uid,t3ver_id,t3ver_state,t3ver_wsid,t3ver_count'
282 ),
283 'selicon_field set' => array(
284 'table' => 'test_table',
285 'prefix' => '',
286 'presetFields' => array(),
287 'tca' => array(
288 'ctrl' => array(
289 'selicon_field' => 'field'
290 )
291 ),
292 'expectedFields' => 'uid,field'
293 ),
294 'typeicon_column set' => array(
295 'table' => 'test_table',
296 'prefix' => '',
297 'presetFields' => array(),
298 'tca' => array(
299 'ctrl' => array(
300 'typeicon_column' => 'field'
301 )
302 ),
303 'expectedFields' => 'uid,field'
304 ),
305 'enablecolumns set' => array(
306 'table' => 'test_table',
307 'prefix' => '',
308 'presetFields' => array(),
309 'tca' => array(
310 'ctrl' => array(
311 'enablecolumns' => array(
312 'disabled' => 'hidden',
313 'starttime' => 'start',
314 'endtime' => 'stop',
315 'fe_group' => 'groups'
316 )
317 )
318 ),
319 'expectedFields' => 'uid,hidden,start,stop,groups'
320 ),
321 'label set to uid' => array(
322 'table' => 'test_table',
323 'prefix' => '',
324 'presetFields' => array(),
325 'tca' => array(
326 'ctrl' => array(
327 'label' => 'uid'
328 )
329 ),
330 'expectedFields' => 'uid'
331 )
332 );
333 }
334
335 /**
336 * @test
337 * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
338 */
339 public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix = '', array $presetFields, array $tca, $expectedFields = '') {
340 $GLOBALS['TCA'][$table] = $tca;
341 $selectFields = $this->subject->getCommonSelectFields($table, $prefix, $presetFields);
342 $this->assertEquals($selectFields, $expectedFields);
343 }
344
345 /**
346 * Tests concerning getLabelFromItemlist
347 */
348
349 /**
350 * Data provider for getLabelFromItemlistReturnsCorrectFields
351 *
352 * @return array The test data with $table, $col, $key, $expectedLabel
353 */
354 public function getLabelFromItemlistReturnsCorrectFieldsDataProvider() {
355 return array(
356 'item set' => array(
357 'table' => 'tt_content',
358 'col' => 'menu_type',
359 'key' => '1',
360 'tca' => array(
361 'columns' => array(
362 'menu_type' => array(
363 'config' => array(
364 'items' => array(
365 array('Item 1', '0'),
366 array('Item 2', '1'),
367 array('Item 3', '3')
368 )
369 )
370 )
371 )
372 ),
373 'expectedLabel' => 'Item 2'
374 ),
375 'item set twice' => array(
376 'table' => 'tt_content',
377 'col' => 'menu_type',
378 'key' => '1',
379 'tca' => array(
380 'columns' => array(
381 'menu_type' => array(
382 'config' => array(
383 'items' => array(
384 array('Item 1', '0'),
385 array('Item 2a', '1'),
386 array('Item 2b', '1'),
387 array('Item 3', '3')
388 )
389 )
390 )
391 )
392 ),
393 'expectedLabel' => 'Item 2a'
394 ),
395 'item not found' => array(
396 'table' => 'tt_content',
397 'col' => 'menu_type',
398 'key' => '5',
399 'tca' => array(
400 'columns' => array(
401 'menu_type' => array(
402 'config' => array(
403 'items' => array(
404 array('Item 1', '0'),
405 array('Item 2', '1'),
406 array('Item 3', '2')
407 )
408 )
409 )
410 )
411 ),
412 'expectedLabel' => NULL
413 )
414 );
415 }
416
417 /**
418 * @test
419 * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
420 */
421 public function getLabelFromItemlistReturnsCorrectFields($table, $col = '', $key = '', array $tca, $expectedLabel = '') {
422 $GLOBALS['TCA'][$table] = $tca;
423 $label = $this->subject->getLabelFromItemlist($table, $col, $key);
424 $this->assertEquals($label, $expectedLabel);
425 }
426
427 /**
428 * Tests concerning getLabelFromItemListMerged
429 */
430
431 /**
432 * Data provider for getLabelFromItemListMerged
433 *
434 * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
435 */
436 public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider() {
437 return array(
438 'no field found' => array(
439 'pageId' => '123',
440 'table' => 'tt_content',
441 'col' => 'menu_type',
442 'key' => '10',
443 'tca' => array(
444 'columns' => array(
445 'menu_type' => array(
446 'config' => array(
447 'items' => array(
448 array('Item 1', '0'),
449 array('Item 2', '1'),
450 array('Item 3', '3')
451 )
452 )
453 )
454 )
455 ),
456 'expectedLabel' => ''
457 ),
458 'no tsconfig set' => array(
459 'pageId' => '123',
460 'table' => 'tt_content',
461 'col' => 'menu_type',
462 'key' => '1',
463 'tca' => array(
464 'columns' => array(
465 'menu_type' => array(
466 'config' => array(
467 'items' => array(
468 array('Item 1', '0'),
469 array('Item 2', '1'),
470 array('Item 3', '3')
471 )
472 )
473 )
474 )
475 ),
476 'expectedLabel' => 'Item 2'
477 )
478 );
479 }
480
481 /**
482 * @test
483 * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
484 */
485 public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column = '', $key = '', array $tca, $expectedLabel = '') {
486 $GLOBALS['TCA'][$table] = $tca;
487 $label = $this->subject->getLabelFromItemListMerged($pageId, $table, $column, $key);
488 $this->assertEquals($label, $expectedLabel);
489 }
490
491 /**
492 * Tests concerning getFuncCheck
493 */
494
495 /**
496 * @test
497 */
498 public function getFuncCheckReturnsInputTagWithValueAttribute() {
499 $this->assertStringMatchesFormat('<input %Svalue="1"%S/>', BackendUtility::getFuncCheck('params', 'test', TRUE));
500 }
501
502 /**
503 * Tests concerning getLabelsFromItemsList
504 */
505
506 /**
507 * @test
508 */
509 public function getLabelsFromItemsListReturnsValueIfItemIsFound() {
510 $table = 'foobar';
511 $col = 'someColumn';
512 $tca = array(
513 'columns' => array(
514 'someColumn' => array(
515 'config' => array(
516 'items' => array(
517 '0' => array('aFooLabel', 'foo'),
518 '1' => array('aBarLabel', 'bar')
519 )
520 )
521 )
522 )
523 );
524 // Stub LanguageService and let sL() return the same value that came in again
525 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
526 $GLOBALS['LANG']->expects($this->any())->method('sL')
527 ->will($this->returnCallback(
528 function($name) {
529 return $name;
530 }
531 ));
532
533 $GLOBALS['TCA'][$table] = $tca;
534 $label = $this->subject->getLabelsFromItemsList($table, $col, 'foo,bar');
535 $this->assertEquals('aFooLabel, aBarLabel', $label);
536 }
537
538 /**
539 * @test
540 */
541 public function getProcessedValueReturnsLabelsForExistingValuesSolely() {
542 $table = 'foobar';
543 $col = 'someColumn';
544 $tca = array(
545 'columns' => array(
546 'someColumn' => array(
547 'config' => array(
548 'type' => 'select',
549 'items' => array(
550 '0' => array('aFooLabel', 'foo'),
551 '1' => array('aBarLabel', 'bar')
552 )
553 )
554 )
555 )
556 );
557 // Stub LanguageService and let sL() return the same value that came in again
558 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
559 $GLOBALS['LANG']->charSet = 'utf-8';
560 $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
561 $GLOBALS['LANG']->expects($this->any())->method('sL')
562 ->will($this->returnCallback(
563 function($name) {
564 return $name;
565 }
566 ));
567 $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
568 ->will($this->returnCallback(
569 function($charset, $string, $len, $crop = '') {
570 return $string;
571 }
572 ));
573
574 $GLOBALS['TCA'][$table] = $tca;
575 $label = $this->subject->getProcessedValue($table, $col, 'foo,invalidKey,bar');
576 $this->assertEquals('aFooLabel, aBarLabel', $label);
577 }
578
579 /**
580 * @test
581 */
582 public function getProcessedValueReturnsPlainValueIfItemIsNotFound() {
583 $table = 'foobar';
584 $col = 'someColumn';
585 $tca = array(
586 'columns' => array(
587 'someColumn' => array(
588 'config' => array(
589 'type' => 'select',
590 'items' => array(
591 '0' => array('aFooLabel', 'foo')
592 )
593 )
594 )
595 )
596 );
597 // Stub LanguageService and let sL() return the same value that came in again
598 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
599 $GLOBALS['LANG']->charSet = 'utf-8';
600 $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
601 $GLOBALS['LANG']->expects($this->any())->method('sL')
602 ->will($this->returnCallback(
603 function($name) {
604 return $name;
605 }
606 ));
607 $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
608 ->will($this->returnCallback(
609 function($charset, $string, $len, $crop = '') {
610 return $string;
611 }
612 ));
613
614 $GLOBALS['TCA'][$table] = $tca;
615 $label = $this->subject->getProcessedValue($table, $col, 'invalidKey');
616 $this->assertEquals('invalidKey', $label);
617 }
618
619 /**
620 * Tests concerning getExcludeFields
621 */
622
623 /**
624 * @return array
625 */
626 public function getExcludeFieldsDataProvider() {
627 return array(
628 'getExcludeFields does not return fields not configured as exclude field' => array(
629 array(
630 'tx_foo' => array(
631 'ctrl' => array(
632 'title' => 'foo',
633 ),
634 'columns' => array(
635 'bar' => array(
636 'label' => 'bar',
637 'exclude' => 1
638 ),
639 'baz' => array(
640 'label' => 'bar',
641 ),
642 )
643 )
644 ),
645 array(
646 array(
647 'foo: bar',
648 'tx_foo:bar',
649 ),
650 )
651 ),
652 'getExcludeFields returns fields from root level tables if root level restriction should be ignored' => array(
653 array(
654 'tx_foo' => array(
655 'ctrl' => array(
656 'title' => 'foo',
657 'rootLevel' => TRUE,
658 'security' => array(
659 'ignoreRootLevelRestriction' => TRUE,
660 ),
661 ),
662 'columns' => array(
663 'bar' => array(
664 'label' => 'bar',
665 'exclude' => 1
666 ),
667 )
668 )
669 ),
670 array(
671 array(
672 'foo: bar',
673 'tx_foo:bar',
674 ),
675 )
676 ),
677 'getExcludeFields does not return fields from root level tables' => array(
678 array(
679 'tx_foo' => array(
680 'ctrl' => array(
681 'title' => 'foo',
682 'rootLevel' => TRUE,
683 ),
684 'columns' => array(
685 'bar' => array(
686 'label' => 'bar',
687 'exclude' => 1
688 ),
689 )
690 )
691 ),
692 array()
693 ),
694 'getExcludeFields does not return fields from admin only level tables' => array(
695 array(
696 'tx_foo' => array(
697 'ctrl' => array(
698 'title' => 'foo',
699 'adminOnly' => TRUE,
700 ),
701 'columns' => array(
702 'bar' => array(
703 'label' => 'bar',
704 'exclude' => 1
705 ),
706 )
707 )
708 ),
709 array()
710 ),
711 'getExcludeFields sorts tables and properties with flexform fields properly' => array(
712 array(
713 'tx_foo' => array(
714 'ctrl' => array(
715 'title' => 'foo'
716 ),
717 'columns' => array(
718 'foo' => array(
719 'label' => 'foo',
720 'exclude' => 1
721 ),
722 'bar' => array(
723 'label' => 'bar',
724 'exclude' => 1
725 ),
726 'abarfoo' => array(
727 'label' => 'abarfoo',
728 'config' => array(
729 'type' => 'flex',
730 'ds' => array(
731 '*,dummy' => '<?xml version="1.0" encoding="utf-8"?>
732 <T3DataStructure>
733 <sheets>
734 <sGeneral>
735 <ROOT>
736 <type>array</type>
737 <el>
738 <xmlTitle>
739 <TCEforms>
740 <exclude>1</exclude>
741 <label>The Title:</label>
742 <config>
743 <type>input</type>
744 <size>48</size>
745 </config>
746 </TCEforms>
747 </xmlTitle>
748 </el>
749 </ROOT>
750 </sGeneral>
751 </sheets>
752 </T3DataStructure>'
753 )
754 )
755 )
756 )
757 ),
758 'tx_foobar' => array(
759 'ctrl' => array(
760 'title' => 'foobar'
761 ),
762 'columns' => array(
763 'foo' => array(
764 'label' => 'foo',
765 'exclude' => 1
766 ),
767 'bar' => array(
768 'label' => 'bar',
769 'exclude' => 1
770 )
771 )
772 ),
773 'tx_bar' => array(
774 'ctrl' => array(
775 'title' => 'bar'
776 ),
777 'columns' => array(
778 'foo' => array(
779 'label' => 'foo',
780 'exclude' => 1
781 ),
782 'bar' => array(
783 'label' => 'bar',
784 'exclude' => 1
785 )
786 )
787 )
788 ),
789 array(
790 array(
791 'bar: bar',
792 'tx_bar:bar'
793 ),
794 array(
795 'bar: foo',
796 'tx_bar:foo'
797 ),
798 array(
799 'abarfoo dummy: The Title:',
800 'tx_foo:abarfoo;dummy;sGeneral;xmlTitle'
801 ),
802 array(
803 'foo: bar',
804 'tx_foo:bar'
805 ),
806 array(
807 'foo: foo',
808 'tx_foo:foo'
809 ),
810 array(
811 'foobar: bar',
812 'tx_foobar:bar'
813 ),
814 array(
815 'foobar: foo',
816 'tx_foobar:foo'
817 ),
818 )
819 )
820 );
821 }
822
823 /**
824 * @param $tca
825 * @param $expected
826 *
827 * @test
828 * @dataProvider getExcludeFieldsDataProvider
829 */
830 public function getExcludeFieldsReturnsCorrectFieldList($tca, $expected) {
831 $GLOBALS['TCA'] = $tca;
832
833 // Stub LanguageService and let sL() return the same value that came in again
834 $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
835 $GLOBALS['LANG']->expects($this->any())->method('sL')
836 ->will($this->returnCallback(
837 function($name) {
838 return $name;
839 }
840 ));
841
842 $this->assertSame($expected, BackendUtility::getExcludeFields());
843 }
844
845 /**
846 * Tests concerning viewOnClick
847 */
848
849 /**
850 * @test
851 */
852 public function viewOnClickReturnsOnClickCodeWithAlternativeUrl() {
853 // Make sure the hook inside viewOnClick is not fired. This may be removed if unit tests
854 // bootstrap does not initialize TYPO3_CONF_VARS anymore.
855 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['viewOnClickClass']);
856
857 $alternativeUrl = 'https://typo3.org/about/typo3-the-cms/the-history-of-typo3/#section';
858 $onclickCode = 'var previewWin = window.open(\'' . $alternativeUrl . '\',\'newTYPO3frontendWindow\');';
859 $this->assertStringMatchesFormat(
860 $onclickCode,
861 BackendUtility::viewOnClick(NULL, NULL, NULL, NULL, $alternativeUrl, NULL, FALSE)
862 );
863 }
864
865 /**
866 * Tests concerning replaceMarkersInWhereClause
867 */
868
869 /**
870 * @return array
871 */
872 public function replaceMarkersInWhereClauseDataProvider() {
873 return array(
874 'replaceMarkersInWhereClause replaces record field marker with quoted string' => array(
875 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'',
876 array(
877 '_THIS_ROW' => array(
878 'dummyfield' => 'Hello World'
879 )
880 ),
881 ' AND dummytable.title=\'Hello World\''
882 ),
883 'replaceMarkersInWhereClause replaces record field marker with fullquoted string' => array(
884 ' AND dummytable.title=###REC_FIELD_dummyfield###',
885 array(
886 '_THIS_ROW' => array(
887 'dummyfield' => 'Hello World'
888 )
889 ),
890 ' AND dummytable.title=\'Hello World\''
891 ),
892 'replaceMarkersInWhereClause replaces multiple record field markers' => array(
893 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\' AND dummytable.pid=###REC_FIELD_pid###',
894 array(
895 '_THIS_ROW' => array(
896 'dummyfield' => 'Hello World',
897 'pid' => 42
898 )
899 ),
900 ' AND dummytable.title=\'Hello World\' AND dummytable.pid=\'42\''
901 ),
902 'replaceMarkersInWhereClause replaces current pid with integer' => array(
903 ' AND dummytable.uid=###CURRENT_PID###',
904 array(
905 '_CURRENT_PID' => 42
906 ),
907 ' AND dummytable.uid=42'
908 ),
909 'replaceMarkersInWhereClause replaces current pid with string' => array(
910 ' AND dummytable.uid=###CURRENT_PID###',
911 array(
912 '_CURRENT_PID' => '42string'
913 ),
914 ' AND dummytable.uid=42'
915 ),
916 'replaceMarkersInWhereClause replaces current record uid with integer' => array(
917 ' AND dummytable.uid=###THIS_UID###',
918 array(
919 '_THIS_UID' => 42
920 ),
921 ' AND dummytable.uid=42'
922 ),
923 'replaceMarkersInWhereClause replaces current record uid with string' => array(
924 ' AND dummytable.uid=###THIS_UID###',
925 array(
926 '_THIS_UID' => '42string'
927 ),
928 ' AND dummytable.uid=42'
929 ),
930 'replaceMarkersInWhereClause replaces current record cid with integer' => array(
931 ' AND dummytable.uid=###THIS_CID###',
932 array(
933 '_THIS_CID' => 42
934 ),
935 ' AND dummytable.uid=42'
936 ),
937 'replaceMarkersInWhereClause replaces current record cid with string' => array(
938 ' AND dummytable.uid=###THIS_CID###',
939 array(
940 '_THIS_CID' => '42string'
941 ),
942 ' AND dummytable.uid=42'
943 ),
944 'replaceMarkersInWhereClause replaces storage pid with integer' => array(
945 ' AND dummytable.uid=###STORAGE_PID###',
946 array(
947 '_STORAGE_PID' => 42
948 ),
949 ' AND dummytable.uid=42'
950 ),
951 'replaceMarkersInWhereClause replaces storage pid with string' => array(
952 ' AND dummytable.uid=###STORAGE_PID###',
953 array(
954 '_STORAGE_PID' => '42string'
955 ),
956 ' AND dummytable.uid=42'
957 ),
958 'replaceMarkersInWhereClause replaces siteroot uid with integer' => array(
959 ' AND dummytable.uid=###SITEROOT###',
960 array(
961 '_SITEROOT' => 42
962 ),
963 ' AND dummytable.uid=42'
964 ),
965 'replaceMarkersInWhereClause replaces siteroot uid with string' => array(
966 ' AND dummytable.uid=###SITEROOT###',
967 array(
968 '_SITEROOT' => '42string'
969 ),
970 ' AND dummytable.uid=42'
971 ),
972 'replaceMarkersInWhereClause replaces page tsconfig id with integer' => array(
973 ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
974 array(
975 'dummyfield' => array(
976 'PAGE_TSCONFIG_ID' => 42
977 )
978 ),
979 ' AND dummytable.uid=42'
980 ),
981 'replaceMarkersInWhereClause replaces page tsconfig id with string' => array(
982 ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
983 array(
984 'dummyfield' => array(
985 'PAGE_TSCONFIG_ID' => '42string'
986 )
987 ),
988 ' AND dummytable.uid=42'
989 ),
990 'replaceMarkersInWhereClause replaces page tsconfig id list' => array(
991 ' AND dummytable.uid IN (###PAGE_TSCONFIG_IDLIST###)',
992 array(
993 'dummyfield' => array(
994 'PAGE_TSCONFIG_IDLIST' => '1,a,2,b,3,c'
995 )
996 ),
997 ' AND dummytable.uid IN (1,0,2,0,3,0)'
998 ),
999 'replaceMarkersInWhereClause replaces page tsconfig id list with empty string' => array(
1000 ' AND dummytable.uid IN (###PAGE_TSCONFIG_IDLIST###)',
1001 array(
1002 'dummyfield' => array(
1003 'PAGE_TSCONFIG_IDLIST' => ''
1004 )
1005 ),
1006 ' AND dummytable.uid IN (0)'
1007 ),
1008 'replaceMarkersInWhereClause replaces page tsconfig string' => array(
1009 ' AND dummytable.title=\'###PAGE_TSCONFIG_STR###\'',
1010 array(
1011 'dummyfield' => array(
1012 'PAGE_TSCONFIG_STR' => '42'
1013 )
1014 ),
1015 ' AND dummytable.title=\'42\''
1016 ),
1017 'replaceMarkersInWhereClause replaces all markers' => array(
1018 ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'' .
1019 ' AND dummytable.uid=###REC_FIELD_uid###' .
1020 ' AND dummytable.pid=###CURRENT_PID###' .
1021 ' AND dummytable.l18n_parent=###THIS_UID###' .
1022 ' AND dummytable.cid=###THIS_CID###' .
1023 ' AND dummytable.storage_pid=###STORAGE_PID###' .
1024 ' AND dummytable.siteroot=###SITEROOT###' .
1025 ' AND dummytable.config_uid=###PAGE_TSCONFIG_ID###' .
1026 ' AND dummytable.idlist IN (###PAGE_TSCONFIG_IDLIST###)' .
1027 ' AND dummytable.string=\'###PAGE_TSCONFIG_STR###\'',
1028 array(
1029 '_THIS_ROW' => array(
1030 'dummyfield' => 'Hello World',
1031 'uid' => 42
1032 ),
1033 '_CURRENT_PID' => '1',
1034 '_THIS_UID' => 2,
1035 '_THIS_CID' => 3,
1036 '_STORAGE_PID' => 4,
1037 '_SITEROOT' => 5,
1038 'dummyfield' => array(
1039 'PAGE_TSCONFIG_ID' => 6,
1040 'PAGE_TSCONFIG_IDLIST' => '1,2,3',
1041 'PAGE_TSCONFIG_STR' => 'string'
1042 )
1043 ),
1044 ' AND dummytable.title=\'Hello World\' AND dummytable.uid=\'42\' AND dummytable.pid=1' .
1045 ' AND dummytable.l18n_parent=2 AND dummytable.cid=3 AND dummytable.storage_pid=4' .
1046 ' AND dummytable.siteroot=5 AND dummytable.config_uid=6 AND dummytable.idlist IN (1,2,3)' .
1047 ' AND dummytable.string=\'string\'',
1048 ),
1049 );
1050 }
1051
1052 /**
1053 * @test
1054 * @dataProvider replaceMarkersInWhereClauseDataProvider
1055 */
1056 public function replaceMarkersInWhereClauseReturnsValidWhereClause($whereClause, $tsConfig, $expected) {
1057 $this->assertSame($expected, BackendUtility::replaceMarkersInWhereClause($whereClause, 'dummytable', 'dummyfield', $tsConfig));
1058 }
1059
1060 /**
1061 * @test
1062 */
1063 public function getModTSconfigIgnoresValuesFromUserTsConfigIfNoSet() {
1064 $completeConfiguration = array(
1065 'value' => 'bar',
1066 'properties' => array(
1067 'permissions.' => array(
1068 'file.' => array(
1069 'default.' => array('readAction' => '1'),
1070 '1.' => array('writeAction' => '1'),
1071 '0.' => array('readAction' => '0'),
1072 ),
1073 )
1074 )
1075 );
1076
1077 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Backend\Utility\BackendUtility $fixture */
1078 $fixture = $this->getMock('TYPO3\\CMS\\Backend\\Utility\\BackendUtility', array('getPagesTSconfig'));
1079
1080 $GLOBALS['BE_USER'] = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
1081 $GLOBALS['BE_USER']->expects($this->at(0))->method('getTSConfig')->will($this->returnValue($completeConfiguration));
1082 $GLOBALS['BE_USER']->expects($this->at(1))->method('getTSConfig')->will($this->returnValue(array('value' => NULL, 'properties' => NULL)));
1083
1084 $this->assertSame($completeConfiguration, $fixture->getModTSconfig(42, 'notrelevant'));
1085 }
1086
1087
1088 }