Skip to content

Commit 77d2b07

Browse files
author
Viktoriia Chernikova
committed
Merge remote-tracking branch 'origin/MAGETWO-92389' into vikapr
2 parents 415b1a9 + 6eccdd4 commit 77d2b07

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

app/code/Magento/Eav/Model/Entity/Attribute.php

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

88
use Magento\Framework\Api\AttributeValueFactory;
99
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Stdlib\DateTime;
1011
use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
1112

1213
/**
@@ -276,12 +277,10 @@ public function beforeSave()
276277

277278
// save default date value as timestamp
278279
if ($hasDefaultValue) {
279-
$format = $this->_localeDate->getDateFormat(
280-
\IntlDateFormatter::SHORT
281-
);
282280
try {
283-
$defaultValue = $this->dateTimeFormatter->formatObject(new \DateTime($defaultValue), $format);
284-
$this->setDefaultValue($defaultValue);
281+
$locale = $this->_localeResolver->getLocale();
282+
$defaultValue = $this->_localeDate->date($defaultValue, $locale, false, false);
283+
$this->setDefaultValue($defaultValue->format(DateTime::DATETIME_PHP_FORMAT));
285284
} catch (\Exception $e) {
286285
throw new LocalizedException(__('Invalid default date'));
287286
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model\Entity;
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Magento\Framework\Locale\ResolverInterface;
10+
11+
class AttributeTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @var Attribute
15+
*/
16+
private $attribute;
17+
18+
/**
19+
* @var \Magento\Framework\ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
/**
24+
* @var ResolverInterface
25+
*/
26+
private $_localeResolver;
27+
28+
protected function setUp()
29+
{
30+
$this->objectManager = Bootstrap::getObjectManager();
31+
$this->attribute = $this->objectManager->get(Attribute::class);
32+
$this->_localeResolver = $this->objectManager->get(ResolverInterface::class);
33+
}
34+
35+
protected function tearDown()
36+
{
37+
$this->attribute = null;
38+
$this->objectManager = null;
39+
$this->_localeResolver = null;
40+
}
41+
42+
/**
43+
* @param string $defaultValue
44+
* @param string $backendType
45+
* @param string $locale
46+
* @param string $expected
47+
* @dataProvider beforeSaveDataProvider
48+
* @throws
49+
*/
50+
public function testBeforeSave($defaultValue, $backendType, $locale, $expected)
51+
{
52+
$this->attribute->setDefaultValue($defaultValue);
53+
$this->attribute->setBackendType($backendType);
54+
$this->_localeResolver->setLocale($locale);
55+
$this->attribute->beforeSave();
56+
57+
$this->assertEquals($expected, $this->attribute->getDefaultValue());
58+
}
59+
60+
public function beforeSaveDataProvider()
61+
{
62+
return [
63+
['21/07/18', 'datetime', 'en_AU', '2018-07-21 00:00:00'],
64+
['07/21/18', 'datetime', 'en_US', '2018-07-21 00:00:00'],
65+
['21/07/18', 'datetime', 'fr_FR', '2018-07-21 00:00:00'],
66+
['21/07/18', 'datetime', 'de_DE', '2018-07-21 00:00:00'],
67+
['21/07/18', 'datetime', 'uk_UA', '2018-07-21 00:00:00'],
68+
['100.50', 'decimal', 'en_US', '100.50'],
69+
['100,50', 'decimal', 'uk_UA', '100.5'],
70+
];
71+
}
72+
73+
/**
74+
* @param string $defaultValue
75+
* @param string $backendType
76+
* @param string $locale
77+
* @param string $expected
78+
* @dataProvider beforeSaveErrorDataDataProvider
79+
* @expectedException \Magento\Framework\Exception\LocalizedException
80+
*/
81+
public function testBeforeSaveErrorData($defaultValue, $backendType, $locale, $expected)
82+
{
83+
$this->attribute->setDefaultValue($defaultValue);
84+
$this->attribute->setBackendType($backendType);
85+
$this->_localeResolver->setLocale($locale);
86+
$this->attribute->beforeSave();
87+
88+
$this->expectExceptionMessage($expected);
89+
}
90+
91+
public function beforeSaveErrorDataDataProvider()
92+
{
93+
return [
94+
'wrong date for Australia' => ['32/38', 'datetime', 'en_AU', 'Invalid default date'],
95+
'wrong date for States' => ['32/38', 'datetime', 'en_US', 'Invalid default date'],
96+
'wrong decimal separator for US' => ['100,50', 'decimal', 'en_US', 'Invalid default decimal value'],
97+
'wrong decimal separator for UA' => ['100.50', 'decimal', 'uk_UA', 'Invalid default decimal value'],
98+
];
99+
}
100+
}

0 commit comments

Comments
 (0)