Skip to content

Commit

Permalink
Merge pull request #453 from magento-fearless-kiwis/FearlessKiwis-MAG…
Browse files Browse the repository at this point in the history
…ETWO-55867-New-Incorrect-Time

Fixed issue:
 - MAGETWO-55867 Date and time are shown incorrectly for staging updates
  • Loading branch information
sdwright authored Sep 30, 2016
2 parents 9ed631d + 7c80c5c commit 6614360
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 66 deletions.
16 changes: 5 additions & 11 deletions app/code/Magento/Ui/Component/Form/Element/DataType/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Date extends AbstractDataType
protected $wrappedComponent;

/**
* Constructor
*
* @param ContextInterface $context
* @param TimezoneInterface $localeDate
* @param ResolverInterface $localeResolver
Expand All @@ -58,23 +60,15 @@ public function __construct(
public function prepare()
{
$config = $this->getData('config');

if (!isset($config['timeOffset'])) {
$config['timeOffset'] = (new \DateTime(
'now',
new \DateTimeZone(
$this->localeDate->getConfigTimezone()
)
))->getOffset();
if (!isset($config['storeTimeZone'])) {
$storeTimeZone = $this->localeDate->getConfigTimezone();
$config['storeTimeZone'] = $storeTimeZone;
}

// 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
Expand Up @@ -6,54 +6,48 @@
namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType;

use Magento\Ui\Component\Form\Element\DataType\Date;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Element\UiComponent\Context;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\View\Element\UiComponent\Processor;

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

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

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

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

public function setUp()
{
$processorMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class)
->disableOriginalConstructor()
->getMock();
/** @var \Magento\Ui\Component\Form\Element\DataType\Date */
private $date;

$this->context = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
->getMockForAbstractClass();
$this->context->expects($this->any())
->method('getProcessor')
->willReturn($processorMock);
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $processorMock;

$this->localeDate = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
->getMockForAbstractClass();
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
private $objectManagerHelper;

$this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
->getMockForAbstractClass();
public function setUp()
{
$this->contextMock = $this->getMock(Context::class, [], [], '', false);
$this->localeDateMock = $this->getMock(TimezoneInterface::class, [], [], '', false);
$this->localeResolverMock = $this->getMock(ResolverInterface::class, [], [], '', false);
$this->objectManagerHelper = new ObjectManager($this);
$this->processorMock = $this->getMock(Processor::class, [], [], '', false);
$this->contextMock->expects($this->any())->method('getProcessor')->willReturn($this->processorMock);
}

public function testPrepareWithTimeOffset()
{
$this->model = new Date(
$this->context,
$this->localeDate,
$this->localeResolver,
$this->date = new Date(
$this->contextMock,
$this->localeDateMock,
$this->localeResolverMock,
[],
[
'config' => [
Expand All @@ -64,13 +58,13 @@ public function testPrepareWithTimeOffset()

$localeDateFormat = 'dd/MM/y';

$this->localeDate->expects($this->once())
$this->localeDateMock->expects($this->once())
->method('getDateFormat')
->willReturn($localeDateFormat);

$this->model->prepare();
$this->date->prepare();

$config = $this->model->getConfig();
$config = $this->date->getConfig();
$this->assertTrue(is_array($config));

$this->assertArrayHasKey('options', $config);
Expand All @@ -85,10 +79,10 @@ public function testPrepareWithoutTimeOffset()
{
$defaultDateFormat = 'MM/dd/y';

$this->model = new Date(
$this->context,
$this->localeDate,
$this->localeResolver,
$this->date = new Date(
$this->contextMock,
$this->localeDateMock,
$this->localeResolverMock,
[],
[
'config' => [
Expand All @@ -102,25 +96,42 @@ public function testPrepareWithoutTimeOffset()

$localeDateFormat = 'dd/MM/y';

$this->localeDate->expects($this->once())
$this->localeDateMock->expects($this->once())
->method('getDateFormat')
->willReturn($localeDateFormat);
$this->localeDate->expects($this->once())
$this->localeDateMock->expects($this->any())
->method('getConfigTimezone')
->willReturn('America/Los_Angeles');

$this->model->prepare();
$this->date->prepare();

$config = $this->model->getConfig();
$config = $this->date->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']);
}

/**
* This tests ensures that userTimeZone is properly saved in the configuration
*/
public function testPrepare()
{
$this->date = $this->objectManagerHelper->getObject(
Date::class,
[
'context' => $this->contextMock,
'localeDate' => $this->localeDateMock,
'localeResolver' => $this->localeResolverMock
]
);
$this->localeDateMock->expects($this->any())->method('getConfigTimezone')->willReturn('America/Chicago');
$this->date->prepare();
$configArray = $this->date->getData('config');
$this->assertEquals('America/Chicago', $configArray['storeTimeZone']);
}
}
16 changes: 9 additions & 7 deletions app/code/Magento/Ui/view/base/web/js/form/element/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
define([
'moment',
'mageUtils',
'./abstract'
'./abstract',
'moment-timezone-with-data'
], function (moment, utils, Abstract) {
'use strict';

return Abstract.extend({
defaults: {
options: {},

timeOffset: 0,
storeTimeZone: 'UTC',

validationParams: {
dateFormat: '${ $.outputDateFormat }'
Expand Down Expand Up @@ -61,7 +62,7 @@ define([

/**
* Date/time value shifted to corresponding timezone
* according to this.timeOffset property. This value
* according to this.storeTimeZone property. This value
* will be sent to the server.
*
* @type {String}
Expand Down Expand Up @@ -109,7 +110,7 @@ define([

if (value) {
if (this.options.showsTime) {
shiftedValue = moment.utc(value).add(this.timeOffset, 'seconds');
shiftedValue = moment.tz(value, 'UTC').tz(this.storeTimeZone);
} else {
dateFormat = this.shiftedValue() ? this.outputDateFormat : this.inputDateFormat;

Expand All @@ -133,12 +134,13 @@ define([
* @param {String} shiftedValue
*/
onShiftedValueChange: function (shiftedValue) {
var value;
var value,
formattedValue;

if (shiftedValue) {
if (this.options.showsTime) {
value = moment.utc(shiftedValue, this.pickerDateTimeFormat);
value = value.subtract(this.timeOffset, 'seconds').toISOString();
formattedValue = moment(shiftedValue).format('YYYY-MM-DD HH:mm');
value = moment.tz(formattedValue, this.storeTimeZone).tz('UTC').toISOString();
} else {
value = moment(shiftedValue, this.pickerDateTimeFormat);
value = value.format(this.outputDateFormat);
Expand Down
1 change: 1 addition & 0 deletions lib/web/mage/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ define([
'EEE': 'ddd',
'e': 'd',
'yyyy': 'YYYY',
'yy': 'YY',
'y': 'YYYY',
'a': 'A'
};
Expand Down
7 changes: 7 additions & 0 deletions lib/web/moment-timezone-with-data.js

Large diffs are not rendered by default.

0 comments on commit 6614360

Please sign in to comment.