Skip to content

Commit 8e9ef18

Browse files
author
Oleksandr Dubovyk
committed
Merge remote-tracking branch 'chaika/MAGETWO-93188' into chaika_ports
2 parents fbf9876 + 9b84332 commit 8e9ef18

File tree

2 files changed

+107
-5
lines changed

2 files changed

+107
-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
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\Api\AttributeValueFactory;
1010
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Stdlib\DateTime;
1112
use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
1213

1314
/**
@@ -284,12 +285,10 @@ public function beforeSave()
284285

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

0 commit comments

Comments
 (0)