From 9bcfeb3f0bc81c7fd44341cecee941a49d012b49 Mon Sep 17 00:00:00 2001 From: Marie Bressy Date: Fri, 13 Nov 2026 15:03:30 +0100 Subject: [PATCH 1/2] fix tag search criterion matching items of other types --- CHANGELOG.md | 1 + hook.php | 2 ++ tests/Units/TagTest.php | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3b78d8..1f5483a 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 ## [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..34ecb8b 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) => "$prefix $name", $expected), $names); + } } From 2223f0bb90fd2b739ea57ad942c50e701a4fdd9a Mon Sep 17 00:00:00 2001 From: Marie Bressy Date: Fri, 13 Nov 2026 15:14:04 +0100 Subject: [PATCH 2/2] Fix missing brace in TagItemTest and apply Rector on new test --- tests/Units/TagItemTest.php | 2 ++ tests/Units/TagTest.php | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Units/TagItemTest.php b/tests/Units/TagItemTest.php index b2f81f4..a1f61ab 100644 --- a/tests/Units/TagItemTest.php +++ b/tests/Units/TagItemTest.php @@ -127,6 +127,8 @@ public function testShowForTagEncodesLinkedItemValues(): void $this->assertStringNotContainsString($raw_name, $output); $this->assertStringContainsString(htmlescape($raw_name), $output); + } + public function testTagAssociationCreatesLink(): void { $this->loginAs(self::TECH_USER); diff --git a/tests/Units/TagTest.php b/tests/Units/TagTest.php index 34ecb8b..7e4c07e 100644 --- a/tests/Units/TagTest.php +++ b/tests/Units/TagTest.php @@ -205,8 +205,8 @@ public function testSearchOnTagIgnoresOtherItemtypes(string $searchtype, bool $b $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]); + $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 @@ -233,6 +233,6 @@ public function testSearchOnTagIgnoresOtherItemtypes(string $searchtype, bool $b ], [1]); $names = array_column(array_column($data['data']['rows'], 'raw'), 'ITEM_Computer_1'); - $this->assertSame(array_map(fn($name) => "$prefix $name", $expected), $names); + $this->assertSame(array_map(fn($name) => sprintf('%s %s', $prefix, $name), $expected), $names); } }