diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b19cb7d..3e758f86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results - Fix item clone failing when a mandatory GLPI item field is in an "Insert in form" block - Fix `Some mandatory fields are empty` error displayed when creating an item with an assigned technician +- Fix item clone failing when a mandatory dropdown field has an empty default value ## [1.24.7] - 2026-10-28 diff --git a/inc/container.class.php b/inc/container.class.php index a18b68df..a99523b1 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -2899,7 +2899,9 @@ private static function populateData($c_id, CommonDBTM $item) } } elseif ($item->isNewItem()) { $default = PluginFieldsField::getDefaultValue($field); - if ($default !== null) { + // an empty dropdown default (0) is not a value, submitting it would fail a mandatory check (ex: clone) + $is_dropdown = $field['type'] === 'dropdown' || str_starts_with((string) $field['type'], 'dropdown-'); + if ($default !== null && (!$is_dropdown || (int) $default !== 0)) { $default_key = $field['type'] === 'dropdown' ? 'plugin_fields_' . $field['name'] . 'dropdowns_id' : $field['name']; diff --git a/tests/Units/ContainerItemUpdateTest.php b/tests/Units/ContainerItemUpdateTest.php index de058c03..8cdc6d01 100644 --- a/tests/Units/ContainerItemUpdateTest.php +++ b/tests/Units/ContainerItemUpdateTest.php @@ -37,8 +37,10 @@ use GlpiPlugin\Field\Tests\FieldTestTrait; use PluginFieldsContainer; use PluginFieldsContainerDisplayCondition; +use PluginFieldsDropdown; use Ticket; use Entity; +use Location; use Notification; use Problem; use Supplier; @@ -1301,4 +1303,87 @@ public function testCloneThroughMassiveActionIsNotBlockedByMandatoryGlpiItemFiel $this->assertGreaterThan(0, $clone_id, 'Clone must not be blocked by a mandatory GLPI item field.'); } + + public function testCloneThroughMassiveActionIsNotBlockedByMandatoryDropdownWithEmptyDefault(): void + { + $this->login(); + + $container = $this->createFieldContainer([ + 'label' => 'Mandatory dropdown Clone Container', + 'type' => 'dom', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Mandatory dropdown', + 'type' => 'dropdown', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + 'mandatory' => 1, + 'default_value' => '0', + ]); + $dropdown_key = 'plugin_fields_' . $field->fields['name'] . 'dropdowns_id'; + $option = $this->createItem(PluginFieldsDropdown::getClassname($field->fields['name']), ['name' => 'Option']); + + $computer = $this->createItem(Computer::class, [ + 'name' => 'Computer with mandatory dropdown', + 'entities_id' => 0, + $dropdown_key => $option->getID(), + ], [$dropdown_key]); + + // The "Clone" massive action, also used from the item form, does not submit the plugin fields + $_REQUEST['massiveaction'] = 1; + try { + $clone_id = $computer->clone(); + } finally { + unset($_REQUEST['massiveaction']); + } + + $this->assertGreaterThan(0, $clone_id, 'Clone must not be blocked by a mandatory dropdown field with an empty default value.'); + } + + public function testCloneThroughMassiveActionIsNotBlockedByMandatoryItemtypeDropdownWithEmptyDefault(): void + { + $this->login(); + + $container = $this->createFieldContainer([ + 'label' => 'Itemtype dropdown Clone', + 'type' => 'dom', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Mandatory itemtype dropdown', + 'type' => 'dropdown-' . Location::class, + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + 'mandatory' => 1, + 'default_value' => '0', + ]); + $location = $this->createItem(Location::class, ['name' => 'Location for clone ' . random_int(1, PHP_INT_MAX), 'entities_id' => 0]); + + $computer = $this->createItem(Computer::class, [ + 'name' => 'Computer with mandatory itemtype dropdown', + 'entities_id' => 0, + $field->fields['name'] => $location->getID(), + ], [$field->fields['name']]); + + // The "Clone" massive action, also used from the item form, does not submit the plugin fields + $_REQUEST['massiveaction'] = 1; + try { + $clone_id = $computer->clone(); + } finally { + unset($_REQUEST['massiveaction']); + } + + $this->assertGreaterThan(0, $clone_id, 'Clone must not be blocked by a mandatory itemtype dropdown field with an empty default value.'); + } }