View source
<?php
namespace Drupal\Tests\datetime\Functional\Views;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\views\Tests\ViewTestData;
class FilterDateTest extends BrowserTestBase {
protected $fieldName = 'field_date';
protected $defaultTheme = 'classy';
protected $nodes = [];
protected static $modules = [
'datetime',
'datetime_test',
'node',
'views',
'views_ui',
];
public static $testViews = [
'test_filter_datetime',
];
protected function setUp() : void {
parent::setUp();
$now = \Drupal::time()
->getRequestTime();
$admin_user = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($admin_user);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'datetime',
'settings' => [
'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
],
]);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field
->save();
$dates = [
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
];
$this->nodes = [];
foreach ($dates as $date) {
$this->nodes[] = $this
->drupalCreateNode([
$this->fieldName => [
'value' => $date,
],
]);
}
$this->nodes[] = $this
->drupalCreateNode();
$this->container
->get('views.views_data')
->clear();
ViewTestData::createTestViews(static::class, [
'datetime_test',
]);
}
public function testExposedGroupedFilters() {
$this
->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $this->fieldName . '_value');
$this
->submitForm([], 'Expose filter');
$this
->submitForm([], 'Grouped filters');
$edit = [];
$edit['options[group_info][group_items][1][title]'] = 'empty';
$edit['options[group_info][group_items][1][operator]'] = 'empty';
$edit['options[group_info][group_items][2][title]'] = 'not empty';
$edit['options[group_info][group_items][2][operator]'] = 'not empty';
$this
->submitForm($edit, 'Apply');
$path = 'test_filter_datetime-path';
$this
->drupalGet('admin/structure/views/view/test_filter_datetime/edit');
$this
->submitForm([], 'Add Page');
$this
->drupalGet('admin/structure/views/nojs/display/test_filter_datetime/page_1/path');
$this
->submitForm([
'path' => $path,
], 'Apply');
$this
->submitForm([], 'Save');
$this
->drupalGet($path);
$this
->getSession()
->getPage()
->findField($this->fieldName . '_value')
->selectOption(1);
$this
->getSession()
->getPage()
->pressButton('Apply');
$results = $this
->cssSelect('.view-content .field-content');
$this
->assertCount(1, $results);
$this
->getSession()
->getPage()
->findField($this->fieldName . '_value')
->selectOption(2);
$this
->getSession()
->getPage()
->pressButton('Apply');
$results = $this
->cssSelect('.view-content .field-content');
$this
->assertCount(3, $results);
}
}