Skip to content

Commit 2ee63de

Browse files
author
Oleksandr Dubovyk
committed
MC-37371: Wrong currency sign in Credit Memo grid
- fixed - modified unit test
1 parent fa211bd commit 2ee63de

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

app/code/Magento/Sales/Test/Unit/Ui/Component/Listing/Column/PriceTest.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1313
use Magento\Framework\View\Element\UiComponent\Processor;
1414
use Magento\Sales\Ui\Component\Listing\Column\Price;
15+
use Magento\Store\Model\Store;
16+
use Magento\Store\Model\StoreManagerInterface;
1517
use PHPUnit\Framework\MockObject\MockObject;
1618
use PHPUnit\Framework\TestCase;
1719

@@ -27,6 +29,11 @@ class PriceTest extends TestCase
2729
*/
2830
protected $currencyMock;
2931

32+
/**
33+
* @var StoreManagerInterface|MockObject
34+
*/
35+
private $storeManagerMock;
36+
3037
protected function setUp(): void
3138
{
3239
$objectManager = new ObjectManager($this);
@@ -40,31 +47,45 @@ protected function setUp(): void
4047
->setMethods(['load', 'format'])
4148
->disableOriginalConstructor()
4249
->getMock();
50+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
51+
->disableOriginalConstructor()
52+
->getMock();
4353
$this->model = $objectManager->getObject(
4454
Price::class,
45-
['currency' => $this->currencyMock, 'context' => $contextMock]
55+
['currency' => $this->currencyMock, 'context' => $contextMock, 'storeManager' => $this->storeManagerMock]
4656
);
4757
}
4858

49-
public function testPrepareDataSource()
59+
/**
60+
* @param $hasCurrency
61+
* @param $dataSource
62+
* @param $currencyCode
63+
* @dataProvider testPrepareDataSourceDataProvider
64+
*/
65+
public function testPrepareDataSource($hasCurrency, $dataSource, $currencyCode)
5066
{
5167
$itemName = 'itemName';
5268
$oldItemValue = 'oldItemValue';
5369
$newItemValue = 'newItemValue';
54-
$dataSource = [
55-
'data' => [
56-
'items' => [
57-
[
58-
$itemName => $oldItemValue,
59-
'base_currency_code' => 'US'
60-
]
61-
]
62-
]
63-
];
70+
71+
$store = $this->getMockBuilder(Store::class)
72+
->disableOriginalConstructor()
73+
->getMock();
74+
$currencyMock = $this->getMockBuilder(Currency::class)
75+
->disableOriginalConstructor()
76+
->getMock();
77+
$currencyMock->expects($hasCurrency ? $this->never() : $this->once())
78+
->method('getCurrencyCode')
79+
->willReturn($currencyCode);
80+
$this->storeManagerMock->expects($hasCurrency ? $this->never() : $this->once())
81+
->method('getStore')
82+
->willReturn($store);
83+
$store->expects($hasCurrency ? $this->never() : $this->once())
84+
->method('getBaseCurrency')
85+
->willReturn($currencyMock);
6486

6587
$this->currencyMock->expects($this->once())
6688
->method('load')
67-
->with($dataSource['data']['items'][0]['base_currency_code'])
6889
->willReturnSelf();
6990

7091
$this->currencyMock->expects($this->once())
@@ -76,4 +97,31 @@ public function testPrepareDataSource()
7697
$dataSource = $this->model->prepareDataSource($dataSource);
7798
$this->assertEquals($newItemValue, $dataSource['data']['items'][0][$itemName]);
7899
}
100+
101+
public function testPrepareDataSourceDataProvider()
102+
{
103+
$dataSource1 = [
104+
'data' => [
105+
'items' => [
106+
[
107+
'itemName' => 'oldItemValue',
108+
'base_currency_code' => 'US'
109+
]
110+
]
111+
]
112+
];
113+
$dataSource2 = [
114+
'data' => [
115+
'items' => [
116+
[
117+
'itemName' => 'oldItemValue'
118+
]
119+
]
120+
]
121+
];
122+
return [
123+
[true, $dataSource1, 'US'],
124+
[false, $dataSource2, 'SAR'],
125+
];
126+
}
79127
}

app/code/Magento/Sales/Ui/Component/Listing/Column/Price.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1111
use Magento\Framework\View\Element\UiComponentFactory;
12+
use Magento\Store\Model\StoreManagerInterface;
1213
use Magento\Ui\Component\Listing\Columns\Column;
1314
use Magento\Framework\Pricing\PriceCurrencyInterface;
1415
use Magento\Directory\Model\Currency;
@@ -28,6 +29,11 @@ class Price extends Column
2829
*/
2930
private $currency;
3031

32+
/**
33+
* @var StoreManagerInterface|null
34+
*/
35+
private $storeManager;
36+
3137
/**
3238
* Constructor
3339
*
@@ -37,18 +43,22 @@ class Price extends Column
3743
* @param array $components
3844
* @param array $data
3945
* @param Currency $currency
46+
* @param StoreManagerInterface|null $storeManager
4047
*/
4148
public function __construct(
4249
ContextInterface $context,
4350
UiComponentFactory $uiComponentFactory,
4451
PriceCurrencyInterface $priceFormatter,
4552
array $components = [],
4653
array $data = [],
47-
Currency $currency = null
54+
Currency $currency = null,
55+
StoreManagerInterface $storeManager = null
4856
) {
4957
$this->priceFormatter = $priceFormatter;
5058
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
5159
->create(Currency::class);
60+
$this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
61+
->create(StoreManagerInterface::class);
5262
parent::__construct($context, $uiComponentFactory, $components, $data);
5363
}
5464

@@ -63,6 +73,12 @@ public function prepareDataSource(array $dataSource)
6373
if (isset($dataSource['data']['items'])) {
6474
foreach ($dataSource['data']['items'] as & $item) {
6575
$currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
76+
if (!$currencyCode) {
77+
$store = $this->storeManager->getStore(
78+
$this->context->getFilterParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
79+
);
80+
$currencyCode = $store->getBaseCurrency()->getCurrencyCode();
81+
}
6682
$basePurchaseCurrency = $this->currency->load($currencyCode);
6783
$item[$this->getData('name')] = $basePurchaseCurrency
6884
->format($item[$this->getData('name')], [], false);

0 commit comments

Comments
 (0)