Skip to content

Commit 7e112ef

Browse files
Fix form export with entity question special default value
1 parent 6d0020e commit 7e112ef

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Glpi/Form/QuestionType/QuestionTypeItem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public function exportDynamicExtraData(
675675
// Stop here if value is invalid or empty
676676
$itemtype = $extra_data_config[QuestionTypeItemExtraDataConfig::ITEMTYPE] ?? "";
677677
$root_id = $extra_data_config[QuestionTypeItemDropdownExtraDataConfig::ROOT_ITEMS_ID] ?? 0;
678-
if ($root_id == 0 || !is_a($itemtype, CommonDBTM::class, true)) {
678+
if ($root_id <= 0 || !is_a($itemtype, CommonDBTM::class, true)) {
679679
return $fallback;
680680
}
681681

@@ -708,6 +708,9 @@ public static function prepareDynamicExtraDataForImport(
708708
if (
709709
!(getItemForItemtype($itemtype) instanceof CommonDBTM)
710710
|| empty($name)
711+
// Both these values represent the root entity, no need to map it
712+
|| $name == 0
713+
|| $name == -1
711714
) {
712715
return $fallback;
713716
}

tests/functional/Glpi/Form/Export/FormSerializerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
use Ramsey\Uuid\Uuid;
9191
use Session;
9292

93+
use function Safe\json_encode;
94+
9395
final class FormSerializerTest extends \DbTestCase
9496
{
9597
use FormTesterTrait;
@@ -1682,6 +1684,25 @@ public function testTagsAreUpdatedWhenImporting(): void
16821684
);
16831685
}
16841686

1687+
public function testWithFormWithSpecialNegativeIdForRootEntity(): void
1688+
{
1689+
// Arrange: create a form with a -1 value for root_items_id
1690+
$builder = new FormBuilder("My form");
1691+
$extra_data = new QuestionTypeItemExtraDataConfig(
1692+
itemtype: Entity::class,
1693+
root_items_id: -1,
1694+
);
1695+
$builder->addQuestion(
1696+
name: "Entity",
1697+
type: QuestionTypeItem::class,
1698+
extra_data: json_encode($extra_data)
1699+
);
1700+
$form = $this->createForm($builder);
1701+
1702+
// Act: export the form, no error should happen
1703+
$this->exportAndImportForm($form);
1704+
}
1705+
16851706
private function compareValuesForRelations(
16861707
$relations,
16871708
$ids_for_default_form,

0 commit comments

Comments
 (0)