Skip to content

Commit 49fe79c

Browse files
author
Robert He
committed
MAGETWO-52891: [FT] CreateSimpleProductEntityTest failed to recognize a comma and a dot in the product price
- added correct price and weight formats on backend product form
1 parent 26630f7 commit 49fe79c

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/EavTest.php

100644100755
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Eav\Model\Config;
1111
use Magento\Framework\App\RequestInterface;
1212
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Store\Api\Data\StoreInterface;
1314
use Magento\Ui\DataProvider\EavValidationRules;
1415
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection as GroupCollection;
1516
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory as GroupCollectionFactory;
@@ -28,6 +29,8 @@
2829
use Magento\Catalog\Api\Data\ProductAttributeInterface;
2930
use Magento\Eav\Api\Data\AttributeGroupInterface;
3031
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
32+
use Magento\Framework\Currency;
33+
use Magento\Framework\Locale\Currency as CurrencyLocale;
3134

3235
/**
3336
* Class EavTest
@@ -138,6 +141,21 @@ class EavTest extends AbstractModifierTest
138141
*/
139142
private $eavAttributeMock;
140143

144+
/**
145+
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
146+
*/
147+
protected $storeMock;
148+
149+
/**
150+
* @var Currency|\PHPUnit_Framework_MockObject_MockObject
151+
*/
152+
protected $currencyMock;
153+
154+
/**
155+
* @var CurrencyLocale|\PHPUnit_Framework_MockObject_MockObject
156+
*/
157+
protected $currencyLocaleMock;
158+
141159
protected function setUp()
142160
{
143161
parent::setUp();
@@ -236,6 +254,17 @@ protected function setUp()
236254
->willReturn([
237255
$this->attributeMock,
238256
]);
257+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
258+
->setMethods(['load', 'getId', 'getConfig', 'getBaseCurrencyCode'])
259+
->getMockForAbstractClass();
260+
$this->currencyMock = $this->getMockBuilder(Currency::class)
261+
->disableOriginalConstructor()
262+
->setMethods(['toCurrency'])
263+
->getMock();
264+
$this->currencyLocaleMock = $this->getMockBuilder(CurrencyLocale::class)
265+
->disableOriginalConstructor()
266+
->setMethods(['getCurrency'])
267+
->getMock();
239268
}
240269

241270
/**
@@ -255,7 +284,8 @@ protected function createModel()
255284
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
256285
'attributeGroupRepository' => $this->attributeGroupRepositoryMock,
257286
'sortOrderBuilder' => $this->sortOrderBuilderMock,
258-
'attributeRepository' => $this->attributeRepositoryMock
287+
'attributeRepository' => $this->attributeRepositoryMock,
288+
'localeCurrency' => $this->currencyLocaleMock,
259289
]);
260290
}
261291

@@ -336,6 +366,19 @@ public function testModifyData()
336366
->method('getItems')
337367
->willReturn([$this->eavAttributeMock]);
338368

369+
$this->storeMock->expects(($this->once()))
370+
->method('getBaseCurrencyCode')
371+
->willReturn('en_US');
372+
$this->storeManagerMock->expects($this->once())
373+
->method('getStore')
374+
->willReturn($this->storeMock);
375+
$this->currencyMock->expects($this->once())
376+
->method('toCurrency')
377+
->willReturn('19.99');
378+
$this->currencyLocaleMock->expects($this->once())
379+
->method('getCurrency')
380+
->willReturn($this->currencyMock);
381+
339382
$this->assertEquals($sourceData, $this->getModel()->modifyData([]));
340383
}
341384
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

100644100755
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Ui\Component\Form\Element\ActionDelete;
2323
use Magento\Ui\Component\Form\Element\DataType\Text;
2424
use Magento\Ui\Component\Form\Element\DataType\Number;
25+
use Magento\Framework\Locale\CurrencyInterface;
2526

2627
/**
2728
* Data provider for "Customizable Options" panel
@@ -112,6 +113,11 @@ class CustomOptions extends AbstractModifier
112113
*/
113114
protected $urlBuilder;
114115

