diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb592f..b2359e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - CI: fix Psalm cache directory, declare a unique composer autoloader suffix - 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 8db6189..4a7b0ff 100644 --- a/hook.php +++ b/hook.php @@ -250,6 +250,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 d23d0a3..617d276 100644 --- a/tests/Units/TagTest.php +++ b/tests/Units/TagTest.php @@ -31,6 +31,7 @@ namespace GlpiPlugin\Tag\Tests\Units; use DBmysql; +use Entity; use Glpi\Search\SearchOption; use GlpiPlugin\Tag\Tests\TagTestCase; use PluginTagTag; @@ -161,4 +162,25 @@ public function testUpdateAcceptsScalarTypeMenu(): void $this->assertSame('["Ticket"]', $tag->fields['type_menu']); } + + 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']); + } }