diff --git a/config/schema/facets.processor.schema.yml b/config/schema/facets.processor.schema.yml index 9f5b6c1..dab9282 100644 --- a/config/schema/facets.processor.schema.yml +++ b/config/schema/facets.processor.schema.yml @@ -41,6 +41,14 @@ plugin.plugin_configuration.facets_processor.active_widget_order: type: string label: sort order +plugin.plugin_configuration.facets_processor.count_widget_order: + type: mapping + label: 'Active widget order' + mapping: + sort: + type: string + label: sort order + plugin.plugin_configuration.facets_processor.count_limit: type: mapping label: 'Count limit widget' diff --git a/src/Tests/ExampleContentTrait.php b/src/Tests/ExampleContentTrait.php index 293ea2a..2d2dac7 100644 --- a/src/Tests/ExampleContentTrait.php +++ b/src/Tests/ExampleContentTrait.php @@ -7,6 +7,7 @@ namespace Drupal\facets\Tests; +use Drupal\Component\Render\FormattableMarkup; use Drupal\search_api\Entity\Index; /** @@ -121,5 +122,23 @@ trait ExampleContentTrait { $this->drupalPostForm(NULL, $form_values, $this->t('Save')); } + /** + * Asserts that a string is before another string. + * + * @param string $x + * A string. + * @param string $y + * Another string. + */ + protected function assertXBeforeY($x, $y) { + $this->assertText($x); + $this->assertText($y); + + $x_position = strpos($this->getRawContent(), $x); + $y_position = strpos($this->getRawContent(), $y); + + $message = new FormattableMarkup('Assert that %x is before %y in the HTML', ['%x' => $x, '%y' => $y]); + $this->assertTrue($x_position < $y_position, $message); + } } diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index 7e97611..67b34b8 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -132,11 +132,26 @@ class ProcessorIntegrationTest extends FacetWebTestBase { } /** + * Tests for processor integration. + */ + public function testSortingWidgets() { + $facet_name = "Huacaya alpaca"; + $facet_id = "huacaya_alpaca"; + $this->editForm = 'admin/config/search/facets/' . $facet_id . '/display'; + + $this->createFacet($facet_name, $facet_id, 'keywords'); + $this->createFacetBlock($facet_id); + + $this->checkSortByActive(); + $this->checkSortByCount(); + $this->checkSortByDisplay(); + } + + /** * Tests the count limit processor. */ private function checkCountLimitProcessor() { $this->drupalGet($this->editForm); - $this->assertNoFieldChecked('edit-facet-settings-count-limit-status'); $form = [ 'widget_configs[show_numbers]' => TRUE, @@ -253,6 +268,99 @@ class ProcessorIntegrationTest extends FacetWebTestBase { } /** + * Tests the active widget order. + */ + protected function checkSortByActive() { + $form = [ + 'facet_sorting[active_widget_order][status]' => TRUE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('grape', 'strawberry'); + $this->clickLink('strawberry'); + $this->assertXBeforeY('(-) strawberry', 'grape'); + + $form = [ + 'facet_sorting[active_widget_order][status]' => TRUE, + 'facet_sorting[active_widget_order][settings][sort]' => 'DESC', + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('grape', 'strawberry'); + $this->clickLink('strawberry'); + $this->assertXBeforeY('grape', '(-) strawberry'); + + $form = [ + 'facet_sorting[active_widget_order][status]' => FALSE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + } + + /** + * Tests the active widget order. + */ + protected function checkSortByCount() { + $form = [ + 'widget_configs[show_numbers]' => TRUE, + 'facet_sorting[count_widget_order][status]' => TRUE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('banana', 'apple'); + $this->assertXBeforeY('banana', 'strawberry'); + $this->assertXBeforeY('apple', 'orange'); + + $form = [ + 'facet_sorting[count_widget_order][status]' => TRUE, + 'facet_sorting[count_widget_order][settings][sort]' => 'DESC', + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('apple', 'banana'); + $this->assertXBeforeY('strawberry', 'banana'); + $this->assertXBeforeY('orange', 'apple'); + + $form = [ + 'widget_configs[show_numbers]' => FALSE, + 'facet_sorting[count_widget_order][status]' => FALSE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + } + + /** + * Tests the display order. + */ + public function checkSortByDisplay() { + $form = [ + 'facet_sorting[display_value_widget_order][status]' => TRUE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('grape', 'strawberry'); + $this->assertXBeforeY('apple', 'banana'); + + $form = [ + 'facet_sorting[display_value_widget_order][status]' => TRUE, + 'facet_sorting[display_value_widget_order][settings][sort]' => 'DESC', + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertXBeforeY('strawberry', 'grape'); + $this->assertXBeforeY('banana', 'apple'); + + $form = [ + 'facet_sorting[display_value_widget_order][status]' => FALSE, + ]; + $this->drupalPostForm($this->editForm, $form, $this->t('Save')); + } + + /** * Creates a facet block by id. * * @param string $id