Skip to content

Commit 1e511e5

Browse files
committed
Merge remote-tracking branch 'chaika/MC-37371' into PR-10-14
2 parents 3297512 + 303ef95 commit 1e511e5

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
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: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77

88
namespace Magento\Sales\Ui\Component\Listing\Column;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1112
use Magento\Framework\View\Element\UiComponentFactory;
13+
use Magento\Store\Model\StoreManagerInterface;
1214
use Magento\Ui\Component\Listing\Columns\Column;
1315
use Magento\Framework\Pricing\PriceCurrencyInterface;
1416
use Magento\Directory\Model\Currency;
1517

1618
/**
1719
* Class Price
20+
*
21+
* UiComponent class for Price format column
1822
*/
1923
class Price extends Column
2024
{
@@ -28,6 +32,11 @@ class Price extends Column
2832
*/
2933
private $currency;
3034

35+
/**
36+
* @var StoreManagerInterface|null
37+
*/
38+
private $storeManager;
39+
3140
/**
3241
* Constructor
3342
*
@@ -37,18 +46,22 @@ class Price extends Column
3746
* @param array $components
3847
* @param array $data
3948
* @param Currency $currency
49+
* @param StoreManagerInterface $storeManager
4050
*/
4151
public function __construct(
4252
ContextInterface $context,
4353
UiComponentFactory $uiComponentFactory,
4454
PriceCurrencyInterface $priceFormatter,
4555
array $components = [],
4656
array $data = [],
47-
Currency $currency = null
57+
Currency $currency = null,
58+
StoreManagerInterface $storeManager = null
4859
) {
4960
$this->priceFormatter = $priceFormatter;
50-
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
51-
->create(Currency::class);
61+
$this->currency = $currency ?: ObjectManager::getInstance()
62+
->get(Currency::class);
63+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
64+
->get(StoreManagerInterface::class);
5265
parent::__construct($context, $uiComponentFactory, $components, $data);
5366
}
5467

@@ -63,6 +76,12 @@ public function prepareDataSource(array $dataSource)
6376
if (isset($dataSource['data']['items'])) {
6477
foreach ($dataSource['data']['items'] as & $item) {
6578
$currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
79+
if (!$currencyCode) {
80+
$store = $this->storeManager->getStore(
81+
$this->context->getFilterParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
82+
);
83+
$currencyCode = $store->getBaseCurrency()->getCurrencyCode();
84+
}
6685
$basePurchaseCurrency = $this->currency->load($currencyCode);
6786
$item[$this->getData('name')] = $basePurchaseCurrency
6887
->format($item[$this->getData('name')], [], false);

0 commit comments

Comments
 (0)