From 034af404deaa448c6ae660166912b0c40dfc7b12 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Thu, 23 Apr 2020 18:49:13 +0300 Subject: [PATCH 01/12] PR 28072 Add coverage for cart items --- .../Model/Resolver/Cart/Item/GiftMessage.php | 79 +++++++++++++++++++ .../GiftMessageGraphQl/etc/schema.graphqls | 16 ++++ .../GiftMessage/Cart/Item/GiftMessageTest.php | 63 +++++++++++++++ .../_files/guest/quote_with_item_message.php | 63 +++++++++++++++ .../quote_with_item_message_rollback.php | 32 ++++++++ 5 files changed, 253 insertions(+) create mode 100644 app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php create mode 100644 dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php create mode 100644 dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message_rollback.php diff --git a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php new file mode 100644 index 0000000000000..34d3d3969835a --- /dev/null +++ b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php @@ -0,0 +1,79 @@ +itemRepository = $itemRepository; + } + + /** + * Return information about Gift message for item cart + * + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array|Value|mixed + * @throws GraphQlInputException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + if (!isset($value['model'])) { + throw new GraphQlInputException(__('"model" value should be specified')); + } + + $quoteItem = $value['model']; + + try { + $giftItemMessage = $this->itemRepository->get($quoteItem->getQuoteId(), $quoteItem->getItemId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__('Can\'t load cart item')); + } + + return [ + 'to' => isset($giftItemMessage) ? $giftItemMessage->getRecipient() : '', + 'from' => isset($giftItemMessage) ? $giftItemMessage->getSender() : '', + 'message'=> isset($giftItemMessage) ? $giftItemMessage->getMessage() : '' + ]; + } +} diff --git a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls index f0c0353cc6305..fc414b027e76a 100644 --- a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls +++ b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls @@ -5,6 +5,22 @@ type Cart { gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart") } +type SimpleCartItem { + gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") +} + +type ConfigurableCartItem { + gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") +} + +type BundleCartItem { + gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") +} + +type GiftCardCartItem { + gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") +} + type GiftMessage { to: String! @doc(description: "Recepient name") from: String! @doc(description: "Sender name") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php new file mode 100644 index 0000000000000..7e5c0e15bd456 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php @@ -0,0 +1,63 @@ +getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); + } + + /** + * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php + * @throws NoSuchEntityException + * @throws Exception + */ + public function testGiftMessageCartForItem() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); + $query = <<graphQlQuery($query); + foreach ($response['cart']['items'] as $item) { + self::assertArrayHasKey('gift_message', $item); + self::assertArrayHasKey('to', $item['gift_message']); + self::assertArrayHasKey('from', $item['gift_message']); + self::assertArrayHasKey('message', $item['gift_message']); + } + } +} diff --git a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php new file mode 100644 index 0000000000000..36217ddbfac68 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php @@ -0,0 +1,63 @@ +create(QuoteResource::class); + +/** @var Quote $quoteModel */ +$quoteModel = $objectManager->create(Quote::class); +$quoteModel->setData(['store_id' => 1, 'is_active' => 1, 'is_multi_shipping' => 0]); +$quote->save($quoteModel); + +$product = $objectManager->create(Product::class); +$quoteProduct = $product->load($product->getIdBySku('simple')); + +$quoteModel->setReservedOrderId('test_guest_order_with_gift_message') + ->addProduct($product->load($product->getIdBySku('simple')), 1); +$quoteModel->collectTotals(); +$quote->save($quoteModel); + +/** @var MessageResource $message */ +$message = $objectManager->create(MessageResource::class); + +/** @var Message $message */ +$messageModel = $objectManager->create(Message::class); + +$messageModel->setSender('John Doe'); +$messageModel->setRecipient('Jane Roe'); +$messageModel->setMessage('Gift Message Text'); +$message->save($messageModel); + +$quoteModel->getItemByProduct($quoteProduct)->setGiftMessageId($messageModel->getId()); +$quote->save($quoteModel); + +/** @var QuoteIdMaskResource $quoteIdMask */ +$quoteIdMask = Bootstrap::getObjectManager() + ->create(QuoteIdMaskFactory::class) + ->create(); + +/** @var QuoteIdMask $quoteIdMaskModel */ +$quoteIdMaskModel = $objectManager->create(QuoteIdMask::class); + +$quoteIdMaskModel->setQuoteId($quoteModel->getId()); +$quoteIdMaskModel->setDataChanges(true); +$quoteIdMask->save($quoteIdMaskModel); diff --git a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message_rollback.php b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message_rollback.php new file mode 100644 index 0000000000000..9c215cb432b45 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message_rollback.php @@ -0,0 +1,32 @@ +get(Registry::class); +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); +$objectManager = Bootstrap::getObjectManager(); +$quote = $objectManager->create(Quote::class); +$quote->load('test_guest_order_with_gift_message', 'reserved_order_id'); +$message = $objectManager->create(Message::class); +$product = $objectManager->create(Product::class); +foreach ($quote->getAllItems() as $item) { + $message->load($item->getGiftMessageId()); + $message->delete(); + $sku = $item->getSku(); + $product->load($product->getIdBySku($sku)); + if ($product->getId()) { + $product->delete(); + } +} +$quote->delete(); +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From 7e6e0239c9895982b4bb1577412b0018beef1361 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 20 May 2020 16:10:07 +0300 Subject: [PATCH 02/12] Added additional checks and test cases --- .../Model/Resolver/Cart/Item/GiftMessage.php | 32 +++++++++++--- .../GiftMessage/Cart/Item/GiftMessageTest.php | 44 +++++++++++++++---- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php index 34d3d3969835a..6d2f546e260e5 100644 --- a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php +++ b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php @@ -8,7 +8,6 @@ namespace Magento\GiftMessageGraphQl\Model\Resolver\Cart\Item; use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; @@ -16,6 +15,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\GiftMessage\Api\ItemRepositoryInterface; +use Magento\GiftMessage\Helper\Message as GiftMessageHelper; /** * Class provides ability to get GiftMessage for cart item @@ -28,14 +28,20 @@ class GiftMessage implements ResolverInterface private $itemRepository; /** - * GiftMessageItem constructor. - * + * @var GiftMessageHelper + */ + private $giftMessageHelper; + + /** * @param ItemRepositoryInterface $itemRepository + * @param GiftMessageHelper $giftMessageHelper */ public function __construct( - ItemRepositoryInterface $itemRepository + ItemRepositoryInterface $itemRepository, + GiftMessageHelper $giftMessageHelper ) { $this->itemRepository = $itemRepository; + $this->giftMessageHelper = $giftMessageHelper; } /** @@ -64,16 +70,28 @@ public function resolve( $quoteItem = $value['model']; + if (!$this->giftMessageHelper->isMessagesAllowed('items', $quoteItem)) { + return null; + } + + if (!$this->giftMessageHelper->isMessagesAllowed('item', $quoteItem)) { + return null; + } + try { $giftItemMessage = $this->itemRepository->get($quoteItem->getQuoteId(), $quoteItem->getItemId()); } catch (LocalizedException $e) { throw new GraphQlInputException(__('Can\'t load cart item')); } + if (!isset($giftItemMessage)) { + return null; + } + return [ - 'to' => isset($giftItemMessage) ? $giftItemMessage->getRecipient() : '', - 'from' => isset($giftItemMessage) ? $giftItemMessage->getSender() : '', - 'message'=> isset($giftItemMessage) ? $giftItemMessage->getMessage() : '' + 'to' => $giftItemMessage->getRecipient() ?? '', + 'from' => $giftItemMessage->getSender() ?? '', + 'message'=> $giftItemMessage->getMessage() ?? '' ]; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php index 7e5c0e15bd456..756caf0e228b4 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php @@ -20,13 +20,14 @@ class GiftMessageTest extends GraphQlAbstract */ private $getMaskedQuoteIdByReservedOrderId; - protected function setUp() + protected function setUp(): void { $objectManager = Bootstrap::getObjectManager(); $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); } /** + * @magentoConfigFixture default_store sales/gift_options/allow_items 1 * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php * @throws NoSuchEntityException * @throws Exception @@ -34,9 +35,40 @@ protected function setUp() public function testGiftMessageCartForItem() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); + foreach ($this->requestCartResult($maskedQuoteId)['cart']['items'] as $item) { + self::assertArrayHasKey('gift_message', $item); + self::assertArrayHasKey('to', $item['gift_message']); + self::assertArrayHasKey('from', $item['gift_message']); + self::assertArrayHasKey('message', $item['gift_message']); + } + } + + /** + * @magentoConfigFixture default_store sales/gift_options/allow_items 0 + * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php + * @throws NoSuchEntityException + * @throws Exception + */ + public function testGiftMessageCartForItemNotAllow() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); + foreach ($this->requestCartResult($maskedQuoteId)['cart']['items'] as $item) { + self::assertArrayHasKey('gift_message', $item); + self::assertNull($item['gift_message']); + } + } + + /** + * @param string $quoteId + * + * @return array|bool|float|int|string + * @throws Exception + */ + private function requestCartResult(string $quoteId) + { $query = <<graphQlQuery($query); - foreach ($response['cart']['items'] as $item) { - self::assertArrayHasKey('gift_message', $item); - self::assertArrayHasKey('to', $item['gift_message']); - self::assertArrayHasKey('from', $item['gift_message']); - self::assertArrayHasKey('message', $item['gift_message']); - } + return $this->graphQlQuery($query); } } From 81737c084b0b80b48cf5b3359e3b145c64aa54f1 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 20 May 2020 20:41:27 +0300 Subject: [PATCH 03/12] Remove redandant line from fixture --- .../GiftMessage/_files/guest/quote_with_item_message.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php index 36217ddbfac68..d72a44e794927 100644 --- a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php +++ b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php @@ -6,7 +6,6 @@ require __DIR__ . '/../../../../Magento/Catalog/_files/products.php'; -use Magento\Catalog\Model\Product; use Magento\Framework\ObjectManagerInterface; use Magento\GiftMessage\Model\Message; use Magento\GiftMessage\Model\ResourceModel\Message as MessageResource; @@ -28,7 +27,6 @@ $quoteModel->setData(['store_id' => 1, 'is_active' => 1, 'is_multi_shipping' => 0]); $quote->save($quoteModel); -$product = $objectManager->create(Product::class); $quoteProduct = $product->load($product->getIdBySku('simple')); $quoteModel->setReservedOrderId('test_guest_order_with_gift_message') From 120a105b5adb6fbc4d35a0067d72aa1975acccc5 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 11 May 2020 19:16:39 +0300 Subject: [PATCH 04/12] PR 28194 Add ability to update Gift Message for cart item --- .../GiftMessageGraphQl/etc/graphql/di.xml | 18 ++++++++ .../GiftMessageGraphQl/etc/schema.graphqls | 15 +++++++ .../Model/Resolver/UpdateCartItems.php | 43 +++++++++++++++++-- app/code/Magento/QuoteGraphQl/composer.json | 3 +- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml diff --git a/app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml b/app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml new file mode 100644 index 0000000000000..bce5b7063e6b9 --- /dev/null +++ b/app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml @@ -0,0 +1,18 @@ + + + + + + + sales/gift_options/allow_order + sales/gift_options/allow_items + + + + diff --git a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls index f0c0353cc6305..d383f900a3911 100644 --- a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls +++ b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls @@ -1,6 +1,11 @@ # Copyright © Magento, Inc. All rights reserved. # See COPYING.txt for license details. +type StoreConfig { + allow_order : String @doc(description: "Allow Gift Messages on order level") + allow_items : String @doc(description: "Allow Gift Messages for order items") +} + type Cart { gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart") } @@ -10,3 +15,13 @@ type GiftMessage { from: String! @doc(description: "Sender name") message: String! @doc(description: "Gift message text") } + +input CartItemUpdateInput { + gift_message: GiftMessageInput @doc(description: "Gift message details for the cart item") +} + +input GiftMessageInput { + to: String! @doc(description: "Recepient name") + from: String! @doc(description: "Sender name") + message: String! @doc(description: "Gift message text") +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php index fa90f08e4b553..5e420e1c27016 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php @@ -14,6 +14,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\GiftMessage\Api\ItemRepositoryInterface; use Magento\Quote\Api\CartItemRepositoryInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; @@ -46,21 +47,29 @@ class UpdateCartItems implements ResolverInterface private $cartRepository; /** - * @param GetCartForUser $getCartForUser + * @var ItemRepositoryInterface + */ + private $itemRepository; + + /** + * @param GetCartForUser $getCartForUser * @param CartItemRepositoryInterface $cartItemRepository - * @param UpdateCartItem $updateCartItem - * @param CartRepositoryInterface $cartRepository + * @param UpdateCartItem $updateCartItem + * @param CartRepositoryInterface $cartRepository + * @param ItemRepositoryInterface $itemRepository */ public function __construct( GetCartForUser $getCartForUser, CartItemRepositoryInterface $cartItemRepository, UpdateCartItem $updateCartItem, - CartRepositoryInterface $cartRepository + CartRepositoryInterface $cartRepository, + ItemRepositoryInterface $itemRepository ) { $this->getCartForUser = $getCartForUser; $this->cartItemRepository = $cartItemRepository; $this->updateCartItem = $updateCartItem; $this->cartRepository = $cartRepository; + $this->itemRepository = $itemRepository; } /** @@ -131,6 +140,32 @@ private function processCartItems(Quote $cart, array $items): void } else { $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); } + + if (!empty($item['gift_message'])) { + $this->updateGiftMessageForItem($cart, $item, $itemId); + } + } + } + + /** + * Update Gift Message for Quote item + * + * @param Quote $cart + * @param array $item + * @param int $itemId + * + * @throws GraphQlInputException + */ + private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId) + { + try { + $giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId); + $giftItemMessage->setRecipient($item['gift_message']['to']); + $giftItemMessage->setSender($item['gift_message']['from']); + $giftItemMessage->setMessage($item['gift_message']['message']); + $this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__('Gift Message can not be updated.')); } } } diff --git a/app/code/Magento/QuoteGraphQl/composer.json b/app/code/Magento/QuoteGraphQl/composer.json index 0652d39b5f426..25f089cf75a62 100644 --- a/app/code/Magento/QuoteGraphQl/composer.json +++ b/app/code/Magento/QuoteGraphQl/composer.json @@ -13,7 +13,8 @@ "magento/module-customer-graph-ql": "*", "magento/module-sales": "*", "magento/module-directory": "*", - "magento/module-graph-ql": "*" + "magento/module-graph-ql": "*", + "magento/module-gift-message": "*" }, "suggest": { "magento/module-graph-ql-cache": "*" From 035c1577d0e5dee7c102375cd2bdba0f1542c85f Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Thu, 21 May 2020 20:16:31 +0300 Subject: [PATCH 05/12] move part logic to other file --- .../CartItem/DataProvider/UpdateCartItems.php | 142 ++++++++++++++++++ .../Model/Resolver/UpdateCartItems.php | 101 ++----------- 2 files changed, 153 insertions(+), 90 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php diff --git a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php new file mode 100644 index 0000000000000..5657e5f4587ec --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php @@ -0,0 +1,142 @@ +cartItemRepository = $cartItemRepository; + $this->updateCartItem = $updateCartItem; + $this->itemRepository = $itemRepository; + $this->giftMessageHelper = $giftMessageHelper; + } + + /** + * Process cart items + * + * @param Quote $cart + * @param array $items + * @throws GraphQlInputException + * @throws LocalizedException + */ + public function processCartItems(Quote $cart, array $items): void + { + foreach ($items as $item) { + if (empty($item['cart_item_id'])) { + throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); + } + + $itemId = (int)$item['cart_item_id']; + $customizableOptions = $item['customizable_options'] ?? []; + $cartItem = $cart->getItemById($itemId); + + if ($cartItem && $cartItem->getParentItemId()) { + throw new GraphQlInputException(__('Child items may not be updated.')); + } + + if (count($customizableOptions) === 0 && !isset($item['quantity'])) { + throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); + } + + $quantity = (float)$item['quantity']; + + if ($quantity <= 0.0) { + $this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); + } else { + $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); + } + + if (!empty($item['gift_message'])) { + try { + if (!$this->giftMessageHelper->isMessagesAllowed('items', $cartItem)) { + continue; + } + if (!$this->giftMessageHelper->isMessagesAllowed('item', $cartItem)) { + continue; + } + + /** @var MessageInterface $giftItemMessage */ + $giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId); + + if (empty($giftItemMessage)) { + continue; + } + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__('Gift Message can not be updated.')); + } + + $this->updateGiftMessageForItem($cart, $giftItemMessage, $item, $itemId); + } + } + } + + /** + * Update Gift Message for Quote item + * + * @param Quote $cart + * @param MessageInterface $giftItemMessage + * @param array $item + * @param int $itemId + * + * @throws GraphQlInputException + */ + private function updateGiftMessageForItem(Quote $cart, MessageInterface $giftItemMessage, array $item, int $itemId) + { + try { + $giftItemMessage->setRecipient($item['gift_message']['to']); + $giftItemMessage->setSender($item['gift_message']['from']); + $giftItemMessage->setMessage($item['gift_message']['message']); + $this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId); + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__('Gift Message can not be updated')); + } + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php index 5e420e1c27016..005baaad0e1e5 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php @@ -14,62 +14,43 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\GiftMessage\Api\ItemRepositoryInterface; -use Magento\Quote\Api\CartItemRepositoryInterface; use Magento\Quote\Api\CartRepositoryInterface; -use Magento\Quote\Model\Quote; use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; -use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem; +use Magento\QuoteGraphQl\Model\CartItem\DataProvider\UpdateCartItems as UpdateCartItemsProvider; /** * @inheritdoc */ class UpdateCartItems implements ResolverInterface { - /** - * @var UpdateCartItem - */ - private $updateCartItem; - /** * @var GetCartForUser */ private $getCartForUser; - /** - * @var CartItemRepositoryInterface - */ - private $cartItemRepository; - /** * @var CartRepositoryInterface */ private $cartRepository; /** - * @var ItemRepositoryInterface + * @var UpdateCartItemsProvider */ - private $itemRepository; + private $updateCartItems; /** - * @param GetCartForUser $getCartForUser - * @param CartItemRepositoryInterface $cartItemRepository - * @param UpdateCartItem $updateCartItem - * @param CartRepositoryInterface $cartRepository - * @param ItemRepositoryInterface $itemRepository + * @param GetCartForUser $getCartForUser + * @param CartRepositoryInterface $cartRepository + * @param UpdateCartItemsProvider $updateCartItems */ public function __construct( GetCartForUser $getCartForUser, - CartItemRepositoryInterface $cartItemRepository, - UpdateCartItem $updateCartItem, CartRepositoryInterface $cartRepository, - ItemRepositoryInterface $itemRepository + UpdateCartItemsProvider $updateCartItems ) { $this->getCartForUser = $getCartForUser; - $this->cartItemRepository = $cartItemRepository; - $this->updateCartItem = $updateCartItem; $this->cartRepository = $cartRepository; - $this->itemRepository = $itemRepository; + $this->updateCartItems = $updateCartItems; } /** @@ -80,6 +61,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value if (empty($args['input']['cart_id'])) { throw new GraphQlInputException(__('Required parameter "cart_id" is missing.')); } + $maskedCartId = $args['input']['cart_id']; if (empty($args['input']['cart_items']) @@ -87,13 +69,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value ) { throw new GraphQlInputException(__('Required parameter "cart_items" is missing.')); } - $cartItems = $args['input']['cart_items']; + $cartItems = $args['input']['cart_items']; $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId); try { - $this->processCartItems($cart, $cartItems); + $this->updateCartItems->processCartItems($cart, $cartItems); $this->cartRepository->save($cart); } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); @@ -107,65 +89,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value ], ]; } - - /** - * Process cart items - * - * @param Quote $cart - * @param array $items - * @throws GraphQlInputException - * @throws LocalizedException - */ - private function processCartItems(Quote $cart, array $items): void - { - foreach ($items as $item) { - if (empty($item['cart_item_id'])) { - throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); - } - $itemId = (int)$item['cart_item_id']; - $customizableOptions = $item['customizable_options'] ?? []; - - $cartItem = $cart->getItemById($itemId); - if ($cartItem && $cartItem->getParentItemId()) { - throw new GraphQlInputException(__('Child items may not be updated.')); - } - - if (count($customizableOptions) === 0 && !isset($item['quantity'])) { - throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); - } - $quantity = (float)$item['quantity']; - - if ($quantity <= 0.0) { - $this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); - } else { - $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); - } - - if (!empty($item['gift_message'])) { - $this->updateGiftMessageForItem($cart, $item, $itemId); - } - } - } - - /** - * Update Gift Message for Quote item - * - * @param Quote $cart - * @param array $item - * @param int $itemId - * - * @throws GraphQlInputException - */ - private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId) - { - try { - $giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId); - $giftItemMessage->setRecipient($item['gift_message']['to']); - $giftItemMessage->setSender($item['gift_message']['from']); - $giftItemMessage->setMessage($item['gift_message']['message']); - $this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId); - } catch (LocalizedException $exception) { - throw new GraphQlInputException(__('Gift Message can not be updated.')); - } - } } From cea930b4d7549bcf476e621ad11ffbfa4847e0e6 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 22 May 2020 13:33:53 +0300 Subject: [PATCH 06/12] changes in test fixture --- .../_files/guest/quote_with_item_message.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php index d72a44e794927..4cbe088893b03 100644 --- a/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php +++ b/dev/tests/integration/testsuite/Magento/GiftMessage/_files/guest/quote_with_item_message.php @@ -4,8 +4,7 @@ * See COPYING.txt for license details. */ -require __DIR__ . '/../../../../Magento/Catalog/_files/products.php'; - +use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\ObjectManagerInterface; use Magento\GiftMessage\Model\Message; use Magento\GiftMessage\Model\ResourceModel\Message as MessageResource; @@ -15,6 +14,9 @@ use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource; use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMaskFactory; use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\Workaround\Override\Fixture\Resolver; + +Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/products.php'); /** @var ObjectManagerInterface $objectManager */ $objectManager = Bootstrap::getObjectManager(); @@ -27,10 +29,12 @@ $quoteModel->setData(['store_id' => 1, 'is_active' => 1, 'is_multi_shipping' => 0]); $quote->save($quoteModel); -$quoteProduct = $product->load($product->getIdBySku('simple')); +/** @var ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(ProductRepositoryInterface::class); +$product = $productRepository->get('simple'); $quoteModel->setReservedOrderId('test_guest_order_with_gift_message') - ->addProduct($product->load($product->getIdBySku('simple')), 1); + ->addProduct($product, 1); $quoteModel->collectTotals(); $quote->save($quoteModel); @@ -45,7 +49,7 @@ $messageModel->setMessage('Gift Message Text'); $message->save($messageModel); -$quoteModel->getItemByProduct($quoteProduct)->setGiftMessageId($messageModel->getId()); +$quoteModel->getItemByProduct($product)->setGiftMessageId($messageModel->getId()); $quote->save($quoteModel); /** @var QuoteIdMaskResource $quoteIdMask */ From dabbd3c07dbc5ee4c09b785f3f315b4fd3c29fa1 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Fri, 22 May 2020 15:38:02 +0300 Subject: [PATCH 07/12] Added test and minor fix --- .../CartItem/DataProvider/UpdateCartItems.php | 3 + .../Quote/Guest/UpdateCartItemsTest.php | 78 ++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php index 5657e5f4587ec..8d98ec039dc94 100644 --- a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php @@ -64,8 +64,11 @@ public function __construct( * * @param Quote $cart * @param array $items + * * @throws GraphQlInputException * @throws LocalizedException + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ public function processCartItems(Quote $cart, array $items): void { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php index a17bc1aa3821a..f6b8cb6198bec 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php @@ -7,8 +7,9 @@ namespace Magento\GraphQl\Quote\Guest; -use Magento\Quote\Model\QuoteFactory; +use Exception; use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; @@ -273,6 +274,81 @@ private function getCartQuery(string $maskedQuoteId) } } } +QUERY; + } + + /** + * @magentoConfigFixture default_store sales/gift_options/allow_items 1 + * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php + * @throws Exception + */ + public function testUpdateGiftMessageCartForItem() + { + $query = $this->getUpdateGiftMessageQuery(); + foreach ($this->graphQlMutation($query)['updateCartItems']['cart']['items'] as $item) { + self::assertArrayHasKey('gift_message', $item); + self::assertSame('Alex', $item['gift_message']['to']); + self::assertSame('Mike', $item['gift_message']['from']); + self::assertSame('Best regards.', $item['gift_message']['message']); + } + } + + /** + * @magentoConfigFixture default_store sales/gift_options/allow_items 0 + * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php + * @throws Exception + */ + public function testUpdateGiftMessageCartForItemNotAllow() + { + $query = $this->getUpdateGiftMessageQuery(); + foreach ($this->graphQlMutation($query)['updateCartItems']['cart']['items'] as $item) { + self::assertNull($item['gift_message']); + } + } + + private function getUpdateGiftMessageQuery() + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, 'test_guest_order_with_gift_message', 'reserved_order_id'); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); + $itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId(); + + return << Date: Fri, 22 May 2020 18:34:20 +0300 Subject: [PATCH 08/12] Remove EE type from CA schema --- app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls index 053d2f2e8e8f0..0ca69a19b711c 100644 --- a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls +++ b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls @@ -22,10 +22,6 @@ type BundleCartItem { gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") } -type GiftCardCartItem { - gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") -} - type GiftMessage { to: String! @doc(description: "Recepient name") from: String! @doc(description: "Sender name") From a2f28d83e8ceab42e60505af371d3e5905ee1d65 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Tue, 2 Jun 2020 14:00:21 +0300 Subject: [PATCH 09/12] minor fix --- .../Quote/Guest/UpdateCartItemsTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php index f6b8cb6198bec..0a22f3ca9721c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php @@ -278,31 +278,31 @@ private function getCartQuery(string $maskedQuoteId) } /** - * @magentoConfigFixture default_store sales/gift_options/allow_items 1 + * @magentoConfigFixture default_store sales/gift_options/allow_items 0 * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php * @throws Exception */ - public function testUpdateGiftMessageCartForItem() + public function testUpdateGiftMessageCartForItemNotAllow() { $query = $this->getUpdateGiftMessageQuery(); foreach ($this->graphQlMutation($query)['updateCartItems']['cart']['items'] as $item) { - self::assertArrayHasKey('gift_message', $item); - self::assertSame('Alex', $item['gift_message']['to']); - self::assertSame('Mike', $item['gift_message']['from']); - self::assertSame('Best regards.', $item['gift_message']['message']); + self::assertNull($item['gift_message']); } } /** - * @magentoConfigFixture default_store sales/gift_options/allow_items 0 + * @magentoConfigFixture default_store sales/gift_options/allow_items 1 * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php * @throws Exception */ - public function testUpdateGiftMessageCartForItemNotAllow() + public function testUpdateGiftMessageCartForItem() { $query = $this->getUpdateGiftMessageQuery(); foreach ($this->graphQlMutation($query)['updateCartItems']['cart']['items'] as $item) { - self::assertNull($item['gift_message']); + self::assertArrayHasKey('gift_message', $item); + self::assertSame('Alex', $item['gift_message']['to']); + self::assertSame('Mike', $item['gift_message']['from']); + self::assertSame('Best regards.', $item['gift_message']['message']); } } From 233b695c4f55b1a4c14009ecc13ced4658d5b868 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 8 Jun 2020 17:14:15 +0300 Subject: [PATCH 10/12] Add some changes for messages --- .../Model/Resolver/Cart/GiftMessage.php | 2 +- .../Model/Resolver/Cart/Item/GiftMessage.php | 2 +- app/code/Magento/GiftMessageGraphQl/README.md | 2 +- .../Magento/GiftMessageGraphQl/etc/schema.graphqls | 12 ++++++------ .../Model/CartItem/DataProvider/UpdateCartItems.php | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/GiftMessage.php b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/GiftMessage.php index c317221fb6ef7..2ce51c8bbf19d 100644 --- a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/GiftMessage.php +++ b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/GiftMessage.php @@ -66,7 +66,7 @@ public function resolve( array $args = null ) { if (!isset($value['model'])) { - throw new GraphQlInputException(__('"model" value should be specified')); + throw new GraphQlInputException(__('"model" value must be specified')); } $cart = $value['model']; diff --git a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php index 6d2f546e260e5..a9a8e682612cc 100644 --- a/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php +++ b/app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php @@ -65,7 +65,7 @@ public function resolve( array $args = null ) { if (!isset($value['model'])) { - throw new GraphQlInputException(__('"model" value should be specified')); + throw new GraphQlInputException(__('"model" value must be specified')); } $quoteItem = $value['model']; diff --git a/app/code/Magento/GiftMessageGraphQl/README.md b/app/code/Magento/GiftMessageGraphQl/README.md index fa2e02116b66c..d73a058e0db9c 100644 --- a/app/code/Magento/GiftMessageGraphQl/README.md +++ b/app/code/Magento/GiftMessageGraphQl/README.md @@ -1,3 +1,3 @@ # GiftMessageGraphQl -**GiftMessageGraphQl** provides information about gift messages for cart, cart items, order and order items. +**GiftMessageGraphQl** provides information about gift messages for carts, cart items, orders and order items. diff --git a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls index 0ca69a19b711c..d7dcbfd2b17dc 100644 --- a/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls +++ b/app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls @@ -2,8 +2,8 @@ # See COPYING.txt for license details. type StoreConfig { - allow_order : String @doc(description: "Allow Gift Messages on order level") - allow_items : String @doc(description: "Allow Gift Messages for order items") + allow_order : String @doc(description: "The value of the Allow Gift Messages on Order Level option") + allow_items : String @doc(description: "The value of the Allow Gift Messages for Order Items option") } type Cart { @@ -22,8 +22,8 @@ type BundleCartItem { gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") } -type GiftMessage { - to: String! @doc(description: "Recepient name") +type GiftMessage @doc(description: "Contains the text of a gift message, its sender, and recipient") { + to: String! @doc(description: "Recipient name") from: String! @doc(description: "Sender name") message: String! @doc(description: "Gift message text") } @@ -32,8 +32,8 @@ input CartItemUpdateInput { gift_message: GiftMessageInput @doc(description: "Gift message details for the cart item") } -input GiftMessageInput { - to: String! @doc(description: "Recepient name") +input GiftMessageInput @doc(description: "Contains the text of a gift message, its sender, and recipient") { + to: String! @doc(description: "Recipient name") from: String! @doc(description: "Sender name") message: String! @doc(description: "Gift message text") } diff --git a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php index 8d98ec039dc94..c9016d7a322c1 100644 --- a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php @@ -113,7 +113,7 @@ public function processCartItems(Quote $cart, array $items): void continue; } } catch (LocalizedException $exception) { - throw new GraphQlInputException(__('Gift Message can not be updated.')); + throw new GraphQlInputException(__('Gift Message cannot be updated.')); } $this->updateGiftMessageForItem($cart, $giftItemMessage, $item, $itemId); @@ -139,7 +139,7 @@ private function updateGiftMessageForItem(Quote $cart, MessageInterface $giftIte $giftItemMessage->setMessage($item['gift_message']['message']); $this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId); } catch (LocalizedException $exception) { - throw new GraphQlInputException(__('Gift Message can not be updated')); + throw new GraphQlInputException(__('Gift Message cannot be updated')); } } } From d994276cbab26239732d64b8c313a38eac13f01c Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 10 Jun 2020 12:53:26 +0300 Subject: [PATCH 11/12] minor fix --- .../GiftMessage/Cart/Item/GiftMessageTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php index 756caf0e228b4..fa0909d556b3a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Cart/Item/GiftMessageTest.php @@ -27,34 +27,34 @@ protected function setUp(): void } /** - * @magentoConfigFixture default_store sales/gift_options/allow_items 1 + * @magentoConfigFixture default_store sales/gift_options/allow_items 0 * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php * @throws NoSuchEntityException * @throws Exception */ - public function testGiftMessageCartForItem() + public function testGiftMessageCartForItemNotAllow() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); foreach ($this->requestCartResult($maskedQuoteId)['cart']['items'] as $item) { self::assertArrayHasKey('gift_message', $item); - self::assertArrayHasKey('to', $item['gift_message']); - self::assertArrayHasKey('from', $item['gift_message']); - self::assertArrayHasKey('message', $item['gift_message']); + self::assertNull($item['gift_message']); } } /** - * @magentoConfigFixture default_store sales/gift_options/allow_items 0 + * @magentoConfigFixture default_store sales/gift_options/allow_items 1 * @magentoApiDataFixture Magento/GiftMessage/_files/guest/quote_with_item_message.php * @throws NoSuchEntityException * @throws Exception */ - public function testGiftMessageCartForItemNotAllow() + public function testGiftMessageCartForItem() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_guest_order_with_gift_message'); foreach ($this->requestCartResult($maskedQuoteId)['cart']['items'] as $item) { self::assertArrayHasKey('gift_message', $item); - self::assertNull($item['gift_message']); + self::assertArrayHasKey('to', $item['gift_message']); + self::assertArrayHasKey('from', $item['gift_message']); + self::assertArrayHasKey('message', $item['gift_message']); } } From 938e43bfae3774afde30523951aa9edd27778ec3 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 10 Jun 2020 14:01:08 +0300 Subject: [PATCH 12/12] Set gift message if gift message missing for cart item --- .../CartItem/DataProvider/UpdateCartItems.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php index c9016d7a322c1..c2e94b215956e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php @@ -10,6 +10,7 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\GiftMessage\Api\Data\MessageInterface; +use Magento\GiftMessage\Api\Data\MessageInterfaceFactory; use Magento\GiftMessage\Api\ItemRepositoryInterface; use Magento\GiftMessage\Helper\Message as GiftMessageHelper; use Magento\Quote\Api\CartItemRepositoryInterface; @@ -41,22 +42,30 @@ class UpdateCartItems */ private $giftMessageHelper; + /** + * @var MessageInterfaceFactory + */ + private $giftMessageFactory; + /** * @param CartItemRepositoryInterface $cartItemRepository * @param UpdateCartItem $updateCartItem * @param ItemRepositoryInterface $itemRepository * @param GiftMessageHelper $giftMessageHelper + * @param MessageInterfaceFactory $giftMessageFactory */ public function __construct( CartItemRepositoryInterface $cartItemRepository, UpdateCartItem $updateCartItem, ItemRepositoryInterface $itemRepository, - GiftMessageHelper $giftMessageHelper + GiftMessageHelper $giftMessageHelper, + MessageInterfaceFactory $giftMessageFactory ) { $this->cartItemRepository = $cartItemRepository; $this->updateCartItem = $updateCartItem; $this->itemRepository = $itemRepository; $this->giftMessageHelper = $giftMessageHelper; + $this->giftMessageFactory = $giftMessageFactory; } /** @@ -110,6 +119,9 @@ public function processCartItems(Quote $cart, array $items): void $giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId); if (empty($giftItemMessage)) { + /** @var MessageInterface $giftMessage */ + $giftMessage = $this->giftMessageFactory->create(); + $this->updateGiftMessageForItem($cart, $giftMessage, $item, $itemId); continue; } } catch (LocalizedException $exception) {