Skip to content

Commit 6cb2ee5

Browse files
committed
MAGETWO-70921: Date sets to 2020 year after save
1 parent 0987a02 commit 6cb2ee5

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public function __construct(
6060
public function prepare()
6161
{
6262
$config = $this->getData('config');
63+
6364
if (!isset($config['storeTimeZone'])) {
6465
$storeTimeZone = $this->localeDate->getConfigTimezone();
6566
$config['storeTimeZone'] = $storeTimeZone;
6667
}
6768
// Set date format pattern by current locale
6869
$localeDateFormat = $this->localeDate->getDateFormat();
6970
$config['options']['dateFormat'] = $localeDateFormat;
70-
$config['outputDateFormat'] = $localeDateFormat;
71+
$config['options']['storeLocale'] = $this->locale;
7172
$this->setData('config', $config);
7273
parent::prepare();
7374
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ public function testPrepareWithTimeOffset()
7676
$this->assertArrayHasKey('options', $config);
7777
$this->assertArrayHasKey('dateFormat', $config['options']);
7878
$this->assertEquals($localeDateFormat, $config['options']['dateFormat']);
79-
80-
$this->assertArrayHasKey('outputDateFormat', $config);
81-
$this->assertEquals($localeDateFormat, $config['outputDateFormat']);
8279
}
8380

8481
/**
@@ -121,15 +118,14 @@ public function testPrepareWithoutTimeOffset()
121118
$this->assertArrayHasKey('dateFormat', $config['options']);
122119
$this->assertEquals($localeDateFormat, $config['options']['dateFormat']);
123120

124-
$this->assertArrayHasKey('outputDateFormat', $config);
125-
$this->assertEquals($localeDateFormat, $config['outputDateFormat']);
126121
}
127122

128123
/**
129124
* This test ensures that userTimeZone is properly saved in the configuration.
130125
*/
131126
public function testPrepare()
132127
{
128+
$this->localeResolverMock->expects($this->any())->method('getLocale')->willReturn('de-DE');
133129
$this->date = $this->objectManagerHelper->getObject(
134130
Date::class,
135131
[
@@ -142,5 +138,6 @@ public function testPrepare()
142138
$this->date->prepare();
143139
$configArray = $this->date->getData('config');
144140
$this->assertEquals('America/Chicago', $configArray['storeTimeZone']);
141+
$this->assertEquals('de-DE', $configArray['options']['storeLocale']);
145142
}
146143
}

app/code/Magento/Ui/view/base/web/js/form/element/date.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright © 2015 Magento. All rights reserved.
2+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
55
define([
@@ -53,6 +53,11 @@ define([
5353
pickerDefaultDateFormat: 'MM/dd/y', // ICU Date Format
5454
pickerDefaultTimeFormat: 'h:mm a', // ICU Time Format
5555

56+
/**
57+
* Format needed by moment timezone for conversion.
58+
*/
59+
timezoneFormat: 'YYYY-MM-DD HH:mm',
60+
5661
elementTmpl: 'ui/form/element/date',
5762

5863
listens: {
@@ -135,15 +140,17 @@ define([
135140
*/
136141
onShiftedValueChange: function (shiftedValue) {
137142
var value,
138-
formattedValue;
143+
formattedValue,
144+
momentValue;
139145

140146
if (shiftedValue) {
147+
momentValue = moment(shiftedValue, this.pickerDateTimeFormat);
148+
141149
if (this.options.showsTime) {
142-
formattedValue = moment(shiftedValue).format('YYYY-MM-DD HH:mm');
150+
formattedValue = moment(momentValue).format(this.timezoneFormat);
143151
value = moment.tz(formattedValue, this.storeTimeZone).tz('UTC').toISOString();
144152
} else {
145-
value = moment(shiftedValue, this.pickerDateTimeFormat);
146-
value = value.format(this.outputDateFormat);
153+
value = momentValue.format(this.outputDateFormat);
147154
}
148155
} else {
149156
value = '';
@@ -165,14 +172,14 @@ define([
165172
this.pickerDateTimeFormat += ' ' + this.options.timeFormat;
166173
}
167174

168-
this.pickerDateTimeFormat = utils.normalizeDate(this.pickerDateTimeFormat);
175+
this.pickerDateTimeFormat = utils.convertToMomentFormat(this.pickerDateTimeFormat);
169176

170-
if (this.dateFormat) {
171-
this.inputDateFormat = this.dateFormat;
177+
if (this.options.dateFormat) {
178+
this.outputDateFormat = this.options.dateFormat;
172179
}
173180

174-
this.inputDateFormat = utils.normalizeDate(this.inputDateFormat);
175-
this.outputDateFormat = utils.normalizeDate(this.outputDateFormat);
181+
this.inputDateFormat = utils.convertToMomentFormat(this.inputDateFormat);
182+
this.outputDateFormat = utils.convertToMomentFormat(this.outputDateFormat);
176183

177184
this.validationParams.dateFormat = this.outputDateFormat;
178185
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Fixture/CatalogProductSimple.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
<field name="custom_options" is_required="0" group="customer-options" source="Magento\Catalog\Test\Fixture\Product\CustomOptions" repository="Magento\Catalog\Test\Repository\Product\CustomOptions" />
7979
<field name="website_ids" group="websites" source="Magento\Catalog\Test\Fixture\Product\WebsiteIds" />
8080
<field name="is_returnable" is_required="0" group="product-details" />
81-
<field name="news_from_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
82-
<field name="news_to_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
81+
<field name="news_from_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
82+
<field name="news_to_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
8383
<field name="stock_data" group="advanced-inventory" />
8484
<field name="checkout_data" group="null" repository="Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData" />
8585
<field name="cross_sell_products" group="related" source="Magento\Catalog\Test\Fixture\Product\RelatedProducts" />

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/UpdateSimpleProductEntityTest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,13 @@
162162
<constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
163163
<constraint name="Magento\Catalog\Test\Constraint\AssertProductCustomOptionsOnProductPage" />
164164
</variation>
165+
<variation name="UpdateSimpleProductEntityTestVariation14" summary="Verify product date attributes are saved and displayed correctly." ticketId="MAGETWO-70929">
166+
<data name="initialProduct/dataset" xsi:type="string">default</data>
167+
<data name="product/data/sku" xsi:type="string">test_simple_product_%isolation%</data>
168+
<data name="product/data/news_from_date" xsi:type="string">12/12/2018</data>
169+
<data name="product/data/news_to_date" xsi:type="string">12/25/2018</data>
170+
<constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
171+
<constraint name="Magento\Catalog\Test\Constraint\AssertProductForm" />
172+
</variation>
165173
</testCase>
166174
</config>

lib/web/mage/utils/misc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ define([
234234
}, this);
235235

236236
return data;
237+
},
238+
239+
/**
240+
* Converts PHP IntlFormatter format to moment format.
241+
*
242+
* @param {String} format - PHP format
243+
* @returns {String} - moment compatible formatting
244+
*/
245+
convertToMomentFormat: function (format) {
246+
var newFormat;
247+
248+
newFormat = format.replace(/yy|y/gi, 'YYYY'); // replace the year
249+
newFormat = newFormat.replace(/dd|d/g, 'DD'); // replace the date
250+
251+
return newFormat;
237252
}
238253
};
239254
});

0 commit comments

Comments
 (0)