Skip to content

Commit faefcde

Browse files
SarmisthaSarmistha
authored andcommitted
Merge remote-tracking branch 'magento-l3/ACP2E-2127' into Tier4-Kings-PR-11-08-2023
2 parents de0507f + f14414a commit faefcde

File tree

2 files changed

+124
-4
lines changed
  • app/code/Magento/Ui

2 files changed

+124
-4
lines changed

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
*/
66
namespace Magento\Ui\Component\Form\Element\DataType;
77

8+
use Exception;
89
use Magento\Framework\Locale\ResolverInterface;
910
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1011
use Magento\Framework\View\Element\UiComponentInterface;
1112
use Magento\Framework\View\Element\UiComponent\ContextInterface;
13+
use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory;
14+
use Magento\Framework\App\ObjectManager;
1215

1316
/**
1417
* UI component date type
1518
*/
1619
class Date extends AbstractDataType
1720
{
18-
const NAME = 'date';
21+
public const NAME = 'date';
1922

2023
/**
2124
* Current locale
@@ -25,7 +28,7 @@ class Date extends AbstractDataType
2528
protected $locale;
2629

2730
/**
28-
* Wrapped component
31+
* Wrapped component for date type
2932
*
3033
* @var UiComponentInterface
3134
*/
@@ -36,6 +39,11 @@ class Date extends AbstractDataType
3639
*/
3740
private $localeDate;
3841

42+
/**
43+
* @var DateFormatterFactory
44+
*/
45+
private $dateFormatterFactory;
46+
3947
/**
4048
* Constructor
4149
*
@@ -44,17 +52,21 @@ class Date extends AbstractDataType
4452
* @param ResolverInterface $localeResolver
4553
* @param array $components
4654
* @param array $data
55+
* @param DateFormatterFactory|null $dateFormatterFactory
4756
*/
4857
public function __construct(
4958
ContextInterface $context,
5059
TimezoneInterface $localeDate,
5160
ResolverInterface $localeResolver,
5261
array $components = [],
53-
array $data = []
62+
array $data = [],
63+
?DateFormatterFactory $dateFormatterFactory = null
5464
) {
5565
$this->locale = $localeResolver->getLocale();
5666
$this->localeDate = $localeDate;
5767
parent::__construct($context, $components, $data);
68+
$objectManager = ObjectManager::getInstance();
69+
$this->dateFormatterFactory = $dateFormatterFactory ?? $objectManager->get(DateFormatterFactory::class);
5870
}
5971

6072
/**
@@ -111,14 +123,15 @@ public function getComponentName()
111123
public function convertDate($date, $hour = 0, $minute = 0, $second = 0, $setUtcTimeZone = true)
112124
{
113125
try {
126+
$date = $this->convertDateFormat($date);
114127
$dateObj = $this->localeDate->date($date, $this->getLocale(), false, false);
115128
$dateObj->setTime($hour, $minute, $second);
116129
//convert store date to default date in UTC timezone without DST
117130
if ($setUtcTimeZone) {
118131
$dateObj->setTimezone(new \DateTimeZone('UTC'));
119132
}
120133
return $dateObj;
121-
} catch (\Exception $e) {
134+
} catch (Exception $e) {
122135
return null;
123136
}
124137
}
@@ -144,4 +157,30 @@ public function convertDatetime(string $date, bool $setUtcTimezone = true): ?\Da
144157
return null;
145158
}
146159
}
160+
161+
/**
162+
* Convert given date to specific date format based on locale
163+
*
164+
* @param string $date
165+
* @return String
166+
* @throws Exception
167+
*/
168+
public function convertDateFormat(string $date): String
169+
{
170+
$formatter = $this->dateFormatterFactory->create(
171+
$this->getLocale(),
172+
\IntlDateFormatter::SHORT,
173+
\IntlDateFormatter::NONE,
174+
date_default_timezone_get()
175+
);
176+
177+
$formatter->setLenient(false);
178+
if (!$formatter->parse($date)) {
179+
$date = $formatter->formatObject(
180+
new \DateTime($date),
181+
$formatter->getPattern()
182+
);
183+
}
184+
return $date;
185+
}
147186
}

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

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

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

10+
use Exception;
1011
use Magento\Framework\Locale\ResolverInterface;
12+
use Magento\Framework\Stdlib\DateTime\Intl\DateFormatterFactory;
1113
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1214
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1315
use Magento\Framework\View\Element\UiComponent\Context;
@@ -36,6 +38,11 @@ class DateTest extends TestCase
3638
/** @var ObjectManager */
3739
private $objectManagerHelper;
3840

41+
/**
42+
* @var DateFormatterFactory|MockObject
43+
*/
44+
private $dateFormatterFactoryMock;
45+
3946
/**
4047
* @inheritdoc
4148
*/
@@ -47,6 +54,7 @@ protected function setUp(): void
4754
$this->objectManagerHelper = new ObjectManager($this);
4855
$this->processorMock = $this->createMock(Processor::class);
4956
$this->contextMock->method('getProcessor')->willReturn($this->processorMock);
57+
$this->dateFormatterFactoryMock = $this->getMockForAbstractClass(DateFormatterFactory::class);
5058
}
5159

5260
/**
@@ -182,4 +190,77 @@ public function convertDatetimeDataProvider(): array
182190
['2019-09-30T12:32:00.000Z', true, '2019-09-30 19:32:00'],
183191
];
184192
}
193+
194+
/**
195+
* Run test for convertDateFormat() method
196+
*
197+
* @param string $date
198+
* @param string $locale
199+
* @param string $expected
200+
* @return void
201+
* @dataProvider convertDateFormatDataProvider
202+
* @throws Exception
203+
*/
204+
public function testConvertDateFormat(
205+
string $date,
206+
string $locale,
207+
string $expected
208+
): void {
209+
$this->localeResolverMock
210+
->expects($this->any())
211+
->method('getLocale')
212+
->willReturn($locale);
213+
$this->date = $this->objectManagerHelper->getObject(
214+
Date::class,
215+
[
216+
'localeResolver' => $this->localeResolverMock,
217+
'dateFormatterFactory' => $this->dateFormatterFactoryMock
218+
]
219+
);
220+
$this->assertEquals(
221+
$expected,
222+
$this->date->convertDateFormat($date)
223+
);
224+
}
225+
226+
/**
227+
* DataProvider for testConvertDateFormat()
228+
*
229+
* @return array
230+
*/
231+
public function convertDateFormatDataProvider(): array
232+
{
233+
return [
234+
[
235+
'2023-10-15',
236+
'en_US',
237+
'10/15/2023'
238+
],
239+
[
240+
'10/15/2023',
241+
'en_US',
242+
'10/15/2023'
243+
],
244+
[
245+
'2023-10-15',
246+
'en_GB',
247+
'15/10/2023'
248+
],
249+
[
250+
'15/10/2023',
251+
'en_GB',
252+
'15/10/2023'
253+
],
254+
[
255+
'2023-10-15',
256+
'ja_JP',
257+
'2023/10/15'
258+
],
259+
[
260+
'2023/10/15',
261+
'ja_JP',
262+
'2023/10/15'
263+
]
264+
];
265+
}
185266
}

0 commit comments

Comments
 (0)