[BUGFIX] FormEngine: Fix keepItems, addItems and removeItems handling
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaSelectItemsTest.php
index 9c90e41..9e4730e 100644 (file)
@@ -1153,6 +1153,124 @@ class TcaSelectItemsTest extends UnitTestCase
         $this->assertSame($expectedItems, $result['processedTca']['columns']['aField']['config']['items']);
     }
 
+    /**
+     * @test
+     */
+    public function addDataAddsItemsByAddItemsFromPageTsConfig()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => '',
+            ],
+            'tableName' => 'aTable',
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectSingle',
+                            'items' => [
+                                0 => [
+                                    0 => 'keepMe',
+                                    1 => 'keep',
+                                    null,
+                                    null,
+                                ],
+                            ],
+                            'maxitems' => 1,
+                        ],
+                    ],
+                ]
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'addItems.' => [
+                                '1' => 'addMe'
+                            ],
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var LanguageService|ObjectProphecy $languageService */
+        $languageService = $this->prophesize(LanguageService::class);
+        $GLOBALS['LANG'] = $languageService->reveal();
+        $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = [];
+        $expected['processedTca']['columns']['aField']['config']['items'][1] = [
+            0 => 'addMe',
+            1 => '1',
+            null,
+            null,
+        ];
+
+        $this->assertEquals($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataAddsItemsByAddItemsWithDuplicateValuesFromPageTsConfig()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => '',
+            ],
+            'tableName' => 'aTable',
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectSingle',
+                            'items' => [
+                                0 => [
+                                    0 => 'keepMe',
+                                    1 => 'keep',
+                                    null,
+                                    null,
+                                ],
+                            ],
+                            'maxitems' => 1,
+                        ],
+                    ],
+                ]
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'addItems.' => [
+                                'keep' => 'addMe'
+                            ],
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var LanguageService|ObjectProphecy $languageService */
+        $languageService = $this->prophesize(LanguageService::class);
+        $GLOBALS['LANG'] = $languageService->reveal();
+        $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = [];
+        $expected['processedTca']['columns']['aField']['config']['items'][1] = [
+            0 => 'addMe',
+            1 => 'keep',
+            null,
+            null,
+        ];
+
+        $this->assertEquals($expected, $this->subject->addData($input));
+    }
+
     /**
      * Data provider
      */
@@ -1647,6 +1765,141 @@ class TcaSelectItemsTest extends UnitTestCase
         $this->assertEquals($expected, $this->subject->addData($input));
     }
 
+    /**
+     * @test
+     */
+    public function addDataRemovesAllItemsByEmptyKeepItemsPageTsConfig()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => '',
+            ],
+            'tableName' => 'aTable',
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectSingle',
+                            'items' => [
+                                0 => [
+                                    0 => 'keepMe',
+                                    1 => 'keep',
+                                    null,
+                                    null,
+                                ],
+                                1 => [
+                                    0 => 'removeMe',
+                                    1 => 'remove',
+                                ],
+                            ],
+                            'maxitems' => 1,
+                        ],
+                    ],
+                ]
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'keepItems' => '',
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var LanguageService|ObjectProphecy $languageService */
+        $languageService = $this->prophesize(LanguageService::class);
+        $GLOBALS['LANG'] = $languageService->reveal();
+        $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = [];
+        $expected['processedTca']['columns']['aField']['config']['items'] = [];
+
+        $this->assertEquals($expected, $this->subject->addData($input));
+    }
+
+    /**
+     * @test
+     */
+    public function addDataEvaluatesKeepItemsBeforeAddItemsFromPageTsConfig()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => '',
+            ],
+            'tableName' => 'aTable',
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectSingle',
+                            'items' => [
+                                0 => [
+                                    0 => 'keepMe',
+                                    1 => '1',
+                                    null,
+                                    null,
+                                ],
+                                1 => [
+                                    0 => 'removeMe',
+                                    1 => 'remove',
+                                ],
+                            ],
+                            'maxitems' => 1,
+                        ],
+                    ],
+                ]
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'keepItems' => '1',
+                            'addItems.' => [
+                                '1' => 'addItem #1',
+                                '12' => 'addItem #12',
+                            ],
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var LanguageService|ObjectProphecy $languageService */
+        $languageService = $this->prophesize(LanguageService::class);
+        $GLOBALS['LANG'] = $languageService->reveal();
+        $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = [];
+        $expected['processedTca']['columns']['aField']['config']['items'] = [
+            0 => [
+                0 => 'keepMe',
+                1 => '1',
+                null,
+                null,
+            ],
+            1 => [
+                0 => 'addItem #1',
+                1 => '1',
+                null,
+                null,
+            ],
+            2 => [
+                0 => 'addItem #12',
+                1 => '12',
+                null,
+                null,
+            ],
+        ];
+
+        $this->assertEquals($expected, $this->subject->addData($input));
+    }
+
     /**
      * @test
      */
@@ -1703,6 +1956,65 @@ class TcaSelectItemsTest extends UnitTestCase
         $this->assertEquals($expected, $this->subject->addData($input));
     }
 
+    /**
+     * @test
+     */
+    public function addDataRemovesItemsAddedByAddItemsFromPageTsConfigByRemoveItemsPageTsConfig()
+    {
+        $input = [
+            'databaseRow' => [
+                'aField' => ''
+            ],
+            'tableName' => 'aTable',
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [
+                        'config' => [
+                            'type' => 'select',
+                            'renderType' => 'selectSingle',
+                            'items' => [
+                                0 => [
+                                    0 => 'keepMe',
+                                    1 => 'keep',
+                                    null,
+                                    null,
+                                ],
+                                1 => [
+                                    0 => 'removeMe',
+                                    1 => 'remove',
+                                ],
+                            ],
+                            'maxitems' => 1,
+                        ],
+                    ],
+                ]
+            ],
+            'pageTsConfig' => [
+                'TCEFORM.' => [
+                    'aTable.' => [
+                        'aField.' => [
+                            'removeItems' => 'remove,add',
+                            'addItems.' => [
+                                'add' => 'addMe'
+                            ]
+                        ],
+                    ],
+                ],
+            ],
+        ];
+
+        /** @var LanguageService|ObjectProphecy $languageService */
+        $languageService = $this->prophesize(LanguageService::class);
+        $GLOBALS['LANG'] = $languageService->reveal();
+        $languageService->sL(Argument::cetera())->willReturnArgument(0);
+
+        $expected = $input;
+        $expected['databaseRow']['aField'] = [];
+        unset($expected['processedTca']['columns']['aField']['config']['items'][1]);
+
+        $this->assertEquals($expected, $this->subject->addData($input));
+    }
+
     /**
      * @test
      */