Skip to content

Commit 7806824

Browse files
ENGCOM-8579: Issues 31197 - Loading wrong order tax info #31198
2 parents c2d3d60 + 1dd77f7 commit 7806824

File tree

2 files changed

+105
-51
lines changed
  • app/code/Magento/Sales

2 files changed

+105
-51
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Totals/Tax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getFullTaxInfo()
9595

9696
$taxClassAmount = $this->_taxHelper->getCalculatedTaxes($source);
9797
if (empty($taxClassAmount)) {
98-
$rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($source)->toArray();
98+
$rates = $this->_taxOrderFactory->create()->getCollection()->loadByOrder($this->getOrder())->toArray();
9999
$taxClassAmount = $this->_taxCalculation->reproduceProcess($rates['items']);
100100
}
101101

app/code/Magento/Sales/Test/Unit/Block/Adminhtml/Order/Totals/TaxTest.php

Lines changed: 104 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
*/
66
declare(strict_types=1);
77

8-
/**
9-
* Test class for \Magento\Sales\Block\Adminhtml\Order\Totals\TaxTest
10-
*/
118
namespace Magento\Sales\Test\Unit\Block\Adminhtml\Order\Totals;
129

1310
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -16,46 +13,75 @@
1613
use Magento\Sales\Model\Order\Creditmemo;
1714
use Magento\Sales\Model\Order\Invoice;
1815
use Magento\Tax\Helper\Data;
16+
use Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection;
17+
use Magento\Tax\Model\Sales\Order\Tax as TaxModel;
18+
use Magento\Tax\Model\Sales\Order\TaxFactory;
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
2121

