|
| 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\GraphQl\DownloadableProduct; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Framework\ObjectManager\ObjectManager; |
| 13 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 14 | +use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku; |
| 15 | +use Magento\Quote\Model\Quote; |
| 16 | +use Magento\Quote\Model\Quote\Item; |
| 17 | +use Magento\Quote\Model\QuoteFactory; |
| 18 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test cases for adding downloadable product to cart. |
| 24 | + */ |
| 25 | +class UpdateDownloadableCartItemsTest extends GraphQlAbstract |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 29 | + */ |
| 30 | + private $getMaskedQuoteIdByReservedOrderId; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ObjectManager |
| 34 | + */ |
| 35 | + private $objectManager; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var GetQuoteItemIdByReservedQuoteIdAndSku |
| 39 | + */ |
| 40 | + private $getQuoteItemIdByReservedQuoteIdAndSku; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var QuoteFactory |
| 44 | + */ |
| 45 | + private $quoteFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ProductRepositoryInterface |
| 49 | + */ |
| 50 | + private $productRepository; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var QuoteResource |
| 54 | + */ |
| 55 | + private $quoteResource; |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc |
| 59 | + */ |
| 60 | + protected function setUp() |
| 61 | + { |
| 62 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 63 | + $this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 64 | + $this->quoteFactory = $this->objectManager->get(QuoteFactory::class); |
| 65 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 66 | + $this->quoteResource = $this->objectManager->get(QuoteResource::class); |
| 67 | + $this->getQuoteItemIdByReservedQuoteIdAndSku = $this->objectManager->get( |
| 68 | + GetQuoteItemIdByReservedQuoteIdAndSku::class |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Update a downloadable product into shopping cart when "Links can be purchased separately" is enabled |
| 74 | + * |
| 75 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote_with_downloadable_product.php |
| 76 | + */ |
| 77 | + public function testUpdateDownloadableCartItemQuantity() |
| 78 | + { |
| 79 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1'); |
| 80 | + $sku = 'downloadable-product'; |
| 81 | + $qty = 1; |
| 82 | + $finalQty = $qty + 1; |
| 83 | + $links = $this->getProductsLinks($sku); |
| 84 | + $linkId = key($links); |
| 85 | + |
| 86 | + $query = <<<MUTATION |
| 87 | +mutation { |
| 88 | + addDownloadableProductsToCart( |
| 89 | + input: { |
| 90 | + cart_id: "{$maskedQuoteId}", |
| 91 | + cart_items: [ |
| 92 | + { |
| 93 | + data: { |
| 94 | + quantity: {$qty}, |
| 95 | + sku: "{$sku}" |
| 96 | + }, |
| 97 | + downloadable_product_links: [ |
| 98 | + { |
| 99 | + link_id: {$linkId} |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + ] |
| 104 | + } |
| 105 | + ) { |
| 106 | + cart { |
| 107 | + items { |
| 108 | + product { |
| 109 | + sku |
| 110 | + } |
| 111 | + quantity |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | +MUTATION; |
| 117 | + $response = $this->graphQlMutation($query); |
| 118 | + |
| 119 | + self::assertArrayHasKey('items', $response['addDownloadableProductsToCart']['cart']); |
| 120 | + self::assertCount(1, $response['addDownloadableProductsToCart']['cart']['items']); |
| 121 | + self::assertEquals($finalQty, $response['addDownloadableProductsToCart']['cart']['items'][0]['quantity']); |
| 122 | + self::assertEquals($sku, $response['addDownloadableProductsToCart']['cart']['items'][0]['product']['sku']); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Update a downloadable product into shopping cart when "Links can be purchased separately" is enabled |
| 127 | + * |
| 128 | + * @magentoApiDataFixture Magento/Checkout/_files/active_quote_with_downloadable_product.php |
| 129 | + */ |
| 130 | + public function testRemoveCartItemIfQuantityIsZero() |
| 131 | + { |
| 132 | + $reservedOrderId = "test_order_1"; |
| 133 | + $sku = "downloadable-product"; |
| 134 | + |
| 135 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId); |
| 136 | + |
| 137 | + /** @var Quote $quote */ |
| 138 | + $quote = $this->quoteFactory->create(); |
| 139 | + $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); |
| 140 | + $qty = 0; |
| 141 | + |
| 142 | + $itemId = 0; |
| 143 | + /** @var Item $item */ |
| 144 | + foreach ($quote->getAllItems() as $item) { |
| 145 | + if ($item->getSku() == $sku) { |
| 146 | + $itemId = $item->getId(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + $query = <<<MUTATION |
| 151 | +mutation { |
| 152 | + updateCartItems(input: { |
| 153 | + cart_id: "{$maskedQuoteId}" |
| 154 | + cart_items:[ |
| 155 | + { |
| 156 | + cart_item_id: {$itemId} |
| 157 | + quantity: {$qty} |
| 158 | + } |
| 159 | + ] |
| 160 | + }) { |
| 161 | + cart { |
| 162 | + items { |
| 163 | + id |
| 164 | + quantity |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +MUTATION; |
| 170 | + $response = $this->graphQlMutation($query); |
| 171 | + |
| 172 | + self::assertArrayHasKey('updateCartItems', $response); |
| 173 | + self::assertArrayHasKey('cart', $response['updateCartItems']); |
| 174 | + |
| 175 | + $responseCart = $response['updateCartItems']['cart']; |
| 176 | + self::assertCount(0, $responseCart['items']); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Function returns array of all product's links |
| 181 | + * |
| 182 | + * @param string $sku |
| 183 | + * @return array |
| 184 | + */ |
| 185 | + private function getProductsLinks(string $sku) : array |
| 186 | + { |
| 187 | + $result = []; |
| 188 | + $productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 189 | + |
| 190 | + $product = $productRepository->get($sku, false, null, true); |
| 191 | + |
| 192 | + foreach ($product->getDownloadableLinks() as $linkObject) { |
| 193 | + $result[$linkObject->getLinkId()] = [ |
| 194 | + 'title' => $linkObject->getTitle(), |
| 195 | + 'link_type' => null, //deprecated field |
| 196 | + 'price' => $linkObject->getPrice(), |
| 197 | + ]; |
| 198 | + } |
| 199 | + |
| 200 | + return $result; |
| 201 | + } |
| 202 | +} |
0 commit comments