Commit 63aa103b authored by mr.baileys's avatar mr.baileys Committed by borisson_

Issue #2596365 by mr.baileys: Create the "facet active" widget order processor

parent 39a98bf6
......@@ -31,6 +31,17 @@ facetapi.processor.plugin.raw_value_widget_order:
type: string
label: sort order
facetapi.processor.plugin.active_widget_order:
type: mapping
label: 'Active widget order'
mapping:
enabled:
type: boolean
label: 'Is the widget enabled?'
sort:
type: string
label: sort order
facetapi.processor.plugin.minimum_count:
type: mapping
label: 'Minimum count widget'
......
<?php
namespace Drupal\facetapi\Plugin\facetapi\processor;
use Drupal\facetapi\Processor\WidgetOrderPluginBase;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
/**
* @FacetApiProcessor(
* id = "active_widget_order",
* label = @Translation("Sort by active state"),
* description = @Translation("Sorts the widget results by active state."),
* stages = {
* "build" = 50
* }
* )
*/
class ActiveWidgetOrderProcessor extends WidgetOrderPluginBase implements WidgetOrderProcessorInterface {
/**
* {@inheritdoc}
*/
public function sortResults(array $results, $order = 'ASC') {
if ($order === 'ASC') {
usort($results, 'self::sortActiveAsc');
}
else {
usort($results, 'self::sortActiveDesc');
}
return $results;
}
protected static function sortActiveAsc(Result $a, Result $b) {
if ($a->isActive() == $b->isActive()) {
return 0;
}
return ($a->isActive()) ? -1 : 1;
}
protected static function sortActiveDesc(Result $a, Result $b) {
if ($a->isActive() == $b->isActive()) {
return 0;
}
return ($a->isActive()) ? 1 : -1;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\Processor\ActiveWidgetOrderProcessorTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
use Drupal\facetapi\Plugin\facetapi\processor\ActiveWidgetOrderProcessor;
use Drupal\facetapi\Processor\WidgetOrderProcessorInterface;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* @group facetapi
*/
class ActiveWidgetOrderProcessorTest 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('Boxer', 'Boxer', 10),
new Result('Old Major', 'Old Major', 3),
new Result('Minimus', 'Minimus', 60),
new Result('Mr Whymper', 'Mr. Whymper', 1),
new Result('Clover', 'Clover', 50),
];
$this->original_results[1]->setActiveState(true);
$this->original_results[2]->setActiveState(true);
$this->original_results[3]->setActiveState(true);
$this->processor = new ActiveWidgetOrderProcessor([], 'active_widget_order', []);
}
/**
* Test sorting ascending.
*/
public function testAscending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'ASC');
$expected_values = [true, true, true, false, false];
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->isActive());
}
}
/**
* Test sorting descending.
*/
public function testDescending() {
$sorted_results = $this->processor->sortResults($this->original_results, 'DESC');
$expected_values = array_reverse([true, true, true, false, false]);
foreach ($expected_values as $index => $value) {
$this->assertEquals($value, $sorted_results[$index]->isActive());
}
}
}
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