Skip to content

Commit 701d1f8

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98886' into 2.3-develop-pr20
2 parents 2dc2fa7 + 9d09177 commit 701d1f8

File tree

2 files changed

+47
-17
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/Data/Form/Element
  • lib/internal/Magento/Framework/Data/Form/Element

2 files changed

+47
-17
lines changed

dev/tests/integration/testsuite/Magento/Framework/Data/Form/Element/DateTest.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,51 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
namespace Magento\Framework\Data\Form\Element;
8+
9+
use Magento\Framework\Data\Form\ElementFactory;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
712
/**
813
* Tests for \Magento\Framework\Data\Form\Element\Date
914
*/
10-
namespace Magento\Framework\Data\Form\Element;
11-
1215
class DateTest extends \PHPUnit\Framework\TestCase
1316
{
1417
/**
15-
* @var \Magento\Framework\Data\Form\ElementFactory
18+
* @var ElementFactory
1619
*/
17-
protected $_elementFactory;
20+
private $elementFactory;
1821

1922
/**
20-
* SetUp method
23+
* @inheritdoc
2124
*/
2225
protected function setUp()
2326
{
24-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
25-
$this->_elementFactory = $objectManager->create(\Magento\Framework\Data\Form\ElementFactory::class);
27+
$objectManager = Bootstrap::getObjectManager();
28+
$this->elementFactory = $objectManager->create(ElementFactory::class);
2629
}
2730

2831
/**
32+
* Test get value
33+
*
34+
* @param array $data
35+
* @param string $expect
36+
* @return void
2937
* @dataProvider getValueDataProvider
3038
*/
31-
public function testGetValue(array $data, $expect)
39+
public function testGetValue(array $data, string $expect): void
3240
{
33-
/** @var $date \Magento\Framework\Data\Form\Element\Date */
34-
$date = $this->_elementFactory->create(\Magento\Framework\Data\Form\Element\Date::class, $data);
41+
/** @var $date Date */
42+
$date = $this->elementFactory->create(Date::class, $data);
3543
$this->assertEquals($expect, $date->getValue());
3644
}
3745

3846
/**
47+
* Get value test data provider
48+
*
3949
* @return array
4050
*/
41-
public function getValueDataProvider()
51+
public function getValueDataProvider(): array
4252
{
4353
$testTimestamp = strtotime('2014-05-18 12:08:16');
4454
$date = new \DateTime('@' . $testTimestamp);
@@ -56,15 +66,22 @@ public function getValueDataProvider()
5666
'time_format' => 'h:mm a',
5767
'value' => $testTimestamp,
5868
],
59-
$date->format('g:i A')
69+
$date->format('g:i A'),
6070
],
6171
[
6272
[
6373
'date_format' => 'MM/d/yy',
6474
'value' => $testTimestamp,
6575
],
66-
$date->format('m/j/y')
67-
]
76+
$date->format('m/j/y'),
77+
],
78+
[
79+
[
80+
'date_format' => 'd-MM-Y',
81+
'value' => $date->format('d-m-Y'),
82+
],
83+
$date->format('d-m-Y'),
84+
],
6885
];
6986
}
7087
}

lib/internal/Magento/Framework/Data/Form/Element/Date.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public function __construct(
5353
}
5454
}
5555

56+
/**
57+
* Check if a string is a date value
58+
*
59+
* @param string $value
60+
* @return bool
61+
*/
62+
private function isDate(string $value): bool
63+
{
64+
$date = date_parse($value);
65+
66+
return !empty($date['year']) && !empty($date['month']) && !empty($date['day']);
67+
}
68+
5669
/**
5770
* If script executes on x64 system, converts large numeric values to timestamp limit
5871
*
@@ -85,13 +98,13 @@ public function setValue($value)
8598
$this->_value = $value;
8699
return $this;
87100
}
88-
89101
try {
90102
if (preg_match('/^[0-9]+$/', $value)) {
91103
$this->_value = (new \DateTime())->setTimestamp($this->_toTimestamp($value));
104+
} elseif (is_string($value) && $this->isDate($value)) {
105+
$this->_value = new \DateTime($value, new \DateTimeZone($this->localeDate->getConfigTimezone()));
92106
} else {
93-
$this->_value = new \DateTime($value);
94-
$this->_value->setTimezone(new \DateTimeZone($this->localeDate->getConfigTimezone()));
107+
$this->_value = '';
95108
}
96109
} catch (\Exception $e) {
97110
$this->_value = '';

0 commit comments

Comments
 (0)