From a6864633c376863fbeb0dbc710d25b88934844c4 Mon Sep 17 00:00:00 2001 From: Rom1-B <8530352+Rom1-B@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:36:53 +0100 Subject: [PATCH] Fix: keep tags reachable when their entity is deleted --- CHANGELOG.md | 1 + hook.php | 14 ++++++++++++++ tests/Units/TagTest.php | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8db0c15..0eb5091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix tags being removed when saving them on a closed ticket - Fix tag search criterion matching items of other types sharing the same ID - Fix error when updating a ticket without creation date (e.g. entity transfer) +- Fix tags left in a deleted entity: declare their entity relation so it is checked and replaced on entity deletion ## [2.14.8] - 2026-09-30 diff --git a/hook.php b/hook.php index 26255ad..eb446b8 100644 --- a/hook.php +++ b/hook.php @@ -251,6 +251,20 @@ function plugin_tag_getDropdown() return ['PluginTagTag' => PluginTagTag::getTypeName(2)]; } +/** + * Declare foreign keys to core tables, so that core usage checks and replacements take them into account + * + * @return array + */ +function plugin_tag_getDatabaseRelations() +{ + return [ + 'glpi_entities' => [ + 'glpi_plugin_tag_tags' => 'entities_id', + ], + ]; +} + /** * Define massive actions for other itemtype * diff --git a/tests/Units/TagTest.php b/tests/Units/TagTest.php index 7e4c07e..97059a8 100644 --- a/tests/Units/TagTest.php +++ b/tests/Units/TagTest.php @@ -33,6 +33,7 @@ use CommonITILObject; use Computer; use DBmysql; +use Entity; use GlpiPlugin\Tag\Tests\TagTestCase; use Phone; use PHPUnit\Framework\Attributes\DataProvider; @@ -235,4 +236,25 @@ public function testSearchOnTagIgnoresOtherItemtypes(string $searchtype, bool $b $names = array_column(array_column($data['data']['rows'], 'raw'), 'ITEM_Computer_1'); $this->assertSame(array_map(fn($name) => sprintf('%s %s', $prefix, $name), $expected), $names); } + + public function testTagIsMovedWhenItsEntityIsReplaced(): void + { + $this->login(); + $root_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + + $entity = $this->createItem(Entity::class, [ + 'name' => __FUNCTION__, + 'entities_id' => $root_id, + ]); + $tag = $this->createItem(PluginTagTag::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($tag->getFromDB($tag->getID())); + $this->assertSame($root_id, $tag->fields['entities_id']); + } }