From 910b93709170f94d0558b0e4d31f825ac96b6fbc Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Thu, 22 Sep 2016 17:02:37 +0300 Subject: [PATCH] MAGETWO-57835: [Github] Cannot save customer dob attribute if admin interface locale is en_GB #6323 --- .../Component/Form/Element/DataType/Date.php | 16 +-- .../Form/Element/DataType/DateTest.php | 126 ++++++++++++++++++ .../js/lib/knockout/bindings/datepicker.js | 13 +- .../view/base/web/js/lib/validation/rules.js | 11 +- 4 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php diff --git a/app/code/Magento/Ui/Component/Form/Element/DataType/Date.php b/app/code/Magento/Ui/Component/Form/Element/DataType/Date.php index c074fcc056fc3..bf29a700aece1 100644 --- a/app/code/Magento/Ui/Component/Form/Element/DataType/Date.php +++ b/app/code/Magento/Ui/Component/Form/Element/DataType/Date.php @@ -7,7 +7,6 @@ use Magento\Framework\Locale\ResolverInterface; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; -use Magento\Framework\View\Element\UiComponentInterface; use Magento\Framework\View\Element\UiComponent\ContextInterface; /** @@ -22,18 +21,14 @@ class Date extends AbstractDataType * * @var string */ - protected $locale; + private $locale; /** - * Wrapped component - * - * @var UiComponentInterface + * @var TimezoneInterface */ - protected $wrappedComponent; + private $localeDate; /** - * Constructor - * * @param ContextInterface $context * @param TimezoneInterface $localeDate * @param ResolverInterface $localeResolver @@ -70,6 +65,11 @@ public function prepare() ))->getOffset(); } + // Set date format pattern by current locale + $localeDateFormat = $this->localeDate->getDateFormat(); + $config['options']['dateFormat'] = $localeDateFormat; + $config['outputDateFormat'] = $localeDateFormat; + $this->setData('config', $config); parent::prepare(); diff --git a/app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php b/app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php new file mode 100644 index 0000000000000..ef0743aa32947 --- /dev/null +++ b/app/code/Magento/Ui/Test/Unit/Component/Form/Element/DataType/DateTest.php @@ -0,0 +1,126 @@ +getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class) + ->getMockForAbstractClass(); + $this->context->expects($this->any()) + ->method('getProcessor') + ->willReturn($processorMock); + + $this->localeDate = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class) + ->getMockForAbstractClass(); + + $this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class) + ->getMockForAbstractClass(); + } + + public function testPrepareWithTimeOffset() + { + $this->model = new Date( + $this->context, + $this->localeDate, + $this->localeResolver, + [], + [ + 'config' => [ + 'timeOffset' => 1, + ], + ] + ); + + $localeDateFormat = 'dd/MM/y'; + + $this->localeDate->expects($this->once()) + ->method('getDateFormat') + ->willReturn($localeDateFormat); + + $this->model->prepare(); + + $config = $this->model->getConfig(); + $this->assertTrue(is_array($config)); + + $this->assertArrayHasKey('options', $config); + $this->assertArrayHasKey('dateFormat', $config['options']); + $this->assertEquals($localeDateFormat, $config['options']['dateFormat']); + + $this->assertArrayHasKey('outputDateFormat', $config); + $this->assertEquals($localeDateFormat, $config['outputDateFormat']); + } + + public function testPrepareWithoutTimeOffset() + { + $defaultDateFormat = 'MM/dd/y'; + + $this->model = new Date( + $this->context, + $this->localeDate, + $this->localeResolver, + [], + [ + 'config' => [ + 'options' => [ + 'dateFormat' => $defaultDateFormat, + ], + 'outputDateFormat' => $defaultDateFormat, + ], + ] + ); + + $localeDateFormat = 'dd/MM/y'; + + $this->localeDate->expects($this->once()) + ->method('getDateFormat') + ->willReturn($localeDateFormat); + $this->localeDate->expects($this->once()) + ->method('getConfigTimezone') + ->willReturn('America/Los_Angeles'); + + $this->model->prepare(); + + $config = $this->model->getConfig(); + $this->assertTrue(is_array($config)); + + $this->assertArrayHasKey('timeOffset', $config); + + $this->assertArrayHasKey('options', $config); + $this->assertArrayHasKey('dateFormat', $config['options']); + $this->assertEquals($localeDateFormat, $config['options']['dateFormat']); + + $this->assertArrayHasKey('outputDateFormat', $config); + $this->assertEquals($localeDateFormat, $config['outputDateFormat']); + } +} diff --git a/app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/datepicker.js b/app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/datepicker.js index 8f0fb7b007caf..16c102b8367f4 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/datepicker.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/datepicker.js @@ -8,8 +8,10 @@ define([ 'underscore', 'jquery', 'mage/translate', - 'mage/calendar' -], function (ko, _, $, $t) { + 'mage/calendar', + 'moment', + 'mageUtils' +], function (ko, _, $, $t, calendar, moment, utils) { 'use strict'; var defaults = { @@ -46,7 +48,12 @@ define([ } $(el).calendar(options); - observable() && $(el).datepicker('setDate', observable()); + + observable() && $(el).datepicker( + 'setDate', + moment(observable(), utils.normalizeDate(config.options.dateFormat)).toDate() + ); + $(el).blur(); ko.utils.registerEventHandler(el, 'change', function () { diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 71f2834190436..aa30cf24805f0 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -9,8 +9,9 @@ define([ 'moment', 'jquery/validate', 'jquery/ui', - 'mage/translate' -], function ($, _, utils, moment) { + 'mage/translate', + 'mageUtils' +], function ($, _, utils, moment, validate, ui, $t, mageUtils) { 'use strict'; /** @@ -682,9 +683,9 @@ define([ $.mage.__('Please use only letters (a-z or A-Z) or numbers (0-9) in this field. No spaces or other characters are allowed.') ], "validate-date": [ - function(value) { - var test = new Date(value); - return utils.isEmptyNoTrim(value) || !isNaN(test); + function(value, params, additionalParams) { + var test = moment(value, additionalParams.dateFormat); + return utils.isEmptyNoTrim(value) || test.isValid(); },$.mage.__('Please enter a valid date.') ],