Commit 6a54f9b3 authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2838236 by borisson_, ruloweb: Add widget unit test base + basic coverage for all widgets

parent 98081e4e
<?php
namespace Drupal\spin_rest\Plugin\facets\widget;
namespace Drupal\facets\Plugin\facets\widget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetInterface;
......
......@@ -2,67 +2,22 @@
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\Core\Field\WidgetPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\ArrayWidget;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Unit test for widget.
*
* @group facets
*/
class ArrayWidgetTest extends UnitTestCase {
class ArrayWidgetTest extends WidgetTestBase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\Widget\WidgetPluginInterface
*/
protected $widget;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* Creates a new processor object for use in the tests.
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
/** @var \Drupal\facets\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 Url('test'));
}
$this->originalResults = $original_results;
// Creates a mocked container, so we can access string translation.
$container = $this->prophesize(ContainerInterface::class);
$string_translation = $this->prophesize(TranslationInterface::class);
$url_generator = $this->prophesize(UrlGeneratorInterface::class);
$widget_manager = $this->prophesize(WidgetPluginManager::class);
$container->get('plugin.manager.facets.widget')->willReturn($widget_manager->reveal());
$container->get('string_translation')->willReturn($string_translation->reveal());
$container->get('url_generator')->willReturn($url_generator->reveal());
\Drupal::setContainer($container->reveal());
$this->widget = new ArrayWidget(['show_numbers' => 1]);
}
......@@ -92,4 +47,15 @@ class ArrayWidgetTest extends UnitTestCase {
}
}
/**
* {@inheritdoc}
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$expected = [
'show_numbers' => FALSE,
];
$this->assertEquals($expected, $default_config);
}
}
......@@ -2,32 +2,15 @@
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\CheckboxWidget;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for widget.
*
* @group facets
*/
class CheckboxWidgetTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\Plugin\facets\widget\CheckboxWidget
*/
protected $widget;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
class CheckboxWidgetTest extends WidgetTestBase {
/**
* Creates a new processor object for use in the tests.
......@@ -35,19 +18,6 @@ class CheckboxWidgetTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
/** @var \Drupal\facets\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 Url('test'));
}
$this->originalResults = $original_results;
$this->widget = new CheckboxWidget(['show_numbers' => TRUE]);
}
......@@ -80,37 +50,4 @@ class CheckboxWidgetTest extends UnitTestCase {
}
}
/**
* Tests default configuration.
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$this->assertEquals(['show_numbers' => FALSE, 'soft_limit' => 0], $default_config);
}
/**
* Build a formattable markup object to use in the other tests.
*
* @param string $text
* Text to display.
* @param int $count
* Number of results.
* @param bool $active
* Link is active.
* @param bool $show_numbers
* Numbers are displayed.
*
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
'#is_active' => $active,
];
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\DateArrayWidget;
/**
* Unit test for widget.
*
* @group facets
*/
class DateArrayWidgetTest extends WidgetTestBase {
/**
* Creates a new processor object for use in the tests.
*/
protected function setUp() {
parent::setUp();
$this->widget = new DateArrayWidget(['show_numbers' => 1]);
}
/**
* Tests widget without filters.
*/
public function testNoFilterResults() {
$facet = new Facet([], 'facets_facet');
$facet->setResults($this->originalResults);
$facet->setFieldIdentifier('tag');
$output = $this->widget->build($facet);
$this->assertInternalType('array', $output);
$this->assertCount(4, $output['tag']);
$expected_links = [
['url' => NULL, 'values' => ['value' => 'Llama', 'count' => 10]],
['url' => NULL, 'values' => ['value' => 'Badger', 'count' => 20]],
['url' => NULL, 'values' => ['value' => 'Duck', 'count' => 15]],
['url' => NULL, 'values' => ['value' => 'Alpaca', 'count' => 9]],
];
foreach ($expected_links as $index => $value) {
$this->assertInternalType('array', $output['tag'][$index]);
$this->assertEquals($value['values']['value'], $output['tag'][$index]['values']['value']);
$this->assertEquals($value['values']['count'], $output['tag'][$index]['values']['count']);
}
}
/**
* Tests get query type.
*/
public function testGetQueryType() {
$result = $this->widget->getQueryType($this->queryTypes);
$this->assertEquals('date', $result);
}
/**
* {@inheritdoc}
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$expected = [
'show_numbers' => FALSE,
'display_relative' => FALSE,
'granularity' => 5,
'date_display' => '',
'relative_granularity' => 1,
'relative_text' => TRUE,
];
$this->assertEquals($expected, $default_config);
}
}
......@@ -2,34 +2,15 @@
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\DropdownWidget;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Unit test for widget.
*
* @group facets
*/
class DropdownWidgetTest extends UnitTestCase {
/**
* The processor to be tested.
*
* @var \Drupal\facets\Plugin\facets\widget\DropdownWidget
*/
protected $widget;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
class DropdownWidgetTest extends WidgetTestBase {
/**
* Creates a new processor object for use in the tests.
......@@ -37,25 +18,6 @@ class DropdownWidgetTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
/** @var \Drupal\facets\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 Url('test'));
}
$this->originalResults = $original_results;
// Creates a mocked container, so we can access string translation.
$container = $this->prophesize(ContainerInterface::class);
$string_translation = $this->prophesize(TranslationInterface::class);
$container->get('string_translation')->willReturn($string_translation->reveal());
\Drupal::setContainer($container->reveal());
$this->widget = new DropdownWidget(['show_numbers' => TRUE]);
}
......@@ -89,28 +51,16 @@ class DropdownWidgetTest extends UnitTestCase {
}
/**
* Build a formattable markup object to use in the other tests.
*
* @param string $text
* Text to display.
* @param int $count
* Number of results.
* @param bool $active
* Link is active.
* @param bool $show_numbers
* Numbers are displayed.
*
* @return array
* A render array.
* {@inheritdoc}
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
'#is_active' => $active,
];
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
// We can't use $this->assertEquals() because that makes mocking here too
// hard, that way we'd need to also mock the translation interface. That's
// not needed.
$this->assertArrayHasKey('show_numbers', $default_config);
$this->assertArrayHasKey('default_option_label', $default_config);
}
}
......@@ -2,52 +2,23 @@
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\LinksWidget;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for widget.
*
* @group facets
*/
class LinksWidgetTest extends UnitTestCase {
class LinksWidgetTest extends WidgetTestBase {
/**
* The processor to be tested.
*
* @var \drupal\facets\Widget\WidgetPluginInterface
*/
protected $widget;
/**
* An array containing the results before the processor has ran.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* Creates a new processor object for use in the tests.
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
/** @var \Drupal\facets\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 Url('test'));
}
$this->originalResults = $original_results;
$this->widget = new LinksWidget();
}
......@@ -211,37 +182,4 @@ class LinksWidgetTest extends UnitTestCase {
}
/**
* Tests default configuration.
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$this->assertEquals(['show_numbers' => FALSE, 'soft_limit' => 0], $default_config);
}
/**
* Build a formattable markup object to use in the other tests.
*
* @param string $text
* Text to display.
* @param int $count
* Number of results.
* @param bool $active
* Link is active.
* @param bool $show_numbers
* Numbers are displayed.
*
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,
'#is_active' => $active,
];
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\facets\Plugin\facets\widget\NumericGranularWidget;
/**
* Unit test for widget.
*
* @group facets
*/
class NumericGranularWidgetTest extends WidgetTestBase {
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->widget = new NumericGranularWidget();
}
/**
* {@inheritdoc}
*/
public function testGetQueryType() {
$result = $this->widget->getQueryType($this->queryTypes);
$this->assertEquals('numeric', $result);
}
/**
* {@inheritdoc}
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$expected = [
'show_numbers' => FALSE,
'soft_limit' => 0,
'granularity' => 0,
];
$this->assertEquals($expected, $default_config);
}
}
<?php
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Field\WidgetPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for widget unit tests.
*/
abstract class WidgetTestBase extends UnitTestCase {
/**
* The widget to be tested.
*
* @var \Drupal\facets\Widget\WidgetPluginInterface
*/
protected $widget;
/**
* An array containing the results for the widget.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* An array of possible query types.
*
* @var string[]
*/
protected $queryTypes;
/**
* Sets up the container and other variables used in all the tests.
*/
protected function setUp() {
parent::setUp();
/** @var \Drupal\facets\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 Url('test'));
}
$this->originalResults = $original_results;
// Creates a mocked container, so we can access string translation.
$container = $this->prophesize(ContainerInterface::class);
$string_translation = $this->prophesize(TranslationInterface::class);
$url_generator = $this->prophesize(UrlGeneratorInterface::class);
$widget_manager = $this->prophesize(WidgetPluginManager::class);
$container->get('plugin.manager.facets.widget')->willReturn($widget_manager->reveal());
$container->get('string_translation')->willReturn($string_translation->reveal());
$container->get('url_generator')->willReturn($url_generator->reveal());
\Drupal::setContainer($container->reveal());
$this->queryTypes = [
'date' => 'date',
'string' => 'string',
'numeric' => 'numeric',
];
}
/**
* Tests default configuration.
*/
public function testDefaultConfiguration() {
$default_config = $this->widget->defaultConfiguration();
$this->assertEquals(['show_numbers' => FALSE, 'soft_limit' => 0], $default_config);
}
/**
* Tests get query type.
*/
public function testGetQueryType() {
$result = $this->widget->getQueryType($this->queryTypes);
$this->assertEquals('string', $result);
}
/**
* Build a formattable markup object to use as assertion.
*
* @param string $text
* Text to display.
* @param int $count
* Number of results.
* @param bool $active
* Link is active.
* @param bool $show_numbers
* Numbers are displayed.
*
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#value' => $text,
'#show_count' => $show_numbers && ($count !== NULL),
'#count' => $count,