Skip to content

Commit b5fe8b2

Browse files
author
Joan He
committed
Merge remote-tracking branch 'trigger/MAGETWO-95773-invoice-refund-api-error' into BugFixPR
2 parents 9c853d2 + ad125d7 commit b5fe8b2

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

app/code/Magento/Sales/Model/Order/Creditmemo/Item/Validation/CreationQuantityValidator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CreationQuantityValidator implements ValidatorInterface
3030

3131
/**
3232
* ItemCreationQuantityValidator constructor.
33+
*
3334
* @param OrderItemRepositoryInterface $orderItemRepository
3435
* @param mixed $context
3536
*/
@@ -53,6 +54,10 @@ public function validate($entity)
5354
return [__('The creditmemo contains product item that is not part of the original order.')];
5455
}
5556

57+
if ($orderItem->isDummy()) {
58+
return [__('The creditmemo contains incorrect product items.')];
59+
}
60+
5661
if (!$this->isQtyAvailable($orderItem, $entity->getQty())) {
5762
return [__('The quantity to refund must not be greater than the unrefunded quantity.')];
5863
}
@@ -61,6 +66,8 @@ public function validate($entity)
6166
}
6267

6368
/**
69+
* Check the quantity to refund is greater than the unrefunded quantity
70+
*
6471
* @param Item $orderItem
6572
* @param int $qty
6673
* @return bool
@@ -71,6 +78,8 @@ private function isQtyAvailable(Item $orderItem, $qty)
7178
}
7279

7380
/**
81+
* Check to see if Item is part of the order
82+
*
7483
* @param OrderItemInterface $orderItem
7584
* @return bool
7685
*/

app/code/Magento/Sales/Model/Order/ItemRepository.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct(
9595
}
9696

