From 27d088d7d7421969d91b8775842382adfa46aa3c Mon Sep 17 00:00:00 2001 From: borisson_ Date: Sat, 9 Jun 2018 10:50:05 +0200 Subject: [PATCH] Issue #2949260 by borisson_: Show active facets even when "Hide facet with 1 result" is selected --- .../processor/HideOnlyOneItemProcessor.php | 2 +- .../Functional/ProcessorIntegrationTest.php | 10 +++++++--- .../HideOnlyOneItemProcessorTest.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Plugin/facets/processor/HideOnlyOneItemProcessor.php b/src/Plugin/facets/processor/HideOnlyOneItemProcessor.php index 7430bff9..0e18da33 100644 --- a/src/Plugin/facets/processor/HideOnlyOneItemProcessor.php +++ b/src/Plugin/facets/processor/HideOnlyOneItemProcessor.php @@ -24,7 +24,7 @@ class HideOnlyOneItemProcessor extends ProcessorPluginBase implements BuildProce * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { - if (count($results) === 1) { + if (count($results) === 1 && !$results[0]->isActive()) { return []; } diff --git a/tests/src/Functional/ProcessorIntegrationTest.php b/tests/src/Functional/ProcessorIntegrationTest.php index 91e8f61a..ec6f87e9 100644 --- a/tests/src/Functional/ProcessorIntegrationTest.php +++ b/tests/src/Functional/ProcessorIntegrationTest.php @@ -768,6 +768,12 @@ class ProcessorIntegrationTest extends FacetsTestBase { public function testHideOnlyOneItemProcessor() { $entity_test_storage = \Drupal::entityTypeManager() ->getStorage('entity_test_mulrev_changed'); + + // Load all items and delete them. + $all = $entity_test_storage->loadMultiple(); + foreach ($all as $item) { + $item->delete(); + } $entity_test_storage->create([ 'name' => 'baz baz', 'body' => 'foo test', @@ -775,7 +781,6 @@ class ProcessorIntegrationTest extends FacetsTestBase { 'keywords' => ['kiwi'], 'category' => 'article_category', ])->save(); - $this->indexItems($this->indexId); $facet_name = 'Drupalcon Vienna'; @@ -790,8 +795,7 @@ class ProcessorIntegrationTest extends FacetsTestBase { $this->drupalPostForm($this->editForm, $form, 'Save'); $this->drupalGet('search-api-test-fulltext'); - $this->assertFacetBlocksAppear(); - $this->clickLink('kiwi'); + $this->assertSession()->pageTextContains(' Displaying 1 search results'); $this->assertNoFacetBlocksAppear(); } diff --git a/tests/src/Unit/Plugin/processor/HideOnlyOneItemProcessorTest.php b/tests/src/Unit/Plugin/processor/HideOnlyOneItemProcessorTest.php index 343defb5..801d1a70 100644 --- a/tests/src/Unit/Plugin/processor/HideOnlyOneItemProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/HideOnlyOneItemProcessorTest.php @@ -33,6 +33,25 @@ class HideOnlyOneItemProcessorTest extends UnitTestCase { $this->assertCount(0, $processed_results); } + /** + * Tests with one result that is already active. + * + * @covers ::build + */ + public function testWithOneActiveResult() { + $processor = new HideOnlyOneItemProcessor([], 'hide_only_one_item', []); + $facet = new Facet([], 'facets_facet'); + $results = [ + new Result($facet, '1', 1, 1), + ]; + $results[0]->setActiveState(TRUE); + $facet = $this->getMockBuilder(Facet::class) + ->disableOriginalConstructor() + ->getMock(); + $processed_results = $processor->build($facet, $results); + $this->assertCount(1, $processed_results); + } + /** * Tests with one result. * -- GitLab