Skip to content

Commit 5d2f424

Browse files
committed
Merge branch 'ACP2E-92' of https://github.com/magento-l3/magento2ce into ACP2E-92
2 parents dc1d1bc + 0960629 commit 5d2f424

File tree

3 files changed

+25
-68
lines changed

3 files changed

+25
-68
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Validation/QuantityValidator.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Magento\Sales\Model\Order\Creditmemo;
1717
use Magento\Sales\Model\Order\Item;
1818
use Magento\Sales\Model\ValidatorInterface;
19+
use Magento\Sales\Api\Data\CreditmemoItemInterface;
20+
use \Magento\Framework\Phrase;
1921

2022
/**
2123
* Creditmemo QuantityValidator
@@ -74,7 +76,7 @@ public function validate($entity)
7476

7577
$totalQuantity = 0;
7678
foreach ($entity->getItems() as $item) {
77-
$message = $this->calculateTotalQuantity(
79+
$message = $this->validateTotalQuantityRefundable(
7880
$orderItemsById,
7981
$item,
8082
$invoiceQtysRefundLimits
@@ -102,7 +104,7 @@ public function validate($entity)
102104
* @param float $itemQty
103105
* @return bool
104106
*/
105-
private function isValidRefundQty($isQtyDecimal, float $itemQty): bool
107+
private function isValidDecimalRefundQty(?int $isQtyDecimal, float $itemQty): bool
106108
{
107109
if (!$isQtyDecimal && (floor($itemQty) !== $itemQty)) {
108110
return true;
@@ -116,9 +118,9 @@ private function isValidRefundQty($isQtyDecimal, float $itemQty): bool
116118
* @param array $orderItemsById
117119
* @param Magento\Sales\Api\Data\CreditmemoItemInterface|mixed $item
118120
* @param array $invoiceQtysRefundLimits
119-
* @return \Magento\Framework\Phrase|void
121+
* @return Phrase|void
120122
*/
121-
private function calculateTotalQuantity(
123+
private function validateTotalQuantityRefundable(
122124
array $orderItemsById,
123125
$item,
124126
array $invoiceQtysRefundLimits
@@ -131,9 +133,9 @@ private function calculateTotalQuantity(
131133
}
132134
$orderItem = $orderItemsById[$item->getOrderItemId()];
133135

134-
if ($this->isValidRefundQty($orderItem->getIsQtyDecimal(), $item->getQty())) {
136+
if ($this->isValidDecimalRefundQty($orderItem->getIsQtyDecimal(), $item->getQty())) {
135137
return __(
136-
'You cannot use decimal quantity to refund item "%1".',
138+
'We found an invalid quantity to refund item "%1".',
137139
$orderItem->getSku()
138140
);
139141
}

app/code/Magento/Sales/Test/Unit/Model/Order/Creditmemo/Validation/QuantityValidatorTest.php

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -152,72 +152,13 @@ public function testValidateWithWrongItemId()
152152
);
153153
}
154154

155-
public function testValidateWithWrongRefundQty()
156-
{
157-
$orderId = 1;
158-
$orderItemId = 1;
159-
$qtyToRequest = 1.5;
160-
$sku = 'test sku';
161-
$total = 0;
162-
$expected = [
163-
__(
164-
'You cannot use decimal quantity to refund item "%1".',
165-
$sku
166-
),
167-
__('The credit memo\'s total must be positive.')
168-
];
169-
$creditmemoMock = $this->getMockBuilder(CreditmemoInterface::class)
170-
->disableOriginalConstructor()
171-
->getMockForAbstractClass();
172-
$creditmemoMock->expects($this->exactly(2))->method('getOrderId')
173-
->willReturn($orderId);
174-
$creditmemoMock->expects($this->once())->method('getGrandTotal')
175-
->willReturn($total);
176-
$creditmemoItemMock = $this->getMockBuilder(
177-
CreditmemoItemInterface::class
178-
)->disableOriginalConstructor()
179-
->getMockForAbstractClass();
180-
$creditmemoItemMock->expects($this->exactly(2))->method('getOrderItemId')
181-
->willReturn($orderItemId);
182-
$creditmemoItemMock->expects($this->never())->method('getSku')
183-
->willReturn($sku);
184-
$creditmemoItemMock->expects($this->atLeastOnce())->method('getQty')
185-
->willReturn($qtyToRequest);
186-
$creditmemoMock->expects($this->exactly(1))->method('getItems')
187-
->willReturn([$creditmemoItemMock]);
188-
189-
$orderMock = $this->getMockBuilder(OrderInterface::class)
190-
->disableOriginalConstructor()
191-
->getMockForAbstractClass();
192-
$orderItemMock = $this->getMockBuilder(Item::class)
193-
->disableOriginalConstructor()
194-
->getMock();
195-
$creditmemoItemMock->expects($this->any())->method('getQty')
196-
->willReturn($qtyToRequest);
197-
$orderMock->expects($this->once())->method('getItems')
198-
->willReturn([$orderItemMock]);
199-
$orderItemMock->expects($this->once())->method('getItemId')
200-
->willReturn($orderItemId);
201-
$orderItemMock->expects($this->any())->method('getSku')
202-
->willReturn($sku);
203-
204-
$this->orderRepositoryMock->expects($this->once())
205-
->method('get')
206-
->with($orderId)
207-
->willReturn($orderMock);
208-
209-
$this->assertEquals(
210-
$expected,
211-
$this->validator->validate($creditmemoMock)
212-
);
213-
}
214-
215155
/**
216156
* @param int $orderId
217157
* @param int $orderItemId
218158
* @param int $qtyToRequest
219159
* @param int $qtyToRefund
220160
* @param string $sku
161+
* @param int $total
221162
* @param array $expected
222163
* @dataProvider dataProviderForValidateQty
223164
*/
@@ -249,7 +190,7 @@ public function testValidate($orderId, $orderItemId, $qtyToRequest, $qtyToRefund
249190
$orderItemMock = $this->getMockBuilder(Item::class)
250191
->disableOriginalConstructor()
251192
->getMock();
252-
$orderItemMock->expects($this->exactly(2))->method('getQtyToRefund')
193+
$orderItemMock->expects($this->any())->method('getQtyToRefund')
253194
->willReturn($qtyToRefund);
254195
$creditmemoItemMock->expects($this->any())->method('getQty')
255196
->willReturn($qtyToRequest);
@@ -297,6 +238,21 @@ public function dataProviderForValidateQty()
297238
'total' => 15,
298239
'expected' => []
299240
],
241+
[
242+
'orderId' => 1,
243+
'orderItemId' => 1,
244+
'qtyToRequest' => 1.5,
245+
'qtyToRefund' => 1,
246+
'sku',
247+
'total' => 0,
248+
'expected' => [
249+
__(
250+
'We found an invalid quantity to refund item "%1".',
251+
$sku
252+
),
253+
__('The credit memo\'s total must be positive.')
254+
]
255+
],
300256
[
301257
'orderId' => 1,
302258
'orderItemId' => 1,

app/code/Magento/Sales/i18n/en_US.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,3 @@ If set YES Email field will be required during Admin order creation for new Cust
806806
"The coupon code has been removed.","The coupon code has been removed."
807807
"This creditmemo no longer exists.","This creditmemo no longer exists."
808808
"Add to address book","Add to address book"
809-
"You cannot use decimal quantity to refund item","You cannot use decimal quantity to refund item"

0 commit comments

Comments
 (0)