diff --git a/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php index 76bbf72..207ce67 100644 --- a/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/ActiveWidgetOrderProcessor.php @@ -34,11 +34,8 @@ class ActiveWidgetOrderProcessor extends SortProcessorPluginBase { if ($order === 'ASC') { return self::sortActiveAsc($a, $b); } - else { - return self::sortActiveDesc($a, $b); - } - return 0; + return self::sortActiveDesc($a, $b); } /** diff --git a/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php index b24d16c..8336665 100644 --- a/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/CountWidgetOrderProcessor.php @@ -34,11 +34,8 @@ class CountWidgetOrderProcessor extends SortProcessorPluginBase { if ($order === 'ASC') { return self::sortCountAsc($a, $b); } - else { - return self::sortCountDesc($a, $b); - } - return 0; + return self::sortCountDesc($a, $b); } /** diff --git a/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php index 25d92b0..00be9cb 100644 --- a/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/DisplayValueWidgetOrderProcessor.php @@ -34,11 +34,8 @@ class DisplayValueWidgetOrderProcessor extends SortProcessorPluginBase { if ($order === 'ASC') { return self::sortDisplayValueAsc($a, $b); } - else { - return self::sortDisplayValueDesc($a, $b); - } - return 0; + return self::sortDisplayValueDesc($a, $b); } /** diff --git a/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php b/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php index bd11443..87600ec 100644 --- a/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php +++ b/src/Plugin/facets/sort_processor/RawValueWidgetOrderProcessor.php @@ -34,11 +34,8 @@ class RawValueWidgetOrderProcessor extends SortProcessorPluginBase { if ($order === 'ASC') { return self::sortRawValueAsc($a, $b); } - else { - return self::sortRawValueDesc($a, $b); - } - return 0; + return self::sortRawValueDesc($a, $b); } /** diff --git a/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php b/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php index be16db2..eadc149 100644 --- a/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/sort_processor/ActiveWidgetOrderProcessorTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\facets\Unit\Plugin\sort_processor; +use Drupal\Component\Render\FormattableMarkup; use Drupal\facets\Plugin\facets\sort_processor\ActiveWidgetOrderProcessor; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; @@ -19,13 +20,6 @@ use Drupal\Tests\UnitTestCase; class ActiveWidgetOrderProcessorTest extends UnitTestCase { /** - * The processor to be tested. - * - * @var \Drupal\facets\SortProcessor\SortProcessorInterface - */ - protected $processor; - - /** * An array containing the results before the processor has ran. * * @var \Drupal\facets\Result\Result[] @@ -33,7 +27,7 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { protected $originalResults; /** - * Creates a new processor object for use in the tests. + * {@inheritdoc} */ protected function setUp() { parent::setUp(); @@ -52,18 +46,20 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { $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]; + $processor = new ActiveWidgetOrderProcessor(['sort' => 'ASC'], 'active_widget_order', []); + $expected_values = [0, 1, 0, 0, -1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->isActive()); + 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); + } } } @@ -71,10 +67,14 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { * Tests sorting descending. */ public function testDescending() { - $sorted_results = $this->processor->sortResults($this->originalResults, 'DESC'); - $expected_values = array_reverse([TRUE, TRUE, TRUE, FALSE, FALSE]); + $processor = new ActiveWidgetOrderProcessor(['sort' => 'DESC'], 'active_widget_order', []); + $expected_values = [0, -1, 0, 0, 1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->isActive()); + 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); + } } } @@ -82,7 +82,8 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase { * Tests configuration. */ public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); + $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 index 7de07e4..3786d83 100644 --- a/tests/src/Unit/Plugin/sort_processor/CountWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/sort_processor/CountWidgetOrderProcessorTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\facets\Unit\Plugin\sort_processor; +use Drupal\Component\Render\FormattableMarkup; use Drupal\facets\Plugin\facets\sort_processor\CountWidgetOrderProcessor; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; @@ -19,13 +20,6 @@ use Drupal\Tests\UnitTestCase; class CountWidgetOrderProcessorTest extends UnitTestCase { /** - * The processor to be tested. - * - * @var \Drupal\facets\SortProcessor\SortProcessorInterface - */ - protected $processor; - - /** * An array containing the results before the processor has ran. * * @var \Drupal\facets\Result\Result[] @@ -33,7 +27,7 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { protected $originalResults; /** - * Creates a new processor object for use in the tests. + * {@inheritdoc} */ protected function setUp() { parent::setUp(); @@ -43,45 +37,44 @@ class CountWidgetOrderProcessorTest extends UnitTestCase { 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()); + $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() { - - $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()); + $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() { - $config = $this->processor->defaultConfiguration(); + $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 index 29e8c41..449156f 100644 --- a/tests/src/Unit/Plugin/sort_processor/DisplayValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/sort_processor/DisplayValueWidgetOrderProcessorTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; +use Drupal\Component\Render\FormattableMarkup; use Drupal\facets\Plugin\facets\sort_processor\DisplayValueWidgetOrderProcessor; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; @@ -19,13 +20,6 @@ use Drupal\Tests\UnitTestCase; class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { /** - * The processor to be tested. - * - * @var \Drupal\facets\SortProcessor\SortProcessorInterface - */ - protected $processor; - - /** * An array containing the results before the processor has ran. * * @var \Drupal\facets\Result\Result[] @@ -33,7 +27,7 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { protected $originalResults; /** - * Creates a new processor object for use in the tests. + * {@inheritdoc} */ protected function setUp() { parent::setUp(); @@ -41,32 +35,26 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { $this->originalResults = [ new Result('thetans', 'thetans', 10), new Result('xenu', 'xenu', 5), - new Result('Tom', 'Tom', 15), new Result('Hubbard', 'Hubbard', 666), + new Result('Tom', 'Tom', 15), new Result('FALSE', 'FALSE', 1), - new Result('1977', '1977', 20), new Result('2', '2', 22), + new Result('1977', '1977', 20), ]; - - $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', - ]; + $processor = new DisplayValueWidgetOrderProcessor(['sort' => 'ASC'], 'display_value_widget_order', []); + $expected_values = [0, -1, 1, -1, 1, 1, -1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); + 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); + } } } @@ -74,18 +62,14 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { * 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', - ]); + $processor = new DisplayValueWidgetOrderProcessor(['sort' => 'DESC'], 'display_value_widget_order', []); + $expected_values = [0, 1, -1, 1, -1, -1, 1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); + 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); + } } } @@ -98,17 +82,17 @@ class DisplayValueWidgetOrderProcessorTest extends UnitTestCase { 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()); + $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() { - $config = $this->processor->defaultConfiguration(); + $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 index 3c8e459..2be23eb 100644 --- a/tests/src/Unit/Plugin/sort_processor/RawValueWidgetOrderProcessorTest.php +++ b/tests/src/Unit/Plugin/sort_processor/RawValueWidgetOrderProcessorTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\facets\Unit\Plugin\processor; +use Drupal\Component\Render\FormattableMarkup; use Drupal\facets\Plugin\facets\sort_processor\RawValueWidgetOrderProcessor; use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; @@ -19,13 +20,6 @@ use Drupal\Tests\UnitTestCase; class RawValueWidgetOrderProcessorTest extends UnitTestCase { /** - * The processor to be tested. - * - * @var \Drupal\facets\SortProcessor\SortProcessorInterface - */ - protected $processor; - - /** * An array containing the results before the processor has ran. * * @var \Drupal\facets\Result\Result[] @@ -33,7 +27,7 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { protected $originalResults; /** - * Creates a new processor object for use in the tests. + * {@inheritdoc} */ protected function setUp() { parent::setUp(); @@ -47,26 +41,20 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { 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', - ]; + $processor = new RawValueWidgetOrderProcessor(['sort' => 'ASC'], 'raw_value_widget_order', []); + $expected_values = [0, 1, 1, -1, -1, -1, 1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); + 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); + } } } @@ -74,18 +62,14 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { * 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', - ]); + $processor = new RawValueWidgetOrderProcessor(['sort' => 'DESC'], 'raw_value_widget_order', []); + $expected_values = [0, -1, -1, 1, 1, 1, -1]; foreach ($expected_values as $index => $value) { - $this->assertEquals($value, $sorted_results[$index]->getDisplayValue()); + 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); + } } } @@ -93,7 +77,8 @@ class RawValueWidgetOrderProcessorTest extends UnitTestCase { * Tests configuration. */ public function testConfiguration() { - $config = $this->processor->defaultConfiguration(); + $processor = new RawValueWidgetOrderProcessor([], 'raw_value_widget_order', []); + $config = $processor->defaultConfiguration(); $this->assertEquals(['sort' => 'ASC'], $config); }