Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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/).

## Unreleased

### Fixed

- Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results

## [1.24.7] - 2026-10-28

### Fixed
Expand Down
13 changes: 11 additions & 2 deletions inc/abstractcontainerinstance.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,23 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
$display_with = ['realname', 'firstname'];
}

return Dropdown::show($itemtype, ['displaywith' => $display_with, 'name' => $name, 'display' => false]);
return Dropdown::show($itemtype, [
'displaywith' => $display_with,
'name' => $name,
'value' => $values[$field] ?? 0,
'display' => false,
]);
} elseif (
$field_specs->fields['type'] === 'dropdown'
&& $field_specs->fields['multiple']
) {
$itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']);

return Dropdown::show($itemtype, ['name' => $name, 'display' => false]);
return Dropdown::show($itemtype, [
'name' => $name,
'value' => $values[$field] ?? 0,
'display' => false,
]);
}
}

Expand Down
141 changes: 141 additions & 0 deletions tests/Units/SearchCriteriaMultipleDropdownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Fields is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Field\Tests\Units;

use Computer;
use Glpi\Tests\DbTestCase;
use Glpi\Tests\GLPITestCase;
use GlpiPlugin\Field\Tests\FieldTestTrait;
use PluginFieldsContainer;
use PluginFieldsDropdown;
use PluginFieldsField;
use Search;
use User;

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

/**
* The value input of a search criterion on a multiple dropdown field must keep
* the searched value once the search is submitted, otherwise sorting or paging
* the results relaunches the search with an empty criterion.
*/
final class SearchCriteriaMultipleDropdownTest extends DbTestCase
{
use FieldTestTrait;

public function setUp(): void
{
GLPITestCase::setUp();
}

public function tearDown(): void
{
$this->tearDownFieldTest();
GLPITestCase::tearDown();
}

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

$container = $this->createComputerContainer();
$field = $this->createField([
'label' => 'Accessoire',
'type' => 'dropdown',
'multiple' => 1,
'default_value' => [],
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
], ['default_value']);

$dropdown_class = PluginFieldsDropdown::getClassname($field->fields['name']);
$value = $this->createItem($dropdown_class, ['name' => 'Searched value', 'entities_id' => 0]);

$html = $this->getCriterionValueInput($container, $field, $value->getID());

$this->assertStringContainsString(
sprintf('<option value="%d" selected="selected">Searched value</option>', $value->getID()),
$html,
);
}

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

$container = $this->createComputerContainer();
$field = $this->createField([
'label' => 'Users',
'type' => 'dropdown-User',
'multiple' => 1,
'default_value' => [],
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
], ['default_value']);

$user_id = getItemByTypeName(User::class, TU_USER, true);

$html = $this->getCriterionValueInput($container, $field, $user_id);

$this->assertStringContainsString(sprintf('<option value="%d" selected="selected">', $user_id), $html);
}

private function createComputerContainer(): PluginFieldsContainer
{
return $this->createFieldContainer([
'label' => 'Search ' . $this->getUniqueString(),
'type' => 'tab',
'itemtypes' => [Computer::class],
'is_active' => 1,
'entities_id' => 0,
'is_recursive' => 1,
]);
}

/**
* Render the value input of a search criterion, as done by the search query builder.
*/
private function getCriterionValueInput(PluginFieldsContainer $container, PluginFieldsField $field, int $value): string
{
$so_id = PluginFieldsField::SEARCH_OPTION_STARTING_INDEX + $field->getID();
$searchopt = Search::getOptions(Computer::class);
$this->assertArrayHasKey($so_id, $searchopt);

$container_class = PluginFieldsContainer::getClassname(Computer::class, $container->fields['name']);
$container_item = new $container_class();

return $container_item->getValueToSelect($searchopt[$so_id], 'criteria[0][value]', $value);
}
}
Loading