View source
<?php
namespace Drupal\Tests\facets\Unit\Plugin\widget;
use Drupal\facets\Entity\Facet;
use Drupal\facets\FacetInterface;
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\ContainerBuilder;
abstract class WidgetTestBase extends UnitTestCase {
protected $widget;
protected $facet;
protected $originalResults;
protected $queryTypes;
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->facet = $facet;
$original_results = [
new Result($facet, 'llama', 'Llama', 10),
new Result($facet, 'badger', 'Badger', 20),
new Result($facet, 'duck', 'Duck', 15),
new Result($facet, 'alpaca', 'Alpaca', 9),
];
foreach ($original_results as $original_result) {
$original_result
->setUrl(new Url('test'));
}
$this->originalResults = $original_results;
$string_translation = $this
->prophesize(TranslationInterface::class);
$url_generator = $this
->prophesize(UrlGeneratorInterface::class);
$widget_manager = $this
->prophesize(WidgetPluginManager::class);
$container = new ContainerBuilder();
$container
->set('plugin.manager.facets.widget', $widget_manager
->reveal());
$container
->set('string_translation', $string_translation
->reveal());
$container
->set('url_generator', $url_generator
->reveal());
\Drupal::setContainer($container);
$this->queryTypes = [
'date' => 'date',
'string' => 'string',
'numeric' => 'numeric',
'range' => 'range',
];
}
public function testDefaultConfiguration() {
$default_config = $this->widget
->defaultConfiguration();
$this
->assertEquals([
'show_numbers' => FALSE,
'soft_limit' => 0,
], $default_config);
}
public function testGetQueryType() {
$result = $this->widget
->getQueryType($this->queryTypes);
$this
->assertEquals(NULL, $result);
}
public function testIsPropertyRequired() {
$this
->assertFalse($this->widget
->isPropertyRequired('llama', 'owl'));
}
protected function buildLinkAssertion($text, $raw_value, FacetInterface $facet, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#raw_value' => $raw_value,
'#facet' => $facet,
'#value' => $text,
'#show_count' => $show_numbers && $count !== NULL,
'#count' => $count,
'#is_active' => $active,
];
}
}