diff --git a/src/FacetManager/DefaultFacetManager.php b/src/FacetManager/DefaultFacetManager.php index cb6da77..c2dab74 100644 --- a/src/FacetManager/DefaultFacetManager.php +++ b/src/FacetManager/DefaultFacetManager.php @@ -13,6 +13,7 @@ use Drupal\facets\Processor\PostQueryProcessorInterface; use Drupal\facets\Processor\PreQueryProcessorInterface; use Drupal\facets\Processor\ProcessorInterface; use Drupal\facets\Processor\ProcessorPluginManager; +use Drupal\facets\Processor\WidgetOrderProcessorInterface; use Drupal\facets\QueryType\QueryTypePluginManager; use Drupal\facets\Widget\WidgetPluginManager; @@ -291,14 +292,37 @@ class DefaultFacetManager { // @see \Drupal\facets\Processor\WidgetOrderProcessorInterface. $results = $facet->getResults(); + $active_sorts = []; foreach ($facet->getProcessorsByStage(ProcessorInterface::STAGE_BUILD) as $processor) { /** @var \Drupal\facets\Processor\BuildProcessorInterface $build_processor */ $build_processor = $this->processorPluginManager->createInstance($processor->getPluginDefinition()['id'], ['facet' => $facet]); - if (!$build_processor instanceof BuildProcessorInterface) { - throw new InvalidProcessorException(new FormattableMarkup("The processor @processor has a build definition but doesn't implement the required BuildProcessorInterface interface", ['@processor' => $processor->getPluginDefinition()['id']])); + if ($build_processor instanceof WidgetOrderProcessorInterface) { + // Sorting is handled last and together, to support nested sorts. + // @todo For some reason getProcessorsByStage does not return the + // correct configuration, thats why we use getProcessors() below. + $processors = $facet->getProcessors(); + $active_sorts[] = $processors[$build_processor->getPluginId()]; + } + else { + if (!$build_processor instanceof BuildProcessorInterface) { + throw new InvalidProcessorException(new FormattableMarkup("The processor @processor has a build definition but doesn't implement the required BuildProcessorInterface interface", ['@processor' => $processor->getPluginDefinition()['id']])); + } + $results = $build_processor->build($facet, $results); } - $results = $build_processor->build($facet, $results); } + uasort($results, function ($a, $b) use ($active_sorts) { + $return = 0; + foreach ($active_sorts as $sort) { + if ($return = $sort->sortResults($a, $b)) { + if ($sort->getConfiguration()['sort'] == 'DESC') { + $return *= -1; + } + break; + } + } + return $return; + }); + $facet->setResults($results); // No results behavior handling. Return a custom text or false depending on diff --git a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php index 93e5da0..750434d 100644 --- a/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/ActiveWidgetOrderProcessor.php @@ -24,35 +24,11 @@ class ActiveWidgetOrderProcessor extends WidgetOrderPluginBase implements Widget /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { - if ($order === 'ASC') { - usort($results, 'self::sortActiveAsc'); - } - else { - usort($results, 'self::sortActiveDesc'); - } - - return $results; - } - - /** - * Sorts ascending. - */ - protected static function sortActiveAsc(Result $a, Result $b) { + public function sortResults(Result $a, Result $b) { if ($a->isActive() == $b->isActive()) { return 0; } return ($a->isActive()) ? -1 : 1; } - /** - * Sorts descending. - */ - protected static function sortActiveDesc(Result $a, Result $b) { - if ($a->isActive() == $b->isActive()) { - return 0; - } - return ($a->isActive()) ? 1 : -1; - } - } diff --git a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php index 9093de0..1bface7 100644 --- a/src/Plugin/facets/processor/CountWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/CountWidgetOrderProcessor.php @@ -23,35 +23,11 @@ class CountWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetO /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { - if ($order === 'ASC') { - usort($results, 'self::sortCountAsc'); - } - else { - usort($results, 'self::sortCountDesc'); - } - - return $results; - } - - /** - * Sorts ascending. - */ - protected static function sortCountAsc(Result $a, Result $b) { + public function sortResults(Result $a, Result $b) { if ($a->getCount() == $b->getCount()) { return 0; } return ($a->getCount() < $b->getCount()) ? -1 : 1; } - /** - * Sorts descending. - */ - protected static function sortCountDesc(Result $a, Result $b) { - if ($a->getCount() == $b->getCount()) { - return 0; - } - return ($a->getCount() > $b->getCount()) ? -1 : 1; - } - } diff --git a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php index 085377c..7f5be4f 100644 --- a/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/DisplayValueWidgetOrderProcessor.php @@ -53,29 +53,13 @@ class DisplayValueWidgetOrderProcessor extends WidgetOrderPluginBase implements /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { - $transliteration = $this->transliteration; - - if ($order === 'ASC') { - // Sorts ascending. - usort($results, function (Result $a, Result $b) use ($transliteration) { - return strnatcasecmp( - $transliteration->removeDiacritics($a->getDisplayValue()), - $transliteration->removeDiacritics($b->getDisplayValue()) - ); - }); - } - else { - // Sorts descending. - usort($results, function (Result $a, Result $b) use ($transliteration) { - return strnatcasecmp( - $transliteration->removeDiacritics($b->getDisplayValue()), - $transliteration->removeDiacritics($a->getDisplayValue()) - ); - }); + public function sortResults(Result $a, Result $b) { + $a = $this->transliteration->removeDiacritics($a->getDisplayValue()); + $b = $this->transliteration->removeDiacritics($b->getDisplayValue()); + if ($a == $b) { + return 0; } - - return $results; + return strnatcasecmp($a, $b); } } diff --git a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php b/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php index 8868bc4..1417e93 100644 --- a/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/processor/RawValueWidgetOrderProcessor.php @@ -23,29 +23,8 @@ class RawValueWidgetOrderProcessor extends WidgetOrderPluginBase implements Widg /** * {@inheritdoc} */ - public function sortResults(array $results, $order = 'ASC') { - if ($order === 'ASC') { - usort($results, 'self::sortRawValueAsc'); - } - else { - usort($results, 'self::sortRawValueDesc'); - } - - return $results; - } - - /** - * Sorts ascending. - */ - protected static function sortRawValueAsc(Result $a, Result $b) { + public function sortResults(Result $a, Result $b) { return strnatcasecmp($a->getRawValue(), $b->getRawValue()); } - /** - * Sorts descending. - */ - protected static function sortRawValueDesc(Result $a, Result $b) { - return strnatcasecmp($b->getRawValue(), $a->getRawValue()); - } - } diff --git a/src/Processor/WidgetOrderProcessorInterface.php b/src/Processor/WidgetOrderProcessorInterface.php index 56f641e..f1f081f 100644 --- a/src/Processor/WidgetOrderProcessorInterface.php +++ b/src/Processor/WidgetOrderProcessorInterface.php @@ -1,6 +1,7 @@ createFacet($name, $id, 'keywords'); + + $values = [ + 'facet_sorting[display_value_widget_order][status]' => TRUE, + 'widget_configs[show_numbers]' => TRUE, + ]; + $this->drupalGet('admin/config/search/facets/' . $id . '/edit'); + $this->drupalPostForm(NULL, $values, $this->t('Save')); + + $expected_results = [ + 'apple', + 'banana', + 'grape', + 'orange', + 'strawberry', + ]; + + $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 . '/edit'); + $this->drupalPostForm(NULL, $values, $this->t('Save')); + + $expected_results = [ + 'banana', + 'apple', + 'strawberry', + 'grape', + 'orange', + ]; + + $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 . '/edit'); + $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', + 'apple', + 'strawberry', + 'grape', + 'orange', + ]; + + $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. */ protected function checkCountLimitProcessor() { diff --git a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php index 215e82d..21aaff3 100644 --- a/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/ActiveWidgetOrderProcessorTest.php @@ -2,12 +2,9 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; -use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\processor\ActiveWidgetOrderProcessor; -use Drupal\facets\Processor\ProcessorPluginManager; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Unit test for processor. @@ -19,7 +16,7 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -55,74 +52,31 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { } /** - * Tests sorting ascending. + * Tests sorting. */ - 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()); - } - } + public function testSorting() { + $sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]); + $this->assertEquals(1, $sort_value); - /** - * 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()); - } + $sort_value = $this->processor->sortResults($this->originalResults[1], $this->originalResults[2]); + $this->assertEquals(0, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[2], $this->originalResults[3]); + $this->assertEquals(0, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[3], $this->originalResults[4]); + $this->assertEquals(-1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[3], $this->originalResults[3]); + $this->assertEquals(0, $sort_value); } /** * Tests configuration. */ - public function testConfiguration() { + public function testDefaultConfiguration() { $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 index 3a75e3c..91427a1 100644 --- a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php @@ -2,12 +2,9 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; -use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\processor\CountWidgetOrderProcessor; -use Drupal\facets\Processor\ProcessorPluginManager; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Unit test for processor. @@ -19,7 +16,7 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -46,80 +43,25 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { } /** - * Tests sorting ascending. + * Tests sorting. */ - public function testAscending() { + public function testSorting() { + $sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]); + $this->assertEquals(1, $sort_value); - $sorted_results = $this->processor->sortResults($this->originalResults, 'ASC'); + $sort_value = $this->processor->sortResults($this->originalResults[1], $this->originalResults[2]); + $this->assertEquals(-1, $sort_value); - $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()); + $sort_value = $this->processor->sortResults($this->originalResults[2], $this->originalResults[2]); + $this->assertEquals(0, $sort_value); } /** * Tests configuration. */ - public function testConfiguration() { + public function testDefaultConfiguration() { $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 index 296ef8c..883ad13 100644 --- a/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/DisplayValueWidgetOrderProcessorTest.php @@ -3,12 +3,9 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; use Drupal\Component\Transliteration\TransliterationInterface; -use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\processor\DisplayValueWidgetOrderProcessor; -use Drupal\facets\Processor\ProcessorPluginManager; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Unit test for processor. @@ -20,7 +17,7 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -47,51 +44,44 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { new Result('2', '2', 22), ]; - $transliteration = $this - ->getMockBuilder(TransliterationInterface::class) + $transliteration = $this->getMockBuilder(TransliterationInterface::class) + ->disableOriginalConstructor() ->getMock(); - $transliteration->method('removeDiacritics')->willReturnCallback(function ($value) { - return str_replace('Ä', 'A', $value); - }); + $transliteration + ->expects($this->any()) + ->method('removeDiacritics') + ->will($this->returnArgument(0)); + $this->processor = new DisplayValueWidgetOrderProcessor([], 'display_value_widget_order', [], $transliteration); } /** - * Tests sorting ascending. + * Tests sorting. */ - 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()); - } - } + public function testSorting() { + $result_count = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]); + $this->assertEquals(-1, $result_count); - /** - * 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()); - } + $result_count = $this->processor->sortResults($this->originalResults[1], $this->originalResults[2]); + $this->assertEquals(1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[2], $this->originalResults[3]); + $this->assertEquals(1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[3], $this->originalResults[4]); + $this->assertEquals(1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[4], $this->originalResults[5]); + $this->assertEquals(1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[5], $this->originalResults[6]); + $this->assertEquals(1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[6], $this->originalResults[5]); + $this->assertEquals(-1, $result_count); + + $result_count = $this->processor->sortResults($this->originalResults[3], $this->originalResults[3]); + $this->assertEquals(0, $result_count); } /** @@ -101,65 +91,21 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { $original = [ new Result('bb_test', 'Test AA', 10), new Result('aa_test', 'Test BB', 10), - new Result('ab_test', 'Test ÄB', 10), ]; - $sorted_results = $this->processor->sortResults($original, 'DESC'); + $sorted_results = $this->processor->sortResults($original[0], $original[1]); + $this->assertEquals(-1, $sorted_results); - $this->assertEquals('Test BB', $sorted_results[0]->getDisplayValue()); - $this->assertEquals('Test ÄB', $sorted_results[1]->getDisplayValue()); - $this->assertEquals('Test AA', $sorted_results[2]->getDisplayValue()); + $sorted_results = $this->processor->sortResults($original[1], $original[0]); + $this->assertEquals(1, $sorted_results); } /** * Tests configuration. */ - public function testConfiguration() { + public function testDefaultConfiguration() { $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 index fcef922..833d1c5 100644 --- a/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php @@ -2,12 +2,9 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; -use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\facets\processor\RawValueWidgetOrderProcessor; -use Drupal\facets\Processor\ProcessorPluginManager; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; -use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Unit test for processor. @@ -19,7 +16,7 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -50,92 +47,37 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { } /** - * Tests sorting ascending. + * Tests sorting. */ - 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()); - } - } + public function testSorting() { + $sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]); + $this->assertEquals(1, $sort_value); - /** - * 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()); - } + $sort_value = $this->processor->sortResults($this->originalResults[1], $this->originalResults[2]); + $this->assertEquals(1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[2], $this->originalResults[3]); + $this->assertEquals(-1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[3], $this->originalResults[4]); + $this->assertEquals(-1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[4], $this->originalResults[5]); + $this->assertEquals(-1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[5], $this->originalResults[6]); + $this->assertEquals(1, $sort_value); + + $sort_value = $this->processor->sortResults($this->originalResults[3], $this->originalResults[3]); + $this->assertEquals(0, $sort_value); } /** * Tests configuration. */ - public function testConfiguration() { + public function testDefaultConfiguration() { $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()); - } - }