Skip to content

Commit b2dff40

Browse files
author
Robert He
committed
MAGETWO-58568: [SalesOrder] HTML tags displayed on the price field.
- use PriceCurrency to format and convert in renderer
1 parent eb5d1a7 commit b2dff40

File tree

2 files changed

+27
-70
lines changed

2 files changed

+27
-70
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Currency.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Abstra
4646
protected $_localeCurrency;
4747

4848
/**
49-
* @var \Magento\Framework\Intl\NumberFormatterFactory
49+
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
5050
*/
51-
private $numberFormatterFactory;
51+
private $priceCurrency;
5252

5353
/**
5454
* @param \Magento\Backend\Block\Context $context
@@ -78,20 +78,20 @@ public function __construct(
7878
}
7979

8080
/**
81-
* Get number formatter factory
81+
* Get price currency
8282
*
83-
* @return \Magento\Framework\Intl\NumberFormatterFactory
83+
* @return \Magento\Framework\Pricing\PriceCurrencyInterface
8484
*
8585
* @deprecated
8686
*/
87-
private function getNumberFormatterFactory()
87+
private function getPriceCurrency()
8888
{
89-
if ($this->numberFormatterFactory === null) {
90-
$this->numberFormatterFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
91-
\Magento\Framework\Intl\NumberFormatterFactory::class
89+
if ($this->priceCurrency === null) {
90+
$this->priceCurrency = \Magento\Framework\App\ObjectManager::getInstance()->get(
91+
\Magento\Framework\Pricing\PriceCurrencyInterface::class
9292
);
9393
}
94-
return $this->numberFormatterFactory;
94+
return $this->priceCurrency;
9595
}
9696

9797
/**
@@ -102,25 +102,9 @@ private function getNumberFormatterFactory()
102102
*/
103103
public function render(\Magento\Framework\DataObject $row)
104104
{
105-
if ($data = (string)$this->_getValue($row)) {
106-
$currency_code = $this->_getCurrencyCode($row);
107-
$currency = $this->_localeCurrency->getCurrency($currency_code);
108-
$numberFormatter = $this->getNumberFormatterFactory()
109-
->create($currency->getLocale(), \NumberFormatter::CURRENCY);
110-
// Optionally need to strip tags from the column field
111-
if (!$stripData = $this->stripTags($data)) {
112-
$stripData = $data;
113-
}
114-
// Optionally need to remove currency symbols
115-
if (!$price = $numberFormatter->parseCurrency($stripData, $currency_code)) {
116-
$price = $stripData;
117-
}
118-
$convertedPrice = $price * $this->_getRate($row);
119-
$sign = (bool)(int)$this->getColumn()->getShowNumberSign() && $data > 0 ? '+' : '';
120-
$data = $currency->toCurrency($convertedPrice);
121-
return $sign . $data;
122-
}
123-
return $this->getColumn()->getDefault();
105+
$price = $this->_getValue($row) ? $this->_getValue($row) : $this->getColumn()->getDefault();
106+
$displayPrice = $this->getPriceCurrency()->convertAndFormat($price, false);
107+
return $displayPrice;
124108
}
125109

126110
/**

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/Column/Renderer/CurrencyTest.php

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase
5050
/*
5151
* @var \PHPUnit_Framework_MockObject_MockObject
5252
*/
53-
protected $numberFactoryMock;
53+
protected $priceCurrencyMock;
5454

5555
protected function setUp()
5656
{
@@ -101,17 +101,19 @@ protected function setUp()
101101

102102
$this->_blockCurrency->setColumn($this->_columnMock);
103103

104-
$this->numberFormatterFactoryMock = $this->getMock(
105-
\Magento\Framework\Intl::class,
106-
['create'],
104+
$this->priceCurrencyMock = $this->getMockForAbstractClass(
105+
\Magento\Framework\Pricing\PriceCurrencyInterface::class,
107106
[],
108107
'',
109-
false
108+
false,
109+
true,
110+
true,
111+
['convertAndFormat']
110112
);
111113
$helper->setBackwardCompatibleProperty(
112114
$this->_blockCurrency,
113-
'numberFormatterFactory',
114-
$this->numberFormatterFactoryMock
115+
'priceCurrency',
116+
$this->priceCurrencyMock
115117
);
116118
}
117119

@@ -131,40 +133,11 @@ protected function tearDown()
131133
*/
132134
public function testRenderWithDefaultCurrency()
133135
{
134-
$this->_currencyMock->expects($this->once())
135-
->method('getRate')
136-
->with('USD')
137-
->willReturn(1.5);
138-
$currLocaleMock = $this->getMock(\Magento\Framework\Currency::class, ['getLocale','toCurrency'], [], '', false);
139-
$currLocaleMock->expects($this->once())
140-
->method('getLocale')
141-
->willReturn('en_US');
142-
$this->_localeMock->expects($this->once())
143-
->method('getCurrency')
144-
->with('USD')
145-
->will($this->returnValue($currLocaleMock));
146-
147-
$numberFormatterMock = $this->getMock(\NumberFormatter::class, ['parseCurrency'], [], '', false);
148-
$this->numberFormatterFactoryMock->expects($this->once())
149-
->method('create')
150-
->with('en_US', \NumberFormatter::CURRENCY)
151-
->will($this->returnValue($numberFormatterMock));
152-
153-
$numberFormatterMock->expects($this->once())
154-
->method('parseCurrency')
155-
->with('$10.00', 'USD')
156-
->willReturn(10);
157-
158-
$this->_curLocatorMock->expects($this->any())
159-
->method('getDefaultCurrency')
160-
->with($this->_requestMock)
161-
->willReturn('USD');
162-
163-
$currLocaleMock->expects($this->once())
164-
->method('toCurrency')
165-
->with(15.0000)
166-
->willReturn('$15.00');
167-
168-
$this->assertEquals('$15.00', $this->_blockCurrency->render($this->_row));
136+
$this->priceCurrencyMock->expects($this->once())
137+
->method('convertAndFormat')
138+
->with('$10.00', false)
139+
->willReturn('$10.00');
140+
141+
$this->assertEquals('$10.00', $this->_blockCurrency->render($this->_row));
169142
}
170143
}

0 commit comments

Comments
 (0)