From f0b8b7b23df9b1a4dfd0d4d886e0d7c4df17ed9c Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 27 Feb 2019 21:17:47 -0600 Subject: [PATCH] GraphQL-37: [Cart Operations] Manage Cart Items -- Refactoring --- .../Model/Cart/QuoteAddressFactory.php | 67 ++++++++ .../Model/Cart/SetBillingAddressOnCart.php | 51 +++--- .../Model/Cart/SetShippingAddressOnCart.php | 44 ++---- .../Quote/Customer/RemoveItemFromCartTest.php | 147 ++++++++++-------- .../Customer/SetPaymentMethodOnCartTest.php | 2 - .../Quote/Guest/RemoveItemFromCartTest.php | 115 ++++---------- .../Guest/SetPaymentMethodOnCartTest.php | 2 - 7 files changed, 215 insertions(+), 213 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php new file mode 100644 index 000000000000..6170d2183491 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -0,0 +1,67 @@ +quoteAddressFactory = $quoteAddressFactory; + } + + /** + * Create QuoteAddress based on input data + * + * @param array $addressInput + * @return QuoteAddress + */ + public function createBasedOnInputData(array $addressInput): QuoteAddress + { + $addressInput['country_id'] = $addressInput['country_code'] ?? ''; + + $quoteAddress = $this->quoteAddressFactory->create(); + $quoteAddress->addData($addressInput); + return $quoteAddress; + } + + /** + * Create QuoteAddress based on CustomerAddress + * + * @param CustomerAddress $customerAddress + * @return QuoteAddress + * @throws GraphQlInputException + */ + public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress + { + $quoteAddress = $this->quoteAddressFactory->create(); + try { + $quoteAddress->importCustomerAddressData($customerAddress); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + return $quoteAddress; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index d019835392ed..5a5ebe23b644 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -7,7 +7,6 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\Customer\Api\Data\AddressInterface as CustomerAddress; use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; @@ -15,9 +14,8 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Quote\Api\Data\CartInterface; -use Magento\Quote\Model\Quote\Address as QuoteAddress; -use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory; use Magento\Quote\Api\BillingAddressManagementInterface; +use Magento\Quote\Model\Quote\Address as QuoteAddress; /** * Set billing address for a specified shopping cart @@ -77,7 +75,8 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b { $customerAddressId = $billingAddress['customer_address_id'] ?? null; $addressInput = $billingAddress['address'] ?? null; - $useForShipping = $billingAddress['use_for_shipping'] ?? false; + $useForShipping = isset($billingAddress['use_for_shipping']) + ? (bool)$billingAddress['use_for_shipping'] : false; if (null === $customerAddressId && null === $addressInput) { throw new GraphQlInputException( @@ -99,48 +98,34 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b } if (null === $customerAddressId) { - $billingAddress = $this->processNewAddress($addressInput); + $billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); } else { $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType()); $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId()); - $billingAddress = $this->processAddressFromAddressBook($customerAddress); - } - - try { - $this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage())); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); + $billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); } - } - - /** - * @param array $addressInput - * @return QuoteAddress - */ - private function processNewAddress(array $addressInput): QuoteAddress - { - $addressInput['country_id'] = $addressInput['country_code'] ?? ''; - $quoteAddress = $this->quoteAddressFactory->create(); - $quoteAddress->addData($addressInput); - return $quoteAddress; + $this->assignBillingAddressToCart($cart, $billingAddress, $useForShipping); } /** - * @param CustomerAddress $customerAddress - * @return QuoteAddress + * @param CartInterface $cart + * @param QuoteAddress $billingAddress + * @param bool $useForShipping * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException */ - private function processAddressFromAddressBook(CustomerAddress $customerAddress): QuoteAddress - { - $quoteAddress = $this->quoteAddressFactory->create(); + private function assignBillingAddressToCart( + CartInterface $cart, + QuoteAddress $billingAddress, + bool $useForShipping + ): void { try { - $quoteAddress->importCustomerAddressData($customerAddress); + $this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage())); } catch (LocalizedException $e) { throw new GraphQlInputException(__($e->getMessage())); } - return $quoteAddress; } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php index fbc652601061..7e9bc6918e9b 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php @@ -7,7 +7,6 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\Customer\Api\Data\AddressInterface as CustomerAddress; use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; @@ -16,7 +15,6 @@ use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Model\Quote\Address as QuoteAddress; -use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory; use Magento\Quote\Model\ShippingAddressManagementInterface; /** @@ -89,48 +87,32 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s } if (null === $customerAddressId) { - $shippingAddress = $this->processNewAddress($addressInput); + $shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); } else { $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType()); $customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId()); - $shippingAddress = $this->processAddressFromAddressBook($customerAddress); + $shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress); } - try { - $this->shippingAddressManagement->assign($cart->getId(), $shippingAddress); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage())); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage())); - } + $this->assignShippingAddressToCart($cart, $shippingAddress); } /** - * @param array $addressInput - * @return QuoteAddress - */ - private function processNewAddress(array $addressInput): QuoteAddress - { - $addressInput['country_id'] = $addressInput['country_code'] ?? ''; - - $quoteAddress = $this->quoteAddressFactory->create(); - $quoteAddress->addData($addressInput); - return $quoteAddress; - } - - /** - * @param CustomerAddress $customerAddress - * @return QuoteAddress + * @param CartInterface $cart + * @param QuoteAddress $shippingAddress * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException */ - private function processAddressFromAddressBook(CustomerAddress $customerAddress): QuoteAddress - { - $quoteAddress = $this->quoteAddressFactory->create(); + private function assignShippingAddressToCart( + CartInterface $cart, + QuoteAddress $shippingAddress + ): void { try { - $quoteAddress->importCustomerAddressData($customerAddress); + $this->shippingAddressManagement->assign($cart->getId(), $shippingAddress); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage())); } catch (LocalizedException $e) { throw new GraphQlInputException(__($e->getMessage())); } - return $quoteAddress; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php index 9053ca32e121..a351a2188a66 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php @@ -63,25 +63,9 @@ public function testRemoveItemFromCart() $quote = $this->quoteFactory->create(); $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); - $itemId = $quote->getItemByProduct($this->productRepository->get('simple'))->getId(); - - $query = <<getItemByProduct($this->productRepository->get('simple'))->getId(); + $query = $this->prepareMutationQuery($maskedQuoteId, $itemId); $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); $this->assertArrayHasKey('removeItemFromCart', $response); @@ -96,24 +80,8 @@ public function testRemoveItemFromCart() */ public function testRemoveItemFromNonExistentCart() { - $maskedCartId = 'non_existent_masked_id'; + $query = $this->prepareMutationQuery('non_existent_masked_id', 1); - $query = <<graphQlQuery($query, [], '', $this->getHeaderMap()); } @@ -125,26 +93,11 @@ public function testRemoveNotExistentItem() $quote = $this->quoteFactory->create(); $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); - $itemId = 999; + $notExistentItemId = 999; - $query = <<expectExceptionMessage("Cart doesn't contain the {$itemId} item."); + $this->expectExceptionMessage("Cart doesn't contain the {$notExistentItemId} item."); + $query = $this->prepareMutationQuery($maskedQuoteId, $notExistentItemId); $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } @@ -154,9 +107,9 @@ public function testRemoveNotExistentItem() */ public function testRemoveItemIfItemIsNotBelongToCart() { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); + $firstQuote = $this->quoteFactory->create(); + $this->quoteResource->load($firstQuote, 'test_order_1', 'reserved_order_id'); + $firstQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId()); $secondQuote = $this->quoteFactory->create(); $this->quoteResource->load( @@ -164,14 +117,85 @@ public function testRemoveItemIfItemIsNotBelongToCart() 'test_order_with_virtual_product_without_address', 'reserved_order_id' ); - $secondQuoteItemId = $secondQuote->getItemByProduct($this->productRepository->get('virtual-product'))->getId(); + $secondQuote->setCustomerId(1); + $this->quoteResource->save($secondQuote); + $secondQuoteItemId = (int)$secondQuote + ->getItemByProduct($this->productRepository->get('virtual-product')) + ->getId(); - $query = <<expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item."); + + $query = $this->prepareMutationQuery($firstQuoteMaskedId, $secondQuoteItemId); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php + */ + public function testRemoveItemFromGuestCart() + { + $guestQuote = $this->quoteFactory->create(); + $this->quoteResource->load( + $guestQuote, + 'test_order_with_virtual_product_without_address', + 'reserved_order_id' + ); + $guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId()); + $guestQuoteItemId = (int)$guestQuote + ->getItemByProduct($this->productRepository->get('virtual-product')) + ->getId(); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$guestQuoteMaskedId\"" + ); + + $query = $this->prepareMutationQuery($guestQuoteMaskedId, $guestQuoteItemId); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php + */ + public function testRemoveItemFromAnotherCustomerCart() + { + $anotherCustomerQuote = $this->quoteFactory->create(); + $this->quoteResource->load( + $anotherCustomerQuote, + 'test_order_with_virtual_product_without_address', + 'reserved_order_id' + ); + $anotherCustomerQuote->setCustomerId(2); + $this->quoteResource->save($anotherCustomerQuote); + + $anotherCustomerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$anotherCustomerQuote->getId()); + $anotherCustomerQuoteItemId = (int)$anotherCustomerQuote + ->getItemByProduct($this->productRepository->get('virtual-product')) + ->getId(); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$anotherCustomerQuoteMaskedId\"" + ); + + $query = $this->prepareMutationQuery($anotherCustomerQuoteMaskedId, $anotherCustomerQuoteItemId); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @param string $maskedQuoteId + * @param int $itemId + * @return string + */ + private function prepareMutationQuery(string $maskedQuoteId, int $itemId): string + { + return <<expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item."); - - $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php index 852fcfe316e1..315cbc86b2de 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php @@ -152,8 +152,6 @@ public function testSetPaymentMethodIfCustomerIsNotOwnerOfCart() } /** - * Generates query for setting the specified shipping method on cart - * * @param string $maskedQuoteId * @param string $methodCode * @return string diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/RemoveItemFromCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/RemoveItemFromCartTest.php index c5564b044366..a6b8f05fc083 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/RemoveItemFromCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/RemoveItemFromCartTest.php @@ -56,24 +56,9 @@ public function testRemoveItemFromCart() $quote = $this->quoteFactory->create(); $this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id'); $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); - $itemId = $quote->getItemByProduct($this->productRepository->get('simple'))->getId(); + $itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId(); - $query = <<prepareMutationQuery($maskedQuoteId, $itemId); $response = $this->graphQlQuery($query); $this->assertArrayHasKey('removeItemFromCart', $response); @@ -87,24 +72,8 @@ public function testRemoveItemFromCart() */ public function testRemoveItemFromNonExistentCart() { - $maskedCartId = 'non_existent_masked_id'; + $query = $this->prepareMutationQuery('non_existent_masked_id', 1); - $query = <<graphQlQuery($query); } @@ -116,26 +85,11 @@ public function testRemoveNotExistentItem() $quote = $this->quoteFactory->create(); $this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id'); $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); - $itemId = 999; + $notExistentItemId = 999; - $query = <<expectExceptionMessage("Cart doesn't contain the {$itemId} item."); + $this->expectExceptionMessage("Cart doesn't contain the {$notExistentItemId} item."); + $query = $this->prepareMutationQuery($maskedQuoteId, $notExistentItemId); $this->graphQlQuery($query); } @@ -147,9 +101,9 @@ public function testRemoveNotExistentItem() */ public function testRemoveItemIfItemIsNotBelongToCart() { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id'); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); + $firstQuote = $this->quoteFactory->create(); + $this->quoteResource->load($firstQuote, 'test_order_with_simple_product_without_address', 'reserved_order_id'); + $firstQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$firstQuote->getId()); $secondQuote = $this->quoteFactory->create(); $this->quoteResource->load( @@ -157,26 +111,13 @@ public function testRemoveItemIfItemIsNotBelongToCart() 'test_order_with_virtual_product_without_address', 'reserved_order_id' ); - $secondQuoteItemId = $secondQuote->getItemByProduct($this->productRepository->get('virtual-product'))->getId(); + $secondQuoteItemId = (int)$secondQuote + ->getItemByProduct($this->productRepository->get('virtual-product')) + ->getId(); - $query = <<expectExceptionMessage("Cart doesn't contain the {$secondQuoteItemId} item."); + $query = $this->prepareMutationQuery($firstQuoteMaskedId, $secondQuoteItemId); $this->graphQlQuery($query); } @@ -187,17 +128,30 @@ public function testRemoveItemIfItemIsNotBelongToCart() */ public function testRemoveItemFromCustomerCart() { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId()); - $itemId = $quote->getItemByProduct($this->productRepository->get('simple'))->getId(); + $customerQuote = $this->quoteFactory->create(); + $this->quoteResource->load($customerQuote, 'test_order_1', 'reserved_order_id'); + $customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId()); + $customerQuoteItemId = (int)$customerQuote->getItemByProduct($this->productRepository->get('simple'))->getId(); + + $this->expectExceptionMessage("The current user cannot perform operations on cart \"$customerQuoteMaskedId\""); - $query = <<prepareMutationQuery($customerQuoteMaskedId, $customerQuoteItemId); + $this->graphQlQuery($query); + } + + /** + * @param string $maskedQuoteId + * @param int $itemId + * @return string + */ + private function prepareMutationQuery(string $maskedQuoteId, int $itemId): string + { + return <<expectExceptionMessage("The current user cannot perform operations on cart \"$maskedQuoteId\""); - - $this->graphQlQuery($query); } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php index 6ccfeb103988..7484b2af7569 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php @@ -127,8 +127,6 @@ public function testSetPaymentMethodToCustomerCart() } /** - * Generates query for setting the specified shipping method on cart - * * @param string $maskedQuoteId * @param string $methodCode * @return string