9797
/**
98-
* load entity
98+
* Load entity
9999
*
100100
* @param int $id
101101
* @return OrderItemInterface
@@ -228,6 +228,14 @@ private function addParentItem(OrderItemInterface $orderItem)
228228
{
229229
if ($parentId = $orderItem->getParentItemId()) {
230230
$orderItem->setParentItem($this->get($parentId));
231+
} else {
232+
$orderCollection = $orderItem->getOrder()->getItemsCollection()->filterByParent($orderItem->getItemId());
233+
234+
foreach ($orderCollection->getItems() as $item) {
235+
if ($item->getParentItemId() === $orderItem->getItemId()) {
236+
$item->setParentItem($orderItem);
237+
}
238+
}
231239
}
232240
}
233241

app/code/Magento/Sales/Test/Unit/Model/Order/ItemRepositoryTest.php

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testGetEmptyEntity()
145145
$model->get($orderItemId);
146146
}
147147

148-
public function testGet()
148+
public function testGetAsParentWithChild()
149149
{
150150
$orderItemId = 1;
151151
$productType = 'configurable';
@@ -154,7 +154,55 @@ public function testGet()
154154

155155
$this->getProductOptionExtensionMock();
156156
$productOption = $this->getProductOptionMock();
157-
$orderItemMock = $this->getOrderMock($productType, $productOption);
157+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
158+
159+
$orderItemCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class);
160+
$orderItemCollectionMock->expects($this->once())
161+
->method('filterByParent')
162+
->with($orderItemId)
163+
->willReturnSelf();
164+
$orderItemCollectionMock->expects($this->once())
165+
->method('getItems')
166+
->willReturn([$orderItemMock]);
167+
168+
$orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
169+
$orderMock->expects($this->once())
170+
->method('getItemsCollection')
171+
->willReturn($orderItemCollectionMock);
172+
173+
$orderItemMock->expects($this->once())
174+
->method('load')
175+
->with($orderItemId)
176+
->willReturn($orderItemMock);
177+
$orderItemMock->expects($this->exactly(3))
178+
->method('getItemId')
179+
->willReturn($orderItemId);
180+
$orderItemMock->expects($this->once())
181+
->method('getOrder')
182+
->willReturn($orderMock);
183+
184+
$this->metadata->expects($this->once())
185+
->method('getNewInstance')
186+
->willReturn($orderItemMock);
187+
188+
$model = $this->getModel($orderItemMock, $productType);
189+
$this->assertSame($orderItemMock, $model->get($orderItemId));
190+
191+
// Assert already registered
192+
$this->assertSame($orderItemMock, $model->get($orderItemId));
193+
}
194+
195+
public function testGetAsChild()
196+
{
197+
$orderItemId = 1;
198+
$parentItemId = 66;
199+
$productType = 'configurable';
200+
201+
$this->productOptionData = ['option1' => 'value1'];
202+
203+
$this->getProductOptionExtensionMock();
204+
$productOption = $this->getProductOptionMock();
205+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
158206

159207
$orderItemMock->expects($this->once())
160208
->method('load')
@@ -163,12 +211,20 @@ public function testGet()
163211
$orderItemMock->expects($this->once())
164212
->method('getItemId')
165213
->willReturn($orderItemId);
214+
$orderItemMock->expects($this->exactly(3))
215+
->method('getParentItemId')
216+
->willReturn($parentItemId);
166217

167218
$this->metadata->expects($this->once())
168219
->method('getNewInstance')
169220
->willReturn($orderItemMock);
170221

222+
$parentItemMock = $this->createMock(\Magento\Sales\Model\Order\Item::class);
223+
171224
$model = $this->getModel($orderItemMock, $productType);
225+
$reflectedRegistryProperty = new \ReflectionProperty($model, 'registry');
226+
$reflectedRegistryProperty->setAccessible(true);
227+
$reflectedRegistryProperty->setValue($model, [$parentItemId => $parentItemMock]);
172228
$this->assertSame($orderItemMock, $model->get($orderItemId));
173229

174230
// Assert already registered
@@ -184,7 +240,7 @@ public function testGetList()
184240
->getMock();
185241
$this->getProductOptionExtensionMock();
186242
$productOption = $this->getProductOptionMock();
187-
$orderItemMock = $this->getOrderMock($productType, $productOption);
243+
$orderItemMock = $this->getOrderItemMock($productType, $productOption);
188244

189245
$searchResultMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Item\Collection::class)
190246
->disableOriginalConstructor()
@@ -206,26 +262,12 @@ public function testDeleteById()
206262
$orderItemId = 1;
207263
$productType = 'configurable';
208264

209-
$requestMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
210-
->disableOriginalConstructor()
211-
->getMock();
212-
213265
$orderItemMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Item::class)
214266
->disableOriginalConstructor()
215267
->getMock();
216268
$orderItemMock->expects($this->once())
217-
->method('load')
218-
->with($orderItemId)
219-
->willReturn($orderItemMock);
220-
$orderItemMock->expects($this->once())
221-
->method('getItemId')
269+
->method('getEntityId')
222270
->willReturn($orderItemId);
223-
$orderItemMock->expects($this->once())
224-
->method('getProductType')
225-
->willReturn($productType);
226-
$orderItemMock->expects($this->once())
227-
->method('getBuyRequest')
228-
->willReturn($requestMock);
229271

230272
$orderItemResourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\Db\AbstractDb::class)
231273
->disableOriginalConstructor()
@@ -235,15 +277,16 @@ public function testDeleteById()
235277
->with($orderItemMock)
236278
->willReturnSelf();
237279

238-
$this->metadata->expects($this->once())
239-
->method('getNewInstance')
240-
->willReturn($orderItemMock);
241280
$this->metadata->expects($this->exactly(1))
242281
->method('getMapper')
243282
->willReturn($orderItemResourceMock);
244283

245284
$model = $this->getModel($orderItemMock, $productType);
285+
$reflectedRegistryProperty = new \ReflectionProperty($model, 'registry');
286+
$reflectedRegistryProperty->setAccessible(true);
287+
$reflectedRegistryProperty->setValue($model, [$orderItemId => $orderItemMock]);
246288
$this->assertTrue($model->deleteById($orderItemId));
289+
$this->assertEmpty($reflectedRegistryProperty->getValue($model));
247290
}
248291

249292
/**
@@ -301,7 +344,7 @@ protected function getModel(
301344
* @param \PHPUnit_Framework_MockObject_MockObject $productOption
302345
* @return \PHPUnit_Framework_MockObject_MockObject
303346
*/
304-
protected function getOrderMock($productType, $productOption)
347+
protected function getOrderItemMock($productType, $productOption)
305348
{
306349
$requestMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
307350
->disableOriginalConstructor()

0 commit comments

Comments
 (0)