Commit c3cf0717 authored by borisson_'s avatar borisson_

Issue #2616590 by borisson_: Uid to username processor (Build processor) for users

parent 8027f943
<?php
/**
* @file
* Contains \Drupal\facetapi\Plugin\facetapi\UidToUserNameCallbackProcessor.
*/
namespace Drupal\facetapi\Plugin\facetapi\processor;
use Drupal\Core\Entity\Entity;
use Drupal\facetapi\FacetInterface;
use Drupal\facetapi\Processor\BuildProcessorInterface;
use Drupal\facetapi\Processor\ProcessorPluginBase;
use Drupal\user\Entity\User;
/**
* Provides a processor that transforms the results to show the user's name
*
* @FacetApiProcessor(
* id = "uid_to_username_callback",
* label = @Translation("uid to username callback"),
* description = @Translation("Transform the uid to user name in facet results"),
* stages = {
* "build" = 5
* }
* )
*/
class UidToUserNameCallbackProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
/** @var \Drupal\facetapi\Result\ResultInterface $result */
foreach ($results as &$result) {
/** @var \Drupal\user\Entity\User $user */
$user = User::load($result->getRawValue());
$result->setDisplayValue($user->getDisplayName());
}
return $results;
}
}
......@@ -106,4 +106,11 @@ class Result implements ResultInterface {
return $this->active;
}
/**
* {@inheritdoc}
*/
public function setDisplayValue($display_value) {
$this->display_value = $display_value;
}
}
......@@ -63,4 +63,12 @@ interface ResultInterface {
*/
public function isActive();
/**
* Override the display value of a result.
*
* @param string $display_value
* Override display value
*/
public function setDisplayValue($display_value);
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\Processor\UidToUserNameCallbackProcessorTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\Processor;
use Drupal\facetapi\Entity\Facet;
use Drupal\facetapi\Plugin\facetapi\processor\UidToUserNameCallbackProcessor;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @group facetapi
*/
class UidToUserNameCallbackProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facetapi\processor\WidgetOrderProcessorInterface
*/
protected $processor;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->processor = new UidToUserNameCallbackProcessor([], 'uid_to_username_callback', []);
$this->createMocks();
}
/**
* Test that results were correctly changed
*/
public function testResultsChanged() {
$original_results = [
new Result(1, 1, 5),
];
$facet = new Facet([], 'facet');
$facet->setResults($original_results);
$expected_results = [
['uid' => 1, 'name' => 'Admin'],
];
foreach ($expected_results as $key => $expected) {
$this->assertEquals($expected['uid'], $original_results[$key]->getRawValue());
$this->assertEquals($expected['uid'], $original_results[$key]->getDisplayValue());
}
$filtered_results = $this->processor->build($facet, $original_results);
foreach ($expected_results as $key => $expected) {
$this->assertEquals($expected['uid'], $filtered_results[$key]->getRawValue());
$this->assertEquals($expected['name'], $filtered_results[$key]->getDisplayValue());
}
}
/**
* Creates and sets up the container to be used in tests.
*/
protected function createMocks() {
$userStorage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$entityManager->expects($this->any())
->method('getStorage')
->willReturn($userStorage);
$user1 = $this->getMock('\Drupal\Core\Session\AccountInterface');
$user1->method('getDisplayName')
->willReturn('Admin');
$userStorage->method('load')
->willReturn($user1);
$container = new ContainerBuilder();
$container->set('entity.manager', $entityManager);
\Drupal::setContainer($container);
}
}
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