Skip to content

Commit 21fb25a

Browse files
Yauhen_Lyskavetsoksana-kr
authored andcommitted
MAGETWO-91676: Model related data missing in order rest api
- Fix added - Fix unit tests added
1 parent 1fd2493 commit 21fb25a

File tree

2 files changed

+137
-10
lines changed

2 files changed

+137
-10
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
2424
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
2525
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
26+
use Magento\Sales\Api\OrderItemRepositoryInterface;
27+
use Magento\Framework\Api\SearchCriteriaBuilder;
2628

2729
/**
2830
* Order model
@@ -286,6 +288,16 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
286288
*/
287289
private $productOption;
288290

291+
/**
292+
* @var OrderItemRepositoryInterface
293+
*/
294+
private $itemRepository;
295+
296+
/**
297+
* @var SearchCriteriaBuilder
298+
*/
299+
private $searchCriteriaBuilder;
300+
289301
/**
290302
* @param \Magento\Framework\Model\Context $context
291303
* @param \Magento\Framework\Registry $registry
@@ -316,6 +328,8 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
316328
* @param array $data
317329
* @param ResolverInterface $localeResolver
318330
* @param ProductOption|null $productOption
331+
* @param OrderItemRepositoryInterface $itemRepository
332+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
319333
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
320334
*/
321335
public function __construct(
@@ -347,7 +361,9 @@ public function __construct(
347361
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
348362
array $data = [],
349363
ResolverInterface $localeResolver = null,
350-
ProductOption $productOption = null
364+
ProductOption $productOption = null,
365+
OrderItemRepositoryInterface $itemRepository = null,
366+
SearchCriteriaBuilder $searchCriteriaBuilder = null
351367
) {
352368
$this->_storeManager = $storeManager;
353369
$this->_orderConfig = $orderConfig;
@@ -371,6 +387,10 @@ public function __construct(
371387
$this->priceCurrency = $priceCurrency;
372388
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
373389
$this->productOption = $productOption ?: ObjectManager::getInstance()->get(ProductOption::class);
390+
$this->itemRepository = $itemRepository ?: ObjectManager::getInstance()
391+
->get(OrderItemRepositoryInterface::class);
392+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
393+
->get(SearchCriteriaBuilder::class);
374394

375395
parent::__construct(
376396
$context,
@@ -668,7 +688,7 @@ private function canCreditmemoForZeroTotalRefunded($totalRefunded)
668688

669689
return true;
670690
}
671-
691+
672692
/**
673693
* Retrieve credit memo for zero total availability.
674694
*
@@ -2076,9 +2096,12 @@ public function getIncrementId()
20762096
public function getItems()
20772097
{
20782098
if ($this->getData(OrderInterface::ITEMS) == null) {
2099+
$this->searchCriteriaBuilder->addFilter(OrderItemInterface::ORDER_ID, $this->getId());
2100+
2101+
$searchCriteria = $this->searchCriteriaBuilder->create();
20792102
$this->setData(
20802103
OrderInterface::ITEMS,
2081-
$this->getItemsCollection()->getItems()
2104+
$this->itemRepository->getList($searchCriteria)->getItems()
20822105
);
20832106
}
20842107
return $this->getData(OrderInterface::ITEMS);
@@ -2919,7 +2942,7 @@ public function getDiscountTaxCompensationRefunded()
29192942
}
29202943

29212944
/**
2922-
* Return hold_before_state
2945+
* Returns hold_before_state
29232946
*
29242947
* @return string|null
29252948
*/

app/code/Magento/Sales/Test/Unit/Model/OrderTest.php

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use Magento\Sales\Api\Data\OrderInterface;
1313
use Magento\Sales\Model\Order;
1414
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
15+
use Magento\Sales\Api\OrderItemRepositoryInterface;
16+
use Magento\Framework\Api\SearchCriteriaBuilder;
17+
use Magento\Framework\Api\SearchCriteria;
18+
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
1519

1620
/**
1721
* Test class for \Magento\Sales\Model\Order
@@ -87,6 +91,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
8791
*/
8892
private $timezone;
8993

94+
/**
95+
* @var OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
96+
*/
97+
private $itemRepository;
98+
99+
/**
100+
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
101+
*/
102+
private $searchCriteriaBuilder;
103+
90104
protected function setUp()
91105
{
92106
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -144,6 +158,15 @@ protected function setUp()
144158
$this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
145159
$context = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
146160
$context->expects($this->any())->method('getEventDispatcher')->willReturn($this->eventManager);
161+
162+
$this->itemRepository = $this->getMockBuilder(OrderItemRepositoryInterface::class)
163+
->setMethods(['getList'])
164+
->disableOriginalConstructor()->getMockForAbstractClass();
165+
166+
$this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
167+
->setMethods(['addFilter', 'create'])
168+
->disableOriginalConstructor()->getMockForAbstractClass();
169+
147170
$this->order = $helper->getObject(
148171
\Magento\Sales\Model\Order::class,
149172
[
@@ -157,37 +180,80 @@ protected function setUp()
157180
'productListFactory' => $this->productCollectionFactoryMock,
158181
'localeResolver' => $this->localeResolver,
159182
'timezone' => $this->timezone,
183+
'itemRepository' => $this->itemRepository,
184+
'searchCriteriaBuilder' => $this->searchCriteriaBuilder
160185
]
161186
);
162187
}
163188

164-
public function testGetItemById()
189+
/**
190+
* Test testGetItems method.
191+
*/
192+
public function testGetItems()
165193
{
166-
$realOrderItemId = 1;
167-
$fakeOrderItemId = 2;
194+
$orderItems = [$this->item];
195+
196+
$this->searchCriteriaBuilder->expects($this->once())->method('addFilter')->willReturnSelf();
197+
198+
$searchCriteria = $this->getMockBuilder(SearchCriteria::class)
199+
->disableOriginalConstructor()->getMockForAbstractClass();
200+
$this->searchCriteriaBuilder->expects($this->once())->method('create')->willReturn($searchCriteria);
168201

169-
$orderItem = $this->createMock(\Magento\Sales\Model\Order\Item::class);
202+
$itemsCollection = $this->getMockBuilder(OrderItemSearchResultInterface::class)
203+
->setMethods(['getItems'])
204+
->disableOriginalConstructor()->getMockForAbstractClass();
205+
$itemsCollection->expects($this->once())->method('getItems')->willReturn($orderItems);
206+
$this->itemRepository->expects($this->once())->method('getList')->willReturn($itemsCollection);
170207

208+
$this->assertEquals($orderItems, $this->order->getItems());
209+
}
210+
211+
/**
212+
* Prepare order item mock.
213+
*
214+
* @param int $orderId
215+
* @return void
216+
*/
217+
private function prepareOrderItem(int $orderId = 0)
218+
{
171219
$this->order->setData(
172220
\Magento\Sales\Api\Data\OrderInterface::ITEMS,
173221
[
174-
$realOrderItemId => $orderItem
222+
$orderId => $this->item
175223
]
176224
);
225+
}
177226

178-
$this->assertEquals($orderItem, $this->order->getItemById($realOrderItemId));
227+
/**
228+
* Test GetItemById method.
229+
*
230+
* @return void
231+
*/
232+
public function testGetItemById()
233+
{
234+
$realOrderItemId = 1;
235+
$fakeOrderItemId = 2;
236+
237+
$this->prepareOrderItem($realOrderItemId);
238+
239+
$this->assertEquals($this->item, $this->order->getItemById($realOrderItemId));
179240
$this->assertEquals(null, $this->order->getItemById($fakeOrderItemId));
180241
}
181242

182243
/**
244+
* Test GetItemByQuoteItemId method.
245+
*
183246
* @param int|null $gettingQuoteItemId
184247
* @param int|null $quoteItemId
185248
* @param string|null $result
186249
*
187250
* @dataProvider dataProviderGetItemByQuoteItemId
251+
* @return void
188252
*/
189253
public function testGetItemByQuoteItemId($gettingQuoteItemId, $quoteItemId, $result)
190254
{
255+
$this->prepareOrderItem();
256+
191257
$this->item->expects($this->any())
192258
->method('getQuoteItemId')
193259
->willReturn($gettingQuoteItemId);
@@ -212,14 +278,19 @@ public function dataProviderGetItemByQuoteItemId()
212278
}
213279

214280
/**
281+
* Test getAllVisibleItems method.
282+
*
215283
* @param bool $isDeleted
216284
* @param int|null $parentItemId
217285
* @param array $result
218286
*
219287
* @dataProvider dataProviderGetAllVisibleItems
288+
* @return void
220289
*/
221290
public function testGetAllVisibleItems($isDeleted, $parentItemId, array $result)
222291
{
292+
$this->prepareOrderItem();
293+
223294
$this->item->expects($this->once())
224295
->method('isDeleted')
225296
->willReturn($isDeleted);
@@ -263,8 +334,15 @@ public function testCanCancelIsPaymentReview()
263334
$this->assertFalse($this->order->canCancel());
264335
}
265336

337+
/**
338+
* Test CanInvoice method.
339+
*
340+
* @return void
341+
*/
266342
public function testCanInvoice()
267343
{
344+
$this->prepareOrderItem();
345+
268346
$this->item->expects($this->any())
269347
->method('getQtyToInvoice')
270348
->willReturn(42);
@@ -304,8 +382,15 @@ public function testCanNotInvoiceWhenActionInvoiceFlagIsFalse()
304382
$this->assertFalse($this->order->canInvoice());
305383
}
306384

385+
/**
386+
* Test CanNotInvoice method when invoice is locked.
387+
*
388+
* @return void
389+
*/
307390
public function testCanNotInvoiceWhenLockedInvoice()
308391
{
392+
$this->prepareOrderItem();
393+
309394
$this->item->expects($this->any())
310395
->method('getQtyToInvoice')
311396
->willReturn(42);
@@ -315,8 +400,15 @@ public function testCanNotInvoiceWhenLockedInvoice()
315400
$this->assertFalse($this->order->canInvoice());
316401
}
317402

403+
/**
404+
* Test CanNotInvoice method when didn't have qty to invoice.
405+
*
406+
* @return void
407+
*/
318408
public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice()
319409
{
410+
$this->prepareOrderItem();
411+
320412
$this->item->expects($this->any())
321413
->method('getQtyToInvoice')
322414
->willReturn(0);
@@ -601,8 +693,15 @@ public function testCanCancelCanReviewPayment()
601693
$this->assertFalse($this->order->canCancel());
602694
}
603695

696+
/**
697+
* Test CanCancelAllInvoiced method.
698+
*
699+
* @return void
700+
*/
604701
public function testCanCancelAllInvoiced()
605702
{
703+
$this->prepareOrderItem();
704+
606705
$paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
607706
->disableOriginalConstructor()
608707
->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])
@@ -662,11 +761,16 @@ public function testCanCancelState()
662761
}
663762

664763
/**
764+
* Test CanCancelActionFlag method.
765+
*
665766
* @param bool $cancelActionFlag
666767
* @dataProvider dataProviderActionFlag
768+
* @return void
667769
*/
668770
public function testCanCancelActionFlag($cancelActionFlag)
669771
{
772+
$this->prepareOrderItem();
773+
670774
$paymentMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Payment::class)
671775
->disableOriginalConstructor()
672776
->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])

0 commit comments

Comments
 (0)