You are here

protected function FilterDateTest::_testFilterDateUI in Drupal 8

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

Test date filter UI.

1 call to FilterDateTest::_testFilterDateUI()
FilterDateTest::testDateFilter in core/modules/views/tests/src/Functional/Handler/FilterDateTest.php
Runs other test methods.

File

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

Class

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

Namespace

Drupal\Tests\views\Functional\Handler

Code

protected function _testFilterDateUI() {
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer views',
  ]));
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
  $this
    ->drupalPostForm(NULL, [], t('Expose filter'));
  $this
    ->drupalPostForm(NULL, [], t('Grouped filters'));
  $edit = [];
  $edit['options[group_info][group_items][1][title]'] = 'simple-offset';
  $edit['options[group_info][group_items][1][operator]'] = '>';
  $edit['options[group_info][group_items][1][value][type]'] = 'offset';
  $edit['options[group_info][group_items][1][value][value]'] = '+1 hour';
  $edit['options[group_info][group_items][2][title]'] = 'between-offset';
  $edit['options[group_info][group_items][2][operator]'] = 'between';
  $edit['options[group_info][group_items][2][value][type]'] = 'offset';
  $edit['options[group_info][group_items][2][value][min]'] = '+1 hour';
  $edit['options[group_info][group_items][2][value][max]'] = '+2 days';
  $edit['options[group_info][group_items][3][title]'] = 'between-date';
  $edit['options[group_info][group_items][3][operator]'] = 'between';
  $edit['options[group_info][group_items][3][value][min]'] = $this->dateFormatter
    ->format(150000, 'custom', 'Y-m-d H:i:s');
  $edit['options[group_info][group_items][3][value][max]'] = $this->dateFormatter
    ->format(250000, 'custom', 'Y-m-d H:i:s');
  $this
    ->drupalPostForm(NULL, $edit, t('Apply'));
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
  foreach ($edit as $name => $value) {
    $this
      ->assertFieldByName($name, $value);
    if (strpos($name, '[value][type]')) {
      $radio = $this
        ->cssSelect('input[name="' . $name . '"][checked="checked"][type="radio"]');
      $this
        ->assertEqual($radio[0]
        ->getAttribute('value'), $value);
    }
  }
  $this
    ->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
  $this
    ->assertConfigSchemaByName('views.view.test_filter_date_between');

  // Test that the exposed filter works as expected.
  $path = 'test_filter_date_between-path';
  $this
    ->drupalPostForm('admin/structure/views/view/test_filter_date_between/edit', [], 'Add Page');
  $this
    ->drupalPostForm('admin/structure/views/nojs/display/test_filter_date_between/page_1/path', [
    'path' => $path,
  ], 'Apply');
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalGet($path);
  $this
    ->drupalPostForm(NULL, [], 'Apply');
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(4, $results);
  $this
    ->drupalPostForm(NULL, [
    'created' => '1',
  ], 'Apply');
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(1, $results);
  $this
    ->assertEqual($results[0]
    ->getText(), $this->nodes[3]
    ->id());
  $this
    ->drupalPostForm(NULL, [
    'created' => '2',
  ], 'Apply');
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(1, $results);
  $this
    ->assertEqual($results[0]
    ->getText(), $this->nodes[3]
    ->id());
  $this
    ->drupalPostForm(NULL, [
    'created' => '3',
  ], 'Apply');
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(1, $results);
  $this
    ->assertEqual($results[0]
    ->getText(), $this->nodes[1]
    ->id());

  // Change the filter to a single filter to test the schema when the operator
  // is not exposed.
  $this
    ->drupalPostForm('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created', [], t('Single filter'));
  $edit = [];
  $edit['options[operator]'] = '>';
  $edit['options[value][type]'] = 'date';
  $edit['options[value][value]'] = $this->dateFormatter
    ->format(350000, 'custom', 'Y-m-d H:i:s');
  $this
    ->drupalPostForm(NULL, $edit, t('Apply'));
  $this
    ->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
  $this
    ->assertConfigSchemaByName('views.view.test_filter_date_between');

  // Test that the filter works as expected.
  $this
    ->drupalGet($path);
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(1, $results);
  $this
    ->assertEqual($results[0]
    ->getText(), $this->nodes[3]
    ->id());
  $this
    ->drupalPostForm(NULL, [
    'created' => $this->dateFormatter
      ->format(250000, 'custom', 'Y-m-d H:i:s'),
  ], 'Apply');
  $results = $this
    ->cssSelect('.view-content .field-content');
  $this
    ->assertCount(2, $results);
  $this
    ->assertEqual($results[0]
    ->getText(), $this->nodes[2]
    ->id());
  $this
    ->assertEqual($results[1]
    ->getText(), $this->nodes[3]
    ->id());
}