116+
/**
117+
* @var CurrencyInterface
118+
*/
119+
protected $localeCurrency;
120+
115121
/**
116122
* @var ArrayManager
117123
*/
@@ -128,6 +134,7 @@ class CustomOptions extends AbstractModifier
128134
* @param ConfigInterface $productOptionsConfig
129135
* @param ProductOptionsPrice $productOptionsPrice
130136
* @param UrlInterface $urlBuilder
137+
* @param CurrencyInterface $localeCurrency
131138
* @param ArrayManager $arrayManager
132139
*/
133140
public function __construct(
@@ -136,13 +143,15 @@ public function __construct(
136143
ConfigInterface $productOptionsConfig,
137144
ProductOptionsPrice $productOptionsPrice,
138145
UrlInterface $urlBuilder,
146+
CurrencyInterface $localeCurrency,
139147
ArrayManager $arrayManager
140148
) {
141149
$this->locator = $locator;
142150
$this->storeManager = $storeManager;
143151
$this->productOptionsConfig = $productOptionsConfig;
144152
$this->productOptionsPrice = $productOptionsPrice;
145153
$this->urlBuilder = $urlBuilder;
154+
$this->localeCurrency = $localeCurrency;
146155
$this->arrayManager = $arrayManager;
147156
}
148157

@@ -1069,4 +1078,23 @@ protected function getCurrencySymbol()
10691078
{
10701079
return $this->storeManager->getStore()->getBaseCurrency()->getCurrencySymbol();
10711080
}
1081+
1082+
/**
1083+
* Format price according to the locale of the currency
1084+
*
1085+
* @param mixed $value
1086+
* @return string
1087+
*/
1088+
protected function formatPrice($value)
1089+
{
1090+
if (!is_numeric($value)) {
1091+
return null;
1092+
}
1093+
1094+
$store = $this->storeManager->getStore();
1095+
$currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
1096+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
1097+
1098+
return $value;
1099+
}
10721100
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

100644100755
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Magento\Ui\DataProvider\Mapper\MetaProperties as MetaPropertiesMapper;
3131
use Magento\Ui\Component\Form\Element\Wysiwyg as WysiwygElement;
3232
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
33+
use Magento\Framework\Locale\CurrencyInterface;
3334

3435
/**
3536
* Class Eav
@@ -161,6 +162,11 @@ class Eav extends AbstractModifier
161162
*/
162163
private $prevSetAttributes;
163164

165+
/**
166+
* @var CurrencyInterface
167+
*/
168+
protected $localeCurrency;
169+
164170
/**
165171
* @param LocatorInterface $locator
166172
* @param CatalogEavValidationRules $catalogEavValidationRules
@@ -179,6 +185,7 @@ class Eav extends AbstractModifier
179185
* @param ArrayManager $arrayManager
180186
* @param ScopeOverriddenValue $scopeOverriddenValue
181187
* @param DataPersistorInterface $dataPersistor
188+
* @param CurrencyInterface $localeCurrency
182189
* @param array $attributesToDisable
183190
* @param array $attributesToEliminate
184191
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -201,6 +208,7 @@ public function __construct(
201208
ArrayManager $arrayManager,
202209
ScopeOverriddenValue $scopeOverriddenValue,
203210
DataPersistorInterface $dataPersistor,
211+
CurrencyInterface $localeCurrency,
204212
$attributesToDisable = [],
205213
$attributesToEliminate = []
206214
) {
@@ -221,6 +229,7 @@ public function __construct(
221229
$this->arrayManager = $arrayManager;
222230
$this->scopeOverriddenValue = $scopeOverriddenValue;
223231
$this->dataPersistor = $dataPersistor;
232+
$this->localeCurrency = $localeCurrency;
224233
$this->attributesToDisable = $attributesToDisable;
225234
$this->attributesToEliminate = $attributesToEliminate;
226235
}
@@ -867,4 +876,23 @@ private function calculateGroupCode(AttributeGroupInterface $group)
867876

868877
return $attributeGroupCode;
869878
}
879+
880+
/**
881+
* Format price according to the locale of the currency
882+
*
883+
* @param mixed $value
884+
* @return string
885+
*/
886+
protected function formatPrice($value)
887+
{
888+
if (!is_numeric($value)) {
889+
return null;
890+
}
891+
892+
$store = $this->storeManager->getStore();
893+
$currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
894+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
895+
896+
return $value;
897+
}
870898
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

100644100755
Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Catalog\Model\Locator\LocatorInterface;
1010
use Magento\Ui\Component\Form;
1111
use Magento\Framework\Stdlib\ArrayManager;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\Locale\CurrencyInterface;
1214

1315
/**
1416
* Data provider for main panel of product page
@@ -24,17 +26,33 @@ class General extends AbstractModifier
2426
* @var ArrayManager
2527
*/
2628
protected $arrayManager;
29+
30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
protected $storeManager;
34+
35+
/**
36+
* @var CurrencyInterface
37+
*/
38+
protected $localeCurrency;
2739

2840
/**
2941
* @param LocatorInterface $locator
3042
* @param ArrayManager $arrayManager
43+
* @param StoreManagerInterface $storeManager
44+
* @param CurrencyInterface $localeCurrency
3145
*/
3246
public function __construct(
3347
LocatorInterface $locator,
34-
ArrayManager $arrayManager
48+
ArrayManager $arrayManager,
49+
StoreManagerInterface $storeManager,
50+
CurrencyInterface $localeCurrency
3551
) {
3652
$this->locator = $locator;
3753
$this->arrayManager = $arrayManager;
54+
$this->storeManager = $storeManager;
55+
$this->localeCurrency = $localeCurrency;
3856
}
3957

4058
/**
@@ -70,7 +88,7 @@ protected function customizeWeightFormat(array $data)
7088
$data = $this->arrayManager->replace(
7189
$path,
7290
$data,
73-
$this->formatWeight($this->arrayManager->get($path, $data))
91+
$this->formatNumber($this->arrayManager->get($path, $data))
7492
);
7593
}
7694

@@ -93,7 +111,7 @@ protected function customizeAdvancedPriceFormat(array $data)
93111
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE] =
94112
$this->formatPrice($value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE]);
95113
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY] =
96-
(int)$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY];
114+
$this->formatNumber((int)$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY]);
97115
}
98116
}
99117

@@ -350,4 +368,45 @@ protected function customizeNameListeners(array $meta)
350368
]
351369
);
352370
}
371+
372+
/**
373+
* Format price according to the locale of the currency
374+
*
375+
* @param mixed $value
376+
* @return string
377+
*/
378+
protected function formatPrice($value)
379+
{
380+
if (!is_numeric($value)) {
381+
return null;
382+
}
383+
384+
$store = $this->storeManager->getStore();
385+
$currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
386+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
387+
388+
return $value;
389+
}
390+
391+
/**
392+
* Format number according to the locale of the currency and precision of input
393+
*
394+
* @param mixed $value
395+
* @return string
396+
*/
397+
protected function formatNumber($value)
398+
{
399+
if (!is_numeric($value)) {
400+
return null;
401+
}
402+
403+
$value = (float)$value;
404+
$precision = strlen(substr(strrchr($value, "."), 1));
405+
$store = $this->storeManager->getStore();
406+
$currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode());
407+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL,
408+
'precision' => $precision]);
409+
410+
return $value;
411+
}
353412
}

0 commit comments

Comments
 (0)