Commit b4639a4e authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2840246 by borisson_: Improve test coverage

parent 010a9794
......@@ -374,15 +374,14 @@ class FacetsDateHandler {
* 0 if they are equal.
*/
public function gapCompare($gap1, $gap2) {
$gap_numbers = array(
$gap_numbers = [
static::FACETS_DATE_YEAR => 6,
static::FACETS_DATE_MONTH => 5,
static::FACETS_DATE_DAY => 4,
static::FACETS_DATE_HOUR => 3,
static::FACETS_DATE_MINUTE => 2,
static::FACETS_DATE_SECOND => 1,
);
];
$gap1_num = isset($gap_numbers[$gap1]) ? $gap_numbers[$gap1] : 6;
$gap2_num = isset($gap_numbers[$gap2]) ? $gap_numbers[$gap2] : 6;
......@@ -396,7 +395,7 @@ class FacetsDateHandler {
}
/**
* Extracts "start" and "end" dates from an active items.
* Extracts "start" and "end" dates from an active item.
*
* @param string $item
* The active item to extract the dates.
......
......@@ -99,7 +99,10 @@ class FacetFacetSourceTest extends EntityKernelTestBase {
/**
* Tests invalid query type.
*
* The error here is triggered because no field id is set.
*
* @covers ::getQueryType
* @covers ::getFacetSource
*/
public function testInvalidQueryType() {
$entity = new Facet([], 'facets_facet');
......@@ -110,4 +113,20 @@ class FacetFacetSourceTest extends EntityKernelTestBase {
$entity->getQueryType();
}
/**
* Tests invalid query type.
*
* @covers ::getQueryType
* @covers ::getFacetSource
*/
public function testQueryType() {
$entity = new Facet([], 'facets_facet');
$entity->setWidget('links');
$entity->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$entity->setFieldIdentifier('name');
$aa = $entity->getQueryType();
$this->assertEquals('search_api_string', $aa);
}
}
......@@ -20,8 +20,8 @@ class SearchApiDateTest extends KernelTestBase {
public function setUp() {
parent::setUp();
// This is the default set by Drupal as well, but to be explicit
// The raw value is the UTC, the displayed value is calculated
// This is the default set by Drupal as well, but to be explicit we set it
// here as well. The raw value is the UTC, the displayed value is calculated
// by the PHP timezone - presently.
date_default_timezone_set('Australia/Sydney');
}
......@@ -41,13 +41,12 @@ class SearchApiDateTest extends KernelTestBase {
$widget = $this->getMockBuilder('Drupal\facets\Widget\WidgetPluginInterface')
->disableOriginalConstructor()
->getMock();
$widget->method('getConfiguration')->will($this->returnValue(
[
'granularity' => $granularity,
'date_display' => '',
'display_relative' => FALSE,
]
));
$widget->method('getConfiguration')
->will($this->returnValue([
'granularity' => $granularity,
'date_display' => '',
'display_relative' => FALSE,
]));
$widget_instance = $facetReflection->getProperty('widgetInstance');
$widget_instance->setAccessible(TRUE);
$widget_instance->setValue($facet, $widget);
......
......@@ -49,7 +49,15 @@ class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
];
$processor_id = 'exclude_specified_items';
$this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, []);
$this->processor = new ExcludeSpecifiedItemsProcessor([], $processor_id, [
'id' => "display_value_widget_order",
'label' => "Sort by display value",
'description' => "Sorts the widget results by display value.",
'default_enabled' => TRUE,
'stages' => [
"build" => 50,
]
]);
$processor_definitions = [
$processor_id => [
......@@ -309,7 +317,7 @@ class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
* Tests testDescription().
*/
public function testDescription() {
$this->assertEquals('', $this->processor->getDescription());
$this->assertEquals('Sorts the widget results by display value.', $this->processor->getDescription());
}
/**
......@@ -326,4 +334,20 @@ class ExcludeSpecifiedItemsProcessorTest extends UnitTestCase {
$this->assertEquals(FALSE, $this->processor->isLocked());
}
/**
* Tests supportsStage().
*/
public function testSupportsStage() {
$this->assertTrue($this->processor->supportsStage('build'));
$this->assertFalse($this->processor->supportsStage('sort'));
}
/**
* Tests getDefaultWeight().
*/
public function testGetDefaultWeight() {
$this->assertEquals(50, $this->processor->getDefaultWeight('build'));
$this->assertEquals(0, $this->processor->getDefaultWeight('sort'));
}
}
......@@ -182,6 +182,29 @@ class QueryStringTest extends UnitTestCase {
}
}
/**
* Tests with only one result.
*/
public function testWithOnlyOneResult() {
$facet = new Facet([], 'facets_facet');
$facet->setFieldIdentifier('test');
$facet->setUrlAlias('test');
$facet->setFacetSourceId('facet_source__dummy');
$facet->setShowOnlyOneResult(TRUE);
$this->originalResults[1]->setActiveState(TRUE);
$this->originalResults[2]->setActiveState(TRUE);
$this->processor = new QueryString(['facet' => $facet], 'query_string', [], new Request());
$results = $this->processor->buildUrls($facet, $this->originalResults);
$this->assertEquals('route:test?f[0]=test%3A' . $results[0]->getRawValue(), $results[0]->getUrl()->toUriString());
$this->assertEquals('route:test?f[0]=test%3A' . $results[3]->getRawValue(), $results[3]->getUrl()->toUriString());
$this->assertEquals('route:test?f[0]=test%3A' . $results[4]->getRawValue(), $results[4]->getUrl()->toUriString());
$this->assertEquals('route:test', $results[1]->getUrl()->toUriString());
$this->assertEquals('route:test', $results[2]->getUrl()->toUriString());
}
/**
* Tests that the facet source configuration filter key override works.
*/
......@@ -232,12 +255,10 @@ class QueryStringTest extends UnitTestCase {
->getMock();
$router->expects($this->any())
->method('matchRequest')
->willReturn(
[
'_raw_variables' => new ParameterBag([]),
'_route' => 'test',
]
);
->willReturn([
'_raw_variables' => new ParameterBag([]),
'_route' => 'test',
]);
$validator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');
......
......@@ -134,6 +134,10 @@ class FacetsDateHandlerTest extends UnitTestCase {
// The best search gap between two dates must be a second.
$date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 59);
$this->assertEquals($fd::FACETS_DATE_SECOND, $date_gap);
// When passing in a minimum gap it should be respected.
$date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 3600, $fd::FACETS_DATE_DAY);
$this->assertEquals($fd::FACETS_DATE_DAY, $date_gap);
}
/**
......@@ -176,6 +180,13 @@ class FacetsDateHandlerTest extends UnitTestCase {
$this->assertEquals($incremented_iso_date, $this->handler->getNextDateIncrement(static::ISO_DATE, $gap));
}
/**
* Tests for ::nextDateIncrement method.
*/
public function testInvalidNextDateIncrement() {
$this->assertFalse($this->handler->getNextDateIncrement('foo', FacetsDateHandler::FACETS_DATE_SECOND));
}
/**
* Tests for ::gapCompare method.
*/
......@@ -201,8 +212,28 @@ class FacetsDateHandlerTest extends UnitTestCase {
public function testFormatTimestamp() {
$fd = $this->handler;
$year = $fd->formatTimestamp(static::TIMESTAMP);
$this->assertEquals(1987, $year);
$formatted = $fd->formatTimestamp(static::TIMESTAMP);
$this->assertEquals('1987', $formatted);
$formatted = $fd->formatTimestamp(static::TIMESTAMP, 'llama');
$this->assertEquals('1987', $formatted);
$formatted = $fd->formatTimestamp(static::TIMESTAMP, $fd::FACETS_DATE_YEAR);
$this->assertEquals('1987', $formatted);
}
/**
* Test extract items.
*/
public function testExtractActiveItems() {
$this->assertFalse($this->handler->extractActiveItems('foo'));
$range = '[2016-03-01T00:00:00Z TO 2016-04-01T00:00:00Z]';
$extracted = $this->handler->extractActiveItems($range);
$this->assertInternalType('array', $extracted);
$this->assertEquals('1456790400', $extracted['start']['timestamp']);
$this->assertEquals('2016-03-01T00:00:00Z', $extracted['start']['iso']);
}
/**
......
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