Skip to content

Commit

Permalink
[BUGFIX] Make getHasChildNodeSelected recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
twojtylak committed Sep 19, 2024
1 parent fe7c9eb commit 9191a7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getHasChildNodeSelected(): bool
{
/** @var Node $childNode */
foreach ($this->childNodes as $childNode) {
if ($childNode->getSelected()) {
if ($childNode->getSelected() || $childNode->getHasChildNodeSelected()) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy\HierarchyFacet;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy\Node;
use ApacheSolrForTypo3\Solr\Tests\Unit\SetUpUnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;

/**
Expand All @@ -40,35 +41,41 @@ public function canGetHasParentNode(): void
}

#[Test]
public function canGetHasChildNodeSelectedReturnFalseWhenNoChildNodeWasAssigned(): void
{
$facetMock = $this->createMock(HierarchyFacet::class);
$node = new Node($facetMock);
#[DataProvider('provideGetHasChildNodeSelectedDataSet')]
public function canHandleGetHasChildNodeSelected(
bool $expectedResult,
array $childNodes
): void {
$node = $this->convertDataToNode(['children' => $childNodes]);

self::assertFalse($node->getHasChildNodeSelected(), 'Node without childnodes should not indicate that it as a selected child node');
self::assertSame($expectedResult, $node->getHasChildNodeSelected());
}

#[Test]
public function canGetHasChildNodeSelectedReturnFalseWhenNoSelectedChildNodeWasAssigned(): void
private function convertDataToNode(array $data): Node
{
$facetMock = $this->createMock(HierarchyFacet::class);
$node = new Node($facetMock);
$node = new Node(
$this->createMock(HierarchyFacet::class),
null,
'',
'',
'',
0,
$data['selected'] ?? false
);

$childNode = new Node($facetMock, $node);
$node->addChildNode($childNode);
foreach (\array_map([$this, 'convertDataToNode'], $data['children'] ?? []) as $childNode) {
$node->addChildNode($childNode);
}

self::assertFalse($node->getHasChildNodeSelected(), 'Node with only unselected childnodes should not indicate that it has a selected child node');
return $node;
}

#[Test]
public function canGetHasChildNodeSelectedReturnTrueWhenSelectedChildNodeWasAssigned(): void
public static function provideGetHasChildNodeSelectedDataSet(): iterable
{
$facetMock = $this->createMock(HierarchyFacet::class);
$node = new Node($facetMock);

$selectedChildNode = new Node($facetMock, $node, '', '', '', 0, true);
$node->addChildNode($selectedChildNode);

self::assertTrue($node->getHasChildNodeSelected(), 'Node with selected child node should indicate that it has a selected child node');
yield 'No child nodes' => [false, []];
yield 'One direct child node: selected' => [true, [['selected' => true]]];
yield 'One direct child node: not selected' => [false, [['selected' => false]]];
yield 'Child node 1 level down: selected' => [true, [['selected' => false, 'children' => [['selected' => true]]]]];
yield 'Child node 2 levels down: selected' => [true, [['selected' => false, 'children' => [['selected' => false, 'children' => [['selected' => true]]]]]]];
}
}

0 comments on commit 9191a7b

Please sign in to comment.