diff --git a/src/Tests/ExampleContentTrait.php b/src/Tests/ExampleContentTrait.php index 1cfd0ea..c2041b0 100644 --- a/src/Tests/ExampleContentTrait.php +++ b/src/Tests/ExampleContentTrait.php @@ -103,8 +103,8 @@ trait ExampleContentTrait { * Another string. */ protected function assertStringPosition($x, $y) { - $this->assertRaw($x); - $this->assertRaw($y); + $this->assertText($x); + $this->assertText($y); $x_position = strpos($this->getRawContent(), $x); $y_position = strpos($this->getRawContent(), $y); diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index 02f0815..c7bca48 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -129,6 +129,88 @@ class ProcessorIntegrationTest extends WebTestBase { } /** + * Tests sorting of results. + */ + public function testResultSorting() { + $id = 'burrowing_owl'; + $name = 'Burrowing owl'; + + $this->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..2ff8ab8 100644 --- a/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/CountWidgetOrderProcessorTest.php @@ -2,9 +2,7 @@ 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; @@ -19,7 +17,7 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -46,80 +44,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..9ef9f51 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,53 +44,47 @@ 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); } + /** * Tests that sorting uses the display value. */ @@ -101,65 +92,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..95409f9 100644 --- a/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/RawValueWidgetOrderProcessorTest.php @@ -2,24 +2,22 @@ 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. * * @group facets */ + class RawValueWidgetOrderProcessorTest extends UnitTestCase { /** * The processor to be tested. * - * @var \Drupal\facets\processor\WidgetOrderProcessorInterface + * @var \Drupal\facets\Processor\WidgetOrderProcessorInterface */ protected $processor; @@ -50,92 +48,36 @@ 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()); - } - }