Skip to content

Commit eb5d1a7

Browse files
author
Robert He
committed
MAGETWO-58568: [SalesOrder] HTML tags displayed on the price field.
- use NumberFormatter to strip tags if necessary in currency-type fields
1 parent a38975d commit eb5d1a7

File tree

4 files changed

+147
-58
lines changed

4 files changed

+147
-58
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Backend\Block\Widget\Grid\Column\Renderer;
108

119
/**
1210
* Backend grid item renderer currency
13-
*
14-
* @author Magento Core Team <core@magentocommerce.com>
1511
*/
1612
class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
1713
{
@@ -49,6 +45,11 @@ class Currency extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\Abstra
4945
*/
5046
protected $_localeCurrency;
5147

48+
/**
49+
* @var \Magento\Framework\Intl\NumberFormatterFactory
50+
*/
51+
private $numberFormatterFactory;
52+
5253
/**
5354
* @param \Magento\Backend\Block\Context $context
5455
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -76,6 +77,23 @@ public function __construct(
7677
$this->_defaultBaseCurrency = $currencyFactory->create()->load($defaultBaseCurrencyCode);
7778
}
7879

80+
/**
81+
* Get number formatter factory
82+
*
83+
* @return \Magento\Framework\Intl\NumberFormatterFactory
84+
*
85+
* @deprecated
86+
*/
87+
private function getNumberFormatterFactory()
88+
{
89+
if ($this->numberFormatterFactory === null) {
90+
$this->numberFormatterFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
91+
\Magento\Framework\Intl\NumberFormatterFactory::class
92+
);
93+
}
94+
return $this->numberFormatterFactory;
95+
}
96+
7997
/**
8098
* Renders grid column
8199
*
@@ -86,10 +104,20 @@ public function render(\Magento\Framework\DataObject $row)
86104
{
87105
if ($data = (string)$this->_getValue($row)) {
88106
$currency_code = $this->_getCurrencyCode($row);
89-
$data = floatval($data) * $this->_getRate($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);
90119
$sign = (bool)(int)$this->getColumn()->getShowNumberSign() && $data > 0 ? '+' : '';
91-
$data = sprintf("%f", $data);
92-
$data = $this->_localeCurrency->getCurrency($currency_code)->toCurrency($data);
120+
$data = $currency->toCurrency($convertedPrice);
93121
return $sign . $data;
94122
}
95123
return $this->getColumn()->getDefault();

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

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,44 +47,49 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase
4747
*/
4848
protected $_row;
4949

