Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Make getHasChildNodeSelected recursive #4156

Open
wants to merge 1 commit into
base: release-12.0.x
Choose a base branch
from
Open
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
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]]]]]]];
}
}