From e95c678c4db2b29fad6bf7f319dcb1f16dd4c15f Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Tue, 29 Jan 2019 14:03:10 -0500 Subject: [PATCH 01/15] Proposal For setPaymentMethodOnCart Ref #293 --- app/code/Magento/BraintreeGraphQl/README.md | 4 + .../Magento/BraintreeGraphQl/composer.json | 24 ++ .../BraintreeGraphQl/etc/graphql/di.xml | 27 ++ .../Magento/BraintreeGraphQl/etc/module.xml | 10 + .../BraintreeGraphQl/etc/schema.graphqls | 18 + .../Magento/BraintreeGraphQl/registration.php | 10 + .../AdditionalDataBuilderInterface.php | 13 + .../Payment/AdditionalDataBuilderPool.php | 32 ++ .../Payment/DefaultAdditionalDataBuilder.php | 43 +++ .../Model/Cart/Payment/MethodBuilder.php | 62 ++++ .../Cart/Payment/PaymentDataProvider.php | 35 ++ .../Model/Resolver/CartPaymentMethod.php | 58 +++ .../Model/Resolver/SetPaymentMethodOnCart.php | 102 ++++++ .../Magento/QuoteGraphQl/etc/schema.graphqls | 21 ++ composer.json | 1 + composer.lock | 4 +- .../Quote/SetPaymentMethodOnCartTest.php | 335 ++++++++++++++++++ 17 files changed, 797 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/BraintreeGraphQl/README.md create mode 100644 app/code/Magento/BraintreeGraphQl/composer.json create mode 100644 app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml create mode 100644 app/code/Magento/BraintreeGraphQl/etc/module.xml create mode 100644 app/code/Magento/BraintreeGraphQl/etc/schema.graphqls create mode 100644 app/code/Magento/BraintreeGraphQl/registration.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderInterface.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderPool.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php diff --git a/app/code/Magento/BraintreeGraphQl/README.md b/app/code/Magento/BraintreeGraphQl/README.md new file mode 100644 index 0000000000000..f6740e4d250e9 --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/README.md @@ -0,0 +1,4 @@ +# BraintreeGraphQl + +**BraintreeGraphQl** provides type and resolver for method additional +information. \ No newline at end of file diff --git a/app/code/Magento/BraintreeGraphQl/composer.json b/app/code/Magento/BraintreeGraphQl/composer.json new file mode 100644 index 0000000000000..a322db9d257dc --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/composer.json @@ -0,0 +1,24 @@ +{ + "name": "magento/module-braintree-graph-ql", + "description": "N/A", + "type": "magento2-module", + "require": { + "php": "~7.1.3||~7.2.0", + "magento/framework": "*" + }, + "suggest": { + "magento/module-graph-ql": "*" + }, + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Magento\\BraintreeGraphQl\\": "" + } + } +} diff --git a/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml b/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml new file mode 100644 index 0000000000000..3788a0c2f1325 --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml @@ -0,0 +1,27 @@ + + + + + + + BraintreeAdditionalDataBuilder + BraintreeVaultAdditionalDataBuilder + + + + + + Magento\Braintree\Model\Ui\ConfigProvider::CODE + + + + + Magento\Braintree\Model\Ui\ConfigProvider::CC_VAULT_CODE + + + diff --git a/app/code/Magento/BraintreeGraphQl/etc/module.xml b/app/code/Magento/BraintreeGraphQl/etc/module.xml new file mode 100644 index 0000000000000..2133e95a69104 --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/etc/module.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls b/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls new file mode 100644 index 0000000000000..83c98a68d85ee --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls @@ -0,0 +1,18 @@ +# Copyright © Magento, Inc. All rights reserved. +# See COPYING.txt for license details. + +input PaymentMethodInput { + braintree: BraintreeInput + braintree_vault: BraintreeVaultInput +} + +input BraintreeInput { + payment_method_nonce: String! + is_active_payment_token_enabler: Boolean! +} + +input BraintreeVaultInput { + payment_method_nonce: String! + public_hash: String! + is_active_payment_token_enabler: Boolean! +} diff --git a/app/code/Magento/BraintreeGraphQl/registration.php b/app/code/Magento/BraintreeGraphQl/registration.php new file mode 100644 index 0000000000000..37f7ef30864cb --- /dev/null +++ b/app/code/Magento/BraintreeGraphQl/registration.php @@ -0,0 +1,10 @@ +builders = $builders; + } + + public function buildForMethod(string $methodCode, array $args): array + { + $additionalData = []; + if (isset($this->builders[$methodCode])) { + $additionalData = $this->builders[$methodCode]->build($args); + } + + return $additionalData; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php new file mode 100644 index 0000000000000..5a032ccc6386d --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php @@ -0,0 +1,43 @@ +arrayManager = $arrayManager; + $this->methodCode = $methodCode; + } + + public function build(array $args): array + { + return $this->arrayManager->get($this->getAdditionalDataPath(), $args) ?? []; + } + + private function getAdditionalDataPath(): string + { + return sprintf(static::INPUT_PATH_ADDITIONAL_DATA, $this->methodCode); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php new file mode 100644 index 0000000000000..110e24d7d0fe5 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php @@ -0,0 +1,62 @@ +paymentFactory = $paymentFactory; + $this->additionalDataBuilderPool = $additionalDataBuilderPool; + $this->arrayManager = $arrayManager; + } + + public function build(array $args): PaymentInterface + { + $method = (string) $this->arrayManager->get('input/payment_method/method', $args); + + return $this->paymentFactory->create([ + 'data' => [ + PaymentInterface::KEY_METHOD => $method, + PaymentInterface::KEY_PO_NUMBER => $this->arrayManager->get('input/payment_method/po_number', $args), + PaymentInterface::KEY_ADDITIONAL_DATA => $this->additionalDataBuilderPool->buildForMethod( + $method, + $args + ), + ] + ]); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php new file mode 100644 index 0000000000000..22adc69d1cc58 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php @@ -0,0 +1,35 @@ +getPayment(); + if (!$payment) { + return []; + } + + return [ + 'method' => $payment->getMethod(), + 'po_number' => $payment->getPoNumber(), + ]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php new file mode 100644 index 0000000000000..9e75256cd9b2b --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php @@ -0,0 +1,58 @@ +paymentDataProvider = $paymentDataProvider; + $this->getCartForUser = $getCartForUser; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['cart_id'])) { + throw new LocalizedException(__('"model" value should be specified')); + } + + $maskedCartId = $value['cart_id']; + $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); + + return $this->paymentDataProvider->getCartPayment($cart); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php new file mode 100644 index 0000000000000..5e48b3413f7ce --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php @@ -0,0 +1,102 @@ +maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; + $this->getCartForUser = $getCartForUser; + $this->arrayManager = $arrayManager; + $this->paymentMethodManagement = $paymentMethodManagement; + $this->methodBuilder = $methodBuilder; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + $paymentMethod = $this->arrayManager->get('input/payment_method', $args); + $maskedCartId = $this->arrayManager->get('input/cart_id', $args); + + if (!$maskedCartId) { + throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); + } + if (!$paymentMethod) { + throw new GraphQlInputException(__('Required parameter "payment_method" is missing')); + } + + $maskedCartId = $args['input']['cart_id']; + $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); + + try { + $this->paymentMethodManagement->set($cart->getId(), $this->methodBuilder->build($args)); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } + + return [ + 'cart' => [ + 'cart_id' => $maskedCartId, + ], + ]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 4c1101a5f90a8..e1dd06f64cf88 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -15,6 +15,17 @@ type Mutation { setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart") addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart") + setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart") +} + +input SetPaymentMethodOnCartInput { + cart_id: String! + payment_method: PaymentMethodInput! +} + +input PaymentMethodInput { + method: String! + po_number: String } input SetShippingAddressesOnCartInput { @@ -64,6 +75,10 @@ input ShippingMethodForAddressInput { shipping_method_code: String! } +type SetPaymentMethodOnCartOutput { + cart: Cart! +} + type SetBillingAddressOnCartOutput { cart: Cart! } @@ -98,6 +113,7 @@ type Cart { items: [CartItemInterface] applied_coupon: AppliedCoupon addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddresses") + payment_method: CartPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPaymentMethod") } type CartAddress { @@ -118,6 +134,11 @@ type CartAddress { cart_items: [CartItemQuantity] } +type CartPaymentMethod { + method: String + po_number: String +} + type CartItemQuantity { cart_item_id: String! quantity: Float! diff --git a/composer.json b/composer.json index 3222de2d1fe0f..69924d836b9e7 100644 --- a/composer.json +++ b/composer.json @@ -108,6 +108,7 @@ "magento/module-backend": "*", "magento/module-backup": "*", "magento/module-braintree": "*", + "magento/module-braintree-graph-ql": "*", "magento/module-bundle": "*", "magento/module-bundle-graph-ql": "*", "magento/module-bundle-import-export": "*", diff --git a/composer.lock b/composer.lock index 697e7df3e19aa..172866d59f4ff 100644 --- a/composer.lock +++ b/composer.lock @@ -1,10 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "3f58ddc5609e6a934ee3706006357646", + "content-hash": "bee21a1a1fa89a8a2dc5aedcd087d1e7", "packages": [ { "name": "braintree/braintree_php", diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php new file mode 100644 index 0000000000000..cd16123d428b0 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php @@ -0,0 +1,335 @@ +quoteResource = $objectManager->create(QuoteResource::class); + $this->quote = $objectManager->create(Quote::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->config = $objectManager->get(Config::class); + $this->cacheList = $objectManager->get(TypeListInterface::class); + + foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { + $this->config->saveConfig( + 'payment/' . $offlineMethodCode . '/active', + '1', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + } + $this->cacheList->cleanType('config'); + } + + /** + * @inheritdoc + */ + protected function tearDown() + { + foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { + //Never no disable checkmo method + if ($offlineMethodCode === Checkmo::PAYMENT_METHOD_CHECKMO_CODE) { + continue; + } + $this->config->saveConfig( + 'payment/' . $offlineMethodCode . '/active', + '0', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + } + $this->cacheList->cleanType('config'); + } + + /** + * @param string $methodCode + * @dataProvider dataProviderOfflinePaymentMethods + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodOnCart(string $methodCode) + { + /** @var \Magento\Config\Model\ResourceModel\Config $config */ + $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); + $config->saveConfig( + 'payment/' . $methodCode . '/active', + 1, + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode + ); + + $response = $this->sendRequestWithToken($query); + + $this->assertArrayHasKey('setPaymentMethodOnCart', $response); + $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); + $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['method']); + } + + public function dataProviderOfflinePaymentMethods(): array + { + $methods = []; + foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { + //Purchase order requires additional input and is tested separately + if ($offlineMethodCode === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE) { + continue; + } + $methods[] = [$offlineMethodCode]; + } + + return $methods; + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetNonExistingPaymentMethod() + { + $paymentMethod = 'noway'; + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $paymentMethod + ); + + $this->expectExceptionMessage('The requested Payment Method is not available.'); + $this->sendRequestWithToken($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodByGuestToCustomerCart() + { + $paymentMethod = 'checkmo'; + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $paymentMethod + ); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodPurchaseOrderOnCart() + { + $methodCode = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; + $poNumber = 'GQL-19002'; + + /** @var \Magento\Config\Model\ResourceModel\Config $config */ + $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); + $config->saveConfig( + 'payment/' . $methodCode . '/active', + 1, + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = <<sendRequestWithToken($query); + + $this->assertArrayHasKey('setPaymentMethodOnCart', $response); + $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); + $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['method']); + $this->assertEquals($poNumber, $response['setPaymentMethodOnCart']['cart']['payment_method']['po_number']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testPurchaseOrderPaymentMethodFailingValidation() + { + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE + ); + + $this->expectExceptionMessage('Purchase order number is a required field.'); + $this->sendRequestWithToken($query); + } + + /** + * Generates query for setting the specified shipping method on cart + * + * @param string $maskedQuoteId + * @param string $methodCode + * @return string + */ + private function prepareMutationQuery( + string $maskedQuoteId, + string $methodCode + ) : string { + return <<customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + + return $this->graphQlQuery($query, [], '', $headerMap); + } +} From 8b09a6ff2e3b681cb92af8558ebbfdf6718bc576 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 19 Feb 2019 15:41:29 -0600 Subject: [PATCH 02/15] MAGETWO-98328: Update FedEx Shipping Dates behavior in Tracking Popup --- .../DataProviders/Tracking/ChangeTitle.php | 32 +++++++++++ .../Block/Tracking/PopupDeliveryDate.php | 55 +++++++++++++++++++ app/code/Magento/Fedex/etc/di.xml | 6 ++ .../Tracking/DeliveryDateTitle.php | 25 +++++++++ .../layout/shipping_tracking_popup.xml | 6 +- .../frontend/templates/tracking/details.phtml | 2 +- 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php create mode 100644 app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php create mode 100644 app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php diff --git a/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php b/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php new file mode 100644 index 0000000000000..d148b91aa3e7a --- /dev/null +++ b/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php @@ -0,0 +1,32 @@ +getCarrier() === Carrier::CODE) { + $result = __('Expected Delivery:'); + } + return $result; + } +} diff --git a/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php b/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php new file mode 100644 index 0000000000000..942d21dfa5e47 --- /dev/null +++ b/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php @@ -0,0 +1,55 @@ +getCarrier($subject) === Carrier::CODE) { + $result = $subject->formatDeliveryDate($date); + } + return $result; + } + + /** + * Retrieve carrier name from tracking info + * + * @param Popup $subject + * @return string + */ + private function getCarrier(Popup $subject): string + { + foreach ($subject->getTrackingInfo() as $trackingData) { + foreach ($trackingData as $trackingInfo) { + if ($trackingInfo instanceof Status) { + $carrier = $trackingInfo->getCarrier(); + return $carrier; + } + } + } + return ''; + } +} diff --git a/app/code/Magento/Fedex/etc/di.xml b/app/code/Magento/Fedex/etc/di.xml index f17f8f2afe663..c542b1f04d1eb 100644 --- a/app/code/Magento/Fedex/etc/di.xml +++ b/app/code/Magento/Fedex/etc/di.xml @@ -22,4 +22,10 @@ + + + + + + diff --git a/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php b/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php new file mode 100644 index 0000000000000..dc66c4f0bd018 --- /dev/null +++ b/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php @@ -0,0 +1,25 @@ +getCarrier() ? __('Delivered on:') : ''; + } +} diff --git a/app/code/Magento/Shipping/view/frontend/layout/shipping_tracking_popup.xml b/app/code/Magento/Shipping/view/frontend/layout/shipping_tracking_popup.xml index 1f5b0ae4630ad..67d03da2599bf 100644 --- a/app/code/Magento/Shipping/view/frontend/layout/shipping_tracking_popup.xml +++ b/app/code/Magento/Shipping/view/frontend/layout/shipping_tracking_popup.xml @@ -8,7 +8,11 @@ - + + + Magento\Shipping\Block\DataProviders\Tracking\DeliveryDateTitle + + diff --git a/app/code/Magento/Shipping/view/frontend/templates/tracking/details.phtml b/app/code/Magento/Shipping/view/frontend/templates/tracking/details.phtml index 9253b47f82f5d..e8584d8f6ad51 100644 --- a/app/code/Magento/Shipping/view/frontend/templates/tracking/details.phtml +++ b/app/code/Magento/Shipping/view/frontend/templates/tracking/details.phtml @@ -77,7 +77,7 @@ $number = is_object($track) ? $track->getTracking() : $track['number']; getDeliverydate()): ?> - escapeHtml(__('Delivered on:')) ?> + escapeHtml($parentBlock->getDeliveryDateTitle()->getTitle($track)) ?> formatDeliveryDateTime($track->getDeliverydate(), $track->getDeliverytime()) ?> From 01bfe90b33206d352b14a80a2a2bdafafd90a7f9 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 20 Feb 2019 10:48:15 -0600 Subject: [PATCH 03/15] MAGETWO-98328: Update FedEx Shipping Dates behavior in Tracking Popup --- .../Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php b/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php index d148b91aa3e7a..86a576f2db650 100644 --- a/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php +++ b/app/code/Magento/Fedex/Plugin/Block/DataProviders/Tracking/ChangeTitle.php @@ -16,13 +16,15 @@ class ChangeTitle { /** + * Title modification in case if FedEx used as carrier + * * @param Subject $subject * @param \Magento\Framework\Phrase|string $result * @param Status $trackingStatus * @return \Magento\Framework\Phrase|string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterGetTitle(Subject $subject, $result, Status $trackingStatus): string + public function afterGetTitle(Subject $subject, $result, Status $trackingStatus) { if ($trackingStatus->getCarrier() === Carrier::CODE) { $result = __('Expected Delivery:'); From 0a56988e31ba69f2fb48500f98ee393fce0bf8d7 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 20 Feb 2019 11:07:31 -0600 Subject: [PATCH 04/15] MAGETWO-98328: Update FedEx Shipping Dates behavior in Tracking Popup --- .../Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php b/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php index dc66c4f0bd018..ec1ee277a5a51 100644 --- a/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php +++ b/app/code/Magento/Shipping/Block/DataProviders/Tracking/DeliveryDateTitle.php @@ -15,6 +15,8 @@ class DeliveryDateTitle implements ArgumentInterface { /** + * Returns Title in case if carrier defined + * * @param Status $trackingStatus * @return \Magento\Framework\Phrase|string */ From ecf6ddd231dff70ad5e2011b71ef9f8a2d3c0e94 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Wed, 20 Feb 2019 13:21:14 -0600 Subject: [PATCH 05/15] MAGETWO-98328: Update FedEx Shipping Dates behavior in Tracking Popup --- .../Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php b/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php index 942d21dfa5e47..e1597707f9d02 100644 --- a/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php +++ b/app/code/Magento/Fedex/Plugin/Block/Tracking/PopupDeliveryDate.php @@ -3,7 +3,6 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -declare(strict_types=1); namespace Magento\Fedex\Plugin\Block\Tracking; @@ -26,7 +25,7 @@ class PopupDeliveryDate * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function afterFormatDeliveryDateTime(Popup $subject, string $result, string $date, string $time): string + public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time) { if ($this->getCarrier($subject) === Carrier::CODE) { $result = $subject->formatDeliveryDate($date); From f4f62ea72dcd73fc8100ae31b76d46c6d3b7a11d Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Fri, 22 Feb 2019 16:48:38 -0600 Subject: [PATCH 06/15] GraphQL-293: [Payment methods] Set Payment Method on Cart --- app/code/Magento/BraintreeGraphQl/README.md | 4 -- .../Magento/BraintreeGraphQl/composer.json | 24 ------- .../BraintreeGraphQl/etc/graphql/di.xml | 27 -------- .../Magento/BraintreeGraphQl/etc/module.xml | 10 --- .../BraintreeGraphQl/etc/schema.graphqls | 18 ------ .../Magento/BraintreeGraphQl/registration.php | 10 --- .../AdditionalDataBuilderInterface.php | 13 ---- .../Payment/AdditionalDataBuilderPool.php | 32 ---------- .../Payment/DefaultAdditionalDataBuilder.php | 43 ------------- .../Model/Cart/Payment/MethodBuilder.php | 62 ------------------- .../Cart/Payment/PaymentDataProvider.php | 35 ----------- .../Model/Resolver/CartPaymentMethod.php | 58 ----------------- .../Model/Resolver/SelectedPaymentMethod.php | 42 +++++++++++++ .../Model/Resolver/SetPaymentMethodOnCart.php | 49 +++++++++------ .../Resolver/SetShippingAddressesOnCart.php | 9 ++- .../Magento/QuoteGraphQl/etc/schema.graphqls | 30 +++++---- .../Quote/SetPaymentMethodOnCartTest.php | 12 ++-- 17 files changed, 103 insertions(+), 375 deletions(-) delete mode 100644 app/code/Magento/BraintreeGraphQl/README.md delete mode 100644 app/code/Magento/BraintreeGraphQl/composer.json delete mode 100644 app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml delete mode 100644 app/code/Magento/BraintreeGraphQl/etc/module.xml delete mode 100644 app/code/Magento/BraintreeGraphQl/etc/schema.graphqls delete mode 100644 app/code/Magento/BraintreeGraphQl/registration.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderInterface.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderPool.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php diff --git a/app/code/Magento/BraintreeGraphQl/README.md b/app/code/Magento/BraintreeGraphQl/README.md deleted file mode 100644 index f6740e4d250e9..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# BraintreeGraphQl - -**BraintreeGraphQl** provides type and resolver for method additional -information. \ No newline at end of file diff --git a/app/code/Magento/BraintreeGraphQl/composer.json b/app/code/Magento/BraintreeGraphQl/composer.json deleted file mode 100644 index a322db9d257dc..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/composer.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "magento/module-braintree-graph-ql", - "description": "N/A", - "type": "magento2-module", - "require": { - "php": "~7.1.3||~7.2.0", - "magento/framework": "*" - }, - "suggest": { - "magento/module-graph-ql": "*" - }, - "license": [ - "OSL-3.0", - "AFL-3.0" - ], - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Magento\\BraintreeGraphQl\\": "" - } - } -} diff --git a/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml b/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml deleted file mode 100644 index 3788a0c2f1325..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - BraintreeAdditionalDataBuilder - BraintreeVaultAdditionalDataBuilder - - - - - - Magento\Braintree\Model\Ui\ConfigProvider::CODE - - - - - Magento\Braintree\Model\Ui\ConfigProvider::CC_VAULT_CODE - - - diff --git a/app/code/Magento/BraintreeGraphQl/etc/module.xml b/app/code/Magento/BraintreeGraphQl/etc/module.xml deleted file mode 100644 index 2133e95a69104..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/etc/module.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - diff --git a/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls b/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls deleted file mode 100644 index 83c98a68d85ee..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/etc/schema.graphqls +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright © Magento, Inc. All rights reserved. -# See COPYING.txt for license details. - -input PaymentMethodInput { - braintree: BraintreeInput - braintree_vault: BraintreeVaultInput -} - -input BraintreeInput { - payment_method_nonce: String! - is_active_payment_token_enabler: Boolean! -} - -input BraintreeVaultInput { - payment_method_nonce: String! - public_hash: String! - is_active_payment_token_enabler: Boolean! -} diff --git a/app/code/Magento/BraintreeGraphQl/registration.php b/app/code/Magento/BraintreeGraphQl/registration.php deleted file mode 100644 index 37f7ef30864cb..0000000000000 --- a/app/code/Magento/BraintreeGraphQl/registration.php +++ /dev/null @@ -1,10 +0,0 @@ -builders = $builders; - } - - public function buildForMethod(string $methodCode, array $args): array - { - $additionalData = []; - if (isset($this->builders[$methodCode])) { - $additionalData = $this->builders[$methodCode]->build($args); - } - - return $additionalData; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php deleted file mode 100644 index 5a032ccc6386d..0000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.php +++ /dev/null @@ -1,43 +0,0 @@ -arrayManager = $arrayManager; - $this->methodCode = $methodCode; - } - - public function build(array $args): array - { - return $this->arrayManager->get($this->getAdditionalDataPath(), $args) ?? []; - } - - private function getAdditionalDataPath(): string - { - return sprintf(static::INPUT_PATH_ADDITIONAL_DATA, $this->methodCode); - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php deleted file mode 100644 index 110e24d7d0fe5..0000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php +++ /dev/null @@ -1,62 +0,0 @@ -paymentFactory = $paymentFactory; - $this->additionalDataBuilderPool = $additionalDataBuilderPool; - $this->arrayManager = $arrayManager; - } - - public function build(array $args): PaymentInterface - { - $method = (string) $this->arrayManager->get('input/payment_method/method', $args); - - return $this->paymentFactory->create([ - 'data' => [ - PaymentInterface::KEY_METHOD => $method, - PaymentInterface::KEY_PO_NUMBER => $this->arrayManager->get('input/payment_method/po_number', $args), - PaymentInterface::KEY_ADDITIONAL_DATA => $this->additionalDataBuilderPool->buildForMethod( - $method, - $args - ), - ] - ]); - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php deleted file mode 100644 index 22adc69d1cc58..0000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.php +++ /dev/null @@ -1,35 +0,0 @@ -getPayment(); - if (!$payment) { - return []; - } - - return [ - 'method' => $payment->getMethod(), - 'po_number' => $payment->getPoNumber(), - ]; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php deleted file mode 100644 index 9e75256cd9b2b..0000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php +++ /dev/null @@ -1,58 +0,0 @@ -paymentDataProvider = $paymentDataProvider; - $this->getCartForUser = $getCartForUser; - } - - /** - * @inheritdoc - */ - public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) - { - if (!isset($value['cart_id'])) { - throw new LocalizedException(__('"model" value should be specified')); - } - - $maskedCartId = $value['cart_id']; - $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); - - return $this->paymentDataProvider->getCartPayment($cart); - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php new file mode 100644 index 0000000000000..5d6aed15821f3 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php @@ -0,0 +1,42 @@ +getPayment(); + if (!$payment) { + return []; + } + + return [ + 'code' => $payment->getMethod(), + 'po_number' => $payment->getPoNumber(), + ]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php index 5e48b3413f7ce..82709de03b9b6 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php @@ -13,21 +13,16 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\Stdlib\ArrayManager; -use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; +use Magento\Quote\Api\Data\PaymentInterface; use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; +use Magento\Quote\Api\Data\PaymentInterfaceFactory; use Magento\Quote\Api\PaymentMethodManagementInterface; -use Magento\QuoteGraphQl\Model\Cart\Payment\MethodBuilder; /** * Mutation resolver for setting payment method for shopping cart */ class SetPaymentMethodOnCart implements ResolverInterface { - /** - * @var MaskedQuoteIdToQuoteIdInterface - */ - private $maskedQuoteIdToQuoteId; - /** * @var GetCartForUser */ @@ -44,29 +39,26 @@ class SetPaymentMethodOnCart implements ResolverInterface private $paymentMethodManagement; /** - * @var MethodBuilder + * @var PaymentInterfaceFactory */ - private $methodBuilder; + private $paymentFactory; /** - * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId * @param GetCartForUser $getCartForUser * @param ArrayManager $arrayManager * @param PaymentMethodManagementInterface $paymentMethodManagement - * @param MethodBuilder $methodBuilder + * @param PaymentInterfaceFactory $paymentFactory */ public function __construct( - MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, GetCartForUser $getCartForUser, ArrayManager $arrayManager, PaymentMethodManagementInterface $paymentMethodManagement, - MethodBuilder $methodBuilder + PaymentInterfaceFactory $paymentFactory ) { - $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; $this->getCartForUser = $getCartForUser; $this->arrayManager = $arrayManager; $this->paymentMethodManagement = $paymentMethodManagement; - $this->methodBuilder = $methodBuilder; + $this->paymentFactory = $paymentFactory; } /** @@ -74,21 +66,37 @@ public function __construct( */ public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { - $paymentMethod = $this->arrayManager->get('input/payment_method', $args); - $maskedCartId = $this->arrayManager->get('input/cart_id', $args); - + $maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args); if (!$maskedCartId) { throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); } + + $paymentMethod = $this->arrayManager->get('input/payment_method', $args); if (!$paymentMethod) { throw new GraphQlInputException(__('Required parameter "payment_method" is missing')); } - $maskedCartId = $args['input']['cart_id']; + $paymentMethodCode = (string) $this->arrayManager->get('input/payment_method/code', $args); + if (!$paymentMethodCode) { + throw new GraphQlInputException(__('Required parameter payment "code" is missing')); + } + + $poNumber = $this->arrayManager->get('input/payment_method/po_number', $args); + if (!$poNumber) { + throw new GraphQlInputException(__('Required parameter payment "po_number" is missing')); + } + $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); + $payment = $this->paymentFactory->create([ + 'data' => [ + PaymentInterface::KEY_METHOD => $paymentMethodCode, + PaymentInterface::KEY_PO_NUMBER => $poNumber, + PaymentInterface::KEY_ADDITIONAL_DATA => [], + ] + ]); try { - $this->paymentMethodManagement->set($cart->getId(), $this->methodBuilder->build($args)); + $this->paymentMethodManagement->set($cart->getId(), $payment); } catch (LocalizedException $e) { throw new GraphQlInputException(__($e->getMessage())); } @@ -96,6 +104,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value return [ 'cart' => [ 'cart_id' => $maskedCartId, + 'model' => $cart, ], ]; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php index ec50bd6ab6ea4..a55e2971e0ef7 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php @@ -7,6 +7,7 @@ namespace Magento\QuoteGraphQl\Model\Resolver; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -67,7 +68,7 @@ public function __construct( public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { $shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args); - $maskedCartId = (string) $this->arrayManager->get('input/cart_id', $args); + $maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args); if (!$maskedCartId) { throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); @@ -79,7 +80,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); - $this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses); + try { + $this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } return [ 'cart' => [ diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 2a83b2969d73d..78c12f939d8fb 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -109,8 +109,12 @@ input SetPaymentMethodOnCartInput { } input PaymentMethodInput { - method: String! - po_number: String + code: String! @doc(description:"Payment method code") + po_number: String! @doc(description:"Purchase order number") + additional_data: PaymentMethodAdditionalDataInput +} + +input PaymentMethodAdditionalDataInput { } type SetPaymentMethodOnCartOutput { @@ -139,8 +143,8 @@ type Cart { applied_coupon: AppliedCoupon shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses") billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress") - available_payment_methods : [PaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods") - payment_method: CartPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPaymentMethod") + available_payment_methods : [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods") + selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod") } type CartAddress { @@ -154,18 +158,13 @@ type CartAddress { country: CartAddressCountry telephone: String address_type: AdressTypeEnum - selected_shipping_method: SelectedShippingMethod available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAdress\\AvailableShippingMethods") + selected_shipping_method: SelectedShippingMethod items_weight: Float customer_notes: String cart_items: [CartItemQuantity] } -type CartPaymentMethod { - method: String - po_number: String -} - type CartItemQuantity { cart_item_id: String! quantity: Float! @@ -197,11 +196,20 @@ type AvailableShippingMethod { price_incl_tax: Float! } -type PaymentMethod { +type AvailablePaymentMethod { code: String @doc(description: "The payment method code") title: String @doc(description: "The payment method title.") } +type SelectedPaymentMethod { + code: String @doc(description: "The payment method code") + po_number: String @doc(description: "The purchase order number.") + additional_data: SelectedPaymentMethodAdditionalData +} + +type SelectedPaymentMethodAdditionalData { +} + enum AdressTypeEnum { SHIPPING BILLING diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php index cd16123d428b0..496c3efbbd898 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php @@ -142,7 +142,7 @@ public function testSetPaymentMethodOnCart(string $methodCode) $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); - $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['method']); + $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['code']); } public function dataProviderOfflinePaymentMethods(): array @@ -236,7 +236,7 @@ public function testSetPaymentMethodPurchaseOrderOnCart() { cart_id: "$maskedQuoteId", payment_method: { - method: "$methodCode" + code: "$methodCode" po_number: "$poNumber" } }) { @@ -244,7 +244,7 @@ public function testSetPaymentMethodPurchaseOrderOnCart() cart { cart_id, payment_method { - method + code po_number } } @@ -259,7 +259,7 @@ public function testSetPaymentMethodPurchaseOrderOnCart() $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); - $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['method']); + $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['code']); $this->assertEquals($poNumber, $response['setPaymentMethodOnCart']['cart']['payment_method']['po_number']); } @@ -301,14 +301,14 @@ private function prepareMutationQuery( { cart_id: "$maskedQuoteId", payment_method: { - method: "$methodCode" + code: "$methodCode" } }) { cart { cart_id, payment_method { - method + code } } } From 7871f4c973102ef12af1748ff296bc4859aa0049 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Fri, 22 Feb 2019 20:26:26 -0500 Subject: [PATCH 07/15] Save country when creating new quote addresses Fixes #389 --- .../Model/Cart/SetBillingAddressOnCart.php | 1 + .../Model/Cart/SetShippingAddressOnCart.php | 1 + .../Quote/SetBillingAddressOnCartTest.php | 19 ++++++++++++++++++- .../Quote/SetShippingAddressOnCartTest.php | 11 ++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index 5d3125ae13ef8..02aec5b6fbaf0 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -93,6 +93,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b ); } if (null === $customerAddressId) { + $addressInput['country_id'] = $addressInput['country_code'] ?? ''; $billingAddress = $this->addressModel->addData($addressInput); } else { $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType()); diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php index e6b18fc88a27a..fc8eff4cfc13a 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressOnCart.php @@ -87,6 +87,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s ); } if (null === $customerAddressId) { + $addressInput['country_id'] = $addressInput['country_code'] ?? ''; $shippingAddress = $this->addressModel->addData($addressInput); } else { $this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType()); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php index bfe57109d107d..754b157a16107 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php @@ -88,6 +88,10 @@ public function testSetNewBillingAddressByGuest() city postcode telephone + country { + code + label + } } } } @@ -140,6 +144,10 @@ public function testSetNewBillingAddressWithUseForShippingParameterByGuest() city postcode telephone + country { + code + label + } } shipping_addresses { firstname @@ -149,6 +157,10 @@ public function testSetNewBillingAddressWithUseForShippingParameterByGuest() city postcode telephone + country { + code + label + } } } } @@ -234,6 +246,10 @@ public function testSetNewBillingAddressByRegisteredCustomer() city postcode telephone + country { + code + label + } } } } @@ -413,7 +429,8 @@ private function assertNewAddressFields(array $billingAddressResponse): void ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']], ['response_field' => 'city', 'expected_value' => 'test city'], ['response_field' => 'postcode', 'expected_value' => '887766'], - ['response_field' => 'telephone', 'expected_value' => '88776655'] + ['response_field' => 'telephone', 'expected_value' => '88776655'], + ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ]; $this->assertResponseFields($billingAddressResponse, $assertionMap); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php index c3685c88ae487..4d7ab85cd475a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php @@ -90,6 +90,10 @@ public function testSetNewShippingAddressByGuest() city postcode telephone + country { + code + label + } } } } @@ -176,6 +180,10 @@ public function testSetNewShippingAddressByRegisteredCustomer() city postcode telephone + country { + label + code + } } } } @@ -462,7 +470,8 @@ private function assertNewShippingAddressFields(array $shippingAddressResponse): ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']], ['response_field' => 'city', 'expected_value' => 'test city'], ['response_field' => 'postcode', 'expected_value' => '887766'], - ['response_field' => 'telephone', 'expected_value' => '88776655'] + ['response_field' => 'telephone', 'expected_value' => '88776655'], + ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ]; $this->assertResponseFields($shippingAddressResponse, $assertionMap); From 5c3cb172328d6f36348e334ee87ecd8a1e69c1db Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Fri, 22 Feb 2019 22:30:48 -0500 Subject: [PATCH 08/15] Add cart address id to shipping address This is required for setting quote address shipping methods --- .../Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php | 1 + app/code/Magento/QuoteGraphQl/etc/schema.graphqls | 1 + 2 files changed, 2 insertions(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php index b0e5070315d87..89aa943f9d211 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php @@ -41,6 +41,7 @@ public function execute(QuoteAddress $address): array $addressData['model'] = $address; $addressData = array_merge($addressData, [ + 'address_id' => $address->getId(), 'country' => [ 'code' => $address->getCountryId(), 'label' => $address->getCountry() diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 2050a2d3ca790..a9a609b01f8c0 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -128,6 +128,7 @@ type Cart { } type CartAddress { + address_id: Int firstname: String lastname: String company: String From 9c4241e3fae07242bfdc1e554c9cb54a11f9ff28 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Fri, 22 Feb 2019 22:31:54 -0500 Subject: [PATCH 09/15] Correct validation of shipping method input in resolver Correlates to the schema changes made: https://github.com/magento/graphql-ce/commit/fa5613bb8ef5d85fdb75a70854f0caf69e58c4fd --- .../Model/Resolver/SetShippingMethodsOnCart.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php index 920829f5d67b1..67947e928796c 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php @@ -72,10 +72,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value if (!$shippingMethod['cart_address_id']) { throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing')); } - if (!$shippingMethod['shipping_carrier_code']) { + if (!$shippingMethod['carrier_code']) { throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing')); } - if (!$shippingMethod['shipping_method_code']) { + if (!$shippingMethod['method_code']) { throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing')); } @@ -85,8 +85,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $this->setShippingMethodOnCart->execute( $cart, $shippingMethod['cart_address_id'], - $shippingMethod['shipping_carrier_code'], - $shippingMethod['shipping_method_code'] + $shippingMethod['carrier_code'], + $shippingMethod['method_code'] ); return [ From 495c540c55b760a61746835139c14ab51f843c51 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 26 Feb 2019 12:18:47 -0600 Subject: [PATCH 10/15] GraphQL-293: [Payment methods] Set Payment Method on Cart --- .../QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php | 2 +- .../QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php | 5 +---- app/code/Magento/QuoteGraphQl/etc/schema.graphqls | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php index 5d6aed15821f3..7a99b04638ac3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.php @@ -36,7 +36,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value return [ 'code' => $payment->getMethod(), - 'po_number' => $payment->getPoNumber(), + 'purchase_order_number' => $payment->getPoNumber(), ]; } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php index 82709de03b9b6..78a841a9cb614 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php @@ -81,10 +81,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value throw new GraphQlInputException(__('Required parameter payment "code" is missing')); } - $poNumber = $this->arrayManager->get('input/payment_method/po_number', $args); - if (!$poNumber) { - throw new GraphQlInputException(__('Required parameter payment "po_number" is missing')); - } + $poNumber = $this->arrayManager->get('input/payment_method/purchase_order_number', $args); $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); $payment = $this->paymentFactory->create([ diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 78c12f939d8fb..41d67020fcf29 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -110,7 +110,7 @@ input SetPaymentMethodOnCartInput { input PaymentMethodInput { code: String! @doc(description:"Payment method code") - po_number: String! @doc(description:"Purchase order number") + purchase_order_number: String @doc(description:"Purchase order number") additional_data: PaymentMethodAdditionalDataInput } @@ -203,7 +203,7 @@ type AvailablePaymentMethod { type SelectedPaymentMethod { code: String @doc(description: "The payment method code") - po_number: String @doc(description: "The purchase order number.") + purchase_order_number: String @doc(description: "The purchase order number.") additional_data: SelectedPaymentMethodAdditionalData } From 287186d61baddc38a64c140e0ee3b20a404e7aaf Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 26 Feb 2019 12:44:27 -0600 Subject: [PATCH 11/15] GraphQL-293: [Payment methods] Set Payment Method on Cart --- .../Quote/SetPaymentMethodOnCartTest.php | 245 +++--------------- 1 file changed, 36 insertions(+), 209 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php index 496c3efbbd898..89e88bd0ce04a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php @@ -7,33 +7,19 @@ namespace Magento\GraphQl\Quote; -use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Integration\Api\CustomerTokenServiceInterface; -use Magento\OfflinePayments\Model\Banktransfer; -use Magento\OfflinePayments\Model\Cashondelivery; use Magento\OfflinePayments\Model\Checkmo; -use Magento\OfflinePayments\Model\Purchaseorder; -use Magento\Quote\Model\Quote; +use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; -use Magento\TestFramework\ObjectManager; use Magento\TestFramework\TestCase\GraphQlAbstract; -use Magento\Framework\App\Cache\TypeListInterface; -use Magento\Config\Model\ResourceModel\Config; /** * Test for setting payment methods on cart */ class SetPaymentMethodOnCartTest extends GraphQlAbstract { - private const OFFLINE_METHOD_CODES = [ - Checkmo::PAYMENT_METHOD_CHECKMO_CODE, - Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE, - Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE, - Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE, - ]; - /** * @var CustomerTokenServiceInterface */ @@ -45,139 +31,56 @@ class SetPaymentMethodOnCartTest extends GraphQlAbstract private $quoteResource; /** - * @var Quote + * @var QuoteFactory */ - private $quote; + private $quoteFactory; /** * @var QuoteIdToMaskedQuoteIdInterface */ private $quoteIdToMaskedId; - /** - * @var Config - */ - private $config; - - /** - * @var TypeListInterface - */ - private $cacheList; - /** * @inheritdoc */ protected function setUp() { $objectManager = Bootstrap::getObjectManager(); - $this->quoteResource = $objectManager->create(QuoteResource::class); - $this->quote = $objectManager->create(Quote::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->config = $objectManager->get(Config::class); - $this->cacheList = $objectManager->get(TypeListInterface::class); - - foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { - $this->config->saveConfig( - 'payment/' . $offlineMethodCode . '/active', - '1', - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - } - $this->cacheList->cleanType('config'); } /** - * @inheritdoc - */ - protected function tearDown() - { - foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { - //Never no disable checkmo method - if ($offlineMethodCode === Checkmo::PAYMENT_METHOD_CHECKMO_CODE) { - continue; - } - $this->config->saveConfig( - 'payment/' . $offlineMethodCode . '/active', - '0', - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - } - $this->cacheList->cleanType('config'); - } - - /** - * @param string $methodCode - * @dataProvider dataProviderOfflinePaymentMethods * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php */ - public function testSetPaymentMethodOnCart(string $methodCode) + public function testSetPaymentMethodOnCart() { - /** @var \Magento\Config\Model\ResourceModel\Config $config */ - $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); - $config->saveConfig( - 'payment/' . $methodCode . '/active', - 1, - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode - ); + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); $response = $this->sendRequestWithToken($query); - $this->assertArrayHasKey('setPaymentMethodOnCart', $response); - $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); - $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); - $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); - $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['code']); - } - - public function dataProviderOfflinePaymentMethods(): array - { - $methods = []; - foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { - //Purchase order requires additional input and is tested separately - if ($offlineMethodCode === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE) { - continue; - } - $methods[] = [$offlineMethodCode]; - } - - return $methods; + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @expectedException \Exception + * @expectedExceptionMessage The requested Payment Method is not available. */ public function testSetNonExistingPaymentMethod() { - $paymentMethod = 'noway'; - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $paymentMethod - ); + $methodCode = 'noway'; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - $this->expectExceptionMessage('The requested Payment Method is not available.'); + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); $this->sendRequestWithToken($query); } @@ -186,18 +89,10 @@ public function testSetNonExistingPaymentMethod() */ public function testSetPaymentMethodByGuestToCustomerCart() { - $paymentMethod = 'checkmo'; - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $methodCode = 'checkmo'; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $paymentMethod - ); + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); $this->expectExceptionMessage( "The current user cannot perform operations on cart \"$maskedQuoteId\"" @@ -206,84 +101,6 @@ public function testSetPaymentMethodByGuestToCustomerCart() $this->graphQlQuery($query); } - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testSetPaymentMethodPurchaseOrderOnCart() - { - $methodCode = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; - $poNumber = 'GQL-19002'; - - /** @var \Magento\Config\Model\ResourceModel\Config $config */ - $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); - $config->saveConfig( - 'payment/' . $methodCode . '/active', - 1, - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - - $query = <<sendRequestWithToken($query); - - $this->assertArrayHasKey('setPaymentMethodOnCart', $response); - $this->assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); - $this->assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); - $this->assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); - $this->assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['payment_method']['code']); - $this->assertEquals($poNumber, $response['setPaymentMethodOnCart']['cart']['payment_method']['po_number']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testPurchaseOrderPaymentMethodFailingValidation() - { - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE - ); - - $this->expectExceptionMessage('Purchase order number is a required field.'); - $this->sendRequestWithToken($query); - } - /** * Generates query for setting the specified shipping method on cart * @@ -307,13 +124,12 @@ private function prepareMutationQuery( cart { cart_id, - payment_method { + selected_payment_method { code } } } } - QUERY; } @@ -326,10 +142,21 @@ private function prepareMutationQuery( */ private function sendRequestWithToken(string $query): array { - $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; return $this->graphQlQuery($query, [], '', $headerMap); } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } } From 4cae1af66482f7f67211e3e87d47456f1db3088a Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 26 Feb 2019 17:43:46 -0600 Subject: [PATCH 12/15] GraphQL-293: [Payment methods] Set Payment Method on Cart --- .../Model/Cart/GetCartForUser.php | 10 +- .../SetPaymentMethodOnCartTest.php | 315 ++++++++++++++++++ .../GraphQl/Quote/CreateEmptyCartTest.php | 91 ----- .../Quote/Customer/CreateEmptyCartTest.php | 61 ++++ .../GetAvailablePaymentMethodsTest.php | 102 ++++++ .../Quote/{ => Customer}/GetCartTest.php | 58 ++-- .../SetBillingAddressOnCartTest.php | 177 ++++------ .../Customer/SetPaymentMethodOnCartTest.php | 227 +++++++++++++ .../Quote/GetAvailablePaymentMethodsTest.php | 152 --------- .../Quote/Guest/CreateEmptyCartTest.php | 47 +++ .../Guest/GetAvailablePaymentMethodsTest.php | 90 +++++ .../GraphQl/Quote/Guest/GetCartTest.php | 151 +++++++++ .../Guest/SetBillingAddressOnCartTest.php | 263 +++++++++++++++ .../SetPaymentMethodOnCartTest.php | 83 +++-- .../Quote/SetShippingAddressOnCartTest.php | 2 +- 15 files changed, 1423 insertions(+), 406 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CreateEmptyCartTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CreateEmptyCartTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php rename dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/{ => Customer}/GetCartTest.php (70%) rename dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/{ => Customer}/SetBillingAddressOnCartTest.php (82%) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailablePaymentMethodsTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CreateEmptyCartTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetCartTest.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php rename dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/{ => Guest}/SetPaymentMethodOnCartTest.php (59%) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php index c3207bf478bbe..21df2271cc7f3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php @@ -45,12 +45,12 @@ public function __construct( * Get cart for user * * @param string $cartHash - * @param int|null $userId + * @param int|null $customerId * @return Quote * @throws GraphQlAuthorizationException * @throws GraphQlNoSuchEntityException */ - public function execute(string $cartHash, ?int $userId): Quote + public function execute(string $cartHash, ?int $customerId): Quote { try { $cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash); @@ -69,14 +69,14 @@ public function execute(string $cartHash, ?int $userId): Quote ); } - $customerId = (int)$cart->getCustomerId(); + $cartCustomerId = (int)$cart->getCustomerId(); /* Guest cart, allow operations */ - if (!$customerId) { + if (!$cartCustomerId && null === $customerId) { return $cart; } - if ($customerId !== $userId) { + if ($cartCustomerId !== $customerId) { throw new GraphQlAuthorizationException( __( 'The current user cannot perform operations on cart "%masked_cart_id"', diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php new file mode 100644 index 0000000000000..3a3e50efa0676 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php @@ -0,0 +1,315 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->config = $objectManager->get(Config::class); + $this->cacheList = $objectManager->get(TypeListInterface::class); + + foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { + $this->config->saveConfig( + 'payment/' . $offlineMethodCode . '/active', + '1', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + } + $this->cacheList->cleanType('config'); + } + + /** + * @inheritdoc + */ + protected function tearDown() + { +// foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { +// //Never no disable checkmo method +// if ($offlineMethodCode === Checkmo::PAYMENT_METHOD_CHECKMO_CODE) { +// continue; +// } +// $this->config->saveConfig( +// 'payment/' . $offlineMethodCode . '/active', +// '0', +// ScopeConfigInterface::SCOPE_TYPE_DEFAULT, +// 0 +// ); +// } +// $this->cacheList->cleanType('config'); + } + + /** + * @param string $methodCode + * @dataProvider dataProviderOfflinePaymentMethods + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodOnCart(string $methodCode) + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $methodCode + ); + + $response = $this->sendRequestWithToken($query); + + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); + } + + public function dataProviderOfflinePaymentMethods(): array + { + $methods = []; + foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { + //Purchase order requires additional input and is tested separately + if ($offlineMethodCode === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE) { + continue; + } + $methods[] = [$offlineMethodCode]; + } + + return $methods; + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetNonExistingPaymentMethod() + { + $paymentMethod = 'noway'; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $paymentMethod + ); + + $this->expectExceptionMessage('The requested Payment Method is not available.'); + $this->sendRequestWithToken($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodByGuestToCustomerCart() + { + $paymentMethod = 'checkmo'; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + $paymentMethod + ); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodPurchaseOrderOnCart() + { + $methodCode = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; + $poNumber = 'GQL-19002'; + + /** @var \Magento\Config\Model\ResourceModel\Config $config */ + $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); + $config->saveConfig( + 'payment/' . $methodCode . '/active', + 1, + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + 0 + ); + + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = <<sendRequestWithToken($query); + + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); + self::assertEquals( + $poNumber, + $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number'] + ); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testPurchaseOrderPaymentMethodFailingValidation() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery( + $maskedQuoteId, + Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE + ); + + $this->expectExceptionMessage('Purchase order number is a required field.'); + $this->sendRequestWithToken($query); + } + + /** + * Generates query for setting the specified shipping method on cart + * + * @param string $maskedQuoteId + * @param string $methodCode + * @return string + */ + private function prepareMutationQuery( + string $maskedQuoteId, + string $methodCode + ) : string { + return <<customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + + return $this->graphQlQuery($query, [], '', $headerMap); + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CreateEmptyCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CreateEmptyCartTest.php deleted file mode 100644 index 6e819b523ec82..0000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CreateEmptyCartTest.php +++ /dev/null @@ -1,91 +0,0 @@ -objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - $this->quoteIdMask = $this->objectManager->create(QuoteIdMask::class); - $this->guestCartRepository = $this->objectManager->create(GuestCartRepositoryInterface::class); - } - - public function testCreateEmptyCartForGuest() - { - $query = <<graphQlQuery($query); - - self::assertArrayHasKey('createEmptyCart', $response); - - $maskedCartId = $response['createEmptyCart']; - /** @var CartInterface $guestCart */ - $guestCart = $this->guestCartRepository->get($maskedCartId); - - self::assertNotNull($guestCart->getId()); - self::assertNull($guestCart->getCustomer()->getId()); - } - - /** - * @magentoApiDataFixture Magento/Customer/_files/customer.php - */ - public function testCreateEmptyCartForRegisteredCustomer() - { - $query = <<objectManager->create( - \Magento\Integration\Api\CustomerTokenServiceInterface::class - ); - $customerToken = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); - $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - - $response = $this->graphQlQuery($query, [], '', $headerMap); - - self::assertArrayHasKey('createEmptyCart', $response); - - $maskedCartId = $response['createEmptyCart']; - /* guestCartRepository is used for registered customer to get the cart hash */ - $guestCart = $this->guestCartRepository->get($maskedCartId); - - self::assertNotNull($guestCart->getId()); - self::assertEquals(1, $guestCart->getCustomer()->getId()); - } -} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CreateEmptyCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CreateEmptyCartTest.php new file mode 100644 index 0000000000000..0cb8a38b0cb5e --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CreateEmptyCartTest.php @@ -0,0 +1,61 @@ +guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testCreateEmptyCart() + { + $query = <<customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + + $response = $this->graphQlQuery($query, [], '', $headerMap); + + self::assertArrayHasKey('createEmptyCart', $response); + + $maskedCartId = $response['createEmptyCart']; + $guestCart = $this->guestCartRepository->get($maskedCartId); + + self::assertNotNull($guestCart->getId()); + self::assertEquals(1, $guestCart->getCustomer()->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php new file mode 100644 index 0000000000000..b5323ee986582 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php @@ -0,0 +1,102 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testGetCartWithPaymentMethods() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items'); + + $query = <<graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response); + self::assertCount(1, $response['cart']['available_payment_methods']); + self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); + self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); + } + + /** + * @param string $username + * @param string $password + * @return array + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetCartTest.php similarity index 70% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetCartTest.php index 6c5add7df6b0f..8c1fcce7fb550 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetCartTest.php @@ -5,7 +5,7 @@ */ declare(strict_types=1); -namespace Magento\GraphQl\Quote; +namespace Magento\GraphQl\Quote\Customer; use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\Quote\Model\QuoteFactory; @@ -49,49 +49,62 @@ protected function setUp() } /** - * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php */ - public function testGetCartForGuest() + public function testGetCart() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items'); $query = $this->getCartQuery($maskedQuoteId); - $response = $this->graphQlQuery($query); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response); self::assertEquals($maskedQuoteId, $response['cart']['cart_id']); + + self::assertArrayHasKey('items', $response['cart']); + self::assertCount(2, $response['cart']['items']); + + self::assertNotEmpty($response['cart']['items'][0]['id']); + self::assertEquals($response['cart']['items'][0]['qty'], 2); + self::assertEquals($response['cart']['items'][0]['product']['sku'], 'simple'); + + self::assertNotEmpty($response['cart']['items'][1]['id']); + self::assertEquals($response['cart']['items'][1]['qty'], 1); + self::assertEquals($response['cart']['items'][1]['product']['sku'], 'simple_one'); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php */ - public function testGetCartByRegisteredCustomer() + public function testGetGuestCart() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items'); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); $query = $this->getCartQuery($maskedQuoteId); - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - - self::assertArrayHasKey('cart', $response); - self::assertEquals($maskedQuoteId, $response['cart']['cart_id']); - self::assertNotEmpty($response['cart']['items']); + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php */ - public function testGetCartOfAnotherCustomerByGuest() + public function testGetCartIfCustomerIsNotOwnerOfCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items'); $query = $this->getCartQuery($maskedQuoteId); - self:$this->expectExceptionMessage( + $this->expectExceptionMessage( "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" ); - $this->graphQlQuery($query); + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com')); } /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php * @expectedException \Exception * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" */ @@ -100,7 +113,7 @@ public function testGetNonExistentCart() $maskedQuoteId = 'non_existent_masked_id'; $query = $this->getCartQuery($maskedQuoteId); - $this->graphQlQuery($query); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** @@ -114,15 +127,12 @@ private function getCartQuery( { cart(cart_id: "$maskedQuoteId") { cart_id - applied_coupon { - code - } items { id - } - shipping_addresses { - firstname, - lastname + qty + product { + sku + } } } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php similarity index 82% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php index bfe57109d107d..d244fcb27f021 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php @@ -5,17 +5,14 @@ */ declare(strict_types=1); -namespace Magento\GraphQl\Quote; +namespace Magento\GraphQl\Quote\Customer; -use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Integration\Api\CustomerTokenServiceInterface; -use Magento\Multishipping\Helper\Data; use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; -use Magento\TestFramework\ObjectManager; /** * Test for set billing address on cart mutation @@ -53,10 +50,11 @@ protected function setUp() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php */ - public function testSetNewBillingAddressByGuest() + public function testSetNewBillingAddress() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->assignQuoteToCustomer(); $query = <<graphQlQuery($query); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); $cartResponse = $response['setBillingAddressOnCart']['cart']; @@ -104,10 +102,11 @@ public function testSetNewBillingAddressByGuest() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php */ - public function testSetNewBillingAddressWithUseForShippingParameterByGuest() + public function testSetNewBillingAddressWithUseForShippingParameter() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->assignQuoteToCustomer(); $query = <<graphQlQuery($query); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); $cartResponse = $response['setBillingAddressOnCart']['cart']; @@ -168,12 +167,12 @@ public function testSetNewBillingAddressWithUseForShippingParameterByGuest() /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php - * @expectedException \Exception - * @expectedExceptionMessage The current customer isn't authorized. + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php */ - public function testSetBillingAddressFromAddressBookByGuest() + public function testSetBillingAddressFromAddressBook() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->assignQuoteToCustomer(); $query = <<graphQlQuery($query); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); + $cartResponse = $response['setBillingAddressOnCart']['cart']; + self::assertArrayHasKey('billing_address', $cartResponse); + $billingAddressResponse = $cartResponse['billing_address']; + $this->assertSavedBillingAddressFields($billingAddressResponse); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @expectedException \Exception + * @expectedExceptionMessage Could not find a address with ID "100" */ - public function testSetNewBillingAddressByRegisteredCustomer() + public function testSetNotExistedBillingAddressFromAddressBook() { $maskedQuoteId = $this->assignQuoteToCustomer(); @@ -210,42 +223,19 @@ public function testSetNewBillingAddressByRegisteredCustomer() input: { cart_id: "$maskedQuoteId" billing_address: { - address: { - firstname: "test firstname" - lastname: "test lastname" - company: "test company" - street: ["test street 1", "test street 2"] - city: "test city" - region: "test region" - postcode: "887766" - country_code: "US" - telephone: "88776655" - save_in_address_book: false - } - } + customer_address_id: 100 + } } ) { cart { billing_address { - firstname - lastname - company - street city - postcode - telephone } } } } QUERY; - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - - self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); - $cartResponse = $response['setBillingAddressOnCart']['cart']; - self::assertArrayHasKey('billing_address', $cartResponse); - $billingAddressResponse = $cartResponse['billing_address']; - $this->assertNewAddressFields($billingAddressResponse); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** @@ -253,7 +243,7 @@ public function testSetNewBillingAddressByRegisteredCustomer() * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php */ - public function testSetBillingAddressFromAddressBookByRegisteredCustomer() + public function testSetNewBillingAddressAndFromAddressBookAtSameTime() { $maskedQuoteId = $this->assignQuoteToCustomer(); @@ -263,42 +253,45 @@ public function testSetBillingAddressFromAddressBookByRegisteredCustomer() input: { cart_id: "$maskedQuoteId" billing_address: { - customer_address_id: 1 - } + customer_address_id: 1 + address: { + firstname: "test firstname" + lastname: "test lastname" + company: "test company" + street: ["test street 1", "test street 2"] + city: "test city" + region: "test region" + postcode: "887766" + country_code: "US" + telephone: "88776655" + save_in_address_book: false + } + } } ) { cart { billing_address { - firstname - lastname - company - street city - postcode - telephone } } } } QUERY; - $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); - self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); - $cartResponse = $response['setBillingAddressOnCart']['cart']; - self::assertArrayHasKey('billing_address', $cartResponse); - $billingAddressResponse = $cartResponse['billing_address']; - $this->assertSavedBillingAddressFields($billingAddressResponse); + self::expectExceptionMessage( + 'The billing address cannot contain "customer_address_id" and "address" at the same time.' + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * @magentoApiDataFixture Magento/Customer/_files/customer.php - * @expectedException \Exception - * @expectedExceptionMessage Could not find a address with ID "100" + * @magentoApiDataFixture Magento/Customer/_files/customer_address.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php */ - public function testSetNotExistedBillingAddressFromAddressBook() + public function testSetBillingAddressToGuestCart() { - $maskedQuoteId = $this->assignQuoteToCustomer(); + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); $query = <<expectExceptionMessage( + "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" + ); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); } /** + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php - * @magentoApiDataFixture Magento/Customer/_files/customer.php - * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php */ - public function testSetNewBillingAddressAndFromAddressBookAtSameTime() + public function testSetBillingAddressIfCustomerIsNotOwnerOfCart() { - $maskedQuoteId = $this->assignQuoteToCustomer(); + $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2); $query = <<expectExceptionMessage( + "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" ); - $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer@search.example.com')); } /** @@ -376,7 +361,7 @@ public function testSetNewBillingAddressAndFromAddressBookAtSameTime() */ public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2); $query = <<quoteResource->save($quote); return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } - - public function tearDown() - { - /** @var \Magento\Config\Model\ResourceModel\Config $config */ - $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); - - //default state of multishipping config - $config->saveConfig( - Data::XML_PATH_CHECKOUT_MULTIPLE_AVAILABLE, - 1, - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - - /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */ - $config = ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class); - $config->reinit(); - } } 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 new file mode 100644 index 0000000000000..8856b2ab44c22 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php @@ -0,0 +1,227 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ + public function testSetPaymentWithVirtualProduct() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_virtual_product'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentWithSimpleProduct() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage The shipping address is missing. Set the address and try again. + */ + public function testSetPaymentWithSimpleProductWithoutAddress() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 1); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + * @expectedException \Exception + * @expectedExceptionMessage The requested Payment Method is not available. + */ + public function testSetNonExistingPaymentMethod() + { + $methodCode = 'noway'; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testSetPaymentMethodToGuestCart() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($query, [], '', $this->getHeaderMap()); + } + + /** + * @magentoApiDataFixture Magento/Customer/_files/three_customers.php + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSetPaymentMethodIfCustomerIsNotOwnerOfCart() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + + $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com')); + } + + /** + * Generates query for setting the specified shipping method on cart + * + * @param string $maskedQuoteId + * @param string $methodCode + * @return string + */ + private function prepareMutationQuery( + string $maskedQuoteId, + string $methodCode + ) : string { + return <<quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $reversedQuoteId + * @param int $customerId + * @return string + */ + private function assignQuoteToCustomer( + string $reversedQuoteId, + int $customerId + ): string { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + $quote->setCustomerId($customerId); + $this->quoteResource->save($quote); + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $username + * @param string $password + * @return array + */ + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailablePaymentMethodsTest.php deleted file mode 100644 index d6eff6816b342..0000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailablePaymentMethodsTest.php +++ /dev/null @@ -1,152 +0,0 @@ -quoteResource = $objectManager->create(QuoteResource::class); - $this->quote = $objectManager->create(Quote::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); - $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php - */ - public function testGetCartWithPaymentMethodsForRegisteredCustomer() - { - $reservedOrderId = 'test_order_item_with_items'; - $this->quoteResource->load( - $this->quote, - $reservedOrderId, - 'reserved_order_id' - ); - - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - $query = $this->prepareGetCartQuery($maskedQuoteId); - - $response = $this->sendRequestWithToken($query); - - self::assertArrayHasKey('cart', $response); - self::assertNotEmpty($response['cart']['items']); - self::assertNotEmpty($response['cart']['available_payment_methods']); - self::assertCount(1, $response['cart']['available_payment_methods']); - self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); - self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php - */ - public function testGetCartWithPaymentMethodsForGuest() - { - $reservedOrderId = 'test_order_1'; - $this->quoteResource->load( - $this->quote, - $reservedOrderId, - 'reserved_order_id' - ); - - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - $query = $this->prepareGetCartQuery($maskedQuoteId); - - $response = $this->graphQlQuery($query); - - self::assertArrayHasKey('cart', $response); - - self::assertNotEmpty($response['cart']['available_payment_methods']); - self::assertCount(2, $response['cart']['available_payment_methods']); - self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); - self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); - self::assertEquals('free', $response['cart']['available_payment_methods'][1]['code']); - self::assertEquals( - 'No Payment Information Required', - $response['cart']['available_payment_methods'][1]['title'] - ); - } - - /** - * Generates query for setting the specified shipping method on cart - * - * @param string $maskedQuoteId - * @return string - */ - private function prepareGetCartQuery( - string $maskedQuoteId - ) : string { - return <<customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); - $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - - return $this->graphQlQuery($query, [], '', $headerMap); - } -} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CreateEmptyCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CreateEmptyCartTest.php new file mode 100644 index 0000000000000..4fd398439913e --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CreateEmptyCartTest.php @@ -0,0 +1,47 @@ +guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class); + } + + public function testCreateEmptyCart() + { + $query = <<graphQlQuery($query); + + self::assertArrayHasKey('createEmptyCart', $response); + + $maskedCartId = $response['createEmptyCart']; + $guestCart = $this->guestCartRepository->get($maskedCartId); + + self::assertNotNull($guestCart->getId()); + self::assertNull($guestCart->getCustomer()->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php new file mode 100644 index 0000000000000..d4459180bc16e --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php @@ -0,0 +1,90 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testGetCartWithPaymentMethods() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = <<graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + self::assertCount(2, $response['cart']['available_payment_methods']); + + self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); + self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); + + self::assertEquals('free', $response['cart']['available_payment_methods'][1]['code']); + self::assertEquals( + 'No Payment Information Required', + $response['cart']['available_payment_methods'][1]['title'] + ); + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetCartTest.php new file mode 100644 index 0000000000000..42b5cbd06b9fc --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetCartTest.php @@ -0,0 +1,151 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testGetCart() + { + $maskedQuoteId = $this->unAssignCustomerFromQuote('test_order_item_with_items'); + $query = $this->getCartQuery($maskedQuoteId); + + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('cart', $response); + self::assertEquals($maskedQuoteId, $response['cart']['cart_id']); + + self::assertArrayHasKey('items', $response['cart']); + self::assertCount(2, $response['cart']['items']); + + self::assertNotEmpty($response['cart']['items'][0]['id']); + self::assertEquals($response['cart']['items'][0]['qty'], 2); + self::assertEquals($response['cart']['items'][0]['product']['sku'], 'simple'); + + self::assertNotEmpty($response['cart']['items'][1]['id']); + self::assertEquals($response['cart']['items'][1]['qty'], 1); + self::assertEquals($response['cart']['items'][1]['product']['sku'], 'simple_one'); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php + */ + public function testGetCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_item_with_items'); + $query = $this->getCartQuery($maskedQuoteId); + + $this->expectExceptionMessage( + "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" + ); + $this->graphQlQuery($query); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id" + */ + public function testGetNonExistentCart() + { + $maskedQuoteId = 'non_existent_masked_id'; + $query = $this->getCartQuery($maskedQuoteId); + + $this->graphQlQuery($query); + } + + /** + * @param string $maskedQuoteId + * @return string + */ + private function getCartQuery( + string $maskedQuoteId + ) : string { + return <<quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $reversedQuoteId + * @param int $customerId + * @return string + */ + private function unAssignCustomerFromQuote( + string $reversedQuoteId + ): string { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + $quote->setCustomerId(0); + $this->quoteResource->save($quote); + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php new file mode 100644 index 0000000000000..98c2fe81f82ab --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php @@ -0,0 +1,263 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testSetNewBillingAddress() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = <<graphQlQuery($query); + + self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); + $cartResponse = $response['setBillingAddressOnCart']['cart']; + self::assertArrayHasKey('billing_address', $cartResponse); + $billingAddressResponse = $cartResponse['billing_address']; + $this->assertNewAddressFields($billingAddressResponse); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testSetNewBillingAddressWithUseForShippingParameter() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = <<graphQlQuery($query); + + self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']); + $cartResponse = $response['setBillingAddressOnCart']['cart']; + self::assertArrayHasKey('billing_address', $cartResponse); + $billingAddressResponse = $cartResponse['billing_address']; + self::assertArrayHasKey('shipping_addresses', $cartResponse); + $shippingAddressResponse = current($cartResponse['shipping_addresses']); + $this->assertNewAddressFields($billingAddressResponse); + $this->assertNewAddressFields($shippingAddressResponse); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testSettBillingAddressToCustomerCart() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + + $query = <<expectExceptionMessage( + "The current user cannot perform operations on cart \"$maskedQuoteId\"" + ); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage The current customer isn't authorized. + */ + public function testSetBillingAddressFromAddressBook() + { + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = <<graphQlQuery($query); + } + + /** + * Verify the all the whitelisted fields for a New Address Object + * + * @param array $billingAddressResponse + */ + private function assertNewAddressFields(array $billingAddressResponse): void + { + $assertionMap = [ + ['response_field' => 'firstname', 'expected_value' => 'test firstname'], + ['response_field' => 'lastname', 'expected_value' => 'test lastname'], + ['response_field' => 'company', 'expected_value' => 'test company'], + ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']], + ['response_field' => 'city', 'expected_value' => 'test city'], + ['response_field' => 'postcode', 'expected_value' => '887766'], + ['response_field' => 'telephone', 'expected_value' => '88776655'] + ]; + + $this->assertResponseFields($billingAddressResponse, $assertionMap); + } + + /** + * @param string $reversedQuoteId + * @return string + */ + private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php similarity index 59% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php index 89e88bd0ce04a..8286f97148953 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetPaymentMethodOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetPaymentMethodOnCartTest.php @@ -5,9 +5,8 @@ */ declare(strict_types=1); -namespace Magento\GraphQl\Quote; +namespace Magento\GraphQl\Quote\Guest; -use Magento\Integration\Api\CustomerTokenServiceInterface; use Magento\OfflinePayments\Model\Checkmo; use Magento\Quote\Model\QuoteFactory; use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; @@ -16,15 +15,10 @@ use Magento\TestFramework\TestCase\GraphQlAbstract; /** - * Test for setting payment methods on cart + * Test for setting payment methods on cart by guest */ class SetPaymentMethodOnCartTest extends GraphQlAbstract { - /** - * @var CustomerTokenServiceInterface - */ - private $customerTokenService; - /** * @var QuoteResource */ @@ -49,19 +43,38 @@ protected function setUp() $this->quoteResource = $objectManager->get(QuoteResource::class); $this->quoteFactory = $objectManager->get(QuoteFactory::class); $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); - $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php + */ + public function testSetPaymentWithVirtualProduct() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_virtual_product'); + $this->unAssignCustomerFromQuote('test_order_with_virtual_product'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $response = $this->graphQlQuery($query); + + self::assertArrayHasKey('setPaymentMethodOnCart', $response); + self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); + self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); + self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); + self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php */ - public function testSetPaymentMethodOnCart() + public function testSetPaymentWithSimpleProduct() { $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $this->unAssignCustomerFromQuote('test_order_1'); $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); - $response = $this->sendRequestWithToken($query); + $response = $this->graphQlQuery($query); self::assertArrayHasKey('setPaymentMethodOnCart', $response); self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); @@ -70,6 +83,20 @@ public function testSetPaymentMethodOnCart() self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); } + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @expectedException \Exception + * @expectedExceptionMessage The shipping address is missing. Set the address and try again. + */ + public function testSetPaymentWithSimpleProductWithoutAddress() + { + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; + $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + + $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); + $this->graphQlQuery($query); + } + /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php * @expectedException \Exception @@ -79,17 +106,18 @@ public function testSetNonExistingPaymentMethod() { $methodCode = 'noway'; $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); + $this->unAssignCustomerFromQuote('test_order_1'); $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); - $this->sendRequestWithToken($query); + $this->graphQlQuery($query); } /** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php */ - public function testSetPaymentMethodByGuestToCustomerCart() + public function testSetPaymentMethodToCustomerCart() { - $methodCode = 'checkmo'; + $methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE; $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); $query = $this->prepareMutationQuery($maskedQuoteId, $methodCode); @@ -97,7 +125,6 @@ public function testSetPaymentMethodByGuestToCustomerCart() $this->expectExceptionMessage( "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); - $this->graphQlQuery($query); } @@ -116,9 +143,9 @@ private function prepareMutationQuery( mutation { setPaymentMethodOnCart(input: { - cart_id: "$maskedQuoteId", + cart_id: "{$maskedQuoteId}", payment_method: { - code: "$methodCode" + code: "{$methodCode}" } }) { @@ -134,18 +161,18 @@ private function prepareMutationQuery( } /** - * Sends a GraphQL request with using a bearer token - * - * @param string $query - * @return array - * @throws \Magento\Framework\Exception\AuthenticationException + * @param string $reversedQuoteId + * @param int $customerId + * @return string */ - private function sendRequestWithToken(string $query): array - { - $customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); - $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - - return $this->graphQlQuery($query, [], '', $headerMap); + private function unAssignCustomerFromQuote( + string $reversedQuoteId + ): string { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); + $quote->setCustomerId(0); + $this->quoteResource->save($quote); + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } /** diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php index c3685c88ae487..f73454b0a8b96 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php @@ -422,7 +422,7 @@ public function testSetMultipleNewShippingAddresses() */ public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress() { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address'); + $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2); $query = << Date: Tue, 26 Feb 2019 17:55:57 -0600 Subject: [PATCH 13/15] GraphQL-293: [Payment methods] Set Payment Method on Cart -- Fixes after merge with mainline --- .../Customer/SetBillingAddressOnCartTest.php | 10 ++++++++-- .../Quote/Guest/SetBillingAddressOnCartTest.php | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php index 2dd0394dfc6df..2e0b57f96fe3a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php @@ -205,6 +205,10 @@ public function testSetBillingAddressFromAddressBook() city postcode telephone + country { + code + label + } } } } @@ -410,7 +414,8 @@ private function assertNewAddressFields(array $billingAddressResponse): void ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']], ['response_field' => 'city', 'expected_value' => 'test city'], ['response_field' => 'postcode', 'expected_value' => '887766'], - ['response_field' => 'telephone', 'expected_value' => '88776655'] + ['response_field' => 'telephone', 'expected_value' => '88776655'], + ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ]; $this->assertResponseFields($billingAddressResponse, $assertionMap); @@ -430,7 +435,8 @@ private function assertSavedBillingAddressFields(array $billingAddressResponse): ['response_field' => 'street', 'expected_value' => [0 => 'Green str, 67']], ['response_field' => 'city', 'expected_value' => 'CityM'], ['response_field' => 'postcode', 'expected_value' => '75477'], - ['response_field' => 'telephone', 'expected_value' => '3468676'] + ['response_field' => 'telephone', 'expected_value' => '3468676'], + ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ]; $this->assertResponseFields($billingAddressResponse, $assertionMap); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php index 98c2fe81f82ab..24fc8353d2552 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php @@ -78,6 +78,10 @@ public function testSetNewBillingAddress() city postcode telephone + country { + code + label + } } } } @@ -130,6 +134,10 @@ public function testSetNewBillingAddressWithUseForShippingParameter() city postcode telephone + country { + code + label + } } shipping_addresses { firstname @@ -139,6 +147,10 @@ public function testSetNewBillingAddressWithUseForShippingParameter() city postcode telephone + country { + code + label + } } } } @@ -243,7 +255,8 @@ private function assertNewAddressFields(array $billingAddressResponse): void ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']], ['response_field' => 'city', 'expected_value' => 'test city'], ['response_field' => 'postcode', 'expected_value' => '887766'], - ['response_field' => 'telephone', 'expected_value' => '88776655'] + ['response_field' => 'telephone', 'expected_value' => '88776655'], + ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ]; $this->assertResponseFields($billingAddressResponse, $assertionMap); From c0c4bdf1a00cf6302ecf63c91467d607d96d1da5 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 26 Feb 2019 18:08:16 -0600 Subject: [PATCH 14/15] GraphQL-293: [Payment methods] Set Payment Method on Cart -- Fixes after merge with mainline --- .../SetPaymentMethodOnCartTest.php | 315 ------------------ 1 file changed, 315 deletions(-) delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php deleted file mode 100644 index 3a3e50efa0676..0000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/OfflinePayments/SetPaymentMethodOnCartTest.php +++ /dev/null @@ -1,315 +0,0 @@ -quoteResource = $objectManager->get(QuoteResource::class); - $this->quoteFactory = $objectManager->get(QuoteFactory::class); - $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); - $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); - $this->config = $objectManager->get(Config::class); - $this->cacheList = $objectManager->get(TypeListInterface::class); - - foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { - $this->config->saveConfig( - 'payment/' . $offlineMethodCode . '/active', - '1', - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - } - $this->cacheList->cleanType('config'); - } - - /** - * @inheritdoc - */ - protected function tearDown() - { -// foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { -// //Never no disable checkmo method -// if ($offlineMethodCode === Checkmo::PAYMENT_METHOD_CHECKMO_CODE) { -// continue; -// } -// $this->config->saveConfig( -// 'payment/' . $offlineMethodCode . '/active', -// '0', -// ScopeConfigInterface::SCOPE_TYPE_DEFAULT, -// 0 -// ); -// } -// $this->cacheList->cleanType('config'); - } - - /** - * @param string $methodCode - * @dataProvider dataProviderOfflinePaymentMethods - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testSetPaymentMethodOnCart(string $methodCode) - { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $methodCode - ); - - $response = $this->sendRequestWithToken($query); - - self::assertArrayHasKey('setPaymentMethodOnCart', $response); - self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); - self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); - self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']); - self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); - } - - public function dataProviderOfflinePaymentMethods(): array - { - $methods = []; - foreach (static::OFFLINE_METHOD_CODES as $offlineMethodCode) { - //Purchase order requires additional input and is tested separately - if ($offlineMethodCode === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE) { - continue; - } - $methods[] = [$offlineMethodCode]; - } - - return $methods; - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testSetNonExistingPaymentMethod() - { - $paymentMethod = 'noway'; - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $paymentMethod - ); - - $this->expectExceptionMessage('The requested Payment Method is not available.'); - $this->sendRequestWithToken($query); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testSetPaymentMethodByGuestToCustomerCart() - { - $paymentMethod = 'checkmo'; - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - $paymentMethod - ); - - $this->expectExceptionMessage( - "The current user cannot perform operations on cart \"$maskedQuoteId\"" - ); - - $this->graphQlQuery($query); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testSetPaymentMethodPurchaseOrderOnCart() - { - $methodCode = \Magento\OfflinePayments\Model\Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE; - $poNumber = 'GQL-19002'; - - /** @var \Magento\Config\Model\ResourceModel\Config $config */ - $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class); - $config->saveConfig( - 'payment/' . $methodCode . '/active', - 1, - ScopeConfigInterface::SCOPE_TYPE_DEFAULT, - 0 - ); - - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - - $query = <<sendRequestWithToken($query); - - self::assertArrayHasKey('setPaymentMethodOnCart', $response); - self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']); - self::assertEquals($maskedQuoteId, $response['setPaymentMethodOnCart']['cart']['cart_id']); - self::assertArrayHasKey('payment_method', $response['setPaymentMethodOnCart']['cart']); - self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']); - self::assertEquals( - $poNumber, - $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number'] - ); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php - */ - public function testPurchaseOrderPaymentMethodFailingValidation() - { - $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1'); - - $query = $this->prepareMutationQuery( - $maskedQuoteId, - Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE - ); - - $this->expectExceptionMessage('Purchase order number is a required field.'); - $this->sendRequestWithToken($query); - } - - /** - * Generates query for setting the specified shipping method on cart - * - * @param string $maskedQuoteId - * @param string $methodCode - * @return string - */ - private function prepareMutationQuery( - string $maskedQuoteId, - string $methodCode - ) : string { - return <<customerTokenService->createCustomerAccessToken('customer@example.com', 'password'); - $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; - - return $this->graphQlQuery($query, [], '', $headerMap); - } - - /** - * @param string $reversedQuoteId - * @return string - */ - private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string - { - $quote = $this->quoteFactory->create(); - $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id'); - - return $this->quoteIdToMaskedId->execute((int)$quote->getId()); - } -} From 8d19d7c52a2efc3572d987f19286a1d1b505bfc7 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Tue, 26 Feb 2019 18:30:03 -0600 Subject: [PATCH 15/15] GraphQL-293: [Payment methods] Set Payment Method on Cart --- composer.json | 1 - .../GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php | 1 - .../GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php | 1 - 3 files changed, 3 deletions(-) diff --git a/composer.json b/composer.json index 2a2e1b2d85947..e6d073563b6e1 100644 --- a/composer.json +++ b/composer.json @@ -109,7 +109,6 @@ "magento/module-backend": "*", "magento/module-backup": "*", "magento/module-braintree": "*", - "magento/module-braintree-graph-ql": "*", "magento/module-bundle": "*", "magento/module-bundle-graph-ql": "*", "magento/module-bundle-import-export": "*", diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php index b5323ee986582..5695aab6854d4 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailablePaymentMethodsTest.php @@ -71,7 +71,6 @@ public function testGetCartWithPaymentMethods() $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response); - self::assertCount(1, $response['cart']['available_payment_methods']); self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php index d4459180bc16e..a5a08aaf39fb1 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailablePaymentMethodsTest.php @@ -64,7 +64,6 @@ public function testGetCartWithPaymentMethods() $response = $this->graphQlQuery($query); self::assertArrayHasKey('cart', $response); - self::assertCount(2, $response['cart']['available_payment_methods']); self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']); self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);