Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions phpmyfaq/src/phpMyFAQ/Category/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,33 @@ public function setCategoryTree(
* Returns the category tree.
*
* @param stdClass[] $categories
* @param int $parentId
* @param array $visited Track visited categories to prevent infinite recursion
*/
public function getCategoryTree(array $categories, int $parentId = 0): array
public function getCategoryTree(array $categories, int $parentId = 0, array &$visited = []): array
{
$result = [];

foreach ($categories as $category) {
if ((int)$category['parent_id'] === $parentId) {
$childCategories = $this->getCategoryTree($categories, (int)$category['category_id']);
$result[$category['category_id']] = $childCategories;
$categoryId = (int)$category['category_id'];
$categoryParentId = (int)$category['parent_id'];

// Skip if category is its own parent or creates a cycle
if ($categoryId === $categoryParentId) {
continue;
}

if ($categoryParentId === $parentId) {
// Check if this category has already been visited to prevent cycles
if (isset($visited[$categoryId])) {
continue;
}

// Add current category to visited list
$visited[$categoryId] = true;

$childCategories = $this->getCategoryTree($categories, $categoryId, $visited);
$result[$categoryId] = $childCategories;
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/phpMyFAQ/Category/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,78 @@ public function testGetParentId(): void

$this->assertEquals($expected, $actual);
}

/**
* Test that getCategoryTree handles self-referencing categories without infinite recursion
*/
public function testGetCategoryTreeWithSelfReference(): void
{
// Simulate a category that references itself as parent
$categories = [
[
'category_id' => 1,
'parent_id' => 1, // Self-reference
'position' => 1,
],
[
'category_id' => 2,
'parent_id' => 0,
'position' => 2,
],
];

$result = $this->categoryOrder->getCategoryTree($categories);

// Category 1 should be skipped due to self-reference
// Only category 2 should be in the result
$expected = [
2 => [],
];

$this->assertEquals($expected, $result);
}

/**
* Test that getCategoryTree handles circular references without infinite recursion
*/
public function testGetCategoryTreeWithCircularReference(): void
{
// Simulate circular reference: 1 -> 2 -> 3 -> 2 (cycle)
$categories = [
[
'category_id' => 1,
'parent_id' => 0,
'position' => 1,
],
[
'category_id' => 2,
'parent_id' => 1,
'position' => 2,
],
[
'category_id' => 3,
'parent_id' => 2,
'position' => 3,
],
[
'category_id' => 2, // Duplicate entry creating circular reference
'parent_id' => 3,
'position' => 4,
],
];

$result = $this->categoryOrder->getCategoryTree($categories);

// Should handle circular reference gracefully
// Category 2 should only be visited once
$expected = [
1 => [
2 => [
3 => [],
],
],
];

$this->assertEquals($expected, $result);
}
}