50+
/*
51+
* @var \PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
protected $numberFactoryMock;
54+
5055
protected function setUp()
5156
{
52-
$this->_storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
53-
$this->_localeMock = $this->getMock('Magento\Framework\Locale\CurrencyInterface');
54-
$this->_requestMock = $this->getMock('Magento\Framework\App\RequestInterface');
57+
$this->_storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
58+
$this->_localeMock = $this->getMock(\Magento\Framework\Locale\CurrencyInterface::class);
59+
$this->_requestMock = $this->getMock(\Magento\Framework\App\RequestInterface::class);
5560

5661
$this->_curLocatorMock = $this->getMock(
57-
'Magento\Directory\Model\Currency\DefaultLocator',
62+
\Magento\Directory\Model\Currency\DefaultLocator::class,
5863
[],
5964
[],
6065
'',
6166
false
6267
);
6368
$this->_columnMock = $this->getMock(
64-
'Magento\Backend\Block\Widget\Grid\Column',
69+
\Magento\Backend\Block\Widget\Grid\Column::class,
6570
['getIndex'],
6671
[],
6772
'',
6873
false
6974
);
7075
$this->_columnMock->expects($this->any())->method('getIndex')->will($this->returnValue('columnIndex'));
7176

72-
$this->_currencyMock = $this->getMock('Magento\Directory\Model\Currency', [], [], '', false);
77+
$this->_currencyMock = $this->getMock(\Magento\Directory\Model\Currency::class, [], [], '', false);
7378
$this->_currencyMock->expects($this->any())->method('load')->will($this->returnSelf());
7479
$currencyFactoryMock = $this->getMock(
75-
'Magento\Directory\Model\CurrencyFactory',
80+
\Magento\Directory\Model\CurrencyFactory::class,
7681
['create'],
7782
[],
7883
'',
7984
false
8085
);
8186
$currencyFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_currencyMock));
8287

83-
$this->_row = new \Magento\Framework\DataObject(['columnIndex' => '10']);
88+
$this->_row = new \Magento\Framework\DataObject(['columnIndex' => '$10.00']);
8489

8590
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
8691
$this->_blockCurrency = $helper->getObject(
87-
'Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency',
92+
\Magento\Backend\Block\Widget\Grid\Column\Renderer\Currency::class,
8893
[
8994
'storeManager' => $this->_storeManagerMock,
9095
'localeCurrency' => $this->_localeMock,
@@ -95,6 +100,19 @@ protected function setUp()
95100
);
96101

97102
$this->_blockCurrency->setColumn($this->_columnMock);
103+
104+
$this->numberFormatterFactoryMock = $this->getMock(
105+
\Magento\Framework\Intl::class,
106+
['create'],
107+
[],
108+
'',
109+
false
110+
);
111+
$helper->setBackwardCompatibleProperty(
112+
$this->_blockCurrency,
113+
'numberFormatterFactory',
114+
$this->numberFormatterFactoryMock
115+
);
98116
}
99117

100118
protected function tearDown()
@@ -113,46 +131,40 @@ protected function tearDown()
113131
*/
114132
public function testRenderWithDefaultCurrency()
115133
{
116-
$this->_currencyMock->expects(
117-
$this->once()
118-
)->method(
119-
'getRate'
120-
)->with(
121-
'defaultCurrency'
122-
)->will(
123-
$this->returnValue(1.5)
124-
);
125-
126-
$this->_curLocatorMock->expects(
127-
$this->any()
128-
)->method(
129-
'getDefaultCurrency'
130-
)->with(
131-
$this->_requestMock
132-
)->will(
133-
$this->returnValue('defaultCurrency')
134-
);
135-
136-
$currLocaleMock = $this->getMock('Zend_Currency', [], [], '', false);
137-
$currLocaleMock->expects(
138-
$this->once()
139-
)->method(
140-
'toCurrency'
141-
)->with(
142-
15.0000
143-
)->will(
144-
$this->returnValue('15USD')
145-
);
146-
$this->_localeMock->expects(
147-
$this->once()
148-
)->method(
149-
'getCurrency'
150-
)->with(
151-
'defaultCurrency'
152-
)->will(
153-
$this->returnValue($currLocaleMock)
154-
);
155-
156-
$this->assertEquals('15USD', $this->_blockCurrency->render($this->_row));
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));
157169
}
158170
}

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
7676
*/
7777
protected $dateTimeFormatter;
7878

79+
/**
80+
* @var \Magento\Framework\Intl\NumberFormatterFactory
81+
*/
82+
private $numberFormatterFactory;
83+
7984
/**
8085
* @param \Magento\Framework\Model\Context $context
8186
* @param \Magento\Framework\Registry $registry
@@ -216,6 +221,23 @@ public function loadEntityAttributeIdBySet()
216221
return $this;
217222
}
218223

224+
/**
225+
* Get number formatter factory
226+
*
227+
* @return \Magento\Framework\Intl\NumberFormatterFactory
228+
*
229+
* @deprecated
230+
*/
231+
private function getNumberFormatterFactory()
232+
{
233+
if ($this->numberFormatterFactory === null) {
234+
$this->numberFormatterFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
235+
\Magento\Framework\Intl\NumberFormatterFactory::class
236+
);
237+
}
238+
return $this->numberFormatterFactory;
239+
}
240+
219241
/**
220242
* Prepare data for save
221243
*
@@ -256,7 +278,8 @@ public function beforeSave()
256278
$hasDefaultValue = (string)$defaultValue != '';
257279

258280
if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
259-
$numberFormatter = new \NumberFormatter($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL);
281+
$numberFormatter = $this->getNumberFormatterFactory()
282+
->create($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL);
260283
$defaultValue = $numberFormatter->parse($defaultValue);
261284
if ($defaultValue === false) {
262285
throw new LocalizedException(__('Invalid default decimal value'));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Intl;
7+
8+
/**
9+
* Class NumberFormatterFactory
10+
* @package Magento\Framework
11+
*/
12+
class NumberFormatterFactory
13+
{
14+
/**
15+
* Factory method for \NumberFormatter
16+
*
17+
* @param string $locale
18+
* @param int $style
19+
* @param string $pattern
20+
* @return \NumberFormatter
21+
*/
22+
public function create($locale, $style, $pattern = null)
23+
{
24+
return new \NumberFormatter($locale, $style, $pattern);
25+
}
26+
}

0 commit comments

Comments
 (0)