Skip to content

Commit ccc482f

Browse files
authored
fix handling of flat categories in browse view (#23395)
1 parent ae4874b commit ccc482f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Glpi/Features/TreeBrowse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public static function getTreeCategoryList(string $itemtype, array $params): arr
281281
}
282282
}
283283
if (($category['items_count'] > 0) || (in_array($category['id'], $parents))) {
284-
$parents[] = $category[$cat_fk];
284+
$parents[] = $category[$cat_fk] ?? 0;
285285
$categories[] = $category;
286286
}
287287
}

tests/functional/UserTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Profile_User;
4444
use Psr\Log\LogLevel;
4545
use User;
46+
use UserCategory;
4647

4748
class UserTest extends DbTestCase
4849
{
@@ -2506,4 +2507,39 @@ public function testGetFromSSOAndRightRules()
25062507
$this->assertEquals("0", $user->fields["_ldap_rules"]["rules_entities_rights"][0][0]); // entities_id
25072508
$this->assertEquals($admin_profile_id, $user->fields["_ldap_rules"]["rules_entities_rights"][0][1]); // profiles_id
25082509
}
2510+
2511+
/**
2512+
* The TreeBrowse list is available for users, but user categories are not hierarchical.
2513+
* This test ensures results can be fetched properly even for flat results.
2514+
* @return void
2515+
*/
2516+
public function testGetTreeCategoryList()
2517+
{
2518+
$this->login();
2519+
2520+
$cat1 = $this->createItem(UserCategory::class, [
2521+
'name' => __FUNCTION__ . '_1',
2522+
]);
2523+
$cat2 = $this->createItem(UserCategory::class, [
2524+
'name' => __FUNCTION__ . '_2',
2525+
]);
2526+
2527+
$this->createItem(User::class, [
2528+
'name' => __FUNCTION__ . '_cat1',
2529+
'usercategories_id' => $cat1->getID(),
2530+
]);
2531+
$this->createItem(User::class, [
2532+
'name' => __FUNCTION__ . '_cat2',
2533+
'usercategories_id' => $cat2->getID(),
2534+
]);
2535+
2536+
$tree = User::getTreeCategoryList(User::class, [
2537+
'itemtype' => User::class,
2538+
'browse' => 1,
2539+
'export_all' => true,
2540+
]);
2541+
$this->assertCount(3, $tree);
2542+
$this->assertStringStartsWith(__FUNCTION__ . '_1', $tree[1]['title']);
2543+
$this->assertStringStartsWith(__FUNCTION__ . '_2', $tree[0]['title']);
2544+
}
25092545
}

0 commit comments

Comments
 (0)