Commit b4682da3 authored by mr.baileys's avatar mr.baileys

Issue #2596371 by mr.baileys: Create the "indexed value" widget order processor

parent ce6740f0
<?php
namespace Drupal\facetapi\Plugin\facetapi\processor;
use Drupal\facetapi\Processor\WidgetOrderPluginBase;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
/**
* @FacetApiProcessor(
* id = "raw_value_widget_order",
* label = @Translation("Sort by raw value"),
* description = @Translation("Sorts the widget results by raw value."),
* stages = {
* "build" = 50
* }
* )
*/
class RawValueWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface {
/**
* {@inheritdoc}
*/
public function sortResults(array $results, $order = 'ASC') {
if ($order === 'ASC') {
usort($results, 'self::sortRawValueAsc');
}
else {
usort($results, 'self::sortRawValueDesc');
}
return $results;
}
protected static function sortRawValueAsc(Result $a, Result $b) {
return strnatcasecmp($a->getRawValue(), $b->getRawValue());
}
protected static function sortRawValueDesc(Result $a, Result $b) {
return strnatcasecmp($b->getRawValue(), $a->getRawValue());
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\Processor\RawValueWidgetOrderProcessorTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
use Drupal\facetapi\Plugin\facetapi\processor\RawValueWidgetOrderProcessor;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* @group facetapi
*/
class RawValueWidgetOrderProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var WidgetOrderProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facetapi\Result\Result[]
*/
protected $original_results;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->original_results = [
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', []);
}
/**
* Test sorting ascending.
*/
public function testAscending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'ASC');
$expected_values = array('Tom', 'xenu', 'thetans', 'Hubbard', 'FALSE', '2', '1977');
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->getDisplayValue());
}
}
/**
* Test sorting descending.
*/
public function testDescending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'DESC');
$expected_values = array_reverse(array('Tom', 'xenu', 'thetans', 'Hubbard', 'FALSE', '2', '1977'));
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->getDisplayValue());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment