Commit 5eca7fe0 authored by michiellucas's avatar michiellucas Committed by borisson_
Browse files

Issue #2648820 by michiellucas, borisson_: Taxonomy facet - translation

parent 6a4c1835
<?php
/**
* @file
* Contains \Drupal\facets\Plugin\facets\TranslateTaxonomyProcessor.
*/
namespace Drupal\facets\Plugin\facets\processor;
use Drupal\facets\FacetInterface;
use Drupal\facets\Processor\BuildProcessorInterface;
use Drupal\facets\Processor\ProcessorPluginBase;
use Drupal\taxonomy\Entity\Term;
/**
* Provides a processor that transforms the results to show the translated term.
*
* @FacetsProcessor(
* id = "translate_taxonomy",
* label = @Translation("Translate taxonomy terms"),
* description = @Translation("Translate the taxonomy terms"),
* stages = {
* "build" = 5
* }
* )
*/
class TranslateTaxonomyProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet, array $results) {
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = Term::load($result->getRawValue());
if ($term->hasTranslation($language_interface->getId())) {
$term_trans = $term->getTranslation($language_interface->getId());
$result->setDisplayValue($term_trans->getName());
}
else {
$result->setDisplayValue($term->getName());
}
}
return $results;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facets\Plugin\Processor\TranslateTaxonomyTermTest.
*/
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\Core\Language\Language;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\TranslateTaxonomyProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Unit test for processor.
*
* @group facets
*/
class TranslateTaxonomyProcessorTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\processor\WidgetOrderProcessorInterface
*/
protected $processor;
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->processor = new TranslateTaxonomyProcessor([], 'translate_taxonomy', []);
$this->createMocks();
}
/**
* Tests that results were correctly changed.
*/
public function testResultsChanged() {
/** @var \Drupal\facets\Result\ResultInterface[] $original_results */
$original_results = [
new Result(1, 1, 5),
];
$facet = new Facet([], 'facet');
$facet->setResults($original_results);
$expected_results = [
['tid' => 1, 'name' => 'Burrowing owl'],
];
foreach ($expected_results as $key => $expected) {
$this->assertEquals($expected['tid'], $original_results[$key]->getRawValue());
$this->assertEquals($expected['tid'], $original_results[$key]->getDisplayValue());
}
$filtered_results = $this->processor->build($facet, $original_results);
foreach ($expected_results as $key => $expected) {
$this->assertEquals($expected['tid'], $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() {
$term = $this->getMockBuilder('\Drupal\taxonomy\Entity\Term')
->disableOriginalConstructor()
->getMock();
$term->expects($this->any())
->method('getName')
->willReturn('Burrowing owl');
$term_storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$term_storage->expects($this->any())
->method('load')
->willReturn($term);
$entity_manager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->any())
->method('getStorage')
->willReturn($term_storage);
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$language = new Language(['langcode' => 'en']);
$language_manager->expects($this->any())
->method('getCurrentLanguage')
->will($this->returnValue($language));
$container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager);
$container->set('language_manager', $language_manager);
\Drupal::setContainer($container);
}
/**
* Tests configuration.
*/
public function testConfiguration() {
$config = $this->processor->defaultConfiguration();
$this->assertEquals([], $config);
}
}
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