Skip to content

Commit 55da81c

Browse files
committed
ACP2E-2127: Date filter is not working in admin grid.
- Added the different solution.
1 parent e03bc48 commit 55da81c

File tree

3 files changed

+34
-82
lines changed

3 files changed

+34
-82
lines changed

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1010
use Magento\Framework\View\Element\UiComponentInterface;
1111
use Magento\Framework\View\Element\UiComponent\ContextInterface;
12+
use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
/**
1416
* UI component date type
@@ -36,6 +38,11 @@ class Date extends AbstractDataType
3638
*/
3739
private $localeDate;
3840

41+
/**
42+
* @var DateFormatterFactory
43+
*/
44+
private $dateFormatterFactory;
45+
3946
/**
4047
* Constructor
4148
*
@@ -44,17 +51,21 @@ class Date extends AbstractDataType
4451
* @param ResolverInterface $localeResolver
4552
* @param array $components
4653
* @param array $data
54+
* @param DateFormatterFactory|null $dateFormatterFactory
4755
*/
4856
public function __construct(
4957
ContextInterface $context,
5058
TimezoneInterface $localeDate,
5159
ResolverInterface $localeResolver,
5260
array $components = [],
53-
array $data = []
61+
array $data = [],
62+
?DateFormatterFactory $dateFormatterFactory = null
5463
) {
5564
$this->locale = $localeResolver->getLocale();
5665
$this->localeDate = $localeDate;
5766
parent::__construct($context, $components, $data);
67+
$objectManager = ObjectManager::getInstance();
68+
$this->dateFormatterFactory = $dateFormatterFactory ?? $objectManager->get(DateFormatterFactory::class);
5869
}
5970

