Skip to content

Commit

Permalink
MAGETWO-57835: [Github] Cannot save customer dob attribute if admin i…
Browse files Browse the repository at this point in the history
…nterface locale is en_GB #6323
  • Loading branch information
Sergey Semenov committed Sep 22, 2016
1 parent 4bb5467 commit 910b937
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 16 deletions.
16 changes: 8 additions & 8 deletions app/code/Magento/Ui/Component/Form/Element/DataType/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType;

use Magento\Ui\Component\Form\Element\DataType\Date;

class DateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\View\Element\UiComponent\ContextInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $context;

/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $localeDate;

/**
* @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $localeResolver;

/**
* @var Date
*/
private $model;

public function setUp()
{
$processorMock = $this->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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 () {
Expand Down
11 changes: 6 additions & 5 deletions app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.')

],
Expand Down

0 comments on commit 910b937

Please sign in to comment.