Skip to content

Commit 603fcb4

Browse files
author
Bomko, Alex(abomko)
committed
Merge pull request #528 from magento-troll/troll-bugfixes
Troll bugfixes
2 parents 4e1a1bc + 296877d commit 603fcb4

File tree

7 files changed

+195
-31
lines changed

7 files changed

+195
-31
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Helper
4040

4141
/**
4242
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
43+
*
44+
* @deprecated
4345
*/
4446
protected $dateFilter;
4547

@@ -68,6 +70,11 @@ class Helper
6870
*/
6971
private $linkResolver;
7072

73+
/**
74+
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
75+
*/
76+
private $dateTimeFilter;
77+
7178
/**
7279
* Helper constructor.
7380
* @param \Magento\Framework\App\RequestInterface $request
@@ -142,7 +149,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
142149
foreach ($attributes as $attrKey => $attribute) {
143150
if ($attribute->getBackend()->getType() == 'datetime') {
144151
if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
145-
$dateFieldFilters[$attrKey] = $this->dateFilter;
152+
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
146153
}
147154
}
148155
}
@@ -363,4 +370,18 @@ private function getLinkResolver()
363370
}
364371
return $this->linkResolver;
365372
}
373+
374+
/**
375+
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
376+
*
377+
* @deprecated
378+
*/
379+
private function getDateTimeFilter()
380+
{
381+
if ($this->dateTimeFilter === null) {
382+
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
383+
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
384+
}
385+
return $this->dateTimeFilter;
386+
}
366387
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
1919
{
2020
/**
2121
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
22+
*
2223
* @deprecated
2324
*/
2425
protected $_dateFilter;

app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ protected function _getDayInterval(\DateTime $dateStart)
176176
\IntlDateFormatter::SHORT,
177177
\IntlDateFormatter::NONE
178178
),
179-
'start' => $dateStart->format('Y-m-d 00:00:00'),
180-
'end' => $dateStart->format('Y-m-d 23:59:59'),
179+
'start' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00')),
180+
'end' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 23:59:59')),
181181
];
182182
return $interval;
183183
}
@@ -195,21 +195,25 @@ protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $
195195
$interval = [];
196196
$interval['period'] = $dateStart->format('m/Y');
197197
if ($firstInterval) {
198-
$interval['start'] = $dateStart->format('Y-m-d 00:00:00');
198+
$interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'));
199199
} else {
200-
$interval['start'] = $dateStart->format('Y-m-01 00:00:00');
200+
$interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-01 00:00:00'));
201201
}
202202

203203
if ($dateStart->diff($dateEnd)->m == 0) {
204-
$interval['end'] = $dateStart->setDate(
205-
$dateStart->format('Y'),
206-
$dateStart->format('m'),
207-
$dateEnd->format('d')
208-
)->format(
209-
'Y-m-d 23:59:59'
204+
$interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
205+
$dateStart->setDate(
206+
$dateStart->format('Y'),
207+
$dateStart->format('m'),
208+
$dateEnd->format('d')
209+
)->format(
210+
'Y-m-d 23:59:59'
211+
)
210212
);
211213
} else {
212-
$interval['end'] = $dateStart->format('Y-m-' . date('t', $dateStart->getTimestamp()) . ' 23:59:59');
214+
$interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
215+
$dateStart->format('Y-m-' . date('t', $dateStart->getTimestamp()) . ' 23:59:59')
216+
);
213217
}
214218

