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