Skip to content

Commit 4157996

Browse files
committed
Issue #2865344 by mpdonadio, Lendude, mbovan, organicwire, alexpott, jibran, jhedstrom, bobemoe, Berdir, larowlan: Exposed date filters 'empty' and 'not empty' are broken
(cherry picked from commit 8e3bcf4)
1 parent 8e87a74 commit 4157996

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Drupal\Tests\datetime\Functional\Views;
4+
5+
use Drupal\Core\Datetime\DrupalDateTime;
6+
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
7+
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
8+
use Drupal\field\Entity\FieldStorageConfig;
9+
use Drupal\field\Entity\FieldConfig;
10+
use Drupal\Tests\BrowserTestBase;
11+
use Drupal\views\Tests\ViewTestData;
12+
13+
/**
14+
* Tests Views filters for datetime fields.
15+
*
16+
* @group datetime
17+
*/
18+
class FilterDateTest extends BrowserTestBase {
19+
20+
/**
21+
* Name of the field.
22+
*
23+
* Note, this is used in the default test view.
24+
*
25+
* @var string
26+
*/
27+
protected $fieldName = 'field_date';
28+
29+
/**
30+
* Nodes to test.
31+
*
32+
* @var \Drupal\node\NodeInterface[]
33+
*/
34+
protected $nodes = [];
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public static $modules = [
40+
'datetime',
41+
'datetime_test',
42+
'node',
43+
'views',
44+
'views_ui',
45+
];
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public static $testViews = ['test_filter_datetime'];
51+
52+
/**
53+
* {@inheritdoc}
54+
*
55+
* Create nodes with relative dates of yesterday, today, and tomorrow.
56+
*/
57+
protected function setUp() {
58+
parent::setUp();
59+
60+
$now = \Drupal::time()->getRequestTime();
61+
62+
$admin_user = $this->drupalCreateUser(['administer views']);
63+
$this->drupalLogin($admin_user);
64+
65+
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
66+
67+
// Add a date field to page nodes.
68+
$fieldStorage = FieldStorageConfig::create([
69+
'field_name' => $this->fieldName,
70+
'entity_type' => 'node',
71+
'type' => 'datetime',
72+
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
73+
]);
74+
$fieldStorage->save();
75+
$field = FieldConfig::create([
76+
'field_storage' => $fieldStorage,
77+
'bundle' => 'page',
78+
'required' => TRUE,
79+
]);
80+
$field->save();
81+
82+
// Create some nodes.
83+
$dates = [
84+
// Tomorrow.
85+
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
86+
// Today.
87+
DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
88+
// Yesterday.
89+
DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT),
90+
];
91+
92+
$this->nodes = [];
93+
foreach ($dates as $date) {
94+
$this->nodes[] = $this->drupalCreateNode([
95+
$this->fieldName => [
96+
'value' => $date,
97+
],
98+
]);
99+
}
100+
// Add a node where the date field is empty.
101+
$this->nodes[] = $this->drupalCreateNode();
102+
103+
// Views needs to be aware of the new field.
104+
$this->container->get('views.views_data')->clear();
105+
106+
// Load test views.
107+
ViewTestData::createTestViews(get_class($this), ['datetime_test']);
108+
}
109+
110+
/**
111+
* Tests exposed grouped filters.
112+
*/
113+
public function testExposedGroupedFilters() {
114+
// Expose the empty and not empty operators in a grouped filter.
115+
$this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $this->fieldName . '_value', [], t('Expose filter'));
116+
$this->drupalPostForm(NULL, [], 'Grouped filters');
117+
118+
$edit = [];
119+
$edit['options[group_info][group_items][1][title]'] = 'empty';
120+
$edit['options[group_info][group_items][1][operator]'] = 'empty';
121+
$edit['options[group_info][group_items][2][title]'] = 'not empty';
122+
$edit['options[group_info][group_items][2][operator]'] = 'not empty';
123+
124+
$this->drupalPostForm(NULL, $edit, 'Apply');
125+
126+
// Test that the exposed filter works as expected.
127+
$path = 'test_filter_datetime-path';
128+
$this->drupalPostForm('admin/structure/views/view/test_filter_datetime/edit', [], 'Add Page');
129+
$this->drupalPostForm('admin/structure/views/nojs/display/test_filter_datetime/page_1/path', ['path' => $path], 'Apply');
130+
$this->drupalPostForm(NULL, [], t('Save'));
131+
132+
$this->drupalGet($path);
133+
134+
// Filter the Preview by 'empty'.
135+
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(1);
136+
$this->getSession()->getPage()->pressButton('Apply');
137+
$results = $this->cssSelect('.view-content .field-content');
138+
$this->assertEquals(1, count($results));
139+
140+
// Filter the Preview by 'not empty'.
141+
$this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(2);
142+
$this->getSession()->getPage()->pressButton('Apply');
143+
$results = $this->cssSelect('.view-content .field-content');
144+
$this->assertEquals(3, count($results));
145+
}
146+
147+
}

modules/datetime/tests/src/Kernel/Views/FilterDateTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ protected function setUp($import_test_views = TRUE) {
7171
$node->save();
7272
$this->nodes[] = $node;
7373
}
74+
75+
// Add a node where the date field is empty.
76+
$node = Node::create([
77+
'title' => $this->randomMachineName(8),
78+
'type' => 'page',
79+
'field_date' => [],
80+
]);
81+
$node->save();
82+
$this->nodes[] = $node;
7483
}
7584

7685
/**
@@ -130,6 +139,30 @@ public function testDateOffsets() {
130139
];
131140
$this->assertIdenticalResultset($view, $expected_result, $this->map);
132141
$view->destroy();
142+
143+
// Test the empty operator.
144+
$view->initHandlers();
145+
$view->filter[$field]->operator = 'empty';
146+
$view->setDisplay('default');
147+
$this->executeView($view);
148+
$expected_result = [
149+
['nid' => $this->nodes[3]->id()],
150+
];
151+
$this->assertIdenticalResultset($view, $expected_result, $this->map);
152+
$view->destroy();
153+
154+
// Test the not empty operator.
155+
$view->initHandlers();
156+
$view->filter[$field]->operator = 'not empty';
157+
$view->setDisplay('default');
158+
$this->executeView($view);
159+
$expected_result = [
160+
['nid' => $this->nodes[0]->id()],
161+
['nid' => $this->nodes[1]->id()],
162+
['nid' => $this->nodes[2]->id()],
163+
];
164+
$this->assertIdenticalResultset($view, $expected_result, $this->map);
165+
$view->destroy();
133166
}
134167
}
135168

modules/views/src/Plugin/views/filter/Date.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,15 @@ public function acceptExposedInput($input) {
148148
}
149149

150150
if ($operators[$operator]['values'] == 1) {
151+
// When the operator is either <, <=, =, !=, >=, > or regular_expression
152+
// the input contains only one value.
151153
if ($this->value['value'] == '') {
152154
return FALSE;
153155
}
154156
}
155-
else {
157+
elseif ($operators[$operator]['values'] == 2) {
158+
// When the operator is either between or not between the input contains
159+
// two values.
156160
if ($this->value['min'] == '' || $this->value['max'] == '') {
157161
return FALSE;
158162
}

0 commit comments

Comments
 (0)