Skip to content

Commit 2ddfe0f

Browse files
authored
Merge pull request #106 from magento-fearless-kiwis/develop
[Kiwis] Bug Fixes
2 parents 982e52c + 8cb6668 commit 2ddfe0f

File tree

5 files changed

+234
-5
lines changed

5 files changed

+234
-5
lines changed

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

100644100755
Lines changed: 63 additions & 2 deletions
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,9 @@
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;
34+
Use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
3135

3236
/**
3337
* Class EavTest
@@ -138,9 +142,35 @@ class EavTest extends AbstractModifierTest
138142
*/
139143
private $eavAttributeMock;
140144

145+
/**
146+
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
147+
*/
148+
protected $storeMock;
149+
150+
/**
151+
* @var Currency|\PHPUnit_Framework_MockObject_MockObject
152+
*/
153+
protected $currencyMock;
154+
155+
/**
156+
* @var CurrencyLocale|\PHPUnit_Framework_MockObject_MockObject
157+
*/
158+
protected $currencyLocaleMock;
159+
160+
/**
161+
* @var ObjectManager
162+
*/
163+
protected $objectManager;
164+
165+
/**
166+
* @var Eav
167+
*/
168+
protected $eav;
169+
141170
protected function setUp()
142171
{
143172
parent::setUp();
173+
$this->objectManager = new ObjectManager($this);
144174
$this->eavConfigMock = $this->getMockBuilder(Config::class)
145175
->disableOriginalConstructor()
146176
->getMock();
@@ -236,6 +266,24 @@ protected function setUp()
236266
->willReturn([
237267
$this->attributeMock,
238268
]);
269+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
270+
->setMethods(['load', 'getId', 'getConfig', 'getBaseCurrencyCode'])
271+
->getMockForAbstractClass();
272+
$this->currencyMock = $this->getMockBuilder(Currency::class)
273+
->disableOriginalConstructor()
274+
->setMethods(['toCurrency'])
275+
->getMock();
276+
$this->currencyLocaleMock = $this->getMockBuilder(CurrencyLocale::class)
277+
->disableOriginalConstructor()
278+
->setMethods(['getCurrency'])
279+
->getMock();
280+
281+
$this->eav =$this->getModel();
282+
$this->objectManager->setBackwardCompatibleProperty(
283+
$this->eav,
284+
'localeCurrency',
285+
$this->currencyLocaleMock
286+
);
239287
}
240288

241289
/**
@@ -255,7 +303,7 @@ protected function createModel()
255303
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
256304
'attributeGroupRepository' => $this->attributeGroupRepositoryMock,
257305
'sortOrderBuilder' => $this->sortOrderBuilderMock,
258-
'attributeRepository' => $this->attributeRepositoryMock
306+
'attributeRepository' => $this->attributeRepositoryMock,
259307
]);
260308
}
261309

@@ -336,6 +384,19 @@ public function testModifyData()
336384
->method('getItems')
337385
->willReturn([$this->eavAttributeMock]);
338386

339-
$this->assertEquals($sourceData, $this->getModel()->modifyData([]));
387+
$this->storeMock->expects(($this->once()))
388+
->method('getBaseCurrencyCode')
389+
->willReturn('en_US');
390+
$this->storeManagerMock->expects($this->once())
391+
->method('getStore')
392+
->willReturn($this->storeMock);
393+
$this->currencyMock->expects($this->once())
394+
->method('toCurrency')
395+
->willReturn('19.99');
396+
$this->currencyLocaleMock->expects($this->once())
397+
->method('getCurrency')
398+
->willReturn($this->currencyMock);
399+
400+
$this->assertEquals($sourceData, $this->eav->modifyData([]));
340401
}
341402
}

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

100644100755
Lines changed: 41 additions & 1 deletion
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
@@ -111,7 +112,7 @@ class CustomOptions extends AbstractModifier
111112
* @var UrlInterface
112113
*/
113114
protected $urlBuilder;
114-
115+
115116
/**
116117
* @var ArrayManager
117118
*/
@@ -122,6 +123,11 @@ class CustomOptions extends AbstractModifier
122123
*/
123124
protected $meta = [];
124125

126+
/**
127+
* @var CurrencyInterface
128+
*/
129+
private $localeCurrency;
130+
125131
/**
126132
* @param LocatorInterface $locator
127133
* @param StoreManagerInterface $storeManager
@@ -1069,4 +1075,38 @@ protected function getCurrencySymbol()
10691075
{
10701076
return $this->storeManager->getStore()->getBaseCurrency()->getCurrencySymbol();
10711077
}
1078+
1079+
/**
1080+
* The getter function to get the locale currency for real application code
1081+
*
1082+
* @return \Magento\Framework\Locale\CurrencyInterface
1083+
*
1084+
* @deprecated
1085+
*/
1086+
private function getLocaleCurrency()
1087+
{
1088+
if ($this->localeCurrency === null) {
1089+
$this->localeCurrency = \Magento\Framework\App\ObjectManager::getInstance()->get(CurrencyInterface::class);
1090+
}
1091+
return $this->localeCurrency;
1092+
}
1093+
1094+
/**
1095+
* Format price according to the locale of the currency
1096+
*
1097+
* @param mixed $value
1098+
* @return string
1099+
*/
1100+
protected function formatPrice($value)
1101+
{
1102+
if (!is_numeric($value)) {
1103+
return null;
1104+
}
1105+
1106+
$store = $this->storeManager->getStore();
1107+
$currency = $this->getLocaleCurrency()->getCurrency($store->getBaseCurrencyCode());
1108+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
1109+
1110+
return $value;
1111+
}
10721112
}

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

