@@ -189,7 +189,7 @@ public function testSetLinkStatusPending($orderState, array $orderStateMapping)
189189 ]
190190 );
191191
192- $ this ->itemsFactory ->expects ($ this ->once ())
192+ $ this ->itemsFactory ->expects ($ this ->any ())
193193 ->method ('create ' )
194194 ->willReturn (
195195 $ this ->createLinkItemCollection (
@@ -243,7 +243,7 @@ public function testSetLinkStatusClosed()
243243 ]
244244 );
245245
246- $ this ->itemsFactory ->expects ($ this ->once ())
246+ $ this ->itemsFactory ->expects ($ this ->any ())
247247 ->method ('create ' )
248248 ->willReturn (
249249 $ this ->createLinkItemCollection (
@@ -308,7 +308,7 @@ public function testSetLinkStatusInvoiced()
308308 ]
309309 );
310310
311- $ this ->itemsFactory ->expects ($ this ->once ())
311+ $ this ->itemsFactory ->expects ($ this ->any ())
312312 ->method ('create ' )
313313 ->willReturn (
314314 $ this ->createLinkItemCollection (
@@ -344,6 +344,137 @@ public function testSetLinkStatusEmptyOrder()
344344 $ this ->assertInstanceOf (SetLinkStatusObserver::class, $ result );
345345 }
346346
347+ public function testSetLinkStatusExpired ()
348+ {
349+ $ this ->scopeConfig ->expects ($ this ->once ())
350+ ->method ('getValue ' )
351+ ->with (
352+ \Magento \Downloadable \Model \Link \Purchased \Item::XML_PATH_ORDER_ITEM_STATUS ,
353+ ScopeInterface::SCOPE_STORE ,
354+ 1
355+ )
356+ ->willReturn (Item::STATUS_PENDING );
357+
358+ $ this ->observerMock ->expects ($ this ->once ())
359+ ->method ('getEvent ' )
360+ ->willReturn ($ this ->eventMock );
361+
362+ $ this ->eventMock ->expects ($ this ->once ())
363+ ->method ('getOrder ' )
364+ ->willReturn ($ this ->orderMock );
365+
366+ $ this ->orderMock ->expects ($ this ->once ())
367+ ->method ('getId ' )
368+ ->willReturn (1 );
369+
370+ $ this ->orderMock ->expects ($ this ->once ())
371+ ->method ('getStoreId ' )
372+ ->willReturn (1 );
373+
374+ $ this ->orderMock ->expects ($ this ->atLeastOnce ())
375+ ->method ('getState ' )
376+ ->willReturn (Order::STATE_PROCESSING );
377+
378+ $ this ->orderMock ->expects ($ this ->any ())
379+ ->method ('getAllItems ' )
380+ ->willReturn (
381+ [
382+ $ this ->createRefundOrderItem (2 , 2 , 2 ),
383+ $ this ->createRefundOrderItem (3 , 2 , 1 ),
384+ $ this ->createRefundOrderItem (4 , 3 , 3 ),
385+ ]
386+ );
387+
388+ $ this ->itemsFactory ->expects ($ this ->any ())
389+ ->method ('create ' )
390+ ->willReturn (
391+ $ this ->createLinkItemToExpireCollection (
392+ [2 , 4 ],
393+ [
394+ $ this ->createLinkItem (
395+ 'available ' ,
396+ 2 ,
397+ true ,
398+ \Magento \Downloadable \Model \Link \Purchased \Item::LINK_STATUS_EXPIRED
399+ ),
400+ $ this ->createLinkItem (
401+ 'pending_payment ' ,
402+ 4 ,
403+ true ,
404+ \Magento \Downloadable \Model \Link \Purchased \Item::LINK_STATUS_EXPIRED
405+ ),
406+ ]
407+ )
408+ );
409+
410+ $ result = $ this ->setLinkStatusObserver ->execute ($ this ->observerMock );
411+ $ this ->assertInstanceOf (SetLinkStatusObserver::class, $ result );
412+ }
413+
414+ /**
415+ * @param $id
416+ * @param int $qtyOrdered
417+ * @param int $qtyRefunded
418+ * @param string $productType
419+ * @param string $realProductType
420+ * @return \Magento\Sales\Model\Order\Item|MockObject
421+ */
422+ private function createRefundOrderItem (
423+ $ id ,
424+ $ qtyOrdered ,
425+ $ qtyRefunded ,
426+ $ productType = DownloadableProductType::TYPE_DOWNLOADABLE ,
427+ $ realProductType = DownloadableProductType::TYPE_DOWNLOADABLE
428+ ) {
429+ $ item = $ this ->getMockBuilder (Item::class)
430+ ->disableOriginalConstructor ()
431+ ->setMethods ([
432+ 'getId ' ,
433+ 'getQtyOrdered ' ,
434+ 'getQtyRefunded ' ,
435+ 'getProductType ' ,
436+ 'getRealProductType '
437+ ])->getMock ();
438+ $ item ->expects ($ this ->any ())
439+ ->method ('getId ' )
440+ ->willReturn ($ id );
441+ $ item ->expects ($ this ->any ())
442+ ->method ('getQtyOrdered ' )
443+ ->willReturn ($ qtyOrdered );
444+ $ item ->expects ($ this ->any ())
445+ ->method ('getQtyRefunded ' )
446+ ->willReturn ($ qtyRefunded );
447+ $ item ->expects ($ this ->any ())
448+ ->method ('getProductType ' )
449+ ->willReturn ($ productType );
450+ $ item ->expects ($ this ->any ())
451+ ->method ('getRealProductType ' )
452+ ->willReturn ($ realProductType );
453+
454+ return $ item ;
455+ }
456+
457+ /**
458+ * @param array $expectedOrderItemIds
459+ * @param array $items
460+ * @return LinkItemCollection|MockObject
461+ */
462+ private function createLinkItemToExpireCollection (array $ expectedOrderItemIds , array $ items )
463+ {
464+ $ linkItemCollection = $ this ->getMockBuilder (
465+ \Magento \Downloadable \Model \ResourceModel \Link \Purchased \Item \Collection::class
466+ )
467+ ->disableOriginalConstructor ()
468+ ->setMethods (['addFieldToFilter ' ])
469+ ->getMock ();
470+ $ linkItemCollection ->expects ($ this ->any ())
471+ ->method ('addFieldToFilter ' )
472+ ->with ('order_item_id ' , ['in ' => $ expectedOrderItemIds ])
473+ ->willReturn ($ items );
474+
475+ return $ linkItemCollection ;
476+ }
477+
347478 /**
348479 * @param $id
349480 * @param int $statusId
@@ -359,7 +490,7 @@ private function createOrderItem(
359490 ) {
360491 $ item = $ this ->getMockBuilder (Item::class)
361492 ->disableOriginalConstructor ()
362- ->setMethods (['getId ' , 'getProductType ' , 'getRealProductType ' , 'getStatusId ' ])
493+ ->setMethods (['getId ' , 'getProductType ' , 'getRealProductType ' , 'getStatusId ' , ' getQtyOrdered ' ])
363494 ->getMock ();
364495 $ item ->expects ($ this ->any ())
365496 ->method ('getId ' )
@@ -373,6 +504,9 @@ private function createOrderItem(
373504 $ item ->expects ($ this ->any ())
374505 ->method ('getStatusId ' )
375506 ->willReturn ($ statusId );
507+ $ item ->expects ($ this ->any ())
508+ ->method ('getQtyOrdered ' )
509+ ->willReturn (1 );
376510
377511 return $ item ;
378512 }
@@ -390,7 +524,7 @@ private function createLinkItemCollection(array $expectedOrderItemIds, array $it
390524 ->disableOriginalConstructor ()
391525 ->setMethods (['addFieldToFilter ' ])
392526 ->getMock ();
393- $ linkItemCollection ->expects ($ this ->once ())
527+ $ linkItemCollection ->expects ($ this ->any ())
394528 ->method ('addFieldToFilter ' )
395529 ->with ('order_item_id ' , ['in ' => $ expectedOrderItemIds ])
396530 ->willReturn ($ items );
@@ -415,11 +549,11 @@ private function createLinkItem($status, $orderItemId, $isSaved = false, $expect
415549 ->method ('getStatus ' )
416550 ->willReturn ($ status );
417551 if ($ isSaved ) {
418- $ linkItem ->expects ($ this ->once ())
552+ $ linkItem ->expects ($ this ->any ())
419553 ->method ('setStatus ' )
420554 ->with ($ expectedStatus )
421555 ->willReturnSelf ();
422- $ linkItem ->expects ($ this ->once ())
556+ $ linkItem ->expects ($ this ->any ())
423557 ->method ('save ' )
424558 ->willReturnSelf ();
425559 }
0 commit comments