function FilterDateTest::testDateOffsets

Same name in this branch
  1. 8.9.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
Same name and namespace in other branches
  1. 9 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
  2. 9 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()
  3. 10 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
  4. 10 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()
  5. 11.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::testDateOffsets()
  6. 11.x core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::testDateOffsets()

Test 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\Views

Code

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.