diff --git a/config/schema/facets.processor.schema.yml b/config/schema/facets.processor.schema.yml index 0e10515..37c18fa 100644 --- a/config/schema/facets.processor.schema.yml +++ b/config/schema/facets.processor.schema.yml @@ -6,14 +6,6 @@ plugin.plugin_configuration.facets_processor.count_widget_widget_order: type: string label: sort order -plugin.plugin_configuration.facets_processor.display_value_widget_order: - type: mapping - label: 'Display value widget order' - mapping: - sort: - type: string - label: sort order - plugin.plugin_configuration.facets_processor.exclude_specified_items: type: mapping label: 'Exclude specified items' @@ -25,30 +17,6 @@ plugin.plugin_configuration.facets_processor.exclude_specified_items: type: boolean label: Regex -plugin.plugin_configuration.facets_processor.raw_value_widget_order: - type: mapping - label: 'Raw value widget order' - mapping: - sort: - type: string - label: sort order - -plugin.plugin_configuration.facets_processor.active_widget_order: - type: mapping - label: 'Active widget order' - mapping: - sort: - 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' @@ -60,6 +28,16 @@ plugin.plugin_configuration.facets_processor.count_limit: type: integer label: 'Maximum amount of items to show.' +plugin.plugin_configuration.facets_processor.sort_processor_handler: + type: mapping + label: settings + settings: + type: sequence + label: 'Facet sort processor configuration.' + sequence: + type: plugin.plugin_configuration.facets_sort_processor.[%key] + label: 'Facet plugin options' + plugin.plugin_configuration.facets_processor.url_processor_handler: type: config_object diff --git a/config/schema/facets.sort_processor.schema.yml b/config/schema/facets.sort_processor.schema.yml new file mode 100644 index 0000000..b0d5ff9 --- /dev/null +++ b/config/schema/facets.sort_processor.schema.yml @@ -0,0 +1,31 @@ +plugin.plugin_configuration.facets_sort_processor.raw_value_widget_order: + type: mapping + label: 'Raw value widget order' + mapping: + sort: + type: string + label: sort order + +plugin.plugin_configuration.facets_sort_processor.active_widget_order: + type: mapping + label: 'Active widget order' + mapping: + sort: + type: string + label: sort order + +plugin.plugin_configuration.facets_sort_processor.count_widget_order: + type: mapping + label: 'Active widget order' + mapping: + sort: + type: string + label: sort order + +plugin.plugin_configuration.facets_sort_processor.display_value_widget_order: + type: mapping + label: 'Display value widget order' + mapping: + sort: + type: string + label: sort order diff --git a/facets.services.yml b/facets.services.yml index 1be585e..811b1f2 100644 --- a/facets.services.yml +++ b/facets.services.yml @@ -14,6 +14,9 @@ services: plugin.manager.facets.url_processor: class: Drupal\facets\UrlProcessor\UrlProcessorPluginManager parent: default_plugin_manager + plugin.manager.facets.sort_processor: + class: Drupal\facets\SortProcessor\SortProcessorPluginManager + parent: default_plugin_manager facets.manager: class: Drupal\facets\FacetManager\DefaultFacetManager arguments: diff --git a/src/Annotation/FacetsSortProcessor.php b/src/Annotation/FacetsSortProcessor.php new file mode 100644 index 0000000..42a83ea --- /dev/null +++ b/src/Annotation/FacetsSortProcessor.php @@ -0,0 +1,48 @@ +entityTypeManager = $entity_type_manager; $this->processorPluginManager = $processor_plugin_manager; $this->widgetPluginManager = $widget_plugin_manager; + $this->sortProcessorPluginManager = $sort_processor_plugin_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */ + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = $container->get('entity_type.manager'); /** @var \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager */ $processor_plugin_manager = $container->get('plugin.manager.facets.processor'); + /** @var \Drupal\facets\Processor\ProcessorPluginManager $processor_plugin_manager */ + $sort_processor_plugin_manager = $container->get('plugin.manager.facets.sort_processor'); + /** @var \Drupal\facets\Widget\WidgetPluginManager $widget_plugin_manager */ $widget_plugin_manager = $container->get('plugin.manager.facets.widget'); - return new static($entity_type_manager, $processor_plugin_manager, $widget_plugin_manager); + return new static($entity_type_manager, $processor_plugin_manager, $widget_plugin_manager, $sort_processor_plugin_manager); } /** @@ -117,7 +132,8 @@ class FacetDisplayForm extends EntityForm { // @todo Create, use and save SubFormState already here, not only in // validate(). Also, use proper subset of $form for first parameter? $config = $this->config('facets.facet.' . $this->entity->id()); - if ($config_form = $widget_instance->buildConfigurationForm([], $form_state, ($config instanceof Config) ? $config : NULL)) { + $config = ($config instanceof Config) ? $config : NULL; + if ($config_form = $widget_instance->buildConfigurationForm([], $form_state, $config)) { $form['widget_configs']['#type'] = 'fieldset'; $form['widget_configs']['#title'] = $this->t('%widget settings', ['%widget' => $this->getWidgetPluginManager()->getDefinition($widget)['label']]); @@ -218,8 +234,9 @@ class FacetDisplayForm extends EntityForm { ), ), ); + foreach ($all_processors as $processor_id => $processor) { - if (!($processor instanceof WidgetOrderProcessorInterface) && !($processor instanceof UrlProcessorInterface)) { + if (!($processor instanceof UrlProcessorInterface)) { $clean_css_id = Html::cleanCssIdentifier($processor_id); $form['facet_settings'][$processor_id]['status'] = array( '#type' => 'checkbox', @@ -236,10 +253,8 @@ class FacetDisplayForm extends EntityForm { '#access' => !$processor->isHidden(), ); - $processor_form_state = new SubFormState( - $form_state, - ['facet_settings', $processor_id, 'settings'] - ); + $sub_keys = ['facet_settings', $processor_id, 'settings']; + $processor_form_state = new SubFormState($form_state, $sub_keys); $processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet); if ($processor_form) { $form['facet_settings'][$processor_id]['settings'] = array( @@ -263,6 +278,7 @@ class FacetDisplayForm extends EntityForm { } } } + // Add the list of widget sort processors with checkboxes to enable/disable // them. $form['facet_sorting'] = array( @@ -274,47 +290,53 @@ class FacetDisplayForm extends EntityForm { ), ), ); - foreach ($all_processors as $processor_id => $processor) { - if ($processor instanceof WidgetOrderProcessorInterface) { - $clean_css_id = Html::cleanCssIdentifier($processor_id); - $form['facet_sorting'][$processor_id]['status'] = array( - '#type' => 'checkbox', - '#title' => (string) $processor->getPluginDefinition()['label'], - '#default_value' => $processor->isLocked() || !empty($enabled_processors[$processor_id]), - '#description' => $processor->getDescription(), + + $sorting_settings = $all_processors['sort_processor_handler']->getConfiguration(); + + foreach ($this->sortProcessorPluginManager->getDefinitions() as $processor_id => $definition) { + $processor = $this->sortProcessorPluginManager->createInstance($processor_id); + $clean_css_id = Html::cleanCssIdentifier($processor_id); + $is_checked = !empty($sorting_settings[$processor_id]) && $sorting_settings[$processor_id]['status']; + $form['facet_sorting'][$processor_id]['status'] = array( + '#type' => 'checkbox', + '#title' => (string) $processor->getPluginDefinition()['label'], + '#default_value' => $processor->isLocked() || $is_checked, + '#description' => $processor->getDescription(), + '#attributes' => array( + 'class' => array( + 'search-api-processor-status-' . $clean_css_id, + ), + 'data-id' => $clean_css_id, + ), + '#disabled' => $processor->isLocked(), + '#access' => !$processor->isHidden(), + ); + + $sub_keys = ['facet_sorting', $processor_id, 'settings']; + $processor_form_state = new SubFormState($form_state, $sub_keys); + $processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet); + if ($processor_form) { + $form['facet_sorting'][$processor_id]['settings'] = array( + '#type' => 'container', + '#open' => TRUE, '#attributes' => array( 'class' => array( - 'search-api-processor-status-' . $clean_css_id, + 'facets-processor-settings-' . Html::cleanCssIdentifier($processor_id), + 'facets-processor-settings-sorting', + 'facets-processor-settings', + ), + ), + '#states' => array( + 'visible' => array( + ':input[name="facet_sorting[' . $processor_id . '][status]"]' => array('checked' => TRUE), ), - 'data-id' => $clean_css_id, ), - '#disabled' => $processor->isLocked(), - '#access' => !$processor->isHidden(), ); + $form['facet_sorting'][$processor_id]['settings'] += $processor_form; - $processor_form_state = new SubFormState( - $form_state, - array('facet_sorting', $processor_id, 'settings') - ); - $processor_form = $processor->buildConfigurationForm($form, $processor_form_state, $facet); - if ($processor_form) { - $form['facet_sorting'][$processor_id]['settings'] = array( - '#type' => 'container', - '#open' => TRUE, - '#attributes' => array( - 'class' => array( - 'facets-processor-settings-' . Html::cleanCssIdentifier($processor_id), - 'facets-processor-settings-sorting', - 'facets-processor-settings', - ), - ), - '#states' => array( - 'visible' => array( - ':input[name="facet_sorting[' . $processor_id . '][status]"]' => array('checked' => TRUE), - ), - ), - ); - $form['facet_sorting'][$processor_id]['settings'] += $processor_form; + // Make sure that settings are saved as expected. + foreach ($sorting_settings[$processor_id]['settings'] as $k => $value) { + $form['facet_sorting'][$processor_id]['settings'][$k]['#default_value'] = $value; } } } @@ -494,24 +516,22 @@ class FacetDisplayForm extends EntityForm { $values = $form_state->getValues(); // Store processor settings. - // @todo Go through all available processors, enable/disable with method on - // processor plugin to allow reaction. /** @var \Drupal\facets\FacetInterface $facet */ $facet = $this->entity; /** @var \Drupal\facets\Processor\ProcessorInterface $processor */ $processors = $facet->getProcessors(FALSE); foreach ($processors as $processor_id => $processor) { - $form_container_key = $processor instanceof WidgetOrderProcessorInterface ? 'facet_sorting' : 'facet_settings'; + $form_container_key = $processor instanceof SortProcessorInterface ? 'facet_sorting' : 'facet_settings'; if (empty($values[$form_container_key][$processor_id]['status'])) { $facet->removeProcessor($processor_id); continue; } - $new_settings = array( + $new_settings = [ 'processor_id' => $processor_id, - 'weights' => array(), - 'settings' => array(), - ); + 'weights' => [], + 'settings' => [], + ]; if (!empty($values['processors'][$processor_id]['weights'])) { $new_settings['weights'] = $values['processors'][$processor_id]['weights']; } @@ -523,6 +543,12 @@ class FacetDisplayForm extends EntityForm { $processor->submitConfigurationForm($form[$form_container_key][$processor_id]['settings'], $processor_form_state, $facet); $new_settings['settings'] = $processor->getConfiguration(); } + + // The sort processor handler should have the settings for all sorting + // facets. + if ($processor_id === 'sort_processor_handler') { + $new_settings['settings'] = $values['facet_sorting']; + } $facet->addProcessor($new_settings); } diff --git a/src/Plugin/facets/processor/SortProcessorHandler.php b/src/Plugin/facets/processor/SortProcessorHandler.php new file mode 100644 index 0000000..56d3a66 --- /dev/null +++ b/src/Plugin/facets/processor/SortProcessorHandler.php @@ -0,0 +1,93 @@ +facet = $configuration['facet']; + + } + + /** + * {@inheritdoc} + */ + public function build(FacetInterface $facet, array $results) { + $processors = $facet->getProcessors(); + $sort_processor_handler = $processors['sort_processor_handler']; + $configs = $sort_processor_handler->getConfiguration(); + + $plugin_manager = \Drupal::getContainer()->get('plugin.manager.facets.sort_processor'); + + foreach ($configs as $id => $sort_config) { + if ($sort_config['status'] == TRUE) { + /** @var \Drupal\facets\SortProcessor\SortProcessorInterface $sort */ + $this->sorts[] = $plugin_manager->createInstance($id, $sort_config); + } + } + + uasort($results, array($this, 'sortCallback')); + + return $results; + } + + /** + * Applies sorts and return the sorting information for uasort(). + */ + protected function sortCallback(Result $a, Result $b) { + $return = 0; + foreach ($this->sorts as $sort) { + if ($return = $sort->sortResults($a, $b)) { + break; + } + } + return $return; + } + +} diff --git a/src/Plugin/facets/processor/UrlProcessorHandler.php b/src/Plugin/facets/processor/UrlProcessorHandler.php index 8779a8f..329ecb5 100644 --- a/src/Plugin/facets/processor/UrlProcessorHandler.php +++ b/src/Plugin/facets/processor/UrlProcessorHandler.php @@ -27,7 +27,7 @@ use Drupal\facets\Processor\ProcessorPluginBase; * description = @Translation("Triggers the URL processor, which is set in the Facet source configuration."), * stages = { * "pre_query" = 50, - * "build" = 15, + * "build" = 25, * }, * locked = true * ) diff --git a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php similarity index 59% rename from src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php rename to src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php index 2b125c7..207ce67 100644 --- a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php @@ -2,20 +2,18 @@ /** * @file - * Contains \Drupal\facets\Plugin\facets\processor\ActiveWidgetOrderProcessor. + * Contains \Drupal\facets\Plugin\facets\sort_processor\ActiveWidgetOrderProcessor. */ -namespace Drupal\facets\Plugin\facets\processor; +namespace Drupal\facets\Plugin\facets\sort_processor; - -use Drupal\facets\Processor\WidgetOrderPluginBase; -use Drupal\facets\Processor\WidgetOrderProcessorInterface; use Drupal\facets\Result\Result; +use Drupal\facets\SortProcessor\SortProcessorPluginBase; /** * A processor that orders the results by active state. * - * @FacetsProcessor( + * @FacetsSortProcessor( * id = "active_widget_order", * label = @Translation("Sort by active state"), * description = @Translation("Sorts the widget results by active state."), @@ -24,20 +22,20 @@ use Drupal\facets\Result\Result; * } * ) */ -class ActiveWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface { +class ActiveWidgetOrderProcessor extends SortProcessorPluginBase { /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { + public function sortResults(Result $a, Result $b) { + + $order = $this->getConfiguration()['sort']; + if ($order === 'ASC') { - usort($results, 'self::sortActiveAsc'); - } - else { - usort($results, 'self::sortActiveDesc'); + return self::sortActiveAsc($a, $b); } - return $results; + return self::sortActiveDesc($a, $b); } /** diff --git a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php similarity index 60% rename from src/Plugin/facets/processor/CountWidgetOrderProcessor.php rename to src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php index f28f71b..8336665 100644 --- a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php @@ -2,19 +2,18 @@ /** * @file - * Contains \Drupal\facets\Plugin\facets\processor\CountWidgetOrderProcessor. + * Contains \Drupal\facets\Plugin\facets\sort_processor\CountWidgetOrderProcessor. */ -namespace Drupal\facets\Plugin\facets\processor; +namespace Drupal\facets\Plugin\facets\sort_processor; -use Drupal\facets\Processor\WidgetOrderPluginBase; -use Drupal\facets\Processor\WidgetOrderProcessorInterface; use Drupal\facets\Result\Result; +use Drupal\facets\SortProcessor\SortProcessorPluginBase; /** * A processor that orders the results by amount. * - * @FacetsProcessor( + * @FacetsSortProcessor( * id = "count_widget_order", * label = @Translation("Sort by count"), * description = @Translation("Sorts the widget results by count."), @@ -23,20 +22,20 @@ use Drupal\facets\Result\Result; * } * ) */ -class CountWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface { +class CountWidgetOrderProcessor extends SortProcessorPluginBase { /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { + public function sortResults(Result $a, Result $b) { + + $order = $this->getConfiguration()['sort']; + if ($order === 'ASC') { - usort($results, 'self::sortCountAsc'); - } - else { - usort($results, 'self::sortCountDesc'); + return self::sortCountAsc($a, $b); } - return $results; + return self::sortCountDesc($a, $b); } /** diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php similarity index 57% rename from src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php rename to src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php index 638ad73..00be9cb 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php @@ -2,19 +2,18 @@ /** * @file - * Contains \Drupal\facets\Plugin\facets\processor\DisplayValueWidgetOrderProcessor. + * Contains \Drupal\facets\Plugin\facets\sort_processor\DisplayValueWidgetOrderProcessor. */ -namespace Drupal\facets\Plugin\facets\processor; +namespace Drupal\facets\Plugin\facets\sort_processor; -use Drupal\facets\Processor\WidgetOrderPluginBase; -use Drupal\facets\Processor\WidgetOrderProcessorInterface; use Drupal\facets\Result\Result; +use Drupal\facets\SortProcessor\SortProcessorPluginBase; /** * A processor that orders the results by display value. * - * @FacetsProcessor( + * @FacetsSortProcessor( * id = "display_value_widget_order", * label = @Translation("Sort by display value"), * description = @Translation("Sorts the widget results by display value."), @@ -23,20 +22,20 @@ use Drupal\facets\Result\Result; * } * ) */ -class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface { +class DisplayValueWidgetOrderProcessor extends SortProcessorPluginBase { /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { + public function sortResults(Result $a, Result $b) { + + $order = $this->getConfiguration()['sort']; + if ($order === 'ASC') { - usort($results, 'self::sortDisplayValueAsc'); - } - else { - usort($results, 'self::sortDisplayValueDesc'); + return self::sortDisplayValueAsc($a, $b); } - return $results; + return self::sortDisplayValueDesc($a, $b); } /** diff --git a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php similarity index 56% rename from src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php rename to src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php index f5c9787..87600ec 100644 --- a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php @@ -2,19 +2,18 @@ /** * @file - * Contains \Drupal\facets\Plugin\facets\processor\RawValueWidgetOrderProcessor. + * Contains \Drupal\facets\Plugin\facets\sort_processor\RawValueWidgetOrderProcessor. */ -namespace Drupal\facets\Plugin\facets\processor; +namespace Drupal\facets\Plugin\facets\sort_processor; -use Drupal\facets\Processor\WidgetOrderPluginBase; -use Drupal\facets\Processor\WidgetOrderProcessorInterface; use Drupal\facets\Result\Result; +use Drupal\facets\SortProcessor\SortProcessorPluginBase; /** * A processor that orders the results by raw value. * - * @FacetsProcessor( + * @FacetsSortProcessor( * id = "raw_value_widget_order", * label = @Translation("Sort by raw value"), * description = @Translation("Sorts the widget results by raw value."), @@ -23,20 +22,20 @@ use Drupal\facets\Result\Result; * } * ) */ -class RawValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface { +class RawValueWidgetOrderProcessor extends SortProcessorPluginBase { /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { + public function sortResults(Result $a, Result $b) { + + $order = $this->getConfiguration()['sort']; + if ($order === 'ASC') { - usort($results, 'self::sortRawValueAsc'); - } - else { - usort($results, 'self::sortRawValueDesc'); + return self::sortRawValueAsc($a, $b); } - return $results; + return self::sortRawValueDesc($a, $b); } /** diff --git a/src/Processor/WidgetOrderProcessorInterface.php b/src/Processor/WidgetOrderProcessorInterface.php deleted file mode 100644 index fadab54..0000000 --- a/src/Processor/WidgetOrderProcessorInterface.php +++ /dev/null @@ -1,29 +0,0 @@ -getProcessors(); - $config = $processors[$this->getPluginId()]; - - // This should load the facet's config to find the ordering direction. - return $this->sortResults($results, $config->getConfiguration()['sort']); - } - - /** - * {@inheritdoc} - */ public function defaultConfiguration() { return ['sort' => 'ASC']; } diff --git a/src/SortProcessor/SortProcessorPluginManager.php b/src/SortProcessor/SortProcessorPluginManager.php new file mode 100644 index 0000000..fc83cfb --- /dev/null +++ b/src/SortProcessor/SortProcessorPluginManager.php @@ -0,0 +1,32 @@ +setCacheBackend($cache_backend, 'facets_sort_processors'); + } + +} diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index 365686d..48774cf 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -138,6 +138,87 @@ class ProcessorIntegrationTest extends FacetWebTestBase { } /** + * Tests sorting of results. + */ + public function testResultSorting() { + $id = 'burrowing_owl'; + $name = 'Burrowing owl'; + + $this->createFacet($name, $id, 'keywords'); + $this->createFacetBlock($id); + + $values = [ + 'facet_sorting[display_value_widget_order][status]' => TRUE, + 'widget_configs[show_numbers]' => TRUE, + ]; + $this->drupalGet('admin/config/search/facets/' . $id . '/display'); + $this->drupalPostForm(NULL, $values, $this->t('Save')); + + $expected_results = [ + 'apple (4)', + 'banana (2)', + 'grape (6)', + 'orange (6)', + 'strawberry (4)', + ]; + + $this->drupalGet('search-api-test-fulltext'); + foreach ($expected_results as $k => $link) { + if ($k > 0) { + $x = $expected_results[($k - 1)]; + $y = $expected_results[$k]; + $this->assertStringPosition($x, $y); + } + } + + $values['facet_sorting[display_value_widget_order][status]'] = FALSE; + $values['facet_sorting[count_widget_order][status]'] = TRUE; + $this->drupalGet('admin/config/search/facets/' . $id . '/display'); + $this->drupalPostForm(NULL, $values, $this->t('Save')); + + $expected_results = [ + 'banana (2)', + 'apple (4)', + 'strawberry (4)', + 'grape (6)', + 'orange (6)', + ]; + + $this->drupalGet('search-api-test-fulltext'); + foreach ($expected_results as $k => $link) { + if ($k > 0) { + $x = $expected_results[($k - 1)]; + $y = $expected_results[$k]; + $this->assertStringPosition($x, $y); + } + } + + $values['facet_sorting[display_value_widget_order][status]'] = TRUE; + $values['facet_sorting[count_widget_order][status]'] = TRUE; + $this->drupalGet('admin/config/search/facets/' . $id . '/display'); + $this->drupalPostForm(NULL, $values, $this->t('Save')); + $this->assertFieldChecked('edit-facet-sorting-display-value-widget-order-status'); + $this->assertFieldChecked('edit-facet-sorting-count-widget-order-status'); + + $expected_results = [ + 'banana (2)', + 'apple (4)', + 'strawberry (4)', + 'grape (6)', + 'orange (6)', + ]; + + $this->drupalGet('search-api-test-fulltext'); + foreach ($expected_results as $k => $link) { + if ($k > 0) { + $x = $expected_results[($k - 1)]; + $y = $expected_results[$k]; + $this->assertStringPosition($x, $y); + } + } + } + + /** * Tests the count limit processor. */ private function checkCountLimitProcessor() { diff --git a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php deleted file mode 100644 index 5da0b37..0000000 --- a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php +++ /dev/null @@ -1,133 +0,0 @@ -setActiveState(TRUE); - $original_results[2]->setActiveState(TRUE); - $original_results[3]->setActiveState(TRUE); - - $this->originalResults = $original_results; - - $this->processor = new ActiveWidgetOrderProcessor([], 'active_widget_order', []); - } - - /** - * Tests sorting ascending. - */ - public function testAscending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'ASC'); - $expected_values = [TRUE, TRUE, TRUE, FALSE, FALSE]; - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->isActive()); - } - } - - /** - * Tests sorting descending. - */ - public function testDescending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'DESC'); - $expected_values = array_reverse([TRUE, TRUE, TRUE, FALSE, FALSE]); - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->isActive()); - } - } - - /** - * Tests configuration. - */ - public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); - } - - /** - * Tests build. - */ - public function testBuild() { - $processor_definitions = [ - 'active_widget_order' => [ - 'id' => 'active_widget_order', - 'class' => 'Drupal\facets\Plugin\facets\processor\ActiveWidgetOrderProcessor', - ], - ]; - $manager = $this->getMockBuilder(ProcessorPluginManager::class) - ->disableOriginalConstructor() - ->getMock(); - $manager->expects($this->once()) - ->method('getDefinitions') - ->willReturn($processor_definitions); - $manager->expects($this->once()) - ->method('createInstance') - ->willReturn($this->processor); - - $container_builder = new ContainerBuilder(); - $container_builder->set('plugin.manager.facets.processor', $manager); - \Drupal::setContainer($container_builder); - - $facet = new Facet( - [ - 'id' => 'the_zoo', - 'results' => $this->originalResults, - 'processor_configs' => $processor_definitions, - ], - 'facets_facet' - ); - $built = $this->processor->build($facet, $this->originalResults); - - $this->assertEquals(TRUE, $built[0]->isActive()); - $this->assertEquals(TRUE, $built[1]->isActive()); - $this->assertEquals(TRUE, $built[2]->isActive()); - $this->assertEquals(FALSE, $built[3]->isActive()); - $this->assertEquals(FALSE, $built[4]->isActive()); - } - -} diff --git a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php deleted file mode 100644 index 7adeec1..0000000 --- a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php +++ /dev/null @@ -1,130 +0,0 @@ -originalResults = [ - new Result('llama', 'llama', 10), - new Result('badger', 'badger', 5), - new Result('duck', 'duck', 15), - ]; - - $this->processor = new CountWidgetOrderProcessor([], 'count_widget_order', []); - } - - /** - * Tests sorting ascending. - */ - public function testAscending() { - - $sorted_results = $this->processor->sortResults($this->originalResults, 'ASC'); - - $this->assertEquals(5, $sorted_results[0]->getCount()); - $this->assertEquals('badger', $sorted_results[0]->getDisplayValue()); - $this->assertEquals(10, $sorted_results[1]->getCount()); - $this->assertEquals('llama', $sorted_results[1]->getDisplayValue()); - $this->assertEquals(15, $sorted_results[2]->getCount()); - $this->assertEquals('duck', $sorted_results[2]->getDisplayValue()); - } - - /** - * Tests sorting descending. - */ - public function testDescending() { - - $sorted_results = $this->processor->sortResults($this->originalResults, 'DESC'); - - $this->assertEquals(15, $sorted_results[0]->getCount()); - $this->assertEquals('duck', $sorted_results[0]->getDisplayValue()); - $this->assertEquals(10, $sorted_results[1]->getCount()); - $this->assertEquals('llama', $sorted_results[1]->getDisplayValue()); - $this->assertEquals(5, $sorted_results[2]->getCount()); - $this->assertEquals('badger', $sorted_results[2]->getDisplayValue()); - } - - /** - * Tests configuration. - */ - public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); - } - - /** - * Tests build. - */ - public function testBuild() { - $processor_definitions = [ - 'count_widget_order' => [ - 'id' => 'count_widget_order', - 'class' => 'Drupal\facets\Plugin\facets\processor\CountWidgetOrderProcessor', - ], - ]; - $manager = $this->getMockBuilder(ProcessorPluginManager::class) - ->disableOriginalConstructor() - ->getMock(); - $manager->expects($this->once()) - ->method('getDefinitions') - ->willReturn($processor_definitions); - $manager->expects($this->once()) - ->method('createInstance') - ->willReturn($this->processor); - - $container_builder = new ContainerBuilder(); - $container_builder->set('plugin.manager.facets.processor', $manager); - \Drupal::setContainer($container_builder); - - $facet = new Facet( - [ - 'id' => 'the_zoo', - 'results' => $this->originalResults, - 'processor_configs' => $processor_definitions, - ], - 'facets_facet' - ); - $built = $this->processor->build($facet, $this->originalResults); - - $this->assertEquals('badger', $built[0]->getDisplayValue()); - $this->assertEquals('llama', $built[1]->getDisplayValue()); - $this->assertEquals('duck', $built[2]->getDisplayValue()); - } - -} diff --git a/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php deleted file mode 100644 index 7609dca..0000000 --- a/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php +++ /dev/null @@ -1,162 +0,0 @@ -originalResults = [ - new Result('thetans', 'thetans', 10), - new Result('xenu', 'xenu', 5), - new Result('Tom', 'Tom', 15), - new Result('Hubbard', 'Hubbard', 666), - new Result('FALSE', 'FALSE', 1), - new Result('1977', '1977', 20), - new Result('2', '2', 22), - ]; - - $this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', []); - } - - /** - * Tests sorting ascending. - */ - public function testAscending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'ASC'); - $expected_values = [ - '2', - '1977', - 'FALSE', - 'Hubbard', - 'thetans', - 'Tom', - 'xenu', - ]; - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); - } - } - - /** - * Tests sorting descending. - */ - public function testDescending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'DESC'); - $expected_values = array_reverse([ - '2', - '1977', - 'FALSE', - 'Hubbard', - 'thetans', - 'Tom', - 'xenu', - ]); - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); - } - } - - /** - * Tests that sorting uses the display value. - */ - public function testUseActualDisplayValue() { - $original = [ - new Result('bb_test', 'Test AA', 10), - new Result('aa_test', 'Test BB', 10), - ]; - - $sorted_results = $this->processor->sortResults($original, 'DESC'); - - $this->assertEquals('Test BB', $sorted_results[0]->getDisplayValue()); - $this->assertEquals('Test AA', $sorted_results[1]->getDisplayValue()); - } - - /** - * Tests configuration. - */ - public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); - } - - - /** - * Tests build. - */ - public function testBuild() { - $processor_definitions = [ - 'display_value_widget_order' => [ - 'id' => 'display_value_widget_order', - 'class' => 'Drupal\facets\Plugin\facets\processor\DisplayValueWidgetOrderProcessor', - ], - ]; - $manager = $this->getMockBuilder(ProcessorPluginManager::class) - ->disableOriginalConstructor() - ->getMock(); - $manager->expects($this->once()) - ->method('getDefinitions') - ->willReturn($processor_definitions); - $manager->expects($this->once()) - ->method('createInstance') - ->willReturn($this->processor); - - $container_builder = new ContainerBuilder(); - $container_builder->set('plugin.manager.facets.processor', $manager); - \Drupal::setContainer($container_builder); - - $facet = new Facet( - [ - 'id' => 'the_zoo', - 'results' => $this->originalResults, - 'processor_configs' => $processor_definitions, - ], - 'facets_facet' - ); - $built = $this->processor->build($facet, $this->originalResults); - - $this->assertEquals('2', $built[0]->getDisplayValue()); - $this->assertEquals('1977', $built[1]->getDisplayValue()); - $this->assertEquals('FALSE', $built[2]->getDisplayValue()); - $this->assertEquals('Hubbard', $built[3]->getDisplayValue()); - $this->assertEquals('thetans', $built[4]->getDisplayValue()); - $this->assertEquals('Tom', $built[5]->getDisplayValue()); - $this->assertEquals('xenu', $built[6]->getDisplayValue()); - } - -} diff --git a/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php deleted file mode 100644 index ffd5a6e..0000000 --- a/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php +++ /dev/null @@ -1,146 +0,0 @@ -originalResults = [ - new Result('C', 'thetans', 10), - new Result('B', 'xenu', 5), - new Result('A', 'Tom', 15), - new Result('D', 'Hubbard', 666), - new Result('E', 'FALSE', 1), - new Result('G', '1977', 20), - new Result('F', '2', 22), - ]; - - $this->processor = new RawValueWidgetOrderProcessor([], 'raw_value_widget_order', []); - } - - /** - * Tests sorting ascending. - */ - public function testAscending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'ASC'); - $expected_values = [ - 'Tom', - 'xenu', - 'thetans', - 'Hubbard', - 'FALSE', - '2', - '1977', - ]; - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); - } - } - - /** - * Tests sorting descending. - */ - public function testDescending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'DESC'); - $expected_values = array_reverse([ - 'Tom', - 'xenu', - 'thetans', - 'Hubbard', - 'FALSE', - '2', - '1977', - ]); - foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); - } - } - - /** - * Tests configuration. - */ - public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); - $this->assertEquals(['sort' => 'ASC'], $config); - } - - /** - * Tests build. - */ - public function testBuild() { - $processor_definitions = [ - 'raw_value_widget_order' => [ - 'id' => 'raw_value_widget_order', - 'class' => 'Drupal\facets\Plugin\facets\processor\RawValueWidgetOrderProcessor', - ], - ]; - $manager = $this->getMockBuilder(ProcessorPluginManager::class) - ->disableOriginalConstructor() - ->getMock(); - $manager->expects($this->once()) - ->method('getDefinitions') - ->willReturn($processor_definitions); - $manager->expects($this->once()) - ->method('createInstance') - ->willReturn($this->processor); - - $container_builder = new ContainerBuilder(); - $container_builder->set('plugin.manager.facets.processor', $manager); - \Drupal::setContainer($container_builder); - - $facet = new Facet( - [ - 'id' => 'the_zoo', - 'results' => $this->originalResults, - 'processor_configs' => $processor_definitions, - ], - 'facets_facet' - ); - $built = $this->processor->build($facet, $this->originalResults); - - $this->assertEquals('Tom', $built[0]->getDisplayValue()); - $this->assertEquals('xenu', $built[1]->getDisplayValue()); - $this->assertEquals('thetans', $built[2]->getDisplayValue()); - $this->assertEquals('Hubbard', $built[3]->getDisplayValue()); - $this->assertEquals('FALSE', $built[4]->getDisplayValue()); - $this->assertEquals('2', $built[5]->getDisplayValue()); - $this->assertEquals('1977', $built[6]->getDisplayValue()); - } - -} diff --git a/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php b/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php new file mode 100644 index 0000000..9745315 --- /dev/null +++ b/tests/src/Unit/Plugin/processor/SortProcessorHandlerTest.php @@ -0,0 +1,78 @@ +setExpectedException('\Drupal\facets\Exception\InvalidProcessorException', "The SortProcessorHandler doesn't have the required 'facet' in the configuration array."); + new SortProcessorHandler([], 'test', []); + } + + /** + * Tests that the processor correctly throws an exception. + */ + public function testInvalidProcessorConfiguration() { + $this->setExpectedException('\Drupal\facets\Exception\InvalidProcessorException', "The SortProcessorHandler doesn't have the required 'facet' in the configuration array."); + new SortProcessorHandler(['facet' => new \stdClass()], 'test', []); + } + + /** + * Tests configuration. + */ + public function testConfiguration() { + $facet = new Facet([], 'facets_facet'); + $processor = new SortProcessorHandler(['facet' => $facet], 'url_processor_handler', []); + + $config = $processor->defaultConfiguration(); + $this->assertEquals([], $config); + } + + /** + * Tests testDescription(). + */ + public function testDescription() { + $facet = new Facet([], 'facets_facet'); + $processor = new SortProcessorHandler(['facet' => $facet], 'url_processor_handler', []); + + $this->assertEquals('', $processor->getDescription()); + } + + /** + * Tests isHidden(). + */ + public function testIsHidden() { + $facet = new Facet([], 'facets_facet'); + $processor = new SortProcessorHandler(['facet' => $facet], 'url_processor_handler', []); + + $this->assertEquals(FALSE, $processor->isHidden()); + } + + /** + * Tests isLocked(). + */ + public function testIsLocked() { + $facet = new Facet([], 'facets_facet'); + $processor = new SortProcessorHandler(['facet' => $facet], 'url_processor_handler', []); + + $this->assertEquals(FALSE, $processor->isLocked()); + } + +} diff --git a/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php new file mode 100644 index 0000000..eadc149 --- /dev/null +++ b/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php @@ -0,0 +1,90 @@ +setActiveState(TRUE); + $original_results[2]->setActiveState(TRUE); + $original_results[3]->setActiveState(TRUE); + + $this->originalResults = $original_results; + } + + /** + * Tests sorting ascending. + */ + public function testAscending() { + $processor = new ActiveWidgetOrderProcessor(['sort' => 'ASC'], 'active_widget_order', []); + $expected_values = [0, 1, 0, 0, -1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests sorting descending. + */ + public function testDescending() { + $processor = new ActiveWidgetOrderProcessor(['sort' => 'DESC'], 'active_widget_order', []); + $expected_values = [0, -1, 0, 0, 1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests configuration. + */ + public function testConfiguration() { + $processor = new ActiveWidgetOrderProcessor([], 'active_widget_order', []); + $config = $processor->defaultConfiguration(); + $this->assertEquals(['sort' => 'ASC'], $config); + } + +} diff --git a/tests/src/Unit/Plugin/sort_processor/CountWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/sort_processor/CountWidgetOrderProcessorTest.php new file mode 100644 index 0000000..3786d83 --- /dev/null +++ b/tests/src/Unit/Plugin/sort_processor/CountWidgetOrderProcessorTest.php @@ -0,0 +1,81 @@ +originalResults = [ + new Result('llama', 'llama', 10), + new Result('badger', 'badger', 5), + new Result('duck', 'duck', 15), + ]; + } + + /** + * Tests sorting ascending. + */ + public function testAscending() { + $processor = new CountWidgetOrderProcessor(['sort' => 'ASC'], 'count_widget_order', []); + $expected_values = [0, 1, -1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests sorting descending. + */ + public function testDescending() { + $processor = new CountWidgetOrderProcessor(['sort' => 'DESC'], 'count_widget_order', []); + $expected_values = [0, -1, 1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests configuration. + */ + public function testConfiguration() { + $processor = new CountWidgetOrderProcessor([], 'count_widget_order', []); + $config = $processor->defaultConfiguration(); + $this->assertEquals(['sort' => 'ASC'], $config); + } + +} diff --git a/tests/src/Unit/Plugin/sort_processor/DisplayValueWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/sort_processor/DisplayValueWidgetOrderProcessorTest.php new file mode 100644 index 0000000..449156f --- /dev/null +++ b/tests/src/Unit/Plugin/sort_processor/DisplayValueWidgetOrderProcessorTest.php @@ -0,0 +1,99 @@ +originalResults = [ + new Result('thetans', 'thetans', 10), + new Result('xenu', 'xenu', 5), + new Result('Hubbard', 'Hubbard', 666), + new Result('Tom', 'Tom', 15), + new Result('FALSE', 'FALSE', 1), + new Result('2', '2', 22), + new Result('1977', '1977', 20), + ]; + } + + /** + * Tests sorting ascending. + */ + public function testAscending() { + $processor = new DisplayValueWidgetOrderProcessor(['sort' => 'ASC'], 'display_value_widget_order', []); + $expected_values = [0, -1, 1, -1, 1, 1, -1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests sorting descending. + */ + public function testDescending() { + $processor = new DisplayValueWidgetOrderProcessor(['sort' => 'DESC'], 'display_value_widget_order', []); + $expected_values = [0, 1, -1, 1, -1, -1, 1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests that sorting uses the display value. + */ + public function testUseActualDisplayValue() { + $original = [ + new Result('bb_test', 'Test AA', 10), + new Result('aa_test', 'Test BB', 10), + ]; + + $processor = new DisplayValueWidgetOrderProcessor(['sort' => 'DESC'], 'display_value_widget_order', []); + $result = $processor->sortResults($original[0], $original[1]); + $this->assertEquals(1, $result); + } + + /** + * Tests configuration. + */ + public function testConfiguration() { + $processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', []); + $config = $processor->defaultConfiguration(); + $this->assertEquals(['sort' => 'ASC'], $config); + } + +} diff --git a/tests/src/Unit/Plugin/sort_processor/RawValueWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/sort_processor/RawValueWidgetOrderProcessorTest.php new file mode 100644 index 0000000..2be23eb --- /dev/null +++ b/tests/src/Unit/Plugin/sort_processor/RawValueWidgetOrderProcessorTest.php @@ -0,0 +1,85 @@ +originalResults = [ + new Result('C', 'thetans', 10), + new Result('B', 'xenu', 5), + new Result('A', 'Tom', 15), + new Result('D', 'Hubbard', 666), + new Result('E', 'FALSE', 1), + new Result('G', '1977', 20), + new Result('F', '2', 22), + ]; + } + + /** + * Tests sorting ascending. + */ + public function testAscending() { + $processor = new RawValueWidgetOrderProcessor(['sort' => 'ASC'], 'raw_value_widget_order', []); + $expected_values = [0, 1, 1, -1, -1, -1, 1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests sorting descending. + */ + public function testDescending() { + $processor = new RawValueWidgetOrderProcessor(['sort' => 'DESC'], 'raw_value_widget_order', []); + $expected_values = [0, -1, -1, 1, 1, 1, -1]; + foreach ($expected_values as $index => $value) { + if ($index >= 1) { + $ua_result = $processor->sortResults($this->originalResults[$index - 1], $this->originalResults[$index]); + $message = new FormattableMarkup('Failed asserting uasort return value for :a and :b', [':a' => $this->originalResults[$index -1]->getDisplayValue(), ':b' => $this->originalResults[$index]->getDisplayValue()]); + $this->assertEquals($value, $ua_result, $message); + } + } + } + + /** + * Tests configuration. + */ + public function testConfiguration() { + $processor = new RawValueWidgetOrderProcessor([], 'raw_value_widget_order', []); + $config = $processor->defaultConfiguration(); + $this->assertEquals(['sort' => 'ASC'], $config); + } + +}