Skip to content

Commit 93dbcc0

Browse files
committed
ACP2E-2127: Date filter is not working in admin grid.
- Fixed the existing test coverage issue and added the test coverage.
1 parent 26dc09b commit 93dbcc0

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

app/code/Magento/Ui/Component/Filters/Type/Date.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
*/
1717
class Date extends AbstractFilter
1818
{
19-
const NAME = 'filter_date';
19+
public const NAME = 'filter_date';
2020

21-
const COMPONENT = 'date';
21+
public const COMPONENT = 'date';
2222

2323
/**
24-
* Wrapped component
24+
* Wrapped component for date grid filter UI Component
2525
*
2626
* @var DataTypeDate
2727
*/
2828
protected $wrappedComponent;
2929

3030
/**
31-
* Date format
31+
* Date format for date grid filter UI Component
3232
*
3333
* @var string
3434
* @since 100.1.2
@@ -84,7 +84,7 @@ protected function applyFilter()
8484

8585
if (is_array($value)) {
8686
if (isset($value['from'])) {
87-
if (!$this->getData('config/options/showsTime')) {
87+
if (!$this->getData('config/options/showsTime') && $this->getData('config/dateFormat')) {
8888
$value['from'] = $this->wrappedComponent->convertDateFormat(
8989
$value['from'],
9090
$this->getData('config/dateFormat')
@@ -97,7 +97,7 @@ protected function applyFilter()
9797
}
9898

9999
if (isset($value['to'])) {
100-
if (!$this->getData('config/options/showsTime')) {
100+
if (!$this->getData('config/options/showsTime') && $this->getData('config/dateFormat')) {
101101
$value['to'] = $this->wrappedComponent->convertDateFormat(
102102
$value['to'],
103103
$this->getData('config/dateFormat')

app/code/Magento/Ui/Test/Unit/Component/Filters/Type/DateTest.php

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,18 @@ public function testGetComponentName(): void
9797
* @param bool $showsTime
9898
* @param array $filterData
9999
* @param array|null $expectedCondition
100+
* @param string|null $dateFormat
100101
*
101102
* @return void
102103
* @dataProvider getPrepareDataProvider
103104
*/
104-
public function testPrepare(string $name, bool $showsTime, array $filterData, ?array $expectedCondition): void
105-
{
105+
public function testPrepare(
106+
string $name,
107+
bool $showsTime,
108+
array $filterData,
109+
?array $expectedCondition,
110+
?string $dateFormat
111+
): void {
106112
$processor = $this->getMockBuilder(Processor::class)
107113
->disableOriginalConstructor()
108114
->getMock();
@@ -132,7 +138,7 @@ public function testPrepare(string $name, bool $showsTime, array $filterData, ?a
132138
->willReturn($this->dataProviderMock);
133139

134140
if ($expectedCondition !== null) {
135-
$this->processFilters($name, $showsTime, $filterData, $expectedCondition, $uiComponent);
141+
$this->processFilters($name, $showsTime, $filterData, $expectedCondition, $dateFormat, $uiComponent);
136142
}
137143

138144
$this->uiComponentFactory->expects($this->any())
@@ -148,7 +154,7 @@ public function testPrepare(string $name, bool $showsTime, array $filterData, ?a
148154
[],
149155
[
150156
'name' => $name,
151-
'config' => ['options' => ['showsTime' => $showsTime]],
157+
'config' => ['options' => ['showsTime' => $showsTime], 'dateFormat' => $dateFormat],
152158
]
153159
);
154160
$date->prepare();
@@ -164,13 +170,15 @@ public function getPrepareDataProvider(): array
164170
'name' => 'test_date',
165171
'showsTime' => false,
166172
'filterData' => ['test_date' => ['from' => '11-05-2015', 'to' => null]],
167-
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'gteq']
173+
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'gteq'],
174+
'dateFormat' => null
168175
],
169176
[
170177
'name' => 'test_date',
171178
'showsTime' => false,
172179
'filterData' => ['test_date' => ['from' => null, 'to' => '11-05-2015']],
173-
'expectedCondition' => ['date' => '2015-05-11 23:59:59', 'type' => 'lteq']
180+
'expectedCondition' => ['date' => '2015-05-11 23:59:59', 'type' => 'lteq'],
181+
'dateFormat' => null
174182
],
175183
[
176184
'name' => 'test_date',
@@ -179,19 +187,22 @@ public function getPrepareDataProvider(): array
179187
'expectedCondition' => [
180188
'date_from' => '2015-05-11 00:00:00', 'type_from' => 'gteq',
181189
'date_to' => '2015-05-11 23:59:59', 'type_to' => 'lteq'
182-
]
190+
],
191+
'dateFormat' => null
183192
],
184193
[
185194
'name' => 'test_date',
186195
'showsTime' => false,
187196
'filterData' => ['test_date' => '11-05-2015'],
188-
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'eq']
197+
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'eq'],
198+
'dateFormat' => null
189199
],
190200
[
191201
'name' => 'test_date',
192202
'showsTime' => false,
193203
'filterData' => ['test_date' => ['from' => '', 'to' => '']],
194-
'expectedCondition' => null
204+
'expectedCondition' => null,
205+
'dateFormat' => null
195206
],
196207
[
197208
'name' => 'test_date',
@@ -200,7 +211,18 @@ public function getPrepareDataProvider(): array
200211
'expectedCondition' => [
201212
'date_from' => '2015-05-11 10:20:00', 'type_from' => 'gteq',
202213
'date_to' => '2015-05-11 18:25:00', 'type_to' => 'lteq'
203-
]
214+
],
215+
'dateFormat' => null
216+
],
217+
[
218+
'name' => 'test_date',
219+
'showsTime' => false,
220+
'filterData' => ['test_date' => ['from' => '2015-05-11', 'to' => '2015-05-11']],
221+
'expectedCondition' => [
222+
'date_from' => '2015-05-11 00:00:00', 'type_from' => 'gteq',
223+
'date_to' => '2015-05-11 23:59:59', 'type_to' => 'lteq'
224+
],
225+
'dateFormat' => 'Y-MM-dd'
204226
]
205227
];
206228
}
@@ -210,6 +232,7 @@ public function getPrepareDataProvider(): array
210232
* @param bool $showsTime
211233
* @param array $filterData
212234
* @param array $expectedCondition
235+
* @param string|null $dateFormat
213236
* @param MockObject $uiComponent
214237
*
215238
* @return void
@@ -220,6 +243,7 @@ private function processFilters(
220243
bool $showsTime,
221244
array $filterData,
222245
array $expectedCondition,
246+
string|null $dateFormat,
223247
FormDate $uiComponent
224248
): void {
225249
if (is_string($filterData[$name])) {
@@ -239,6 +263,18 @@ private function processFilters(
239263
]
240264
);
241265
} else {
266+
if ($dateFormat === 'Y-MM-dd') {
267+
$uiComponent->method('convertDateFormat')
268+
->willReturn(
269+
date_format(date_create($filterData[$name]['from']), 'm/d/Y')
270+
);
271+
$uiComponent->method('convertDateFormat')
272+
->willReturn(
273+
date_format(date_create($filterData[$name]['to']), 'm/d/Y')
274+
);
275+
$filterData[$name]['from'] = date_format(date_create($filterData[$name]['from']), 'm/d/Y');
276+
$filterData[$name]['to'] = date_format(date_create($filterData[$name]['to']), 'm/d/Y');
277+
}
242278
$from = new \DateTime($filterData[$name]['from'] ?? 'now');
243279
$to = new \DateTime($filterData[$name]['to'] ? $filterData[$name]['to'] . ' 23:59:59' : 'now');
244280
$uiComponent->method('convertDate')

app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType;
99

10+
use Exception;
1011
use Magento\Framework\Locale\ResolverInterface;
1112
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1213
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -171,6 +172,33 @@ public function testConvertDatetime(string $dateStr, bool $setUtcTimeZone, strin
171172
);
172173
}
173174

175+
/**
176+
* Test to Convert given date to specific date format based on locale
177+
*
178+
* @throws Exception
179+
*/
180+
public function testConvertDateFormat()
181+
{
182+
$dateStr = '2023-10-11';
183+
$dateFormat = 'Y-MM-dd';
184+
$this->date = $this->objectManagerHelper->getObject(
185+
Date::class,
186+
[
187+
'localeDate' => $this->localeDateMock,
188+
]
189+
);
190+
$this->localeDateMock->expects($this->once())
191+
->method('formatDateTime')
192+
->with(
193+
$dateStr
194+
)
195+
->willReturn(date_format(date_create($dateStr), 'm/d/Y'));
196+
$this->assertEquals(
197+
date_format(date_create($dateStr), 'm/d/Y'),
198+
$this->date->convertDateFormat($dateStr, $dateFormat)
199+
);
200+
}
201+
174202
/**
175203
* @return array
176204
*/

0 commit comments

Comments
 (0)