View source
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
class FilterDateTimeTest extends DateTimeHandlerTestBase {
public static $testViews = [
'test_filter_datetime',
];
protected static $date;
protected static $timezone = 'America/Vancouver';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
static::$date = REQUEST_TIME + 86400;
date_default_timezone_set(static::$timezone);
$this
->config('system.date')
->set('timezone.default', static::$timezone)
->save();
$dates = [
'2000-10-10T00:01:30',
'2001-10-10T12:12:12',
'2002-10-10T14:14:14',
\Drupal::service('date.formatter')
->format(static::$date, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this
->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node
->save();
$this->nodes[] = $node;
}
}
public function testDatetimeFilter() {
$this
->_testOffset();
$this
->_testBetween();
$this
->_testExact();
}
protected function _testOffset() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
$view
->initHandlers();
$view->filter[$field]->operator = '>';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['value'] = '+1 hour';
$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 = 'between';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['max'] = '+2 days';
$view->filter[$field]->value['min'] = '+1 hour';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[3]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
}
protected function _testBetween() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
$view
->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['min'] = '2001-01-01';
$view->filter[$field]->value['max'] = '2002-01-01';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[1]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['max'] = '2002-01-01';
$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 = 'not between';
$view->filter[$field]->value['min'] = '2001-01-01';
$view->filter[$field]->value['max'] = '2001-10-10T12:12:12';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[0]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
$view
->initHandlers();
$view->filter[$field]->operator = 'not between';
$view->filter[$field]->value['max'] = '2001-01-01';
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[1]
->id(),
],
[
'nid' => $this->nodes[2]
->id(),
],
[
'nid' => $this->nodes[3]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
}
protected function _testExact() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
$view
->initHandlers();
$view->filter[$field]->operator = '=';
$view->filter[$field]->value['min'] = '';
$view->filter[$field]->value['max'] = '';
$view->filter[$field]->value['value'] = \Drupal::service('date.formatter')
->format(static::$date, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, static::$timezone);
$view
->setDisplay('default');
$this
->executeView($view);
$expected_result = [
[
'nid' => $this->nodes[3]
->id(),
],
];
$this
->assertIdenticalResultset($view, $expected_result, $this->map);
$view
->destroy();
}
}