Skip to content

Commit 63c5409

Browse files
author
Roman Ganin
committed
Merge remote-tracking branch 'origin/MAGETWO-39201' into MAGETWO-38540
2 parents 3ad1194 + 037a051 commit 63c5409

File tree

2 files changed

+179
-60
lines changed
  • app/code/Magento/Reports
    • Block/Adminhtml/Sales/Grid/Column/Renderer
    • Test/Unit/Block/Adminhtml/Sales/Grid/Column/Renderer

2 files changed

+179
-60
lines changed

app/code/Magento/Reports/Block/Adminhtml/Sales/Grid/Column/Renderer/Date.php

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,20 @@ protected function _getFormat()
3737
{
3838
$format = $this->getColumn()->getFormat();
3939
if (!$format) {
40-
if (self::$_format === null) {
41-
try {
42-
$formats = (new DataBundle())->get(
43-
$this->_localeResolver->getLocale()
44-
)['calendar']['gregorian']['availableFormats'];
45-
46-
switch ($this->getColumn()->getPeriodType()) {
47-
case 'month':
48-
self::$_format = $formats['yM'];
49-
break;
50-
51-
case 'year':
52-
self::$_format = $formats['y'];
53-
break;
54-
55-
default:
56-
self::$_format = $this->_localeDate->getDateFormat(
57-
\IntlDateFormatter::MEDIUM
58-
);
59-
break;
60-
}
61-
} catch (\Exception $e) {
62-
}
40+
$dataBundle = new DataBundle();
41+
$resourceBundle = $dataBundle->get($this->_localeResolver->getLocale());
42+
$formats = $resourceBundle['calendar']['gregorian']['availableFormats'];
43+
switch ($this->getColumn()->getPeriodType()) {
44+
case 'month':
45+
$format = $formats['yM'];
46+
break;
47+
case 'year':
48+
$format = $formats['y'];
49+
break;
50+
default:
51+
$format = $this->_localeDate->getDateFormat(\IntlDateFormatter::MEDIUM);
52+
break;
6353
}
64-
$format = self::$_format;
6554
}
6655
return $format;
6756
}
@@ -74,51 +63,22 @@ protected function _getFormat()
7463
*/
7564
public function render(\Magento\Framework\Object $row)
7665
{
77-
//@todo: check this logic manually
7866
if ($data = $row->getData($this->getColumn()->getIndex())) {
7967
switch ($this->getColumn()->getPeriodType()) {
8068
case 'month':
81-
$dateFormat = 'yyyy-MM';
69+
$data = $data . '-01';
8270
break;
8371
case 'year':
84-
$dateFormat = 'yyyy';
85-
break;
86-
default:
87-
$dateFormat = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT;
72+
$data = $data . '-01-01';
8873
break;
8974
}
90-
9175
$format = $this->_getFormat();
92-
try {
93-
$data = $this->getColumn()->getGmtoffset()
94-
? \IntlDateFormatter::formatObject(
95-
$this->_localeDate->date(new \DateTime($data)),
96-
$format
97-
)
98-
: \IntlDateFormatter::formatObject(
99-
$this->_localeDate->date(
100-
new \DateTime($data),
101-
null,
102-
false
103-
),
104-
$format
105-
);
106-
} catch (\Exception $e) {
107-
$data = $this->getColumn()->getTimezone()
108-
? \IntlDateFormatter::formatObject(
109-
$this->_localeDate->date(new \DateTime($data)),
110-
$format
111-
)
112-
: \IntlDateFormatter::formatObject(
113-
$this->_localeDate->date(
114-
new \DateTime($data),
115-
null,
116-
false
117-
),
118-
$format
119-
);
76+
if ($this->getColumn()->getGmtoffset() || $this->getColumn()->getTimezone()) {
77+
$date = $this->_localeDate->date(new \DateTime($data));
78+
} else {
79+
$date = $this->_localeDate->date(new \DateTime($data), null, false);
12080
}
121-
return $data;
81+
return \IntlDateFormatter::formatObject($date, $format);
12282
}
12383
return $this->getColumn()->getDefault();
12484
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer;
8+
9+
use Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date;
10+
11+
class DateTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date
15+
*/
16+
protected $date;
17+
18+
/**
19+
* @var \Magento\Backend\Block\Context|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $contextMock;
22+
23+
/**
24+
* @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $resolverMock;
27+
28+
/**
29+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $localeDate;
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
protected function setUp()
37+
{
38+
$this->localeDate = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\TimezoneInterface')
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->localeDate
42+
->expects($this->once())
43+
->method('date')
44+
->will($this->returnArgument(0));
45+
46+
$this->contextMock = $this->getMockBuilder('Magento\Backend\Block\Context')
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$this->contextMock
50+
->expects($this->once())
51+
->method('getLocaleDate')
52+
->will($this->returnValue($this->localeDate));
53+
54+
$this->resolverMock = $this->getMockBuilder('Magento\Framework\Locale\ResolverInterface')
55+
->getMock();
56+
57+
$this->date = new Date(
58+
$this->contextMock,
59+
$this->resolverMock
60+
);
61+
}
62+
63+
/**
64+
* @param string $data
65+
* @param string $index
66+
* @param string $locale
67+
* @param string $period
68+
* @param string $result
69+
* @dataProvider datesDataProvider
70+
* @return void
71+
*/
72+
public function testRender($data, $index, $locale, $period, $result)
73+
{
74+
$this->resolverMock->expects($this->any())->method('getLocale')->will($this->returnValue($locale));
75+
$this->localeDate->expects($this->any())->method('getDateFormat')->willReturnCallback(
76+
function ($value) use ($locale) {
77+
return (new \IntlDateFormatter(
78+
$locale,
79+
$value,
80+
\IntlDateFormatter::NONE
81+
))->getPattern();
82+
}
83+
);
84+
85+
$objectMock = $this->getMockBuilder('Magento\Framework\Object')
86+
->setMethods(['getData'])
87+
->getMock();
88+
$objectMock->expects($this->once())->method('getData')->will($this->returnValue($data));
89+
90+
$columnMock = $this->getMockBuilder('Magento\Backend\Block\Widget\Grid\Column')
91+
->disableOriginalConstructor()
92+
->setMethods(['getIndex', 'getPeriodType'])
93+
->getMock();
94+
$columnMock->expects($this->once())->method('getIndex')->will($this->returnValue($index));
95+
$columnMock->expects($this->atLeastOnce())->method('getPeriodType')->will($this->returnValue($period));
96+
97+
$this->date->setColumn($columnMock);
98+
99+
$this->assertEquals($result, $this->date->render($objectMock));
100+
}
101+
102+
/**
103+
* @return array
104+
*/
105+
public function datesDataProvider()
106+
{
107+
return [
108+
[
109+
'data' => '2000',
110+
'index' => 'period',
111+
'locale' => 'en_US',
112+
'period' => 'year',
113+
'result' => '2000'
114+
],
115+
[
116+
'data' => '2030',
117+
'index' => 'period',
118+
'locale' => 'en_US',
119+
'period' => 'year',
120+
'result' => '2030'
121+
],
122+
[
123+
'data' => '2000-01',
124+
'index' => 'period',
125+
'locale' => 'en_US',
126+
'period' => 'month',
127+
'result' => '1/2000'
128+
],
129+
[
130+
'data' => '2030-12',
131+
'index' => 'period',
132+
'locale' => 'en_US',
133+
'period' => 'month',
134+
'result' => '12/2030'
135+
],
136+
[
137+
'data' => '2016-12',
138+
'index' => 'period',
139+
'locale' => 'uk_UA',
140+
'period' => 'month',
141+
'result' => '12.2016'
142+
],
143+
[
144+
'data' => '2014-06-25',
145+
'index' => 'period',
146+
'locale' => 'en_US',
147+
'period' => 'day',
148+
'result' => 'Jun 25, 2014'
149+
],
150+
[
151+
'data' => '2014-06-25',
152+
'index' => 'period',
153+
'locale' => 'uk_UA',
154+
'period' => 'day',
155+
'result' => '25 Jun 2014'
156+
]
157+
];
158+
}
159+
}

0 commit comments

Comments
 (0)