function FilterDateTest::testExposedOffsetBetweenDefaultAppliedOnPageLoad

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Functional/Handler/FilterDateTest.php \Drupal\Tests\views\Functional\Handler\FilterDateTest::testExposedOffsetBetweenDefaultAppliedOnPageLoad()

Tests an exposed date filter with offset default value (between operator).

Verify that results are filtered on page load without requiring form submission.

File

core/modules/views/tests/src/Functional/Handler/FilterDateTest.php, line 403

Class

FilterDateTest
Tests the core Drupal\views\Plugin\views\filter\Date handler.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testExposedOffsetBetweenDefaultAppliedOnPageLoad() : void {
  $this->drupalLogin($this->drupalCreateUser([
    'administer views',
  ]));
  // Expose the filter with operator "between" and offset defaults.
  $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
  $this->submitForm([], 'Expose filter');
  $edit = [
    'options[operator]' => 'between',
    'options[value][type]' => 'offset',
    'options[value][min]' => 'now',
    'options[value][max]' => '+2 days',
  ];
  $this->submitForm($edit, 'Apply');
  // Add a page display and save.
  $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit');
  $this->submitForm([], 'Add Page');
  $this->drupalGet('admin/structure/views/nojs/display/test_filter_date_between/page_1/path');
  $this->submitForm([
    'path' => 'exposed-offset-between-default',
  ], 'Apply');
  $this->submitForm([], 'Save');
  // Add a node beyond the max offset (+2 days) to verify it is excluded.
  $this->nodes[] = $this->drupalCreateNode([
    'created' => time() + 86400 * 3,
  ]);
  // On page load (no form submission), only the node within [now, +2 days]
  // should appear; the node beyond +2 days must be excluded.
  $this->drupalGet('exposed-offset-between-default');
  $results = $this->cssSelect('.view-content .field-content');
  $this->assertCount(1, $results);
  $this->assertEquals($this->nodes[3]
    ->id(), $results[0]->getText());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.