215219
$dateStart->modify('+1 month');
@@ -234,13 +238,15 @@ protected function _getYearInterval(\DateTime $dateStart, \DateTime $dateEnd, $f
234238
$interval = [];
235239
$interval['period'] = $dateStart->format('Y');
236240
$interval['start'] = $firstInterval
237-
? $dateStart->format('Y-m-d 00:00:00')
238-
: $dateStart->format('Y-01-01 00:00:00');
241+
? $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'))
242+
: $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-01-01 00:00:00'));
239243

240244
$interval['end'] = $dateStart->diff($dateEnd)->y == 0
241-
? $dateStart->setDate($dateStart->format('Y'), $dateEnd->format('m'), $dateEnd->format('d'))
242-
->format('Y-m-d 23:59:59')
243-
: $dateStart->format('Y-12-31 23:59:59');
245+
? $this->_localeDate->convertConfigTimeToUtc(
246+
$dateStart->setDate($dateStart->format('Y'), $dateEnd->format('m'), $dateEnd->format('d'))
247+
->format('Y-m-d 23:59:59')
248+
)
249+
: $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-12-31 23:59:59'));
244250
$dateStart->modify('+1 year');
245251

246252
if ($dateStart->diff($dateEnd)->y == 0) {
@@ -355,7 +361,7 @@ public function getReports()
355361
}
356362
return $this->_reports;
357363
}
358-
364+
359365
/**
360366
* Load data
361367
*

lib/internal/Magento/Framework/Stdlib/DateTime/Filter/Date.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,40 @@
88
namespace Magento\Framework\Stdlib\DateTime\Filter;
99

1010
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Phrase;
1113

1214
class Date implements \Zend_Filter_Interface
1315
{
1416
/**
1517
* Filter that converts localized input into normalized format
1618
*
1719
* @var \Zend_Filter_LocalizedToNormalized
20+
*
21+
* @deprecated
1822
*/
1923
protected $_localToNormalFilter;
2024

2125
/**
2226
* Filter that converts normalized input into internal format
2327
*
2428
* @var \Zend_Filter_NormalizedToLocalized
29+
*
30+
* @deprecated
2531
*/
2632
protected $_normalToLocalFilter;
2733

2834
/**
2935
* @var TimezoneInterface
36+
*
37+
* @deprecated
3038
*/
3139
protected $_localeDate;
3240

3341
/**
3442
* @param TimezoneInterface $localeDate
43+
*
44+
* @deprecated
3545
*/
3646
public function __construct(TimezoneInterface $localeDate)
3747
{
@@ -46,17 +56,18 @@ public function __construct(TimezoneInterface $localeDate)
4656

4757
/**
4858
* Convert date from localized to internal format
49-
*
59+
*
5060
* @param string $value
5161
* @return string
62+
* @throws LocalizedException
5263
*/
5364
public function filter($value)
5465
{
55-
$value = $this->_normalToLocalFilter->filter($this->_localToNormalFilter->filter($value));
56-
57-
/**
58-
* @todo MAGETWO-51391
59-
*/
60-
return str_replace("\xc2\xa0", '', $value);
66+
try {
67+
$value = new \DateTime($value);
68+
return $value->format('Y-m-d');
69+
} catch (\Exception $e) {
70+
throw new \Exception('Invalid input date format');
71+
}
6172
}
6273
}

lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
*/
88
namespace Magento\Framework\Stdlib\DateTime\Filter;
99

10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Phrase;
12+
1013
class DateTime extends Date
1114
{
1215
/**
1316
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
17+
*
18+
* @deprecated
1419
*/
1520
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
1621
{
@@ -26,4 +31,21 @@ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface
2631
['date_format' => \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT]
2732
);
2833
}
34+
35+
/**
36+
* Convert date from localized to internal format
37+
*
38+
* @param string $value
39+
* @return string
40+
* @throws LocalizedException
41+
*/
42+
public function filter($value)
43+
{
44+
try {
45+
$value = new \DateTime($value);
46+
return $value->format('Y-m-d H:i:s');
47+
} catch (\Exception $e) {
48+
throw new \Exception('Invalid input datetime format');
49+
}
50+
}
2951
}

lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/Filter/DateTest.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010
class DateTest extends \PHPUnit_Framework_TestCase
1111
{
12-
public function testFilter()
12+
/**
13+
* @param string $inputData
14+
* @param string $expectedDate
15+
*
16+
* @dataProvider dateFilterDataProvider
17+
*/
18+
public function testFilter($inputData, $expectedDate)
1319
{
1420
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
1521
$localeMock->expects(
@@ -22,7 +28,52 @@ public function testFilter()
2228
$this->returnValue('MM-dd-yyyy')
2329
);
2430
$model = new Date($localeMock);
25-
// Check that date is converted to 'yyyy-MM-dd' format
26-
$this->assertEquals('2241-12-31', $model->filter('12-31-2241'));
31+
32+
$this->assertEquals($expectedDate, $model->filter($inputData));
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function dateFilterDataProvider()
39+
{
40+
return [
41+
['2000-01-01', '2000-01-01'],
42+
['2014-03-30T02:30:00', '2014-03-30'],
43+
['12/31/2000', '2000-12-31']
44+
];
45+
}
46+
47+
/**
48+
* @dataProvider dateFilterWithExceptionDataProvider
49+
*/
50+
public function testFilterWithException($inputData)
51+
{
52+
$this->setExpectedException('\Exception');
53+
54+
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
55+
$localeMock->expects(
56+
$this->once()
57+
)->method(
58+
'getDateFormat'
59+
)->with(
60+
\IntlDateFormatter::SHORT
61+
)->will(
62+
$this->returnValue('MM-dd-yyyy')
63+
);
64+
$model = new Date($localeMock);
65+
66+
$model->filter($inputData);
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public function dateFilterWithExceptionDataProvider()
73+
{
74+
return [
75+
['12-31-2000'],
76+
['22/2000-01'],
77+
];
2778
}
2879
}

lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/Filter/DateTimeTest.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010
class DateTimeTest extends \PHPUnit_Framework_TestCase
1111
{
12-
public function testFilter()
12+
/**
13+
* @param string $inputData
14+
* @param string $expectedDate
15+
*
16+
* @dataProvider dateTimeFilterDataProvider
17+
*/
18+
public function testFilter($inputData, $expectedDate)
1319
{
1420
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
1521
$localeMock->expects(
@@ -22,7 +28,53 @@ public function testFilter()
2228
$this->returnValue('HH:mm:ss MM-dd-yyyy')
2329
);
2430
$model = new DateTime($localeMock);
25-
// Check that datetime is converted to 'yyyy-MM-dd HH:mm:ss' format
26-
$this->assertEquals('2241-12-31 23:59:53', $model->filter('23:59:53 12-31-2241'));
31+
32+
$this->assertEquals($expectedDate, $model->filter($inputData));
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function dateTimeFilterDataProvider()
39+
{
40+
return [
41+
['2000-01-01 02:30:00', '2000-01-01 02:30:00'],
42+
['2014-03-30T02:30:00', '2014-03-30 02:30:00'],
43+
['12/31/2000 02:30:00', '2000-12-31 02:30:00'],
44+
['02:30:00 12/31/2000', '2000-12-31 02:30:00'],
45+
];
46+
}
47+
48+
/**
49+
* @dataProvider dateTimeFilterWithExceptionDataProvider
50+
*/
51+
public function testFilterWithException($inputData)
52+
{
53+
$this->setExpectedException('\Exception');
54+
55+
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
56+
$localeMock->expects(
57+
$this->once()
58+
)->method(
59+
'getDateFormat'
60+
)->with(
61+
\IntlDateFormatter::SHORT
62+
)->will(
63+
$this->returnValue('MM-dd-yyyy')
64+
);
65+
$model = new DateTime($localeMock);
66+
67+
$model->filter($inputData);
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function dateTimeFilterWithExceptionDataProvider()
74+
{
75+
return [
76+
['12-31-2000 22:22:22'],
77+
['22/2000-01 22:22:22'],
78+
];
2779
}
2880
}

0 commit comments

Comments
 (0)