100644100755
Lines changed: 40 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+
private $localeCurrency;
169+
164170
/**
165171
* @param LocatorInterface $locator
166172
* @param CatalogEavValidationRules $catalogEavValidationRules
@@ -867,4 +873,38 @@ private function calculateGroupCode(AttributeGroupInterface $group)
867873

868874
return $attributeGroupCode;
869875
}
876+
877+
/**
878+
* The getter function to get the locale currency for real application code
879+
*
880+
* @return \Magento\Framework\Locale\CurrencyInterface
881+
*
882+
* @deprecated
883+
*/
884+
private function getLocaleCurrency()
885+
{
886+
if ($this->localeCurrency === null) {
887+
$this->localeCurrency = \Magento\Framework\App\ObjectManager::getInstance()->get(CurrencyInterface::class);
888+
}
889+
return $this->localeCurrency;
890+
}
891+
892+
/**
893+
* Format price according to the locale of the currency
894+
*
895+
* @param mixed $value
896+
* @return string
897+
*/
898+
protected function formatPrice($value)
899+
{
900+
if (!is_numeric($value)) {
901+
return null;
902+
}
903+
904+
$store = $this->storeManager->getStore();
905+
$currency = $this->getLocaleCurrency()->getCurrency($store->getBaseCurrencyCode());
906+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
907+
908+
return $value;
909+
}
870910
}

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

100644100755
Lines changed: 87 additions & 2 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,6 +26,16 @@ class General extends AbstractModifier
2426
* @var ArrayManager
2527
*/
2628
protected $arrayManager;
29+
30+
/**
31+
* @var StoreManagerInterface
32+
*/
33+
private $storeManager;
34+
35+
/**
36+
* @var CurrencyInterface
37+
*/
38+
private $localeCurrency;
2739

2840
/**
2941
* @param LocatorInterface $locator
@@ -70,7 +82,7 @@ protected function customizeWeightFormat(array $data)
7082
$data = $this->arrayManager->replace(
7183
$path,
7284
$data,
73-
$this->formatWeight($this->arrayManager->get($path, $data))
85+
$this->formatNumber($this->arrayManager->get($path, $data))
7486
);
7587
}
7688

@@ -93,7 +105,7 @@ protected function customizeAdvancedPriceFormat(array $data)
93105
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE] =
94106
$this->formatPrice($value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE]);
95107
$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY] =
96-
(int)$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY];
108+
$this->formatNumber((int)$value[ProductAttributeInterface::CODE_TIER_PRICE_FIELD_PRICE_QTY]);
97109
}
98110
}
99111

@@ -350,4 +362,77 @@ protected function customizeNameListeners(array $meta)
350362
]
351363
);
352364
}
365+
366+
/**
367+
* The getter function to get the locale currency for real application code
368+
*
369+
* @return \Magento\Framework\Locale\CurrencyInterface
370+
*
371+
* @deprecated
372+
*/
373+
private function getLocaleCurrency()
374+
{
375+
if ($this->localeCurrency === null) {
376+
$this->localeCurrency = \Magento\Framework\App\ObjectManager::getInstance()->get(CurrencyInterface::class);
377+
}
378+
return $this->localeCurrency;
379+
}
380+
381+
/**
382+
* The getter function to get the store manager for real application code
383+
*
384+
* @return \Magento\Store\Model\StoreManagerInterface
385+
*
386+
* @deprecated
387+
*/
388+
private function getStoreManager()
389+
{
390+
if ($this->storeManager === null) {
391+
$this->storeManager =
392+
\Magento\Framework\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
393+
}
394+
return $this->storeManager;
395+
}
396+
397+
398+
/**
399+
* Format price according to the locale of the currency
400+
*
401+
* @param mixed $value
402+
* @return string
403+
*/
404+
protected function formatPrice($value)
405+
{
406+
if (!is_numeric($value)) {
407+
return null;
408+
}
409+
410+
$store = $this->getStoreManager()->getStore();
411+
$currency = $this->getLocaleCurrency()->getCurrency($store->getBaseCurrencyCode());
412+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
413+
414+
return $value;
415+
}
416+
417+
/**
418+
* Format number according to the locale of the currency and precision of input
419+
*
420+
* @param mixed $value
421+
* @return string
422+
*/
423+
protected function formatNumber($value)
424+
{
425+
if (!is_numeric($value)) {
426+
return null;
427+
}
428+
429+
$value = (float)$value;
430+
$precision = strlen(substr(strrchr($value, "."), 1));
431+
$store = $this->getStoreManager()->getStore();
432+
$currency = $this->getLocaleCurrency()->getCurrency($store->getBaseCurrencyCode());
433+
$value = $currency->toCurrency($value, ['display' => \Magento\Framework\Currency::NO_SYMBOL,
434+
'precision' => $precision]);
435+
436+
return $value;
437+
}
353438
}

lib/internal/Magento/Framework/Model/AbstractModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ public function getCollection()
523523
* @param integer $modelId
524524
* @param null|string $field
525525
* @return $this
526+
* @deprecated
526527
*/
527528
public function load($modelId, $field = null)
528529
{
@@ -623,6 +624,7 @@ public function setHasDataChanges($flag)
623624
*
624625
* @return $this
625626
* @throws \Exception
627+
* @deprecated
626628
*/
627629
public function save()
628630
{
@@ -807,6 +809,7 @@ public function afterSave()
807809
*
808810
* @return $this
809811
* @throws \Exception
812+
* @deprecated
810813
*/
811814
public function delete()
812815
{

0 commit comments

Comments
 (0)