X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fdatetime%2Fsrc%2FTests%2FViews%2FFilterDateTest.php;fp=web%2Fcore%2Fmodules%2Fdatetime%2Fsrc%2FTests%2FViews%2FFilterDateTest.php;h=2a8d7cdb299793a8594a6c5125902bb2cbfc85b9;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/datetime/src/Tests/Views/FilterDateTest.php b/web/core/modules/datetime/src/Tests/Views/FilterDateTest.php new file mode 100644 index 000000000..2a8d7cdb2 --- /dev/null +++ b/web/core/modules/datetime/src/Tests/Views/FilterDateTest.php @@ -0,0 +1,110 @@ +setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE); + $storage->save(); + + $dates = [ + // Tomorrow. + \Drupal::service('date.formatter')->format(static::$date + 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), + // Today. + \Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), + // Yesterday. + \Drupal::service('date.formatter')->format(static::$date - 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), + ]; + + foreach ($dates as $date) { + $this->nodes[] = $this->drupalCreateNode([ + 'field_date' => [ + 'value' => $date, + ] + ]); + } + } + + /** + * Test offsets with date-only fields. + */ + public function testDateOffsets() { + $view = Views::getView('test_filter_datetime'); + $field = static::$field_name . '_value'; + + // 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 the '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); + } + +}