diff --git a/src/Plugin/facets/processor/ShowOnlyDeepestLevelItemsProcessor.php b/src/Plugin/facets/processor/ShowOnlyDeepestLevelItemsProcessor.php new file mode 100644 index 0000000..1b51e28 --- /dev/null +++ b/src/Plugin/facets/processor/ShowOnlyDeepestLevelItemsProcessor.php @@ -0,0 +1,36 @@ + $result) { + if (!empty($result->getChildren())) { + unset($results[$id]); + } + } + return $results; + } + +} diff --git a/tests/src/Unit/Plugin/processor/ShowOnlyDeepestLevelItemsProcessorTest.php b/tests/src/Unit/Plugin/processor/ShowOnlyDeepestLevelItemsProcessorTest.php new file mode 100644 index 0000000..b9a9b75 --- /dev/null +++ b/tests/src/Unit/Plugin/processor/ShowOnlyDeepestLevelItemsProcessorTest.php @@ -0,0 +1,60 @@ +processor = new ShowOnlyDeepestLevelItemsProcessor([], 'test', []); + } + + /** + * Tests that only items without children survive. + * + * @covers ::build + */ + public function testRemoveItemsWithoutChildren() { + // Setup results. + $results = [ + new Result('a', 'A', 5), + new Result('b', 'B', 2), + new Result('c', 'C', 4), + ]; + $child = new Result('a_1', 'A 1', 3); + $results[0]->setChildren([$child]); + + $facet = new Facet(['id' => 'llama'], 'facets_facet'); + + // Execute the build method, so we can test the behavior. + $built_results = $this->processor->build($facet, $results); + + // Sort to have a 0-indexed array. + sort($built_results); + + // Check the output. + $this->assertCount(2, $built_results); + $this->assertSame('b', $built_results[0]->getRawValue()); + $this->assertSame('c', $built_results[1]->getRawValue()); + } + +}