Skip to content

Commit 11b3612

Browse files
committed
Merge branch 'ACP2E-2215' of https://github.com/magento-l3/magento2ce into PR-L3-08112023
2 parents d83550b + cc859ba commit 11b3612

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

app/code/Magento/Downloadable/Observer/SaveDownloadableOrderItemObserver.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
9898
if ($purchasedLink->getId()) {
9999
return $this;
100100
}
101-
$storeId = $orderItem->getOrder()->getStoreId();
102-
$orderStatusToEnableItem = $this->_scopeConfig->getValue(
103-
\Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
104-
ScopeInterface::SCOPE_STORE,
105-
$storeId
106-
);
101+
$storeId = $orderItem->getOrder()->getStoreId() !== null ? (int)$orderItem->getOrder()->getStoreId() : null;
102+
$orderItemStatusToEnableDownload = $this->getEnableDownloadStatus($storeId);
107103
if (!$product) {
108104
$product = $this->_createProductModel()->setStoreId(
109105
$storeId
@@ -136,8 +132,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
136132
);
137133
$linkPurchased->setLinkSectionTitle($linkSectionTitle)->save();
138134
$linkStatus = \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING;
139-
if ($orderStatusToEnableItem == \Magento\Sales\Model\Order\Item::STATUS_PENDING
135+
if ($orderItemStatusToEnableDownload === \Magento\Sales\Model\Order\Item::STATUS_PENDING
140136
|| $orderItem->getOrder()->getState() == \Magento\Sales\Model\Order::STATE_COMPLETE
137+
|| $orderItem->getStatusId() === $orderItemStatusToEnableDownload
141138
) {
142139
$linkStatus = \Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_AVAILABLE;
143140
}
@@ -211,6 +208,21 @@ protected function _createPurchasedItemModel()
211208
return $this->_itemFactory->create();
212209
}
213210

211+
/**
212+
* Returns order item status to enable download.
213+
*
214+
* @param int|null $storeId
215+
* @return int
216+
*/
217+
private function getEnableDownloadStatus(?int $storeId): int
218+
{
219+
return (int)$this->_scopeConfig->getValue(
220+
\Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
221+
ScopeInterface::SCOPE_STORE,
222+
$storeId
223+
);
224+
}
225+
214226
/**
215227
* Create items collection.
216228
*

app/code/Magento/Downloadable/Test/Unit/Observer/SaveDownloadableOrderItemObserverTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ public function testSaveDownloadableOrderItem()
180180
->method('getRealProductType')
181181
->willReturn(DownloadableProductType::TYPE_DOWNLOADABLE);
182182

183-
$this->orderMock->expects($this->once())
184-
->method('getStoreId')
183+
$this->orderMock->method('getStoreId')
185184
->willReturn(10500);
186185

187186
$product = $this->getMockBuilder(Product::class)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Downloadable\Model\Observer;
9+
10+
use Magento\Downloadable\Model\Link\Purchased\Item;
11+
use Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\Collection;
12+
use Magento\Downloadable\Model\ResourceModel\Link\Purchased\Item\CollectionFactory;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Sales\Model\Order\Item as OrderItem;
16+
use Magento\Sales\Model\Order\ItemRepository;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\TestFramework\Downloadable\Model\RemoveLinkPurchasedByOrderIncrementId;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
21+
/**
22+
* Test for case, when customer is able to download downloadable product.
23+
*/
24+
class SaveDownloadableOrderItemObserverTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* @var \Magento\Framework\ObjectManagerInterface
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* Initialization of dependencies
33+
*/
34+
protected function setUp(): void
35+
{
36+
$this->objectManager = Bootstrap::getObjectManager();
37+
}
38+
39+
/**
40+
* Asserting, that links status is 'Available' when order is in processing state,
41+
* and 'Order Item Status to Enable Downloads' is 'Invoiced'.
42+
*
43+
* @magentoDataFixture Magento/Downloadable/_files/order_with_customer_and_downloadable_product.php
44+
* @magentoDataFixture Magento/Downloadable/_files/customer_order_with_invoice_downloadable_product.php
45+
*/
46+
public function testOrderStateIsProcessingAndInvoicedOrderItemLinkIsDownloadable()
47+
{
48+
$orderIncremetId = '100000001';
49+
/** @var Order $order */
50+
$order = $this->objectManager->create(Order::class);
51+
$order->loadByIncrementId($orderIncremetId);
52+
/** @var OrderItem $orderItem */
53+
$orderItem = current($order->getAllItems());
54+
$config = $this->objectManager->get(ScopeConfigInterface::class);
55+
$orderItemStatusToEnableDownload = $config->getValue(
56+
\Magento\Downloadable\Model\Link\Purchased\Item::XML_PATH_ORDER_ITEM_STATUS,
57+
ScopeInterface::SCOPE_STORE,
58+
$orderItem->getStoreId()
59+
);
60+
61+
/** Remove downloadable links from order item to create them from scratch */
62+
$removeLinkPurchasedByOrderIncrementId = $this->objectManager->get(
63+
RemoveLinkPurchasedByOrderIncrementId::class
64+
);
65+
$removeLinkPurchasedByOrderIncrementId->execute($orderIncremetId);
66+
67+
$this->assertEquals(Order::STATE_PROCESSING, $order->getState());
68+
$this->assertEquals(OrderItem::STATUS_INVOICED, $orderItem->getStatusId());
69+
$this->assertEquals(OrderItem::STATUS_INVOICED, $orderItemStatusToEnableDownload);
70+
71+
/** Save order item to trigger observers */
72+
$orderItemRepository = $this->objectManager->get(ItemRepository::class);
73+
$orderItemRepository->save($orderItem);
74+
75+
$this->assertOrderItemLinkStatus((int)$orderItem->getId(), Item::LINK_STATUS_AVAILABLE);
76+
}
77+
78+
/**
79+
* Assert that order item link status is expected.
80+
*
81+
* @param int $orderItemId
82+
* @param string $linkStatus
83+
* @return void
84+
*/
85+
public function assertOrderItemLinkStatus(int $orderItemId, string $linkStatus): void
86+
{
87+
/** @var Collection $linkCollection */
88+
$linkCollection = $this->objectManager->create(CollectionFactory::class)->create();
89+
$linkCollection->addFieldToFilter('order_item_id', $orderItemId);
90+
91+
/** Assert there are items in linkCollection to avoid false-positive test result. */
92+
$this->assertGreaterThan(0, $linkCollection->count());
93+
94+
/** @var Item $linkItem */
95+
foreach ($linkCollection->getItems() as $linkItem) {
96+
$this->assertEquals(
97+
$linkStatus,
98+
$linkItem->getStatus()
99+
);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)