22+
/**
23+
* Test for \Magento\Sales\Block\Adminhtml\Order\Totals\Tax
24+
*/
2225
class TaxTest extends TestCase
2326
{
24-
/** @var MockObject|Tax */
27+
/**
28+
* @var array
29+
*/
30+
private $calculatedData = [
31+
'tax' => 'tax',
32+
'shipping_tax' => 'shipping_tax',
33+
];
34+
35+
/**
36+
* @var MockObject|Tax
37+
*/
2538
private $taxMock;
2639

40+
/**
41+
* @var Data|MockObject
42+
*/
43+
private $taxHelperMock;
44+
45+
/**
46+
* @var TaxFactory|MockObject
47+
*/
48+
private $taxOrderFactory;
49+
50+
/**
51+
* @inheridoc
52+
*/
2753
protected function setUp(): void
2854
{
29-
$getCalculatedTax = [
30-
'tax' => 'tax',
31-
'shipping_tax' => 'shipping_tax',
32-
];
33-
$taxHelperMock = $this->getMockBuilder(Data::class)
34-
->setMethods(['getCalculatedTaxes'])
55+
$this->taxHelperMock = $this->getMockBuilder(Data::class)
56+
->onlyMethods(['getCalculatedTaxes'])
3557
->disableOriginalConstructor()
3658
->getMock();
37-
$taxHelperMock->expects($this->any())
38-
->method('getCalculatedTaxes')
39-
->willReturn($getCalculatedTax);
4059

60+
$this->taxOrderFactory = $this->createMock(TaxFactory::class);
61+
62+
$arguments = $this->getModelArguments(
63+
['taxHelper' => $this->taxHelperMock, 'taxOrderFactory' => $this->taxOrderFactory]
64+
);
4165
$this->taxMock = $this->getMockBuilder(Tax::class)
42-
->setConstructorArgs($this->_getConstructArguments($taxHelperMock))
43-
->setMethods(['getOrder', 'getSource'])
66+
->setConstructorArgs($arguments)
67+
->onlyMethods(['getOrder', 'getSource'])
4468
->getMock();
4569
}
4670

4771
/**
4872
* Test method for getFullTaxInfo
4973
*
50-
* @param Order $source
51-
* @param array $getCalculatedTax
52-
* @param array $getShippingTax
74+
* @param Order|null $source
5375
* @param array $expectedResult
76+
* @return void
5477
*
5578
* @dataProvider getFullTaxInfoDataProvider
5679
*/
57-
public function testGetFullTaxInfo($source, $expectedResult)
80+
public function testGetFullTaxInfo(?Order $source, array $expectedResult): void
5881
{
82+
$this->taxHelperMock->expects($this->any())
83+
->method('getCalculatedTaxes')
84+
->willReturn($this->calculatedData);
5985
$this->taxMock->expects($this->once())
6086
->method('getOrder')
6187
->willReturn($source);
@@ -69,13 +95,15 @@ public function testGetFullTaxInfo($source, $expectedResult)
6995
*
7096
* @param Invoice|Creditmemo $source
7197
* @param array $expectedResult
98+
* @return void
7299
*
73100
* @dataProvider getCreditAndInvoiceFullTaxInfoDataProvider
74101
*/
75-
public function testGetFullTaxInfoWithCreditAndInvoice(
76-
$source,
77-
$expectedResult
78-
) {
102+
public function testGetFullTaxInfoWithCreditAndInvoice($source, array $expectedResult): void
103+
{
104+
$this->taxHelperMock->expects($this->any())
105+
->method('getCalculatedTaxes')
106+
->willReturn($this->calculatedData);
79107
$this->taxMock->expects($this->once())
80108
->method('getSource')
81109
->willReturn($source);
@@ -84,19 +112,57 @@ public function testGetFullTaxInfoWithCreditAndInvoice(
84112
$this->assertSame($expectedResult, $actualResult);
85113
}
86114

115+
/**
116+
* Test method for getFullTaxInfo when order doesn't have tax
117+
*
118+
* @return void
119+
*/
120+
public function testGetFullTaxInfoOrderWithoutTax(): void
121+
{
122+
$this->taxHelperMock->expects($this->once())
123+
->method('getCalculatedTaxes')
124+
->willReturn(null);
125+
126+
$orderMock = $this->createMock(Order::class);
127+
$taxCollection = $this->createMock(Collection::class);
128+
$taxCollection->expects($this->once())
129+
->method('loadByOrder')
130+
->with($orderMock)
131+
->willReturnSelf();
132+
$taxCollection->expects($this->once())
133+
->method('toArray')
134+
->willReturn(['items' => []]);
135+
136+
$taxOrder = $this->createMock(TaxModel::class);
137+
$taxOrder->expects($this->once())
138+
->method('getCollection')
139+
->willReturn($taxCollection);
140+
$this->taxOrderFactory->expects($this->once())
141+
->method('create')
142+
->willReturn($taxOrder);
143+
144+
$invoiceMock = $this->createMock(Invoice::class);
145+
$this->taxMock->expects($this->once())
146+
->method('getSource')
147+
->willReturn($invoiceMock);
148+
$this->taxMock->expects($this->once())
149+
->method('getOrder')
150+
->willReturn($orderMock);
151+
152+
$this->assertNull($this->taxMock->getFullTaxInfo());
153+
}
154+
87155
/**
88156
* Provide the tax helper mock as a constructor argument
89157
*
90-
* @param $taxHelperMock
158+
* @param array $arguments
91159
* @return array
92160
*/
93-
protected function _getConstructArguments($taxHelperMock)
161+
private function getModelArguments(array $arguments): array
94162
{
95163
$objectManagerHelper = new ObjectManager($this);
96-
return $objectManagerHelper->getConstructArguments(
97-
Tax::class,
98-
['taxHelper' => $taxHelperMock]
99-
);
164+
165+
return $objectManagerHelper->getConstructArguments(Tax::class, $arguments);
100166
}
101167

102168
/**
@@ -106,19 +172,15 @@ protected function _getConstructArguments($taxHelperMock)
106172
*
107173
* @return array
108174
*/
109-
public function getFullTaxInfoDataProvider()
175+
public function getFullTaxInfoDataProvider(): array
110176
{
111-
$salesModelOrderMock = $this->getMockBuilder(Order::class)
112-
->disableOriginalConstructor()
113-
->getMock();
177+
$salesModelOrderMock = $this->createMock(Order::class);
178+
114179
return [
115180
'source is not an instance of \Magento\Sales\Model\Order' => [null, []],
116181
'source is an instance of \Magento\Sales\Model\Order and has reasonable data' => [
117182
$salesModelOrderMock,
118-
[
119-
'tax' => 'tax',
120-
'shipping_tax' => 'shipping_tax',
121-
],
183+
$this->calculatedData,
122184
]
123185
];
124186
}
@@ -130,22 +192,14 @@ public function getFullTaxInfoDataProvider()
130192
*
131193
* @return array
132194
*/
133-
public function getCreditAndInvoiceFullTaxInfoDataProvider()
195+
public function getCreditAndInvoiceFullTaxInfoDataProvider(): array
134196
{
135-
$invoiceMock = $this->getMockBuilder(Invoice::class)
136-
->disableOriginalConstructor()
137-
->getMock();
138-
$creditMemoMock = $this->getMockBuilder(Creditmemo::class)
139-
->disableOriginalConstructor()
140-
->getMock();
197+
$invoiceMock = $this->createMock(Invoice::class);
198+
$creditMemoMock = $this->createMock(Creditmemo::class);
141199

142-
$expected = [
143-
'tax' => 'tax',
144-
'shipping_tax' => 'shipping_tax',
145-
];
146200
return [
147-
'invoice' => [$invoiceMock, $expected],
148-
'creditMemo' => [$creditMemoMock, $expected]
201+
'invoice' => [$invoiceMock, $this->calculatedData],
202+
'creditMemo' => [$creditMemoMock, $this->calculatedData]
149203
];
150204
}
151205
}

0 commit comments

Comments
 (0)