Skip to content

Commit 89cb767

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-61267' into 2.1.8-develop-pr18
# Conflicts: # dev/tests/functional/utils/command.php
2 parents b04025f + e6140f0 commit 89cb767

File tree

11 files changed

+245
-19
lines changed

11 files changed

+245
-19
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,15 @@ public function __construct(
6060
public function prepare()
6161
{
6262
$config = $this->getData('config');
63-
64-
if (!isset($config['timeOffset'])) {
65-
$config['timeOffset'] = (new \DateTime(
66-
'now',
67-
new \DateTimeZone(
68-
$this->localeDate->getConfigTimezone()
69-
)
70-
))->getOffset();
63+
if (!isset($config['storeTimeZone'])) {
64+
$storeTimeZone = $this->localeDate->getConfigTimezone();
65+
$config['storeTimeZone'] = $storeTimeZone;
7166
}
72-
67+
// Set date format pattern by current locale
68+
$localeDateFormat = $this->localeDate->getDateFormat();
69+
$config['options']['dateFormat'] = $localeDateFormat;
70+
$config['outputDateFormat'] = $localeDateFormat;
7371
$this->setData('config', $config);
74-
7572
parent::prepare();
7673
}
7774

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Ui\Test\Unit\Component\Form\Element\DataType;
7+
8+
use Magento\Ui\Component\Form\Element\DataType\Date;
9+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\View\Element\UiComponent\Context;
12+
use Magento\Framework\Locale\ResolverInterface;
13+
use Magento\Framework\View\Element\UiComponent\Processor;
14+
15+
/**
16+
* Tests Magento\Ui\Test\Unit\Component\Form\Element\DataType Class.
17+
*/
18+
class DateTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/** @var \PHPUnit_Framework_MockObject_MockObject */
21+
private $contextMock;
22+
23+
/** @var \PHPUnit_Framework_MockObject_MockObject */
24+
private $localeDateMock;
25+
26+
/** @var \PHPUnit_Framework_MockObject_MockObject */
27+
private $localeResolverMock;
28+
29+
/** @var \Magento\Ui\Component\Form\Element\DataType\Date */
30+
private $date;
31+
32+
/** @var \PHPUnit_Framework_MockObject_MockObject */
33+
private $processorMock;
34+
35+
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
36+
private $objectManagerHelper;
37+
38+
public function setUp()
39+
{
40+
$this->contextMock = $this->getMock(Context::class, [], [], '', false);
41+
$this->localeDateMock = $this->getMock(TimezoneInterface::class, [], [], '', false);
42+
$this->localeResolverMock = $this->getMock(ResolverInterface::class, [], [], '', false);
43+
$this->objectManagerHelper = new ObjectManager($this);
44+
$this->processorMock = $this->getMock(Processor::class, [], [], '', false);
45+
$this->contextMock->expects($this->any())->method('getProcessor')->willReturn($this->processorMock);
46+
}
47+
48+
/**
49+
* This test ensures that outputDateFormat is properly saved in the configuration with timeOffset.
50+
*/
51+
public function testPrepareWithTimeOffset()
52+
{
53+
$this->date = new Date(
54+
$this->contextMock,
55+
$this->localeDateMock,
56+
$this->localeResolverMock,
57+
[],
58+
[
59+
'config' => [
60+
'timeOffset' => 1,
61+
],
62+
]
63+
);
64+
65+
$localeDateFormat = 'dd/MM/y';
66+
67+
$this->localeDateMock->expects($this->once())
68+
->method('getDateFormat')
69+
->willReturn($localeDateFormat);
70+
71+
$this->date->prepare();
72+
73+
$config = $this->date->getConfig();
74+
$this->assertTrue(is_array($config));
75+
76+
$this->assertArrayHasKey('options', $config);
77+
$this->assertArrayHasKey('dateFormat', $config['options']);
78+
$this->assertEquals($localeDateFormat, $config['options']['dateFormat']);
79+
80+
$this->assertArrayHasKey('outputDateFormat', $config);
81+
$this->assertEquals($localeDateFormat, $config['outputDateFormat']);
82+
}
83+
84+
/**
85+
* This test ensures that outputDateFormat is properly saved in the configuration without timeOffset.
86+
*/
87+
public function testPrepareWithoutTimeOffset()
88+
{
89+
$defaultDateFormat = 'MM/dd/y';
90+
91+
$this->date = new Date(
92+
$this->contextMock,
93+
$this->localeDateMock,
94+
$this->localeResolverMock,
95+
[],
96+
[
97+
'config' => [
98+
'options' => [
99+
'dateFormat' => $defaultDateFormat,
100+
],
101+
'outputDateFormat' => $defaultDateFormat,
102+
],
103+
]
104+
);
105+
106+
$localeDateFormat = 'dd/MM/y';
107+
108+
$this->localeDateMock->expects($this->once())
109+
->method('getDateFormat')
110+
->willReturn($localeDateFormat);
111+
$this->localeDateMock->expects($this->any())
112+
->method('getConfigTimezone')
113+
->willReturn('America/Los_Angeles');
114+
115+
$this->date->prepare();
116+
117+
$config = $this->date->getConfig();
118+
$this->assertTrue(is_array($config));
119+
120+
$this->assertArrayHasKey('options', $config);
121+
$this->assertArrayHasKey('dateFormat', $config['options']);
122+
$this->assertEquals($localeDateFormat, $config['options']['dateFormat']);
123+
124+
$this->assertArrayHasKey('outputDateFormat', $config);
125+
$this->assertEquals($localeDateFormat, $config['outputDateFormat']);
126+
}
127+
128+
/**
129+
* This test ensures that userTimeZone is properly saved in the configuration.
130+
*/
131+
public function testPrepare()
132+
{
133+
$this->date = $this->objectManagerHelper->getObject(
134+
Date::class,
135+
[
136+
'context' => $this->contextMock,
137+
'localeDate' => $this->localeDateMock,
138+
'localeResolver' => $this->localeResolverMock
139+
]
140+
);
141+
$this->localeDateMock->expects($this->any())->method('getConfigTimezone')->willReturn('America/Chicago');
142+
$this->date->prepare();
143+
$configArray = $this->date->getData('config');
144+
$this->assertEquals('America/Chicago', $configArray['storeTimeZone']);
145+
}
146+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
define([
66
'moment',
77
'mageUtils',
8-
'./abstract'
8+
'./abstract',
9+
'moment-timezone-with-data'
910
], function (moment, utils, Abstract) {
1011
'use strict';
1112

1213
return Abstract.extend({
1314
defaults: {
1415
options: {},
1516

16-
timeOffset: 0,
17+
storeTimeZone: 'UTC',
1718

1819
validationParams: {
1920
dateFormat: '${ $.outputDateFormat }'
@@ -61,7 +62,7 @@ define([
6162

6263
/**
6364
* Date/time value shifted to corresponding timezone
64-
* according to this.timeOffset property. This value
65+
* according to this.storeTimeZone property. This value
6566
* will be sent to the server.
6667
*
6768
* @type {String}
@@ -109,7 +110,7 @@ define([
109110

110111
if (value) {
111112
if (this.options.showsTime) {
112-
shiftedValue = moment.utc(value).add(this.timeOffset, 'seconds');
113+
shiftedValue = moment.tz(value, 'UTC').tz(this.storeTimeZone);
113114
} else {
114115
dateFormat = this.shiftedValue() ? this.outputDateFormat : this.inputDateFormat;
115116

@@ -133,12 +134,13 @@ define([
133134
* @param {String} shiftedValue
134135
*/
135136
onShiftedValueChange: function (shiftedValue) {
136-
var value;
137+
var value,
138+
formattedValue;
137139

138140
if (shiftedValue) {
139141
if (this.options.showsTime) {
140-
value = moment.utc(shiftedValue, this.pickerDateTimeFormat);
141-
value = value.subtract(this.timeOffset, 'seconds').toISOString();
142+
formattedValue = moment(shiftedValue).format('YYYY-MM-DD HH:mm');
143+
value = moment.tz(formattedValue, this.storeTimeZone).tz('UTC').toISOString();
142144
} else {
143145
value = moment(shiftedValue, this.pickerDateTimeFormat);
144146
value = value.format(this.outputDateFormat);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Mtf\Util\Command\Cli;
8+
9+
use Magento\Mtf\Util\Command\Cli;
10+
11+
/**
12+
* Handle cron for tests executions.
13+
*/
14+
class Cron extends Cli
15+
{
16+
/**
17+
* Parameter for cron run command.
18+
*/
19+
const PARAM_CRON_RUN = 'cron:run';
20+
21+
/**
22+
* Run cron.
23+
*
24+
* @return void
25+
*/
26+
public function run()
27+
{
28+
parent::execute(Cron::PARAM_CRON_RUN);
29+
sleep(60); // According to Magento 2 documentation cron job running takes 600 ms
30+
}
31+
}

dev/tests/functional/tests/app/Magento/Backend/Test/Fixture/Source/Date.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
namespace Magento\Backend\Test\Fixture\Source;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Mtf\Fixture\DataSource;
1011

1112
/**
1213
* Class Date.
1314
*
1415
* Data keys:
1516
* - pattern (Format a local time/date with delta, e.g. 'm/d/Y -3 days' = current day - 3 days)
17+
* - apply_timezone (true if it is needed to apply timezone)
1618
*/
1719
class Date extends DataSource
1820
{
@@ -35,7 +37,17 @@ public function __construct(array $params, $data = [])
3537
if (!$timestamp) {
3638
throw new \Exception('Invalid date format for "' . $this->params['attribute_code'] . '" field');
3739
}
38-
$date = date(str_replace($delta, '', $data['pattern']), $timestamp);
40+
if (isset($data['apply_timezone']) && $data['apply_timezone'] === true) {
41+
42+
$timezone = ObjectManager::getInstance()
43+
->get(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
44+
$date = new \DateTime();
45+
$date->setTimestamp($timestamp);
46+
$date->setTimezone(new \DateTimeZone($timezone->getConfigTimezone()));
47+
$date = $date->format(str_replace($delta, '', $data['pattern']));
48+
} else {
49+
$date = date(str_replace($delta, '', $data['pattern']), $timestamp);
50+
}
3951
if (!$date) {
4052
$date = date('m/d/Y');
4153
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@
8888
<field name="product_has_weight" group="product-details" />
8989
<field name="attributes" />
9090
<field name="fpt" is_required="0" group="product-details" repository="Magento\Catalog\Test\Repository\Product\Fpt" />
91+
<field name="store_code" is_required="0" />
9192
</fixture>
9293
</config>

dev/tests/functional/tests/app/Magento/Catalog/Test/Handler/CatalogProductSimple/Webapi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ public function persist(FixtureInterface $fixture = null)
9494
$this->fields = $this->handlerCurl->prepareData($this->fixture);
9595
$this->prepareData();
9696
$this->convertData();
97+
$storeCode = $this->fixture->hasData('store_code') ? $this->fixture->getStoreCode() : 'default';
9798

9899
/** @var CatalogProductSimple $fixture */
99-
$url = $_ENV['app_frontend_url'] . 'rest/default/V1/products';
100+
$url = $_ENV['app_frontend_url'] . 'rest/'. $storeCode .'/V1/products';
100101
$this->webapiTransport->write($url, $this->fields, CurlInterface::POST);
101102
$encodedResponse = $this->webapiTransport->read();
102103
$response = json_decode($encodedResponse, true);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,5 +1267,31 @@
12671267
</field>
12681268
</dataset>
12691269

1270+
<dataset name="product_with_store_code">
1271+
<field name="sku" xsi:type="string">simple_product_with_category_%isolation%</field>
1272+
<field name="name" xsi:type="string">Simple product with category %isolation%</field>
1273+
<field name="quantity_and_stock_status" xsi:type="array">
1274+
<item name="qty" xsi:type="string">777</item>
1275+
<item name="is_in_stock" xsi:type="string">In Stock</item>
1276+
</field>
1277+
<field name="product_has_weight" xsi:type="string">This item has weight</field>
1278+
<field name="weight" xsi:type="string">1</field>
1279+
<field name="attribute_set_id" xsi:type="array">
1280+
<item name="dataset" xsi:type="string">default</item>
1281+
</field>
1282+
<field name="price" xsi:type="array">
1283+
<item name="value" xsi:type="string">100</item>
1284+
<item name="dataset" xsi:type="string" />
1285+
</field>
1286+
<field name="category_ids" xsi:type="array">
1287+
<item name="dataset" xsi:type="string">default_subcategory</item>
1288+
</field>
1289+
<field name="website_ids" xsi:type="array">
1290+
<item name="0" xsi:type="string">Main Website</item>
1291+
</field>
1292+
<field name="mtf_dataset_name" xsi:type="string">simple_with_category</field>
1293+
<field name="url_key" xsi:type="string">simple-product-%isolation%</field>
1294+
<field name="store_code" xsi:type="string">all</field>
1295+
</dataset>
12701296
</repository>
12711297
</config>

dev/tests/functional/utils/command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'cache:disable',
1010
'cache:enable',
1111
'setup:static-content:deploy',
12+
'cron:run',
1213
];
1314

1415
if (isset($_GET['command'])) {

lib/web/mage/utils/misc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ define([
3838
'EEEE': 'dddd',
3939
'EEE': 'ddd',
4040
'e': 'd',
41+
'yyyy': 'YYYY',
42+
'yy': 'YY',
4143
'y': 'YYYY',
4244
'a': 'A'
4345
};

0 commit comments

Comments
 (0)