function FilterDateTest::testDateOffsets
Same name in this branch
- 9 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
Same name in other branches
- 8.9.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
- 8.9.x core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()
- 10 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
- 10 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()
- 11.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
- 11.x core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()
Tests offsets with date-only fields.
File
-
core/
modules/ datetime/ tests/ src/ Kernel/ Views/ FilterDateTest.php, line 88
Class
- FilterDateTest
- Tests date-only fields.
Namespace
Drupal\Tests\datetime\Kernel\ViewsCode
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);
// Test simple operations.
$view->initHandlers();
// A greater than or equal to 'now', should return the 'today' and the
// 'tomorrow' node.
$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();
// Only dates in the past.
$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();
// Test offset for between operator. Only 'tomorrow' node should appear.
$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();
// Test the empty operator.
$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();
// Test the not empty operator.
$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();
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.