Skip to content

Commit cbdd06b

Browse files
georgringerohader
authored andcommitted
[BUGFIX] Allow zero and blank string as valid type values
Adjusts the `array_filter` logic added in #94408 to allow zero (`0`, `'0'`) and a blank string (`''`) as valid type value. Related: #94408 Resolves: #106697 Releases: main, 13.4 Change-Id: I35d4722ed7d04eaefed0b03124404d637e6de160 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/89411 Reviewed-by: Markus Sommer <markus@letsbenow.de> Tested-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: core-ci <typo3@b13.com> Reviewed-by: Sebastian Iffland <sebastian.iffland@fullhaus.de> Tested-by: Friedemann Altrock <hallo@faltrock.de> Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Tested-by: Markus Sommer <markus@letsbenow.de> Tested-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: Sebastian Iffland <sebastian.iffland@fullhaus.de> Tested-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Oliver Hader <oliver.hader@typo3.org> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Friedemann Altrock <hallo@faltrock.de>
1 parent 4ff957a commit cbdd06b

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRecordTypeValue.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public function addData(array $result)
6868
$validItems = $this->removeItemsByUserAuthMode($result, $tcaTypeField, $result['processedTca']['columns'][$tcaTypeField]['config']['items'] ?? []);
6969
$validItems = $this->removeItemsByRemoveItemsPageTsConfig($result, $tcaTypeField, $validItems);
7070
$typeList = $this->getValidTypeValues($validItems);
71-
if (empty($recordTypeValue) || ($typeList !== [] && !in_array($recordTypeValue, $typeList, false))) {
71+
// allowing type-casts for `in_array` since the database value
72+
// might be an integer value, but `items` be a string value
73+
if ($typeList !== [] && !in_array($recordTypeValue, $typeList)) {
7274
$recordTypeValue = array_shift($typeList);
7375
}
7476
} else {
@@ -153,11 +155,19 @@ protected function getDatabaseRow(string $tableName, int $uid, string $fieldName
153155
return $row ?: [];
154156
}
155157

158+
/**
159+
* @param list<array{label?: string, value?: string, icon?: string, group?: string}> $items
160+
* @return list<string>
161+
*/
156162
protected function getValidTypeValues(array $items): array
157163
{
158-
return array_filter(array_map(static function (array $item) {
159-
$type = $item['value'] ?? '';
160-
return $type !== '--div--' ? $type : '';
161-
}, $items));
164+
return array_values(
165+
array_filter(
166+
// resolve `value` key
167+
array_map(static fn(array $item): ?string => $item['value'] ?? null, $items),
168+
// filter undefined `value` keys and `--div--` items
169+
static fn(?string $value): bool => $value !== null && $value !== '--div--'
170+
)
171+
);
162172
}
163173
}

typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRecordTypeValueTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
1919

20+
use PHPUnit\Framework\Attributes\DataProvider;
2021
use PHPUnit\Framework\Attributes\Test;
2122
use PHPUnit\Framework\MockObject\MockObject;
2223
use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue;
@@ -144,7 +145,7 @@ public function addDataSetsRecordTypeValueToValueOfDatabaseField(): void
144145
{
145146
$input = [
146147
'tableName' => 'aTable',
147-
'recordTypeValue' => '3',
148+
'recordTypeValue' => '',
148149
'processedTca' => [
149150
'ctrl' => [
150151
'type' => 'aField',
@@ -214,6 +215,54 @@ public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsEmptyStr
214215
self::assertSame($expected, $this->subject->addData($input));
215216
}
216217

218+
public static function addDataSetsRecordTypeValueToDatabaseValueIfEmptyDataProvider(): \Generator
219+
{
220+
yield 'empty string' => [''];
221+
yield 'zero string' => ['0'];
222+
yield 'one string' => ['1'];
223+
}
224+
225+
#[Test]
226+
#[DataProvider('addDataSetsRecordTypeValueToDatabaseValueIfEmptyDataProvider')]
227+
public function addDataSetsRecordTypeValueToDatabaseValueIfEmpty(string $databaseValue): void
228+
{
229+
$input = [
230+
'tableName' => 'aTable',
231+
'recordTypeValue' => '',
232+
'processedTca' => [
233+
'ctrl' => [
234+
'type' => 'aField',
235+
],
236+
'types' => [
237+
'' => 'blank',
238+
'0' => 'zero',
239+
'1' => 'one',
240+
'2' => 'two',
241+
],
242+
'columns' => [
243+
'aField' => [
244+
'config' => [
245+
'items' => [
246+
['label' => 'blank', 'value' => ''],
247+
['label' => 'zero', 'value' => '0'],
248+
['label' => 'one', 'value' => '1'],
249+
['label' => 'two', 'value' => '2'],
250+
],
251+
],
252+
],
253+
],
254+
],
255+
'databaseRow' => [
256+
'aField' => is_numeric($databaseValue) ? (int)$databaseValue : $databaseValue,
257+
],
258+
];
259+
260+
$expected = $input;
261+
$expected['recordTypeValue'] = $databaseValue;
262+
263+
self::assertSame($expected, $this->subject->addData($input));
264+
}
265+
217266
#[Test]
218267
public function addDataThrowsExceptionIfValueTypesNotExistsAndNoFallbackExists(): void
219268
{

0 commit comments

Comments
 (0)