Issue #2636086 by Matroskeen, Spokje, jian he, ravi.shankar, larowlan, Lendude, dawehner, Sweetchuck: Add extra test coverage for operators of views date filters

(cherry picked from commit 248d012cb1)
merge-requests/2045/head
Lee Rowlands 2022-04-01 09:54:58 +10:00
parent eba2a2df7a
commit ebc14e6a3a
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
1 changed files with 60 additions and 14 deletions

View File

@ -7,7 +7,7 @@ use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData; use Drupal\views\Tests\ViewTestData;
/** /**
@ -15,7 +15,7 @@ use Drupal\views\Tests\ViewTestData;
* *
* @group datetime * @group datetime
*/ */
class FilterDateTest extends BrowserTestBase { class FilterDateTest extends ViewTestBase {
/** /**
* Name of the field. * Name of the field.
@ -38,6 +38,13 @@ class FilterDateTest extends BrowserTestBase {
*/ */
protected $nodes = []; protected $nodes = [];
/**
* Dates of test nodes in date storage format.
*
* @var string[]
*/
protected $dates;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -45,7 +52,6 @@ class FilterDateTest extends BrowserTestBase {
'datetime', 'datetime',
'datetime_test', 'datetime_test',
'node', 'node',
'views',
'views_ui', 'views_ui',
]; ];
@ -59,8 +65,8 @@ class FilterDateTest extends BrowserTestBase {
* *
* Create nodes with relative dates of yesterday, today, and tomorrow. * Create nodes with relative dates of yesterday, today, and tomorrow.
*/ */
protected function setUp(): void { protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void {
parent::setUp(); parent::setUp($import_test_views, $modules);
$now = \Drupal::time()->getRequestTime(); $now = \Drupal::time()->getRequestTime();
@ -85,17 +91,17 @@ class FilterDateTest extends BrowserTestBase {
$field->save(); $field->save();
// Create some nodes. // Create some nodes.
$dates = [ $this->dates = [
// Tomorrow. // Tomorrow.
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
// Today. // Today.
DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
// Yesterday. // Yesterday.
DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
]; ];
$this->nodes = []; $this->nodes = [];
foreach ($dates as $date) { foreach ($this->dates as $date) {
$this->nodes[] = $this->drupalCreateNode([ $this->nodes[] = $this->drupalCreateNode([
$this->fieldName => [ $this->fieldName => [
'value' => $date, 'value' => $date,
@ -121,12 +127,26 @@ class FilterDateTest extends BrowserTestBase {
$this->submitForm([], 'Expose filter'); $this->submitForm([], 'Expose filter');
$this->submitForm([], 'Grouped filters'); $this->submitForm([], 'Grouped filters');
// Test operators with different amount of expected values.
$edit = []; $edit = [];
// No values are required.
$edit['options[group_info][group_items][1][title]'] = 'empty'; $edit['options[group_info][group_items][1][title]'] = 'empty';
$edit['options[group_info][group_items][1][operator]'] = '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][title]'] = 'not empty';
$edit['options[group_info][group_items][2][operator]'] = 'not empty'; $edit['options[group_info][group_items][2][operator]'] = 'not empty';
// One value is required.
$edit['options[group_info][group_items][3][title]'] = 'less than';
$edit['options[group_info][group_items][3][operator]'] = '<';
$edit['options[group_info][group_items][3][value][value]'] = $this->dates[0];
// Two values are required (min and max).
$this->submitForm($edit, 'Add another item');
$edit['options[group_info][group_items][4][title]'] = 'between';
$edit['options[group_info][group_items][4][operator]'] = 'between';
$edit['options[group_info][group_items][4][value][type]'] = 'offset';
$edit['options[group_info][group_items][4][value][min]'] = '-2 hours';
$edit['options[group_info][group_items][4][value][max]'] = '+2 hours';
$this->submitForm($edit, 'Apply'); $this->submitForm($edit, 'Apply');
// Test that the exposed filter works as expected. // Test that the exposed filter works as expected.
@ -142,14 +162,40 @@ class FilterDateTest extends BrowserTestBase {
// Filter the Preview by 'empty'. // Filter the Preview by 'empty'.
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(1); $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(1);
$this->getSession()->getPage()->pressButton('Apply'); $this->getSession()->getPage()->pressButton('Apply');
$results = $this->cssSelect('.views-row .field-content'); $this->assertIds([4]);
$this->assertCount(1, $results);
// Filter the Preview by 'not empty'. // Filter the Preview by 'not empty'.
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(2); $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(2);
$this->getSession()->getPage()->pressButton('Apply'); $this->getSession()->getPage()->pressButton('Apply');
$results = $this->cssSelect('.views-row .field-content'); $this->assertIds([1, 2, 3]);
$this->assertCount(3, $results);
// Filter the Preview by 'less than'.
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(3);
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([2, 3]);
// Filter the Preview by 'between'.
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(4);
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([2]);
}
/**
* Ensures that a given list of items appear on the view result.
*
* @param array $expected_ids
* An array of IDs.
*/
protected function assertIds(array $expected_ids = []): void {
// First verify the count.
$elements = $this->cssSelect('.views-row .field-content');
$this->assertCount(count($expected_ids), $elements);
$actual_ids = [];
foreach ($elements as $element) {
$actual_ids[] = (int) $element->getText();
}
$this->assertEquals($expected_ids, $actual_ids);
} }
} }