6071
/**
@@ -153,15 +164,20 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da
153164
*/
154165
public function convertDateFormat(string $date): String
155166
{
156-
if ($this->getLocale() === 'en_GB' && str_contains($date, '/')) {
157-
$date = \DateTime::createFromFormat('d/m/Y', $date);
158-
}
159-
return $this->localeDate->formatDateTime(
160-
$date,
161-
null,
162-
null,
167+
$formatter = $this->dateFormatterFactory->create(
163168
$this->getLocale(),
169+
\IntlDateFormatter::SHORT,
170+
\IntlDateFormatter::NONE,
164171
date_default_timezone_get()
165172
);
173+
$formatter->setLenient(false);
174+
if (!$formatter->parse($date)) {
175+
$date = $formatter->formatObject(
176+
new \DateTime($date),
177+
$formatter->getPattern()
178+
);
179+
}
180+
$formatter->setLenient(true);
181+
return $date;
166182
}
167183
}

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

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,12 @@ public function testGetComponentName(): void
9797
* @param bool $showsTime
9898
* @param array $filterData
9999
* @param array|null $expectedCondition
100-
* @param string|null $dateFormat
101100
*
102101
* @return void
103102
* @dataProvider getPrepareDataProvider
104103
*/
105-
public function testPrepare(
106-
string $name,
107-
bool $showsTime,
108-
array $filterData,
109-
?array $expectedCondition,
110-
?string $dateFormat
111-
): void {
104+
public function testPrepare(string $name, bool $showsTime, array $filterData, ?array $expectedCondition): void
105+
{
112106
$processor = $this->getMockBuilder(Processor::class)
113107
->disableOriginalConstructor()
114108
->getMock();
@@ -138,7 +132,7 @@ public function testPrepare(
138132
->willReturn($this->dataProviderMock);
139133

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

144138
$this->uiComponentFactory->expects($this->any())
@@ -154,7 +148,7 @@ public function testPrepare(
154148
[],
155149
[
156150
'name' => $name,
157-
'config' => ['options' => ['showsTime' => $showsTime], 'dateFormat' => $dateFormat],
151+
'config' => ['options' => ['showsTime' => $showsTime]],
158152
]
159153
);
160154
$date->prepare();
@@ -170,15 +164,13 @@ public function getPrepareDataProvider(): array
170164
'name' => 'test_date',
171165
'showsTime' => false,
172166
'filterData' => ['test_date' => ['from' => '11-05-2015', 'to' => null]],
173-
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'gteq'],
174-
'dateFormat' => null
167+
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'gteq']
175168
],
176169
[
177170
'name' => 'test_date',
178171
'showsTime' => false,
179172
'filterData' => ['test_date' => ['from' => null, 'to' => '11-05-2015']],
180-
'expectedCondition' => ['date' => '2015-05-11 23:59:59', 'type' => 'lteq'],
181-
'dateFormat' => null
173+
'expectedCondition' => ['date' => '2015-05-11 23:59:59', 'type' => 'lteq']
182174
],
183175
[
184176
'name' => 'test_date',
@@ -187,22 +179,19 @@ public function getPrepareDataProvider(): array
187179
'expectedCondition' => [
188180
'date_from' => '2015-05-11 00:00:00', 'type_from' => 'gteq',
189181
'date_to' => '2015-05-11 23:59:59', 'type_to' => 'lteq'
190-
],
191-
'dateFormat' => null
182+
]
192183
],
193184
[
194185
'name' => 'test_date',
195186
'showsTime' => false,
196187
'filterData' => ['test_date' => '11-05-2015'],
197-
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'eq'],
198-
'dateFormat' => null
188+
'expectedCondition' => ['date' => '2015-05-11 00:00:00', 'type' => 'eq']
199189
],
200190
[
201191
'name' => 'test_date',
202192
'showsTime' => false,
203193
'filterData' => ['test_date' => ['from' => '', 'to' => '']],
204-
'expectedCondition' => null,
205-
'dateFormat' => null
194+
'expectedCondition' => null
206195
],
207196
[
208197
'name' => 'test_date',
@@ -211,18 +200,7 @@ public function getPrepareDataProvider(): array
211200
'expectedCondition' => [
212201
'date_from' => '2015-05-11 10:20:00', 'type_from' => 'gteq',
213202
'date_to' => '2015-05-11 18:25:00', 'type_to' => 'lteq'
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'
203+
]
226204
]
227205
];
228206
}
@@ -232,19 +210,16 @@ public function getPrepareDataProvider(): array
232210
* @param bool $showsTime
233211
* @param array $filterData
234212
* @param array $expectedCondition
235-
* @param string|null $dateFormat
236213
* @param MockObject $uiComponent
237214
*
238215
* @return void
239216
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
240-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
241217
*/
242218
private function processFilters(
243219
string $name,
244220
bool $showsTime,
245221
array $filterData,
246222
array $expectedCondition,
247-
string|null $dateFormat,
248223
FormDate $uiComponent
249224
): void {
250225
if (is_string($filterData[$name])) {
@@ -264,18 +239,6 @@ private function processFilters(
264239
]
265240
);
266241
} else {
267-
if ($dateFormat === 'Y-MM-dd') {
268-
$uiComponent->method('convertDateFormat')
269-
->willReturn(
270-
date_format(date_create($filterData[$name]['from']), 'm/d/Y')
271-
);
272-
$uiComponent->method('convertDateFormat')
273-
->willReturn(
274-
date_format(date_create($filterData[$name]['to']), 'm/d/Y')
275-
);
276-
$filterData[$name]['from'] = date_format(date_create($filterData[$name]['from']), 'm/d/Y');
277-
$filterData[$name]['to'] = date_format(date_create($filterData[$name]['to']), 'm/d/Y');
278-
}
279242
$from = new \DateTime($filterData[$name]['from'] ?? 'now');
280243
$to = new \DateTime($filterData[$name]['to'] ? $filterData[$name]['to'] . ' 23:59:59' : 'now');
281244
$uiComponent->method('convertDate')

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

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

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

10-
use Exception;
1110
use Magento\Framework\Locale\ResolverInterface;
1211
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1312
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -172,32 +171,6 @@ public function testConvertDatetime(string $dateStr, bool $setUtcTimeZone, strin
172171
);
173172
}
174173

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-
$this->date = $this->objectManagerHelper->getObject(
184-
Date::class,
185-
[
186-
'localeDate' => $this->localeDateMock,
187-
]
188-
);
189-
$this->localeDateMock->expects($this->once())
190-
->method('formatDateTime')
191-
->with(
192-
$dateStr
193-
)
194-
->willReturn(date_format(date_create($dateStr), 'm/d/Y'));
195-
$this->assertEquals(
196-
date_format(date_create($dateStr), 'm/d/Y'),
197-
$this->date->convertDateFormat($dateStr)
198-
);
199-
}
200-
201174
/**
202175
* @return array
203176
*/

0 commit comments

Comments
 (0)