From 0cac418c7bbb7dcb14665fa287e314139f314d52 Mon Sep 17 00:00:00 2001 From: Tobias Wojtylak Date: Wed, 18 Sep 2024 08:40:09 +0200 Subject: [PATCH] [BUGFIX] Make getHasChildNodeSelected recursive --- .../Facets/OptionBased/Hierarchy/Node.php | 2 +- .../Facets/OptionBased/Hierarchy/NodeTest.php | 49 +++++++++++-------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Classes/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/Node.php b/Classes/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/Node.php index 796167f746..5d07b18a78 100644 --- a/Classes/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/Node.php +++ b/Classes/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/Node.php @@ -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; } } diff --git a/Tests/Unit/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/NodeTest.php b/Tests/Unit/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/NodeTest.php index 2a57b46c87..9924113001 100644 --- a/Tests/Unit/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/NodeTest.php +++ b/Tests/Unit/Domain/Search/ResultSet/Facets/OptionBased/Hierarchy/NodeTest.php @@ -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; /** @@ -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]]]]]]]; } }