From 01503c4977df25f0de16c361528cf010427f1bd7 Mon Sep 17 00:00:00 2001 From: Laura <35998899+Herafia@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:03:29 +0100 Subject: [PATCH] BACKPORT-CONFLICT --- CHANGELOG.md | 14 +++ inc/container.class.php | 4 +- tests/Units/ContainerItemUpdateTest.php | 135 ++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be1c398a..846bebdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +<<<<<<< HEAD ## [Unreleased] +======= +## [UNRELEASED] + +### Fixed + +- Fix form submission error when a multiple dropdown field question is left empty +- 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 +>>>>>>> 4215589 (fix clone mandatory dropdown empty default (#1357)) ### Fixed diff --git a/inc/container.class.php b/inc/container.class.php index 5925314c..2e69c951 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -2830,7 +2830,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 2b74ba03..7de509b7 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; @@ -1120,4 +1122,137 @@ public function testNotBlockedWhenMandatoryDomContainerIsHidden(): void ERROR, ); } +<<<<<<< HEAD +======= + + public function testCloneThroughMassiveActionIsNotBlockedByMandatoryGlpiItemField(): void + { + $this->login(); + + $container = $this->createFieldContainer([ + 'label' => 'Mandatory GLPI item Clone Container', + 'type' => 'dom', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Mandatory GLPI item', + 'type' => 'glpi_item', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + 'mandatory' => 1, + 'allowed_values' => [Supplier::class], + ]); + $itemtype_key = 'itemtype_' . $field->fields['name']; + $items_id_key = 'items_id_' . $field->fields['name']; + + $supplier = $this->createItem(Supplier::class, [ + 'name' => 'Supplier for clone', + 'entities_id' => 0, + ]); + $computer = $this->createItem(Computer::class, [ + 'name' => 'Computer with mandatory GLPI item', + 'entities_id' => 0, + $itemtype_key => Supplier::class, + $items_id_key => $supplier->getID(), + ], [$itemtype_key, $items_id_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 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.'); + } +>>>>>>> 4215589 (fix clone mandatory dropdown empty default (#1357)) }