View source
<?php
namespace Drupal\Tests\datetime_range\Kernel\Views;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\datetime\Kernel\Views\DateTimeHandlerTestBase;
use Drupal\views\Views;
class FilterDateTest extends DateTimeHandlerTestBase {
protected static $modules = [
'datetime_test',
'node',
'datetime_range',
'field',
];
protected static $field_type = 'daterange';
public static $testViews = [
'test_filter_datetime',
];
protected static $date;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
static::$date = $this
->getUTCEquivalentOfUserNowAsTimestamp();
$storage = FieldStorageConfig::load('node.' . static::$field_name);
$storage
->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATE);
$storage
->save();
$dates = $this
->getRelativeDateValuesFromTimestamp(static::$date);
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $dates[2],
'end_value' => $dates[1],
],
]);
$node
->save();
$this->nodes[] = $node;
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $dates[1],
'end_value' => $dates[1],
],
]);
$node
->save();
$this->nodes[] = $node;
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $dates[1],
'end_value' => $dates[0],
],
]);
$node
->save();
$this->nodes[] = $node;
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('test_filter_datetime');
$field_end = static::$field_name . '_end_value';
$display = $view
->getDisplay('default');
$filter_end_date = $display['display_options']['filters'][static::$field_name . '_value'];
$filter_end_date['id'] = $field_end;
$filter_end_date['field'] = $field_end;
$view
->getDisplay('default')['display_options']['filters'][$field_end] = $filter_end_date;
$view
->save();
}
public function testDateOffsets() {
$view = Views::getView('test_filter_datetime');
$field_start = static::$field_name . '_value';
$field_end = static::$field_name . '_end_value';
$view
->initHandlers();
$view->filter[$field_start]->operator = '>=';
$view->filter[$field_start]->value['type'] = 'offset';
$view->filter[$field_start]->value['value'] = '-1 day';
$view->filter[$field_end]->operator = '<=';
$view->filter[$field_end]->value['type'] = 'offset';
$view->filter[$field_end]->value['value'] = 'now';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[1]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field_start]->operator = '>=';
$view->filter[$field_start]->value['type'] = 'offset';
$view->filter[$field_start]->value['value'] = '-1 day';
$view->filter[$field_end]->operator = '>';
$view->filter[$field_end]->value['type'] = 'offset';
$view->filter[$field_end]->value['value'] = 'now';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[2]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
}
}