Skip to content

Commit 940d74a

Browse files
committed
MC-23915: Wrong currency symbol in creditmemo_grid & sales_order_view > creditmemo grid for subtotal & Shipping and Handling fee
1 parent 07f0fad commit 940d74a

File tree

3 files changed

+199
-51
lines changed

3 files changed

+199
-51
lines changed

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

Lines changed: 96 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
namespace Magento\Sales\Test\Unit\Ui\Component\Listing\Column;
99

1010
use Magento\Directory\Model\Currency;
11+
use Magento\Sales\Api\Data\OrderInterface;
12+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
13+
use Magento\Framework\Api\SearchCriteria;
14+
use Magento\Framework\Api\SearchCriteriaBuilder;
1115
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1216
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1317
use Magento\Framework\View\Element\UiComponent\Processor;
@@ -17,6 +21,9 @@
1721
use PHPUnit\Framework\MockObject\MockObject;
1822
use PHPUnit\Framework\TestCase;
1923

24+
/**
25+
* Test for \Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice
26+
*/
2027
class PurchasedPriceTest extends TestCase
2128
{
2229
/**
@@ -34,6 +41,21 @@ class PurchasedPriceTest extends TestCase
3441
*/
3542
private $orderRepository;
3643

44+
/**
45+
* @var SearchCriteria|MockObject
46+
*/
47+
private $searchCriteriaMock;
48+
49+
/**
50+
* @var OrderSearchResultInterface|MockObject
51+
*/
52+
private $orderSearchResultMock;
53+
54+
/**
55+
* @var OrderInterface|MockObject
56+
*/
57+
private $order;
58+
3759
protected function setUp(): void
3860
{
3961
$objectManager = new ObjectManager($this);
@@ -47,56 +69,61 @@ protected function setUp(): void
4769
->setMethods(['load', 'format'])
4870
->disableOriginalConstructor()
4971
->getMock();
50-
$this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
51-
->setMethods(['getList','get','delete','save','getOrderCurrencyCode'])
52-
->disableOriginalConstructor()
53-
->getMock();
72+
$this->orderRepository = $this->getMockForAbstractClass(OrderRepositoryInterface::class);
73+
$this->order = $this->getMockForAbstractClass(OrderInterface::class);
74+
$this->orderSearchResultMock = $this->getMockForAbstractClass(OrderSearchResultInterface::class);
75+
$this->searchCriteriaMock = $this->createMock(SearchCriteria::class);
76+
$searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
77+
$searchCriteriaBuilderMock->expects($this->once())
78+
->method('addFilter')
79+
->willReturnSelf();
80+
81+
$searchCriteriaBuilderMock->expects($this->once())
82+
->method('create')
83+
->willReturn($this->searchCriteriaMock);
84+
5485
$this->model = $objectManager->getObject(
5586
PurchasedPrice::class,
5687
[
5788
'currency' => $this->currencyMock,
5889
'context' => $contextMock,
59-
'order' => $this->orderRepository,
90+
'orderRepository' => $this->orderRepository,
91+
'order' => $this->order,
92+
'searchCriteriaBuilder' => $searchCriteriaBuilderMock,
93+
'searchCriteria' => $this->searchCriteriaMock,
94+
'orderSearchResult' => $this->orderSearchResultMock,
6095
]
6196
);
6297
}
6398

6499
/**
65-
* @param string $itemName
66-
* @param string $oldItemValue
67-
* @param string $newItemValue
68-
* @param string|null $orderCurrencyCode
100+
* @param array $orderData
101+
* @param array $dataSource
69102
* @dataProvider prepareDataSourceDataProvider
70103
*/
71-
public function testPrepareDataSource(
72-
$itemName,
73-
$oldItemValue,
74-
$newItemValue,
75-
$orderCurrencyCode
76-
): void {
77-
$dataSource = [
78-
'data' => [
79-
'items' => [
80-
[
81-
$itemName => $oldItemValue,
82-
'order_currency_code' => $orderCurrencyCode,
83-
'order_id' => 1,
84-
]
85-
]
86-
]
87-
];
104+
public function testPrepareDataSource(array $orderData,array $dataSource): void
105+
{
106+
$oldItemValue = 'oldItemValue';
107+
$newItemValue = 'newItemValue';
108+
109+
$this->orderRepository->expects($this->once())
110+
->method('getList')
111+
->with($this->searchCriteriaMock)
112+
->willReturn($this->orderSearchResultMock);
113+
114+
$this->orderSearchResultMock->expects($this->once())
115+
->method('getItems')
116+
->willReturn([$this->order]);
117+
118+
$this->order->expects($this->once())
119+
->method('getEntityId')
120+
->willReturn($orderData['entity_id']);
88121

89-
if (isset($dataSource['data']['items'][0]['order_currency_code'])) {
90-
$currencyCode = $dataSource['data']['items'][0]['order_currency_code'];
91-
} else {
92-
$currencyCode = 'FR';
93-
$this->orderRepository->expects($this->once())
94-
->method('get')
95-
->willReturnSelf();
96-
$this->orderRepository->expects($this->once())
97-
->method('getOrderCurrencyCode')
98-
->willReturn($currencyCode);
99-
}
122+
$this->order->expects($this->once())
123+
->method('getOrderCurrencyCode')
124+
->willReturn($orderData['order_currency_code']);
125+
126+
$currencyCode = $dataSource['data']['items'][0]['order_currency_code'] ?? $orderData['order_currency_code'];
100127

101128
$this->currencyMock->expects($this->once())
102129
->method('load')
@@ -108,9 +135,9 @@ public function testPrepareDataSource(
108135
->with($oldItemValue, [], false)
109136
->willReturn($newItemValue);
110137

111-
$this->model->setData('name', $itemName);
138+
$this->model->setData('name', 'item_name');
112139
$dataSource = $this->model->prepareDataSource($dataSource);
113-
$this->assertEquals($newItemValue, $dataSource['data']['items'][0][$itemName]);
140+
$this->assertEquals($newItemValue, $dataSource['data']['items'][0]['item_name']);
114141
}
115142

116143
/**
@@ -120,17 +147,38 @@ public function prepareDataSourceDataProvider(): array
120147
{
121148
return [
122149
[
123-
'item_name' => 'itemName',
124-
'old_item_value' => 'oldItemValue',
125-
'new_item_value' => 'newItemValue',
126-
'order_currency_code' => 'US',
150+
'orderData' => [
151+
'entity_id' => 1,
152+
'order_currency_code' => 'US',
153+
],
154+
'dataSource' => [
155+
'data' => [
156+
'items' => [
157+
[
158+
'item_name' => 'oldItemValue',
159+
'order_currency_code' => 'US',
160+
'order_id' => 1,
161+
]
162+
]
163+
]
164+
]
127165
],
128166
[
129-
'item_name' => 'itemName',
130-
'old_item_value' => 'oldItemValue',
131-
'new_item_value' => 'newItemValue',
132-
'order_currency_code' => null,
133-
],
167+
'orderData' => [
168+
'entity_id' => 1,
169+
'order_currency_code' => 'FR',
170+
],
171+
'dataSource' => [
172+
'data' => [
173+
'items' => [
174+
[
175+
'item_name' => 'oldItemValue',
176+
'order_id' => 1,
177+
]
178+
]
179+
]
180+
]
181+
]
134182
];
135183
}
136184
}

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

10+
use Magento\Framework\Api\SearchCriteriaBuilder;
1011
use Magento\Framework\View\Element\UiComponent\ContextInterface;
1112
use Magento\Framework\View\Element\UiComponentFactory;
1213
use Magento\Sales\Api\OrderRepositoryInterface;
@@ -34,6 +35,11 @@ class PurchasedPrice extends Column
3435
*/
3536
private $orderRepository;
3637

38+
/**
39+
* @var SearchCriteriaBuilder
40+
*/
41+
private $searchCriteriaBuilder;
42+
3743
/**
3844
* Constructor
3945
*
@@ -44,6 +50,7 @@ class PurchasedPrice extends Column
4450
* @param array $data
4551
* @param Currency $currency
4652
* @param OrderRepositoryInterface $orderRepository
53+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4754
*/
4855
public function __construct(
4956
ContextInterface $context,
@@ -52,16 +59,20 @@ public function __construct(
5259
array $components = [],
5360
array $data = [],
5461
Currency $currency = null,
55-
OrderRepositoryInterface $orderRepository = null
62+
OrderRepositoryInterface $orderRepository = null,
63+
SearchCriteriaBuilder $searchCriteriaBuilder = null
5664
) {
5765
$this->priceFormatter = $priceFormatter;
5866
$this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
5967
->create(Currency::class);
6068
$this->orderRepository = $orderRepository ?: \Magento\Framework\App\ObjectManager::getInstance()
6169
->create(OrderRepositoryInterface::class);
70+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: \Magento\Framework\App\ObjectManager::getInstance()
71+
->create(SearchCriteriaBuilder::class);
6272
parent::__construct($context, $uiComponentFactory, $components, $data);
6373
}
6474

75+
6576
/**
6677
* Prepare Data Source
6778
*
@@ -71,9 +82,10 @@ public function __construct(
7182
public function prepareDataSource(array $dataSource)
7283
{
7384
if (isset($dataSource['data']['items'])) {
85+
$orderIds = array_column($dataSource['data']['items'],'order_id');
86+
$orderCurrencyCodes = $this->getOrdersCurrency($orderIds);
7487
foreach ($dataSource['data']['items'] as & $item) {
75-
$currencyCode = $item['order_currency_code']
76-
?? $this->orderRepository->get($item['order_id'])->getOrderCurrencyCode();
88+
$currencyCode = $item['order_currency_code'] ?? $orderCurrencyCodes[$item['order_id']];
7789
$purchaseCurrency = $this->currency->load($currencyCode);
7890
$item[$this->getData('name')] = $purchaseCurrency
7991
->format($item[$this->getData('name')], [], false);
@@ -82,4 +94,23 @@ public function prepareDataSource(array $dataSource)
8294

8395
return $dataSource;
8496
}
97+
98+
/**
99+
* @param array $orderIds
100+
* @return array
101+
*/
102+
private function getOrdersCurrency(array $orderIds): array
103+
{
104+
$orderCurrencyCodes = [];
105+
106+
$searchCriteria = $this->searchCriteriaBuilder
107+
->addFilter('entity_id', $orderIds ,'in')
108+
->create();
109+
110+
foreach ($this->orderRepository->getList($searchCriteria)->getItems() as $order) {
111+
$orderCurrencyCodes[$order->getEntityId()] = $order->getOrderCurrencyCode();
112+
}
113+
114+
return $orderCurrencyCodes;
115+
}
85116
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Ui\Component\Listing\Column;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Sales\Model\Order;
12+
use PHPUnit\Framework\TestCase;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
/**
16+
* Test for \Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice
17+
*
18+
* @magentoAppArea adminhtml
19+
*/
20+
class PurchasedPriceTest extends TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var PurchasedPrice
29+
*/
30+
private $model;
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
$this->objectManager = Bootstrap::getObjectManager();
38+
$this->model = $this->objectManager->create(PurchasedPrice::class, [
39+
'data' => [
40+
'name' => 'subtotal',
41+
],
42+
]);
43+
}
44+
45+
/**
46+
* Verify prepare data source without order currency code
47+
*
48+
* @magentoDataFixture Magento/Sales/_files/order.php
49+
* @return void
50+
*/
51+
public function testPrepareDataSourceWithoutOrderCurrencyCode(): void
52+
{
53+
$order = $this->objectManager->get(Order::class)->loadByIncrementId('100000001');
54+
55+
$dataSource = [
56+
'data' => [
57+
'items' => [
58+
[
59+
'order_id' => $order->getEntityId(),
60+
'subtotal' => 100,
61+
],
62+
],
63+
],
64+
];
65+
66+
$result = $this->model->prepareDataSource($dataSource);
67+
$this->assertEquals('$100.00', $result['data']['items'][0]['subtotal']);
68+
}
69+
}

0 commit comments

Comments
 (0)