Commit ba1d5de3 authored by borisson_'s avatar borisson_

Issue #2608228: Add tests for widgets

parent b7040f3f
......@@ -22,6 +22,11 @@ class CheckboxWidget implements WidgetInterface {
use StringTranslationTrait;
/**
* @var \Drupal\Core\Utility\LinkGeneratorInterface $linkGenerator
*/
protected $linkGenerator;
/**
* {@inheritdoc}
*/
......@@ -33,7 +38,7 @@ class CheckboxWidget implements WidgetInterface {
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
/** @var Result[] $results */
/** @var \Drupal\facetapi\Result\Result[] $results */
$results = $facet->getResults();
$items = [];
foreach ($results as $result) {
......@@ -43,8 +48,7 @@ class CheckboxWidget implements WidgetInterface {
if ($result->isActive()) {
$text = '(-) ' . $text;
}
$link_generator = \Drupal::linkGenerator();
$link = $link_generator->generate($text, $result->getUrl());
$link = $this->linkGenerator()->generate($text, $result->getUrl());
$items[] = $link;
}
}
......@@ -91,4 +95,15 @@ class CheckboxWidget implements WidgetInterface {
return $query_types['string'];
}
/**
* Gets the link generator.
*
* @return \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected function linkGenerator() {
if (!isset($this->linkGenerator)) {
$this->linkGenerator = \Drupal::linkGenerator();
}
return $this->linkGenerator;
}
}
......@@ -21,6 +21,11 @@ use Drupal\facetapi\Widget\WidgetInterface;
*/
class LinksWidget implements WidgetInterface {
/**
* @var \Drupal\Core\Utility\LinkGeneratorInterface $linkGenerator
*/
protected $linkGenerator;
/**
* {@inheritdoc}
*/
......@@ -32,7 +37,7 @@ class LinksWidget implements WidgetInterface {
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
/** @var Result[] $results */
/** @var \Drupal\facetapi\Result\Result[] $results */
$results = $facet->getResults();
$items = [];
foreach ($results as $result) {
......@@ -44,15 +49,14 @@ class LinksWidget implements WidgetInterface {
}
if (is_null($result->getUrl())) {
$link = $text;
$items[] = $text;
}
else {
$link_generator = \Drupal::linkGenerator();
$link = $link_generator->generate($text, $result->getUrl());
$items[] = $this->linkGenerator()->generate($text, $result->getUrl());
}
$items[] = $link;
}
}
$build = [
'#theme' => 'item_list',
'#items' => $items,
......@@ -74,4 +78,16 @@ class LinksWidget implements WidgetInterface {
return $query_types['string'];
}
/**
* Gets the link generator.
*
* @return \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected function linkGenerator() {
if (!isset($this->linkGenerator)) {
$this->linkGenerator = \Drupal::linkGenerator();
}
return $this->linkGenerator;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\widget\CheckboxWidgetTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\widget;
use Drupal\facetapi\Entity\Facet;
use Drupal\facetapi\Plugin\facetapi\Widget\CheckboxWidget;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @group facetapi
*/
class CheckboxWidgetTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \drupal\facetapi\Widget\WidgetInterface
*/
protected $widget;
/**
* 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();
/** @var \Drupal\facetapi\Result\Result[] $original_results */
$original_results = [
new Result('llama', 'Llama', 10),
new Result('badger', 'Badger', 20),
new Result('duck', 'Duck', 15),
new Result('alpaca', 'Alpaca', 9),
];
foreach ($original_results as $original_result) {
$original_result->setUrl(new \Drupal\Core\Url('test'));
}
$this->original_results = $original_results;
$link_generator = $this->getMockBuilder('\Drupal\Core\Utility\LinkGenerator')
->disableOriginalConstructor()
->getMock();
$link_generator->expects($this->atLeastOnce())
->method('generate')
->will($this->returnArgument(0));
$string_translation = $this->getMockBuilder('\Drupal\Core\StringTranslation\TranslationManager')
->disableOriginalConstructor()
->getMock();
$container_builder = new ContainerBuilder();
$container_builder->set('link_generator', $link_generator);
$container_builder->set('string_translation', $string_translation);
\Drupal::setContainer($container_builder);
$this->widget = new CheckboxWidget();
}
/**
* Test widget
*/
public function testNoFilterResults() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['#items']);
$expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\facetapi\Plugin\widget\LinksWidgetTest.
*/
namespace Drupal\Tests\facetapi\Unit\Plugin\widget;
use Drupal\facetapi\Entity\Facet;
use Drupal\facetapi\Plugin\facetapi\Widget\LinksWidget;
use Drupal\facetapi\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @group facetapi
*/
class LinksWidgetTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \drupal\facetapi\Widget\WidgetInterface
*/
protected $widget;
/**
* 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();
/** @var \Drupal\facetapi\Result\Result[] $original_results */
$original_results = [
new Result('llama', 'Llama', 10),
new Result('badger', 'Badger', 20),
new Result('duck', 'Duck', 15),
new Result('alpaca', 'Alpaca', 9),
];
foreach ($original_results as $original_result) {
$original_result->setUrl(new \Drupal\Core\Url('test'));
}
$this->original_results = $original_results;
$link_generator = $this->getMockBuilder('\Drupal\Core\Utility\LinkGenerator')
->disableOriginalConstructor()
->getMock();
$link_generator->expects($this->atLeastOnce())
->method('generate')
->will($this->returnArgument(0));
$container_builder = new ContainerBuilder();
$container_builder->set('link_generator', $link_generator);
\Drupal::setContainer($container_builder);
$this->widget = new LinksWidget();
}
/**
* Test widget
*/
public function testNoFilterResults() {
$facet = new Facet([], 'facet');
$facet->setResults($this->original_results);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['#items']);
$expected_links = ['Llama (10)', 'Badger (20)', 'Duck (15)', 'Alpaca (9)'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
}
/**
* Test widget
*/
public function testHideEmptyCount() {
$original_results = $this->original_results;
$original_results[1] = new Result('badger', 'Badger', 0);
$facet = new Facet([], 'facet');
$facet->setResults($original_results);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(3, $output['#items']);
$expected_links = ['Llama (10)', 'Duck (15)', 'Alpaca (9)'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
}
/**
* Test widget
*/
public function testActiveItems() {
$original_results = $this->original_results;
$original_results[0]->setActiveState(TRUE);
$original_results[3]->setActiveState(TRUE);
$facet = new Facet([], 'facet');
$facet->setResults($original_results);
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['#items']);
$expected_links = ['(-) Llama (10)', 'Badger (20)', 'Duck (15)', '(-) Alpaca (9)'];
foreach ($expected_links as $index => $value) {
$this->assertEquals($value, $output['#items'][$index]);
}
}
}
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