|
| 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