View source
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
class FilterDateTest extends DateTimeHandlerTestBase {
public static $testViews = [
'test_filter_datetime',
];
protected static $timezones = [
'Pacific/Kwajalein',
'Pacific/Midway',
'America/Phoenix',
'UTC',
'Asia/Kolkata',
'Pacific/Funafuti',
'Pacific/Tongatapu',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$storage = FieldStorageConfig::load('node.' . static::$field_name);
$storage
->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$storage
->save();
$timestamp = $this
->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this
->getRelativeDateValuesFromTimestamp($timestamp);
$this->nodes = [];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node
->save();
$this->nodes[] = $node;
}
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [],
]);
$node
->save();
$this->nodes[] = $node;
}
public function testDateOffsets() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
foreach (static::$timezones as $timezone) {
$this
->setSiteTimezone($timezone);
$timestamp = $this
->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this
->getRelativeDateValuesFromTimestamp($timestamp);
$this
->updateNodesDateFieldsValues($dates);
$view
->initHandlers();
$view->filter[$field]->operator = '>=';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->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]->operator = '<';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['value'] = 'now';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[2]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['max'] = '+2 days';
$view->filter[$field]->value['min'] = '+1 day';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'empty';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[3]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'not empty';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[1]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
}
}
public function testDateIs() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
foreach (static::$timezones as $timezone) {
$this
->setSiteTimezone($timezone);
$timestamp = $this
->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this
->getRelativeDateValuesFromTimestamp($timestamp);
$this
->updateNodesDateFieldsValues($dates);
$view
->initHandlers();
$view->filter[$field]->operator = '=';
$view->filter[$field]->value['type'] = 'date';
$view->filter[$field]->value['value'] = $this->nodes[2]->field_date
->first()
->getValue()['value'];
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[2]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['type'] = 'date';
$view->filter[$field]->value['max'] = $this->nodes[0]->field_date
->first()
->getValue()['value'];
$view->filter[$field]->value['min'] = $this->nodes[1]->field_date
->first()
->getValue()['value'];
$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();
}
}
protected function updateNodesDateFieldsValues(array $dates) {
foreach ($dates as $index => $date) {
$this->nodes[$index]->{static::$field_name}->value = $date;
$this->nodes[$index]
->save();
}
}
}