Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ 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

## [1.24.7] - 2026-10-28
>>>>>>> 1ce0087 (fix(core): Fix mandatory fields error when creating an item with an assigned technician (#1332))

### Fixed

Expand Down
73 changes: 72 additions & 1 deletion inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

public static string $rightname = 'config';

/**
* Values of the items being added, not stored yet
*/
private static ?WeakMap $pending_values = null;

public static function canCreate(): bool
{
return self::canUpdate();
Expand Down Expand Up @@ -2422,15 +2427,21 @@
*/
public static function postItemAdd(CommonDBTM $item)
{
<<<<<<< HEAD

Check failure on line 2430 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_SL on line 2430

Check failure on line 2430 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_SL on line 2430
=======
PluginFieldsDestinationField::registerCreatedItem($item);
self::$pending_values?->offsetUnset($item);

>>>>>>> 1ce0087 (fix(core): Fix mandatory fields error when creating an item with an assigned technician (#1332))
if (array_key_exists('_plugin_fields_data', $item->input)) {
$container = new self();
foreach (self::getSubmittedContainersData($item) as $data) {
$data['itemtype'] = $item::class;

Check failure on line 2439 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting '-' or T_STRING or T_VARIABLE or T_NUM_STRING on line 2439

Check failure on line 2439 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting '-' or T_STRING or T_VARIABLE or T_NUM_STRING on line 2439
$data['items_id'] = $item->getID();

Check failure on line 2440 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2440

Check failure on line 2440 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2440
$data['entities_id'] = $item->isEntityAssign() ? $item->getEntityID() : 0;

Check failure on line 2441 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2441

Check failure on line 2441 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2441
//update data
if (!$container->updateFieldsValues($data, $item::class, isset($_REQUEST['massiveaction']))) {

Check failure on line 2443 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2443

Check failure on line 2443 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2443
$item->input = [];

Check failure on line 2444 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2444

Check failure on line 2444 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2444
return $item;
}
}
Expand All @@ -2452,14 +2463,14 @@
public static function preItemUpdate(CommonDBTM $item)
{
self::preItem($item);
if (array_key_exists('_plugin_fields_data', $item->input)) {

Check failure on line 2466 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2466

Check failure on line 2466 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2466
// Only save plugin fields if the user can update this specific item.
// Automated contexts (cron jobs, API without active profile) bypass this check.
if (
isset($_SESSION['glpiactiveprofile']['id'])
&& !$item->canUpdateItem()

Check failure on line 2471 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2471

Check failure on line 2471 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2471
) {
unset($item->input['_plugin_fields_data']);

Check failure on line 2473 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2473

Check failure on line 2473 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2473
Session::addMessageAfterRedirect(
__('You do not have the right to modify the fields of this item', 'fields'),
false,
Expand All @@ -2471,7 +2482,7 @@
$container = new self();
foreach (self::getSubmittedContainersData($item) as $data) {
$data['itemtype'] = $item::class;
$data['entities_id'] = $item->isEntityAssign() ? $item->getEntityID() : 0;

Check failure on line 2485 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2485

Check failure on line 2485 in inc/container.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 2485
//update data
if (!$container->updateFieldsValues($data, $item::class, isset($_REQUEST['massiveaction']))) {
return false;
Expand Down Expand Up @@ -2589,6 +2600,16 @@
}
}

if ($item->isNewItem()) {
$values_by_container = [];
foreach ($submitted_data as $data) {
$values_by_container[(int) $data['plugin_fields_containers_id']] = $data;
}

self::$pending_values ??= new WeakMap();
self::$pending_values[$item] = $values_by_container;
}

if ($submitted_data !== []) {
$item->input['_plugin_fields_data'] = $submitted_data;

Expand All @@ -2598,6 +2619,45 @@
return false;
}

/**
* Values by container of an item being added
* or null if the item is not being added
*
* @return array<int, array<string, mixed>>|null
*/
private static function getPendingEntry(CommonDBTM $item): ?array
{
if (!self::$pending_values instanceof WeakMap || $item->isNewItem()) {
return null;
}

foreach (self::$pending_values as $added_item => $values) {
if ($added_item::class === $item::class && (int) $added_item->getID() === (int) $item->getID()) {
return $values;
}
}

return null;
}

/**
* Whether the item is being added
*/
private static function isBeingAdded(CommonDBTM $item): bool
{
return self::getPendingEntry($item) !== null;
}

/**
* Container values of an item being added, not stored yet
*
* @return array<string, mixed>
*/
private static function getPendingValues(CommonDBTM $item, int $c_id): array
{
return self::getPendingEntry($item)[$c_id] ?? [];
}

/**
* Validate a single container's mandatory fields for the given item, using
* either the values submitted in this request or, if none were submitted
Expand Down Expand Up @@ -2625,7 +2685,8 @@

//nothing submitted for this container in this request (e.g. untouched Tab)
//tab containers can't be filled before the item exists, so skip on creation
if ($item->isNewItem() && $loc_c->fields['type'] !== 'dom') {
//(including the updates triggered while the item is being added)
if (($item->isNewItem() || self::isBeingAdded($item)) && $loc_c->fields['type'] !== 'dom') {
return [];
}

Expand All @@ -2650,12 +2711,22 @@
}

if (!$item->isNewItem()) {
<<<<<<< HEAD
// merge already persisted values to avoid false positives
$classname = self::getClassname($item::class, $loc_c->fields['name']);
=======
$data['items_id'] = $item->getID();

// merge already persisted values to avoid false positives,
// or the values still to be stored when the item is being added
$classname = self::getClassname($item::getType(), $loc_c->fields['name']);
>>>>>>> 1ce0087 (fix(core): Fix mandatory fields error when creating an item with an assigned technician (#1332))
$dbu = new DbUtils();
$obj = $dbu->getItemForItemtype($classname);
if ($obj !== false && $obj->getFromDBByCrit(['items_id' => $item->getID()])) {
$data += $obj->fields;
} else {
$data += self::getPendingValues($item, $c_id);
}
}

Expand Down
140 changes: 140 additions & 0 deletions tests/Units/ContainerItemUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
use Entity;
use Notification;
use Problem;
<<<<<<< HEAD
=======
use Supplier;
use User;
>>>>>>> 1ce0087 (fix(core): Fix mandatory fields error when creating an item with an assigned technician (#1332))

require_once __DIR__ . '/../FieldTestCase.php';

Expand Down Expand Up @@ -389,6 +394,141 @@ public function testCreateIsBlockedWhenMandatoryDomFieldIsMissing(): void
$this->assertSame('filled value', $plugin_row[$field_name]);
}

public function testCreateWithAssignedTechnicianAndMandatoryDomField(): void
{
$this->login();

$container = $this->createFieldContainer([
'label' => 'Mandatory Assign Container',
'type' => 'dom',
'itemtypes' => [Ticket::class],
'is_active' => 1,
'entities_id' => 0,
'is_recursive' => 1,
]);

$field = $this->createField([
'label' => 'Mandatory Field',
'type' => 'text',
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
'mandatory' => 1,
]);
$field_name = $field->fields['name'];

$this->simulateApiBoot();

$ticket = $this->createItem(Ticket::class, [
'name' => 'Ticket with mandatory field and technician',
'content' => 'Test creation',
'entities_id' => 0,
'_actors' => [
'assign' => [
['itemtype' => User::class, 'items_id' => getItemByTypeName(User::class, 'tech', true)],
],
],
$field_name => 'filled value',
], [$field_name]);
$this->hasNoSessionMessage(ERROR);
$this->assertSame(Ticket::ASSIGNED, $ticket->fields['status']);

$plugin_row = $this->getPluginFieldValues(Ticket::class, $ticket->getID(), $container->getID());
$this->assertNotFalse($plugin_row);
$this->assertSame('filled value', $plugin_row[$field_name]);
}

public function testCreateFromFormWithAssignedTechnicianAndMandatoryDomField(): void
{
$this->login();

$container = $this->createFieldContainer([
'label' => 'Mandatory Assign Form Container',
'type' => 'dom',
'itemtypes' => [Ticket::class],
'is_active' => 1,
'entities_id' => 0,
'is_recursive' => 1,
]);

$field = $this->createField([
'label' => 'Mandatory Field',
'type' => 'text',
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
'mandatory' => 1,
]);
$field_name = $field->fields['name'];

$this->simulateApiBoot();

// the ticket form submits the dom container id
$_REQUEST['c_id'] = $container->getID();

$ticket = $this->createItem(Ticket::class, [
'name' => 'Ticket from form with mandatory field and technician',
'content' => 'Test creation',
'entities_id' => 0,
'_actors' => [
'assign' => [
['itemtype' => User::class, 'items_id' => getItemByTypeName(User::class, 'tech', true)],
],
],
$field_name => 'filled value',
], [$field_name]);
$this->hasNoSessionMessage(ERROR);
$this->assertSame(Ticket::ASSIGNED, $ticket->fields['status']);

$plugin_row = $this->getPluginFieldValues(Ticket::class, $ticket->getID(), $container->getID());
$this->assertNotFalse($plugin_row);
$this->assertSame('filled value', $plugin_row[$field_name]);
}

public function testCreateWithAssignedTechnicianAndMandatoryTabField(): void
{
$this->login();

$container = $this->createFieldContainer([
'label' => 'Mandatory Assign Tab Container',
'type' => 'tab',
'itemtypes' => [Ticket::class],
'is_active' => 1,
'entities_id' => 0,
'is_recursive' => 1,
]);

$this->createField([
'label' => 'Mandatory Tab Field',
'type' => 'text',
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
'mandatory' => 1,
]);

$this->simulateApiBoot();

// tab fields can't be filled before the item exists: the status update
// triggered by the assigned technician must not be blocked by them
$ticket = $this->createItem(Ticket::class, [
'name' => 'Ticket with mandatory tab field and technician',
'content' => 'Test creation',
'entities_id' => 0,
'_actors' => [
'assign' => [
['itemtype' => User::class, 'items_id' => getItemByTypeName(User::class, 'tech', true)],
],
],
]);
$this->hasNoSessionMessage(ERROR);
$this->assertSame(Ticket::ASSIGNED, $ticket->fields['status']);
$this->assertFalse($this->getPluginFieldValues(Ticket::class, $ticket->getID(), $container->getID()));
}

public function testCreateIsNotBlockedForInventoryCreatedItem(): void
{
$this->login();
Expand Down
Loading