2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
;
5 * This file is part of the TYPO3 CMS project.
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use Prophecy\Argument
;
18 use Prophecy\Prophecy\ObjectProphecy
;
19 use TYPO3\CMS\Backend\Form\FormDataGroup\FlexFormSegment
;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexProcess
;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication
;
22 use TYPO3\CMS\Core\Tests\UnitTestCase
;
23 use TYPO3\CMS\Core\Utility\GeneralUtility
;
24 use TYPO3\CMS\Lang\LanguageService
;
29 class TcaFlexProcessTest
extends UnitTestCase
37 * @var BackendUserAuthentication|ObjectProphecy
39 protected $backendUserProphecy;
41 protected function setUp()
43 /** @var BackendUserAuthentication|ObjectProphecy backendUserProphecy */
44 $this->backendUserProphecy
= $this->prophesize(BackendUserAuthentication
::class);
45 $GLOBALS['BE_USER'] = $this->backendUserProphecy
->reveal();
46 $GLOBALS['BE_USER']->groupData
['non_exclude_fields'] = '';
48 // Some tests call FormDataCompiler for sub elements. Those tests have functional test characteristics.
49 // This is ok for the time being, but this settings takes care only parts of the compiler are called
50 // to have less dependencies.
51 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [];
53 $this->subject
= new TcaFlexProcess();
59 public function addDataRemovesSheetIfDisabledByPageTsConfig()
62 'tableName' => 'aTable',
67 'pointerField' => 'aFlex',
74 'ds_pointerField' => 'pointerField',
82 'label' => 'aFlexFieldLabel',
112 $expected['processedTca']['columns']['aField']['config']['ds'] = [
115 'dataStructurePointers' => [
116 'pointerField' => 'aFlex',
121 $this->assertEquals($expected, $this->subject
->addData($input));
127 public function addDataSetsSheetTitleFromPageTsConfig()
130 'tableName' => 'aTable',
135 'pointerField' => 'aFlex',
142 'ds_pointerField' => 'pointerField',
150 'label' => 'aFlexFieldLabel',
170 'sheetTitle' => 'aTitle',
180 $expected['processedTca']['columns']['aField']['config']['ds'] = [
187 'label' => 'aFlexFieldLabel',
193 'sheetTitle' => 'aTitle',
198 'dataStructurePointers' => [
199 'pointerField' => 'aFlex',
204 $this->assertEquals($expected, $this->subject
->addData($input));
210 public function addDataSetsSheetDescriptionFromPageTsConfig()
213 'tableName' => 'aTable',
218 'pointerField' => 'aFlex',
225 'ds_pointerField' => 'pointerField',
233 'label' => 'aFlexFieldLabel',
253 'sheetDescription' => 'aDescription',
263 $expected['processedTca']['columns']['aField']['config']['ds'] = [
270 'label' => 'aFlexFieldLabel',
276 'sheetDescription' => 'aDescription',
281 'dataStructurePointers' => [
282 'pointerField' => 'aFlex',
287 $this->assertEquals($expected, $this->subject
->addData($input));
293 public function addDataSetsSheetShortDescriptionFromPageTsConfig()
296 'tableName' => 'aTable',
301 'pointerField' => 'aFlex',
308 'ds_pointerField' => 'pointerField',
316 'label' => 'aFlexFieldLabel',
336 'sheetDescription' => 'sheetShortDescr',
346 $expected['processedTca']['columns']['aField']['config']['ds'] = [
353 'label' => 'aFlexFieldLabel',
359 'sheetDescription' => 'sheetShortDescr',
364 'dataStructurePointers' => [
365 'pointerField' => 'aFlex',
370 $this->assertEquals($expected, $this->subject
->addData($input));
376 public function addDataSetsSheetShortDescriptionForSingleSheetFromPageTsConfig()
379 'tableName' => 'aTable',
384 'pointerField' => 'aFlex',
391 'ds_pointerField' => 'pointerField',
399 'label' => 'aFlexFieldLabel',
419 'sheetDescription' => 'sheetShortDescr',
429 $expected['processedTca']['columns']['aField']['config']['ds'] = [
436 'label' => 'aFlexFieldLabel',
442 'sheetDescription' => 'sheetShortDescr',
447 'dataStructurePointers' => [
448 'pointerField' => 'aFlex',
453 $this->assertEquals($expected, $this->subject
->addData($input));
459 public function addDataRemovesExcludeFieldFromDataStructure()
462 'tableName' => 'aTable',
467 'pointerField' => 'aFlex',
474 'ds_pointerField' => 'pointerField',
482 'label' => 'aFlexFieldLabel',
497 'pageTsConfig' => [],
500 $this->backendUserProphecy
->isAdmin()->shouldBeCalled()->willReturn(false);
501 $GLOBALS['BE_USER']->groupData
['non_exclude_fields'] = '';
504 $expected['processedTca']['columns']['aField']['config']['ds'] = [
514 'dataStructurePointers' => [
515 'pointerField' => 'aFlex',
520 $this->assertEquals($expected, $this->subject
->addData($input));
526 public function addDataKeepsExcludeFieldInDataStructureWithUserAccess()
529 'tableName' => 'aTable',
534 'pointerField' => 'aFlex',
541 'ds_pointerField' => 'pointerField',
549 'label' => 'aFlexFieldLabel',
564 'pageTsConfig' => [],
567 $this->backendUserProphecy
->isAdmin()->shouldBeCalled()->willReturn(false);
568 $GLOBALS['BE_USER']->groupData
['non_exclude_fields'] = 'aTable:aField;aFlex;sDEF;aFlexField';
571 $expected['processedTca']['columns']['aField']['config']['ds'] = [
578 'label' => 'aFlexFieldLabel',
589 'dataStructurePointers' => [
590 'pointerField' => 'aFlex',
595 $this->assertEquals($expected, $this->subject
->addData($input));
601 public function addDataKeepsExcludeFieldInDataStructureForAdminUser()
604 'tableName' => 'aTable',
609 'pointerField' => 'aFlex',
616 'ds_pointerField' => 'pointerField',
624 'label' => 'aFlexFieldLabel',
639 'pageTsConfig' => [],
642 $this->backendUserProphecy
->isAdmin()->shouldBeCalled()->willReturn(true);
643 $GLOBALS['BE_USER']->groupData
['non_exclude_fields'] = '';
646 $expected['processedTca']['columns']['aField']['config']['ds'] = [
653 'label' => 'aFlexFieldLabel',
664 'dataStructurePointers' => [
665 'pointerField' => 'aFlex',
670 $this->assertEquals($expected, $this->subject
->addData($input));
676 public function addDataRemovesPageTsDisabledFieldFromDataStructure()
679 'tableName' => 'aTable',
684 'pointerField' => 'aFlex',
691 'ds_pointerField' => 'pointerField',
699 'label' => 'aFlexFieldLabel',
731 $expected['processedTca']['columns']['aField']['config']['ds'] = [
741 'dataStructurePointers' => [
742 'pointerField' => 'aFlex',
747 $this->assertEquals($expected, $this->subject
->addData($input));
753 public function addDataHandlesPageTsConfigSettingsOfSingleFlexField()
756 'tableName' => 'aTable',
761 'pointerField' => 'aFlex',
768 'ds_pointerField' => 'pointerField',
776 'label' => 'aFlexFieldLabel',
804 '0' => 'labelOverride',
815 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [
816 \TYPO3\CMS\Backend\Form\FormDataProvider\TcaRadioItems
::class => [],
819 /** @var LanguageService|ObjectProphecy $languageService */
820 $languageService = $this->prophesize(LanguageService
::class);
821 $GLOBALS['LANG'] = $languageService->reveal();
822 $languageService->sL(Argument
::cetera())->willReturnArgument(0);
824 $this->backendUserProphecy
->isAdmin()->willReturn(true);
825 $this->backendUserProphecy
->checkLanguageAccess(Argument
::cetera())->willReturn(true);
828 $expected['processedTca']['columns']['aField']['config']['ds'] = [
835 'label' => 'aFlexFieldLabel',
840 0 => 'labelOverride',
851 'dataStructurePointers' => [
852 'pointerField' => 'aFlex',
857 $this->assertEquals($expected, $this->subject
->addData($input));
863 public function addDataSetsDefaultValueFromFlexTcaForField()
866 'tableName' => 'aTable',
871 'pointerField' => 'aFlex',
878 'ds_pointerField' => 'pointerField',
886 'label' => 'aFlexFieldLabel',
889 'default' => 'defaultValue',
901 'pageTsConfig' => [],
904 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [
905 \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues
::class => [],
908 /** @var LanguageService|ObjectProphecy $languageService */
909 $languageService = $this->prophesize(LanguageService
::class);
910 $GLOBALS['LANG'] = $languageService->reveal();
911 $languageService->sL(Argument
::cetera())->willReturnArgument(0);
913 $this->backendUserProphecy
->isAdmin()->willReturn(true);
914 $this->backendUserProphecy
->checkLanguageAccess(Argument
::cetera())->willReturn(true);
917 $expected['processedTca']['columns']['aField']['config']['ds']['meta'] = [
918 'dataStructurePointers' => [
919 'pointerField' => 'aFlex'
922 $expected['databaseRow']['aField']['data']['sDEF']['lDEF']['aFlexField']['vDEF'] = 'defaultValue';
924 $this->assertEquals($expected, $this->subject
->addData($input));
930 public function addDataThrowsExceptionForDataStructureTypeArrayWithoutSection()
933 'tableName' => 'aTable',
963 'pageTsConfig' => [],
966 $this->backendUserProphecy
->isAdmin()->willReturn(true);
967 $this->backendUserProphecy
->checkLanguageAccess(Argument
::cetera())->willReturn(true);
969 $this->setExpectedException(\UnexpectedValueException
::class, $this->anything(), 1440685208);
971 $this->subject
->addData($input);
977 public function addDataThrowsExceptionForDataStructureSectionWithoutTypeArray()
980 'tableName' => 'aTable',
1010 'pageTsConfig' => [],
1013 $this->backendUserProphecy
->isAdmin()->willReturn(true);
1014 $this->backendUserProphecy
->checkLanguageAccess(Argument
::cetera())->willReturn(true);
1016 $this->setExpectedException(\UnexpectedValueException
::class, $this->anything(), 1440685208);
1018 $this->subject
->addData($input);
1024 public function addDataSetsValuesAndStructureForSectionContainerElements()
1027 'tableName' => 'aTable',
1037 // It should set the default value for aFlexField here
1046 // It should keep this value
1047 'vDEF' => 'dbValue',
1060 // It should add the default value for aFlexField here
1090 'label' => 'aFlexFieldLabel',
1093 'default' => 'defaultValue',
1109 'pageTsConfig' => [],
1112 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['flexFormSegment'] = [
1113 \TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowDefaultValues
::class => [],
1116 /** @var LanguageService|ObjectProphecy $languageService */
1117 $languageService = $this->prophesize(LanguageService
::class);
1118 $GLOBALS['LANG'] = $languageService->reveal();
1119 $languageService->sL(Argument
::cetera())->willReturnArgument(0);
1121 $this->backendUserProphecy
->isAdmin()->willReturn(true);
1122 $this->backendUserProphecy
->checkLanguageAccess(Argument
::cetera())->willReturn(true);
1126 // A default value for existing container field aFlexField should have been set
1127 $expected['databaseRow']['aField']['data']['sDEF']['lDEF']['section_1']['el']['1']['container_1']['el']['aFlexField']['vDEF'] = 'defaultValue';
1129 // Dummy row values for container_1 on lDEF sheet
1130 $expected['databaseRow']['aField']['data']['sDEF']['lDEF']['section_1']['templateRows']['container_1']['el']['aFlexField']['vDEF'] = 'defaultValue';
1132 $this->assertEquals($expected, $this->subject
->addData($input));
1138 public function addDataCallsFlexFormSegmentGroupForFieldAndAddsFlexParentDatabaseRow()
1141 'tableName' => 'aTable',
1146 'pointerField' => 'aFlex',
1153 'ds_pointerField' => 'pointerField',
1161 'label' => 'aFlexFieldLabel',
1175 'pageTsConfig' => [],
1178 /** @var FlexFormSegment|ObjectProphecy $dummyGroup */
1179 $dummyGroup = $this->prophesize(FlexFormSegment
::class);
1180 GeneralUtility
::addInstance(FlexFormSegment
::class, $dummyGroup->reveal());
1182 // Check array given to flex group contains databaseRow as flexParentDatabaseRow and check compile is called
1183 $dummyGroup->compile(Argument
::that(function ($result) use ($input) {
1184 if ($result['flexParentDatabaseRow'] === $input['databaseRow']) {
1188 }))->shouldBeCalled()->willReturnArgument(0);
1190 $this->subject
->addData($input);
1196 public function addDataCallsFlexFormSegmentGroupForDummyContainerAndAddsFlexParentDatabaseRow()
1199 'tableName' => 'aTable',
1204 'pointerField' => 'aFlex',
1211 'ds_pointerField' => 'pointerField',
1219 'label' => 'aFlexFieldLabel',
1233 'pageTsConfig' => [],
1236 /** @var FlexFormSegment|ObjectProphecy $dummyGroupExisting */
1237 $dummyGroupExisting = $this->prophesize(FlexFormSegment
::class);
1238 GeneralUtility
::addInstance(FlexFormSegment
::class, $dummyGroupExisting->reveal());
1239 // Check array given to flex group contains databaseRow as flexParentDatabaseRow and check compile is called
1240 $dummyGroupExisting->compile(Argument
::that(function ($result) use ($input) {
1241 if ($result['flexParentDatabaseRow'] === $input['databaseRow']) {
1245 }))->shouldBeCalled()->willReturnArgument(0);
1247 $this->subject
->addData($input);