diff --git a/CHANGELOG.md b/CHANGELOG.md index 01ef137..8db0c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - CI: fix Psalm cache directory, declare a unique composer autoloader suffix - Fix tags from rules not applied on ticket update - 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) ## [2.14.8] - 2026-09-30 diff --git a/hook.php b/hook.php index 59c3f53..26255ad 100644 --- a/hook.php +++ b/hook.php @@ -108,6 +108,8 @@ function plugin_tag_getAddSearchOptionsNew($itemtype) 'joinparams' => [ 'jointype' => 'itemtype_item', 'specific_itemtype' => $itemtype, + // Also applied by the core in the `use_subquery` WHERE clause, which ignores `specific_itemtype` + 'condition' => ['NEWTABLE.itemtype' => $itemtype], 'beforejoin' => [ 'table' => getTableForItemType($itemtype), ], diff --git a/tests/Units/TagTest.php b/tests/Units/TagTest.php index a180cc8..7e4c07e 100644 --- a/tests/Units/TagTest.php +++ b/tests/Units/TagTest.php @@ -31,9 +31,14 @@ namespace GlpiPlugin\Tag\Tests\Units; use CommonITILObject; +use Computer; use DBmysql; use GlpiPlugin\Tag\Tests\TagTestCase; +use Phone; +use PHPUnit\Framework\Attributes\DataProvider; use PluginTagTag; +use PluginTagTagItem; +use Search; use Symfony\Component\DomCrawler\Crawler; use Ticket; @@ -181,4 +186,53 @@ public function testSaveButtonOnClosedTicketTargetsTagDropdown(): void $this->assertCount(1, $select); $this->assertSame('_plugin_tag_tag_values[]', $select->attr('name')); } + + public static function searchOnTagIgnoresOtherItemtypesProvider(): iterable + { + yield 'equals' => ['equals', true, ['tagged']]; + yield 'notequals' => ['notequals', true, ['untagged']]; + yield 'contains' => ['contains', false, ['tagged']]; + } + + #[DataProvider('searchOnTagIgnoresOtherItemtypesProvider')] + public function testSearchOnTagIgnoresOtherItemtypes(string $searchtype, bool $by_id, array $expected): void + { + /** @var DBmysql $DB */ + global $DB; + + $this->loginAs(['login' => 'glpi', 'pass' => 'glpi']); + $entity = $this->getTestRootEntity(true); + $tag_id = $this->createTag('Search itemtype tag', ['Computer']); + + $prefix = uniqid('tagsearch-'); + $tagged = $this->createItem(Computer::class, ['name' => $prefix . ' tagged', 'entities_id' => $entity]); + $untagged = $this->createItem(Computer::class, ['name' => $prefix . ' untagged', 'entities_id' => $entity]); + $links = [ + [Computer::class, $tagged->getID()], + // Another itemtype sharing the untagged computer ID must not match + [Phone::class, $untagged->getID()], + ]; + foreach ($links as [$itemtype, $items_id]) { + $DB->insert(PluginTagTagItem::getTable(), [ + 'plugin_tag_tags_id' => $tag_id, + 'itemtype' => $itemtype, + 'items_id' => $items_id, + ]); + } + + $data = Search::getDatas(Computer::class, [ + 'criteria' => [ + ['field' => 1, 'searchtype' => 'contains', 'value' => $prefix], + [ + 'link' => 'AND', + 'field' => PluginTagTag::S_OPTION, + 'searchtype' => $searchtype, + 'value' => $by_id ? $tag_id : 'Search itemtype tag', + ], + ], + ], [1]); + + $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); + } }