You are here

public function FilterDateTest::testDateIs in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateIs()
  2. 10 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateIs()

Tests date filter with date-only fields.

File

core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php, line 172

Class

FilterDateTest
Tests date-only fields.

Namespace

Drupal\Tests\datetime\Kernel\Views

Code

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);

    // Test simple operations.
    $view
      ->initHandlers();

    // Filtering with nodes date-only values (format: Y-m-d) to test UTC
    // conversion does NOT change the day.
    $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();

    // Test offset for between operator. Only 'today' and 'tomorrow' nodes
    // should appear.
    $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();
  }
}