Commit 33f383d5 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2894637 by borisson_, agoradesign: Add testcoverage for range + slider widgets/processors

parent 5e4be677
......@@ -54,9 +54,11 @@ class RangeSliderProcessor extends SliderProcessor implements PreQueryProcessorI
$query = $url->getOption('query');
// Remove all the query filters for the field of the facet.
foreach ($query[$filter_key] as $id => $filter) {
if (strpos($filter . $url_processor->getSeparator(), $facet->getUrlAlias()) === 0) {
unset($query[$filter_key][$id]);
if ($query !== NULL) {
foreach ($query[$filter_key] as $id => $filter) {
if (strpos($filter . $url_processor->getSeparator(), $facet->getUrlAlias()) === 0) {
unset($query[$filter_key][$id]);
}
}
}
......
......@@ -36,6 +36,10 @@ class SliderProcessor extends ProcessorPluginBase implements PostQueryProcessorI
'count' => (int) $result->getCount(),
];
}
uasort($simple_results, function($a, $b) {
if ($a['value'] === $b['value']) { return 0; }
return $a['value'] < $b['value'] ? -1 : 1;
});
if ($config['min_type'] == 'fixed') {
$min = $config['min_value'];
......
......@@ -47,8 +47,11 @@ class SliderWidget extends WidgetPluginBase {
$urls['f_' . $result->getRawValue()] = $result->getUrl()->toString();
$labels[] = $result->getDisplayValue() . ($show_numbers ? ' (' . $result->getCount() . ')' : '');
}
$min = (float) min($results)->getRawValue();
$max = (float) max($results)->getRawValue();
// The results set on the facet are sorted where the minimum is the first
// item and the last one is the one with the highest results, so it's safe
// to use min/max.
$min = (float) reset($results)->getRawValue();
$max = (float) end($results)->getRawValue();
$build['slider'] = [
'#type' => 'html_tag',
......
......@@ -60,13 +60,23 @@ class SliderIntegrationTest extends FacetsTestBase {
$this->assertSession()->checkboxChecked('edit-facet-settings-slider-status');
$startTime = \Drupal::time()->getCurrentMicroTime();
$this->drupalGet('search-api-test-fulltext');
$this->assertFacetBlocksAppear();
$this->assertSession()->pageTextContains('Displaying 12 search results');
$stopTime = \Drupal::time()->getCurrentMicroTime();
if (($stopTime - $startTime) > 1) {
$this->fail('Filtering takes too long');
}
// Change the facet block.
$startTime = \Drupal::time()->getCurrentMicroTime();
$url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'owl:2']]);
$this->drupalGet($url->setAbsolute()->toString());
$stopTime = \Drupal::time()->getCurrentMicroTime();
if (($stopTime - $startTime) > 1) {
$this->fail('Filtering takes too long');
}
// Check that the results have changed to the correct amount of results.
$this->assertSession()->pageTextContains('Displaying 1 search results');
......
<?php
namespace Drupal\Tests\facets_range_widget\Unit\Plugin\processor;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\UrlProcessorHandler;
use Drupal\facets\Plugin\facets\url_processor\QueryString;
use Drupal\facets\Result\Result;
use Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for processor.
*
* @group facets
* @coversDefaultClass \Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor
*/
class RangeSliderProcessorTest extends UnitTestCase {
/**
* @var \Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor
*/
protected $processor;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->processor = new RangeSliderProcessor([], 'range_slider', []);
$url_generator = $this->prophesize(UrlGeneratorInterface::class);
$container = new ContainerBuilder();
$container->set('url_generator', $url_generator->reveal());
\Drupal::setContainer($container);
}
/**
* Tests the pre query method.
*
* @covers ::preQuery
*/
public function testPreQuery() {
$facet = new Facet(['id' => 'llama'], 'facets_facet');
$facet->setActiveItems(['(min:2,max:10)']);
$this->processor->preQuery($facet);
$this->assertCount(2, $facet->getActiveItems()[0]);
$this->assertEquals([2, 10], $facet->getActiveItems()[0]);
}
/**
* Tests the build method.
*
* @covers ::build
*/
public function testBuild() {
// Create the Url processor.
$queryString = $this->prophesize(QueryString::class);
$queryString->getFilterKey()->willReturn('f');
$queryString->getSeparator()->willReturn('::');
$urlHandler = $this->prophesize(UrlProcessorHandler::class);
$urlHandler->getProcessor()->willReturn($queryString->reveal());
$facet = $this->prophesize(Facet::class);
$facet->getProcessors()->willReturn(['url_processor_handler' => $urlHandler->reveal()]);
$facet->getUrlAlias()->willReturn('animals');
/** @var \Drupal\facets\Result\ResultInterface[] $results */
$results = [
new Result(1, 1, 1),
new Result(5, 5, 5),
];
$results[0]->setUrl(new Url('test'));
$results[1]->setUrl(new Url('test'));
$new_results = $this->processor->build($facet->reveal(), $results);
$this->assertCount(2, $new_results);
$params = UrlHelper::buildQuery(['f' => ['animals::(min:__range_slider_min__,max:__range_slider_max__)']]);
$expected_route = 'route:test?' . $params;
$this->assertEquals($expected_route, $new_results[0]->getUrl()->toUriString());
$this->assertEquals($expected_route, $new_results[1]->getUrl()->toUriString());
}
}
<?php
namespace Drupal\Tests\facets_range_widget\Unit\Plugin\processor;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\widget\ArrayWidget;
use Drupal\facets\Result\Result;
use Drupal\facets\Widget\WidgetPluginManager;
use Drupal\facets_range_widget\Plugin\facets\processor\SliderProcessor;
use Drupal\Tests\UnitTestCase;
/**
* Unit test for processor.
*
* @group facets
* @coversDefaultClass \Drupal\facets_range_widget\Plugin\facets\processor\SliderProcessor
*/
class SliderProcessorTest extends UnitTestCase {
/**
* @var \Drupal\facets\Processor\ProcessorInterface
*/
protected $processor;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->processor = new SliderProcessor([], 'slider_processor', []);
}
/**
* Tests the post query method.
*
* @covers ::postQuery
*/
public function testPostQuery() {
$widgetconfig = ['min_type' => 'foo', 'step' => 1];
$facet = new Facet([], 'facets_facet');
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 1);
$result_higher = new Result(150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
$startTime = microtime();
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
$stopTime = microtime();
if (($stopTime - $startTime) > 1) {
$this->fail('Test is too slow');
}
$this->assertCount(146, $new_results);
$this->assertEquals(5, $new_results[0]->getRawValue());
$this->assertEquals(1, $new_results[0]->getCount());
$this->assertEquals(6, $new_results[1]->getRawValue());
$this->assertEquals(0, $new_results[1]->getCount());
}
/**
* Tests the post query method with a big dataset.
*
* @covers ::postQuery
*/
public function testPostQueryBigDataSet() {
$widgetconfig = ['min_type' => 'foo', 'step' => 1];
$facet = new Facet([], 'facets_facet');
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$original_results[] = new Result(1, 'Small', 5);
foreach (range(100, 100000, 10) as $k) {
$original_results[] = new Result($k, 'result ' . $k, 1);
}
$original_results[] = new Result(150000, 'Big', 5);
$facet->setResults($original_results);
// Process the data
$startTime = microtime();
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
$stopTime = microtime();
if (($stopTime - $startTime) > 1) {
$this->fail('Test is too slow');
}
$this->assertCount(150000, $new_results);
}
/**
* Tests the post query method result sorting.
*
* @covers ::postQuery
*/
public function testPostQueryResultSorting() {
$widgetconfig = ['min_type' => 'foo', 'step' => 1];
$facet = new Facet([], 'facets_facet');
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$original_results = [];
foreach ([10, 100, 200, 5] as $k) {
$original_results[] = new Result($k, 'result ' . $k, 1);
}
$facet->setResults($original_results);
// Process the data
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
$this->assertCount(196, $new_results);
$this->assertEquals(5, $new_results[0]->getRawValue());
$this->assertEquals(200, $new_results[195]->getRawValue());
}
/**
* Tests the post query method with fixed min/max.
*
* @covers ::postQuery
*/
public function testPostQueryFixedMinMax() {
$widgetconfig = [
'min_type' => 'fixed',
'min_value' => 10,
'max_value' => 20,
'step' => 1,
];
$facet = new Facet([], 'facets_facet');
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 1);
$result_higher = new Result(150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
$this->assertCount(11, $new_results);
}
/**
* Tests the post query method with step > 1.
*
* @covers ::postQuery
*/
public function testPostQueryStep() {
$widgetconfig = ['min_type' => 'foo', 'step' => 2];
$facet = new Facet([], 'facets_facet');
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 4);
$result_higher = new Result(15, '15', 4);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
$this->assertCount(6, $new_results);
$this->assertEquals(5, $new_results[0]->getRawValue());
$this->assertEquals(4, $new_results[0]->getCount());
$this->assertEquals(7, $new_results[1]->getRawValue());
$this->assertEquals(0, $new_results[1]->getCount());
$this->assertEquals(15, $new_results[5]->getRawValue());
$this->assertEquals(4, $new_results[5]->getCount());
}
/**
* Configures the container.
*
* @param array $config
* The config for the widget.
*/
protected function configureContainer(array $config = []) {
$widget = $this->prophesize(ArrayWidget::class);
$widget->getConfiguration()->willReturn($config);
$pluginManager = $this->prophesize(WidgetPluginManager::class);
$pluginManager->createInstance('raw', $config)
->willReturn($widget->reveal());
$container = new ContainerBuilder();
$container->set('plugin.manager.facets.widget', $pluginManager->reveal());
\Drupal::setContainer($container);
}
}
......@@ -3,14 +3,13 @@
namespace Drupal\Tests\facets_range_widget\Unit\Plugin\widget;
use Drupal\facets_range_widget\Plugin\facets\widget\RangeSliderWidget;
use Drupal\Tests\facets\Unit\Plugin\widget\WidgetTestBase;
/**
* Unit test for widget.
*
* @group facets
*/
class RangeSliderWidgetTest extends WidgetTestBase {
class RangeSliderWidgetTest extends SliderWidgetTest {
/**
* {@inheritdoc}
......@@ -46,4 +45,22 @@ class RangeSliderWidgetTest extends WidgetTestBase {
$this->assertEquals($expected, $default_config);
}
/**
* {@inheritdoc}
*/
public function testIsPropertyRequired() {
$this->assertFalse($this->widget->isPropertyRequired('llama', 'owl'));
$this->assertTrue($this->widget->isPropertyRequired('range_slider', 'processors'));
$this->assertTrue($this->widget->isPropertyRequired('show_only_one_result', 'settings'));
}
/**
* {@inheritdoc}
*/
public function testBuild() {
$build = parent::testBuild();
$this->assertTrue($build['range']);
$this->assertEquals([3, 19999], $build['values']);
}
}
......@@ -2,8 +2,14 @@
namespace Drupal\Tests\facets_range_widget\Unit\Plugin\widget;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Result\Result;
use Drupal\facets\Widget\WidgetPluginManager;
use Drupal\facets_range_widget\Plugin\facets\widget\SliderWidget;
use Drupal\Tests\facets\Unit\Plugin\widget\WidgetTestBase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Unit test for widget.
......@@ -46,4 +52,59 @@ class SliderWidgetTest extends WidgetTestBase {
$this->assertEquals($expected, $default_config);
}
/**
* {@inheritdoc}
*/
public function testIsPropertyRequired() {
$this->assertFalse($this->widget->isPropertyRequired('llama', 'owl'));
$this->assertTrue($this->widget->isPropertyRequired('slider', 'processors'));
$this->assertTrue($this->widget->isPropertyRequired('show_only_one_result', 'settings'));
}
/**
* Tests building of the widget.
*/
public function testBuild() {
$widget = $this->prophesize(SliderWidget::class);
$widget->getConfiguration()->willReturn(['show_numbers' => FALSE]);
$pluginManager = $this->prophesize(WidgetPluginManager::class);
$pluginManager->createInstance('slider', [])
->willReturn($widget->reveal());
$url_generator = $this->prophesize(UrlGeneratorInterface::class);
$container = new ContainerBuilder();
$container->set('plugin.manager.facets.widget', $pluginManager->reveal());
$container->set('url_generator', $url_generator->reveal());
\Drupal::setContainer($container);
$originalResults = [];
foreach (range(3, 20000, 2) as $rv) {
$res = new Result($rv, 'Value: ' . $rv, ceil($rv/2));
$res->setUrl(new Url('test'));
$originalResults[] = $res;
}
$this->originalResults = $originalResults;
$facet = new Facet(['id' => 'barn_owl'], 'facets_facet');
$facet->setResults($this->originalResults);
$facet->setFieldIdentifier('owl');
$facet->setWidget('slider', []);
$startTime = microtime();
$build = $this->widget->build($facet);
$stopTime = microtime();
if (($stopTime - $startTime) > 1) {
$this->fail('Test is too slow');
}
$this->assertInternalType('array', $build);
$build = $build['#attached']['drupalSettings']['facets']['sliders']['barn_owl'];
$this->assertEquals(3, $build['min']);
$this->assertEquals(19999, $build['max']);
return $build;
}
}
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