Commit 4b8eff1b authored by danquah's avatar danquah Committed by borisson_

Issue #2833340 by danquah: Sort term-based facets by term weight

parent b48b467c
<?php
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\facets\Processor\SortProcessorPluginBase;
use Drupal\facets\Result\Result;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* A processor that orders the term-results by their weight.
*
* @FacetsProcessor(
* id = "term_weight_widget_order",
* label = @Translation("Sort by taxonomy term weight"),
* description = @Translation("Sorts the widget results by taxonomy term weight. This sort is only applicable for term-based facets."),
* stages = {
* "sort" = 60
* }
* )
*/
class TermWeightWidgetOrderProcessor extends SortProcessorPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function sortResults(Result $a, Result $b) {
$ids = [$a->getRawValue(), $b->getRawValue()];
$entities = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadMultiple($ids);
// Bail out if we could not load the term (eg. if the result is not
// referring to a term).
if (empty($entities[$a->getRawValue()]) || empty($entities[$b->getRawValue()])) {
return 0;
}
/** @var \Drupal\taxonomy\Entity\Term $term_a */
$term_a = $entities[$a->getRawValue()];
/** @var \Drupal\taxonomy\Entity\Term $term_b */
$term_b = $entities[$b->getRawValue()];
if ($term_a->getWeight() === $term_b->getWeight()) {
return 0;
}
return ($term_a->getWeight() < $term_b->getWeight()) ? -1 : 1;
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\Plugin\facets\processor\TermWeightWidgetOrderProcessor;
use Drupal\facets\Result\Result;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for processor.
*
* @group facets
*/
class TermWeightWidgetOrderProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\Processor\SortProcessorInterface
*/
protected $processor;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* Mocked term, used for comparison.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $termA;
/**
* Mocked term, used for comparison.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $termB;
/**
* Mocked entity (term) storage.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $termStorage;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
// Build up a chain of mocks that we will have the processor use to fetch
// the weight of the terms that are being compared.
$this->termStorage = $this->getMockBuilder(EntityStorageInterface::class)
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager = $this->getMockBuilder(EntityTypeManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager->expects($this->any())
->method('getStorage')
->willReturn($this->termStorage);
// Instantiate the processor and load it up with our mock chain.
$this->processor = new TermWeightWidgetOrderProcessor([], 'term_weight_widget_order', [], $this->entityTypeManager);
// Setup two mock terms that will be set up to have specific weights before
// the processor is used to compare them.
// The mocks are used in the individual tests.
$this->termA = $this->getMockBuilder(Term::class)
->disableOriginalConstructor()
->getMock();
$this->termB = $this->getMockBuilder(Term::class)
->disableOriginalConstructor()
->getMock();
// Prepare the terms that will be returned when the processor loads its list
// of term-ids from the Results raw values.
$terms = [
1 => $this->termA,
2 => $this->termB,
];
// Setup the termStorage mock to return our terms. As we keep a reference to
// the terms via $this the individual tests can set up the weights later.
$this->termStorage->expects($this->any())
->method('loadMultiple')
->willReturn($terms);
// Prepare the results that we use the processor to sort, the raw_value has
// to match the term_id keys used above in $terms. Display_value and count
// is not used.
$this->originalResults = [
new Result(1, 10, 100),
new Result(2, 20, 200),
];
}
/**
* Tests that sorting two terms of equal weight yields 0.
*/
public function testEqual() {
$this->termA->expects($this->any())
->method('getWeight')
->willReturn('1');
$this->termB->expects($this->any())
->method('getWeight')
->willReturn('1');
$sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]);
$this->assertEquals(0, $sort_value);
}
/**
* Compare a term with a high weight with a term with a low.
*/
public function testHigher() {
$this->termA->expects($this->any())
->method('getWeight')
->willReturn('10');
$this->termB->expects($this->any())
->method('getWeight')
->willReturn('-10');
$sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]);
$this->assertGreaterThan(0, $sort_value);
}
/**
* Compare a term with a low weight with a term with a high.
*/
public function testLow() {
$this->termA->expects($this->any())
->method('getWeight')
->willReturn('-10');
$this->termB->expects($this->any())
->method('getWeight')
->willReturn('10');
// Compare the two values and check the result with an assertion.
$sort_value = $this->processor->sortResults($this->originalResults[0], $this->originalResults[1]);
$this->assertLessThan(0, $sort_value);
}
}
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