diff --git a/CHANGELOG.md b/CHANGELOG.md index ddbd55a5..0dd0c026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name - Fix GenericObject migration error message to name the required 3.0.x version - Fix sort on number fields: values were sorted as text (`1000`, `12345`, `1500`, `999`) +- Fix blocks and dropdown values left in a deleted entity: declare their entity relation so it is checked and replaced on entity deletion ## [1.24.7] - 2026-10-28 diff --git a/hook.php b/hook.php index 9bc9ef52..21f83dd3 100644 --- a/hook.php +++ b/hook.php @@ -209,6 +209,36 @@ function plugin_fields_getDropdown() return $dropdowns; } +/** + * Declare foreign keys to core tables, so that core usage checks and replacements take them into account. + * Values tables are not declared: their entity is a copy of the parent item one, kept in sync by the transfer hook. + * + * @return array + */ +function plugin_fields_getDatabaseRelations() +{ + /** @var DBmysql $DB */ + global $DB; + + if (!$DB->tableExists(PluginFieldsField::getTable())) { + return []; + } + + $entity_relations = [ + PluginFieldsContainer::getTable() => 'entities_id', + ]; + foreach ((new PluginFieldsField())->find(['type' => 'dropdown']) as $field) { + // Core warns on every relations lookup when a declared table has no loadable class + $dropdown_class = PluginFieldsDropdown::getClassname($field['name']); + $dropdown_table = getTableForItemType($dropdown_class); + if (class_exists($dropdown_class) && $DB->tableExists($dropdown_table)) { + $entity_relations[$dropdown_table] = 'entities_id'; + } + } + + return ['glpi_entities' => $entity_relations]; +} + /**** MASSIVE ACTIONS ****/ diff --git a/tests/Units/EntityRelationTest.php b/tests/Units/EntityRelationTest.php new file mode 100644 index 00000000..65a95ba6 --- /dev/null +++ b/tests/Units/EntityRelationTest.php @@ -0,0 +1,101 @@ +. + * ------------------------------------------------------------------------- + * @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 Entity; +use Glpi\Tests\DbTestCase; +use Glpi\Tests\GLPITestCase; +use GlpiPlugin\Field\Tests\FieldTestTrait; +use PluginFieldsContainer; +use PluginFieldsDropdown; + +require_once __DIR__ . '/../FieldTestCase.php'; + +final class EntityRelationTest extends DbTestCase +{ + use FieldTestTrait; + + public function setUp(): void + { + GLPITestCase::setUp(); + } + + public function tearDown(): void + { + $this->tearDownFieldTest(); + GLPITestCase::tearDown(); + } + + public function testContainerAndDropdownValuesAreMovedWhenTheirEntityIsReplaced(): void + { + $this->login(); + $root_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + + $entity = $this->createItem(Entity::class, [ + 'name' => __FUNCTION__ . ' ' . $this->getUniqueString(), + 'entities_id' => $root_id, + ]); + + // Short label: the values table name is derived from it and limited to 64 chars + $container = $this->createFieldContainer([ + 'label' => 'Entity relation ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => $entity->getID(), + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Entity relation dropdown', + 'type' => 'dropdown', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + ], ['default_value']); + + $dropdown_class = PluginFieldsDropdown::getClassname($field->fields['name']); + $dropdown_value = $this->createItem($dropdown_class, [ + 'name' => __FUNCTION__, + 'entities_id' => $entity->getID(), + ]); + + $this->assertTrue($entity->isUsed()); + + $this->assertTrue($entity->delete(['id' => $entity->getID(), '_replace_by' => $root_id], true)); + + $this->assertTrue($container->getFromDB($container->getID())); + $this->assertSame($root_id, $container->fields['entities_id']); + $this->assertTrue($dropdown_value->getFromDB($dropdown_value->getID())); + $this->assertSame($root_id, $dropdown_value->fields['entities_id']); + } +}