diff --git a/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php b/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php index 83612e0..54c476b 100644 --- a/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php +++ b/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php @@ -102,7 +102,7 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac $build['#attached']['drupalSettings']['facets_views_ajax'] = [ 'facets_summary_ajax' => [ - 'facets_summary_id' => $build['#attributes']['data-drupal-facets-summary-id'], + 'facets_summary_id' => $facets_summary->id(), 'view_id' => $view->id(), 'current_display_id' => $view->current_display, 'ajax_path' => Url::fromRoute('views.ajax')->toString(), diff --git a/tests/src/FunctionalJavascript/AjaxBehaviorTest.php b/tests/src/FunctionalJavascript/AjaxBehaviorTest.php index a4c8d71..1fa81f9 100644 --- a/tests/src/FunctionalJavascript/AjaxBehaviorTest.php +++ b/tests/src/FunctionalJavascript/AjaxBehaviorTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\facets\FunctionalJavascript; +use Drupal\views\Entity\View; + /** * Tests for the JS that powers ajax. * @@ -20,55 +22,52 @@ class AjaxBehaviorTest extends JsBase { * Tests ajax links. */ public function testAjaxLinks() { + // Create facets. $this->createFacet('owl'); $this->createFacet('duck', 'keywords'); + // Force ajax. + $view = View::load('search_api_test_view'); + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */ + $display = $view->getDisplay('page_1'); + $display->setOption('use_ajax', TRUE); + $view->save(); + // Go to the views page. $this->drupalGet('search-api-test-fulltext'); - // Make sure the block is shown on the page. + // Make sure the blocks are shown on the page. $page = $this->getSession()->getPage(); $block_owl = $page->findById('block-owl-block'); $block_owl->isVisible(); $block_duck = $page->findById('block-duck-block'); $block_duck->isVisible(); - } - /** - * Create a facet. - * - * @param string $id - * The id of the facet. - * @param string $field - * The field name. - */ - protected function createFacet($id, $field = 'type') { - $facet_storage = \Drupal::entityTypeManager()->getStorage('facets_facet'); - // Create and save a facet with a checkbox widget. - $facet_storage->create([ - 'id' => $id, - 'name' => strtoupper($id), - 'url_alias' => $id, - 'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1', - 'field_identifier' => $field, - 'empty_behavior' => ['behavior' => 'none'], - 'weight' => 1, - 'widget' => [ - 'type' => 'links', - 'config' => [ - 'show_numbers' => TRUE, - 'soft_limit' => 0, - ], - ], - 'processor_configs' => [ - 'url_processor_handler' => [ - 'processor_id' => 'url_processor_handler', - 'weights' => ['pre_query' => -10, 'build' => -10], - 'settings' => [], - ], - ], - ])->save(); - $this->createBlock($id); + // Check that the article link exists (and is formatted like a facet) link. + $links = $this->xpath('//a//span[normalize-space(text())=:label]', [':label' => 'article']); + $this->assertEmpty($links); + + // Click the item facet. + $this->clickLink('item'); + $this->assertSession()->assertWaitOnAjaxRequest(); + + // Check that the article facet is now gone. + $links = $this->xpath('//a//span[normalize-space(text())=:label]', [':label' => 'article']); + $this->assertEmpty($links); + + // Click the item facet again, and check that the article facet is back. + $this->clickLink('item'); + $this->assertSession()->assertWaitOnAjaxRequest(); + $links = $this->xpath('//a//span[normalize-space(text())=:label]', [':label' => 'article']); + $this->assertNotEmpty($links); + + // Check that the strawberry link disappears when filtering on items. + $links = $this->xpath('//a//span[normalize-space(text())=:label]', [':label' => 'strawberry']); + $this->assertNotEmpty($links); + $this->clickLink('item'); + $this->assertSession()->assertWaitOnAjaxRequest(); + $links = $this->xpath('//a//span[normalize-space(text())=:label]', [':label' => 'strawberry']); + $this->assertEmpty($links); } } diff --git a/tests/src/FunctionalJavascript/JsBase.php b/tests/src/FunctionalJavascript/JsBase.php index 28b9448..8a6f5b8 100644 --- a/tests/src/FunctionalJavascript/JsBase.php +++ b/tests/src/FunctionalJavascript/JsBase.php @@ -127,4 +127,41 @@ abstract class JsBase extends JavascriptTestBase { $block->save(); } + /** + * Create a facet. + * + * @param string $id + * The id of the facet. + * @param string $field + * The field name. + */ + protected function createFacet($id, $field = 'type') { + $facet_storage = \Drupal::entityTypeManager()->getStorage('facets_facet'); + // Create and save a facet with a checkbox widget. + $facet_storage->create([ + 'id' => $id, + 'name' => strtoupper($id), + 'url_alias' => $id, + 'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1', + 'field_identifier' => $field, + 'empty_behavior' => ['behavior' => 'none'], + 'weight' => 1, + 'widget' => [ + 'type' => 'links', + 'config' => [ + 'show_numbers' => TRUE, + 'soft_limit' => 0, + ], + ], + 'processor_configs' => [ + 'url_processor_handler' => [ + 'processor_id' => 'url_processor_handler', + 'weights' => ['pre_query' => -10, 'build' => -10], + 'settings' => [], + ], + ], + ])->save(); + $this->createBlock($id); + } + }