From 39c9c80c589e86f8d1586400b7166d5149f495fb Mon Sep 17 00:00:00 2001 From: RomanKis Date: Fri, 27 Sep 2019 09:45:53 +0300 Subject: [PATCH 1/9] graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema --- .../Model/Cart/QuoteAddressFactory.php | 4 +++ .../Model/Cart/SetBillingAddressOnCart.php | 27 ++++++++++++++++ .../Model/Cart/SetShippingAddressesOnCart.php | 26 +++++++++++++++ .../Customer/SetBillingAddressOnCartTest.php | 32 ++++++++++++++----- .../Customer/SetShippingAddressOnCartTest.php | 30 +++++++++++++---- 5 files changed, 104 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index afc88f026ed6..52f5387f1578 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -66,6 +66,10 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress $addressInput['country_id'] = $addressInput['country_code']; } + if (isset($addressInput['region'])) { + $addressInput['region_code'] = $addressInput['region']; + } + $maxAllowedLineCount = $this->addressHelper->getStreetLines(); if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) { throw new GraphQlInputException( diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index 673debefd087..cf8e38ebbfcc 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -111,6 +111,33 @@ private function createBillingAddress( (int)$context->getUserId() ); } + + $errors = $billingAddress->validate(); + + if (true !== $errors) { + throw new GraphQlInputException( + __('Shipping address error: %message', ['message' => $this->getAddressErrors($errors)]) + ); + } + return $billingAddress; } + + /** + * Collecting errors. + * + * @param array $errors + * @return string + */ + private function getAddressErrors(array $errors): string + { + $errorMessages = []; + + /** @var \Magento\Framework\Phrase $error */ + foreach ($errors as $error) { + $errorMessages[] = $error->render(); + } + + return implode(PHP_EOL, $errorMessages); + } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 260f1343556f..9e39992eed83 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -82,6 +82,32 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s ); } + $errors = $shippingAddress->validate(); + + if (true !== $errors) { + throw new GraphQlInputException( + __('Shipping address error: %message', ['message' => $this->getAddressErrors($errors)]) + ); + } + $this->assignShippingAddressToCart->execute($cart, $shippingAddress); } + + /** + * Collecting errors. + * + * @param array $errors + * @return string + */ + private function getAddressErrors(array $errors): string + { + $errorMessages = []; + + /** @var \Magento\Framework\Phrase $error */ + foreach ($errors as $error) { + $errorMessages[] = $error->render(); + } + + return implode(PHP_EOL, $errorMessages); + } } 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 011930e72327..29109f89352f 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 @@ -77,7 +77,7 @@ public function testSetNewBillingAddress() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -136,7 +136,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -297,7 +297,7 @@ public function testSetNewBillingAddressAndFromAddressBookAtSameTime() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -379,7 +379,7 @@ public function testSetNewBillingAddressWithUseForShippingAndMultishipping() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -573,7 +573,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st QUERY; $this->expectExceptionMessage($message); - $this->graphQlMutation($query); + $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** @@ -590,7 +590,23 @@ public function dataProviderSetWithoutRequiredParameters(): array 'missed_cart_id' => [ 'billing_address: {}', 'Required parameter "cart_id" is missing' - ] + ], + 'missed_region' => [ + 'cart_id: "cart_id_value" + billing_address: { + address: { + firstname: "test firstname" + lastname: "test lastname" + company: "test company" + street: ["test street 1", "test street 2"] + city: "test city" + postcode: "887766" + country_code: "US" + telephone: "88776655" + } + }', + '"regionId" is required. Enter and try again.' + ], ]; } @@ -616,7 +632,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine() company: "test company" street: ["test street 1", "test street 2", "test street 3"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -659,7 +675,7 @@ public function testSetBillingAddressWithLowerCaseCountry() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "us" telephone: "88776655" diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index e74b7c41b398..fd21475f1250 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -78,7 +78,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -144,7 +144,7 @@ public function testSetNewShippingAddressOnCartWithVirtualProduct() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -272,7 +272,7 @@ public function testSetNewShippingAddressAndFromAddressBookAtSameTime() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -424,7 +424,23 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array 'missed_cart_id' => [ 'shipping_addresses: {}', 'Required parameter "cart_id" is missing' - ] + ], + 'missed_region' => [ + 'cart_id: "cart_id_value" + shipping_addresses: [{ + address: { + firstname: "test firstname" + lastname: "test lastname" + company: "test company" + street: ["test street 1", "test street 2"] + city: "test city" + postcode: "887766" + country_code: "US" + telephone: "88776655" + } + }]', + '"regionId" is required. Enter and try again.' + ], ]; } @@ -454,7 +470,7 @@ public function testSetMultipleNewShippingAddresses() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -468,7 +484,7 @@ public function testSetMultipleNewShippingAddresses() company: "test company 2" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -512,7 +528,7 @@ public function testSetNewShippingAddressOnCartWithRedundantStreetLine() company: "test company" street: ["test street 1", "test street 2", "test street 3"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" From 18a32604d8069af1823c7a4306f3dcf16deb1f84 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Sun, 29 Sep 2019 13:29:42 +0300 Subject: [PATCH 2/9] graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema --- .../Model/Cart/QuoteAddressFactory.php | 25 ++++++++++++++++--- .../Guest/SetBillingAddressOnCartTest.php | 14 +++++------ .../Guest/SetShippingAddressOnCartTest.php | 12 ++++----- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 52f5387f1578..9fe8d34435d5 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -9,12 +9,14 @@ use Magento\Customer\Helper\Address as AddressHelper; use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; +use Magento\Directory\Api\CountryInformationAcquirerInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Model\Quote\Address as QuoteAddress; use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory; +use Magento\Framework\App\ObjectManager; /** * Create QuoteAddress @@ -36,38 +38,53 @@ class QuoteAddressFactory */ private $addressHelper; + /** + * @var CountryInformationAcquirerInterface + */ + private $countryInformationAcquirer; + /** * @param BaseQuoteAddressFactory $quoteAddressFactory * @param GetCustomerAddress $getCustomerAddress * @param AddressHelper $addressHelper + * @param CountryInformationAcquirerInterface|null $countryInformationAcquirer */ public function __construct( BaseQuoteAddressFactory $quoteAddressFactory, GetCustomerAddress $getCustomerAddress, - AddressHelper $addressHelper + AddressHelper $addressHelper, + CountryInformationAcquirerInterface $countryInformationAcquirer = null ) { $this->quoteAddressFactory = $quoteAddressFactory; $this->getCustomerAddress = $getCustomerAddress; $this->addressHelper = $addressHelper; + $this->countryInformationAcquirer = $countryInformationAcquirer; + $this->countryInformationAcquirer = $countryInformationAcquirer + ?: ObjectManager::getInstance()->get(CountryInformationAcquirerInterface::class); } /** * Create QuoteAddress based on input data * * @param array $addressInput + * * @return QuoteAddress * @throws GraphQlInputException */ public function createBasedOnInputData(array $addressInput): QuoteAddress { $addressInput['country_id'] = ''; - if ($addressInput['country_code']) { + if (isset($addressInput['country_code']) && $addressInput['country_code']) { $addressInput['country_code'] = strtoupper($addressInput['country_code']); $addressInput['country_id'] = $addressInput['country_code']; } - if (isset($addressInput['region'])) { - $addressInput['region_code'] = $addressInput['region']; + if ($addressInput['country_id'] && isset($addressInput['region'])) { + $countryInformation = $this->countryInformationAcquirer->getCountryInfo($addressInput['country_id']); + $availableRegions = $countryInformation->getAvailableRegions(); + if (null !== $availableRegions) { + $addressInput['region_code'] = $addressInput['region']; + } } $maxAllowedLineCount = $this->addressHelper->getStreetLines(); 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 730e65b4ba8a..dd113c2a0205 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 @@ -48,7 +48,7 @@ public function testSetNewBillingAddress() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -106,7 +106,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -182,7 +182,7 @@ public function testSetBillingAddressToCustomerCart() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -259,7 +259,7 @@ public function testSetBillingAddressOnNonExistentCart() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -387,7 +387,7 @@ public function testSetNewBillingAddressWithUseForShippingAndMultishipping() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -433,7 +433,7 @@ public function testSetNewBillingAddressRedundantStreetLine() company: "test company" street: ["test street 1", "test street 2", "test street 3"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -476,7 +476,7 @@ public function testSetBillingAddressWithLowerCaseCountry() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "us" telephone: "88776655" diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php index 0351a4f58a8e..217759edf10f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php @@ -49,7 +49,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -114,7 +114,7 @@ public function testSetNewShippingAddressOnCartWithVirtualProduct() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -266,7 +266,7 @@ public function testSetNewShippingAddressOnCartWithRedundantStreetLine() company: "test company" street: ["test street 1", "test street 2", "test street 3"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -335,7 +335,7 @@ public function testSetMultipleNewShippingAddresses() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -349,7 +349,7 @@ public function testSetMultipleNewShippingAddresses() company: "test company 2" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" @@ -389,7 +389,7 @@ public function testSetShippingAddressOnNonExistentCart() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AL" postcode: "887766" country_code: "US" telephone: "88776655" From 18784c217649e6f6eb2137dc939845bb12a79755 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Sun, 29 Sep 2019 13:58:46 +0300 Subject: [PATCH 3/9] graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema --- .../Model/Cart/SetBillingAddressOnCart.php | 18 +++++++++++++++--- .../Model/Cart/SetShippingAddressesOnCart.php | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index cf8e38ebbfcc..dd2daa6cb24f 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -112,15 +112,27 @@ private function createBillingAddress( ); } - $errors = $billingAddress->validate(); + $this->validateAddress($billingAddress); + + return $billingAddress; + } + + /** + * Validate quote address. + * + * @param Address $shippingAddress + * + * @throws GraphQlInputException + */ + private function validateAddress(Address $shippingAddress) + { + $errors = $shippingAddress->validate(); if (true !== $errors) { throw new GraphQlInputException( __('Shipping address error: %message', ['message' => $this->getAddressErrors($errors)]) ); } - - return $billingAddress; } /** diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 9e39992eed83..a15398806efa 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -11,6 +11,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\GraphQl\Model\Query\ContextInterface; use Magento\Quote\Api\Data\CartInterface; +use Magento\Quote\Model\Quote\Address; /** * Set single shipping address for a specified shopping cart @@ -82,6 +83,20 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s ); } + $this->validateAddress($shippingAddress); + + $this->assignShippingAddressToCart->execute($cart, $shippingAddress); + } + + /** + * Validate quote address. + * + * @param Address $shippingAddress + * + * @throws GraphQlInputException + */ + private function validateAddress(Address $shippingAddress) + { $errors = $shippingAddress->validate(); if (true !== $errors) { @@ -89,8 +104,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s __('Shipping address error: %message', ['message' => $this->getAddressErrors($errors)]) ); } - - $this->assignShippingAddressToCart->execute($cart, $shippingAddress); } /** From f8b47b3606bc4e852f31271313c57e34a3bda69f Mon Sep 17 00:00:00 2001 From: RomanKis Date: Thu, 3 Oct 2019 17:12:26 +0300 Subject: [PATCH 4/9] graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema --- .../Model/Cart/QuoteAddressFactory.php | 8 +++----- .../Model/Cart/SetBillingAddressOnCart.php | 2 ++ .../Model/Cart/SetShippingAddressesOnCart.php | 2 ++ .../Customer/SetShippingAddressOnCartTest.php | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 9fe8d34435d5..0fca8a19aa03 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -16,7 +16,6 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Model\Quote\Address as QuoteAddress; use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory; -use Magento\Framework\App\ObjectManager; /** * Create QuoteAddress @@ -47,20 +46,19 @@ class QuoteAddressFactory * @param BaseQuoteAddressFactory $quoteAddressFactory * @param GetCustomerAddress $getCustomerAddress * @param AddressHelper $addressHelper - * @param CountryInformationAcquirerInterface|null $countryInformationAcquirer + * @param CountryInformationAcquirerInterface $countryInformationAcquirer */ public function __construct( BaseQuoteAddressFactory $quoteAddressFactory, GetCustomerAddress $getCustomerAddress, AddressHelper $addressHelper, - CountryInformationAcquirerInterface $countryInformationAcquirer = null + CountryInformationAcquirerInterface $countryInformationAcquirer ) { $this->quoteAddressFactory = $quoteAddressFactory; $this->getCustomerAddress = $getCustomerAddress; $this->addressHelper = $addressHelper; $this->countryInformationAcquirer = $countryInformationAcquirer; - $this->countryInformationAcquirer = $countryInformationAcquirer - ?: ObjectManager::getInstance()->get(CountryInformationAcquirerInterface::class); + $this->countryInformationAcquirer = $countryInformationAcquirer; } /** diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index dd2daa6cb24f..d3e4b13d2023 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -140,6 +140,8 @@ private function validateAddress(Address $shippingAddress) * * @param array $errors * @return string + * + * @todo change implementation in https://github.com/magento/graphql-ce/issues/970. */ private function getAddressErrors(array $errors): string { diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 499f6a6e9e40..b2db19d4ffe4 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -81,6 +81,8 @@ private function validateAddress(Address $shippingAddress) * * @param array $errors * @return string + * + * @todo change implementation in https://github.com/magento/graphql-ce/issues/970. */ private function getAddressErrors(array $errors): string { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index fd21475f1250..2fcbddfad1a6 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -441,6 +441,22 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array }]', '"regionId" is required. Enter and try again.' ], + 'missed_multiple_fields' => [ + 'cart_id: "cart_id_value" + shipping_addresses: [{ + address: { + firstname: "test firstname" + lastname: "test lastname" + company: "test company" + street: ["test street 1", "test street 2"] + city: "test city" + country_code: "US" + telephone: "88776655" + } + }]', + '"postcode" is required. Enter and try again. +"regionId" is required. Enter and try again.' + ], ]; } From e9a701f758de3e5449933567eee62b26a7639e1e Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 23 Oct 2019 16:21:31 -0500 Subject: [PATCH 5/9] magento/graphql-ce#961: ShippingAddressInput.postcode: String, is not required by Schema --- .../Model/Cart/SetBillingAddressOnCart.php | 10 ++++- .../Model/Cart/SetShippingAddressesOnCart.php | 42 +++---------------- .../Customer/SetBillingAddressOnCartTest.php | 16 +++++++ 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index 3a8d4e06d8aa..a264c9bbc91e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -129,9 +129,15 @@ private function createBillingAddress( (int)$context->getUserId() ); } + $errors = $billingAddress->validate(); - - $this->validateAddress($billingAddress); + if (true !== $errors) { + $e = new GraphQlInputException(__('Billing address errors')); + foreach ($errors as $error){ + $e->addError(new GraphQlInputException($error)); + } + throw $e; + } return $billingAddress; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index a8820d8a7068..07964a70ecaf 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -53,46 +53,14 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s $shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput); - $this->validateAddress($shippingAddress); - - $this->assignShippingAddressToCart->execute($cart, $shippingAddress); - } - - /** - * Validate quote address. - * - * @param Address $shippingAddress - * - * @throws GraphQlInputException - */ - private function validateAddress(Address $shippingAddress) - { $errors = $shippingAddress->validate(); if (true !== $errors) { - throw new GraphQlInputException( - __('Shipping address error: %message', ['message' => $this->getAddressErrors($errors)]) - ); + $e = new GraphQlInputException(__('Shipping address error')); + foreach ($errors as $error){ + $e->addError(new GraphQlInputException($error)); + } + throw $e; } } - - /** - * Collecting errors. - * - * @param array $errors - * @return string - * - * @todo change implementation in https://github.com/magento/graphql-ce/issues/970. - */ - private function getAddressErrors(array $errors): string - { - $errorMessages = []; - - /** @var \Magento\Framework\Phrase $error */ - foreach ($errors as $error) { - $errorMessages[] = $error->render(); - } - - return implode(PHP_EOL, $errorMessages); - } } 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 ff75a4fa828c..cca126e25896 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 @@ -658,6 +658,22 @@ public function dataProviderSetWithoutRequiredParameters(): array }', '"regionId" is required. Enter and try again.' ], + 'missed_multiple_fields' => [ + 'cart_id: "cart_id_value" + billing_address: { + address: { + firstname: "test firstname" + lastname: "test lastname" + company: "test company" + street: ["test street 1", "test street 2"] + city: "test city" + country_code: "US" + telephone: "88776655" + } + }', + '"postcode" is required. Enter and try again. +"regionId" is required. Enter and try again.' + ] ]; } From 9ced73c76cd168ddbd7c138efa29996e7b6443e2 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 23 Oct 2019 16:47:44 -0500 Subject: [PATCH 6/9] magento/graphql-ce#961: ShippingAddressInput.postcode: String, is not required by Schema --- .../QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php | 2 +- .../QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php index a264c9bbc91e..e70ce5a10666 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php @@ -133,7 +133,7 @@ private function createBillingAddress( if (true !== $errors) { $e = new GraphQlInputException(__('Billing address errors')); - foreach ($errors as $error){ + foreach ($errors as $error) { $e->addError(new GraphQlInputException($error)); } throw $e; diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 07964a70ecaf..2c24acbdf63e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -56,8 +56,8 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s $errors = $shippingAddress->validate(); if (true !== $errors) { - $e = new GraphQlInputException(__('Shipping address error')); - foreach ($errors as $error){ + $e = new GraphQlInputException(__('Shipping address errors')); + foreach ($errors as $error) { $e->addError(new GraphQlInputException($error)); } throw $e; From 2e4cd74a7287db2c53e911fbd8403b1338e67541 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Thu, 24 Oct 2019 09:49:36 -0500 Subject: [PATCH 7/9] magento/graphql-ce#961: ShippingAddressInput.postcode: String, is not required by Schema --- .../QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 2c24acbdf63e..e058913dde1d 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -62,5 +62,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s } throw $e; } + $this->assignShippingAddressToCart->execute($cart, $shippingAddress); } } From b72a5c6fa149582bf4ab80360639b040b1f018b5 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Thu, 24 Oct 2019 10:47:36 -0500 Subject: [PATCH 8/9] magento/graphql-ce#961: ShippingAddressInput.postcode: String, is not required by Schema --- .../QuoteGraphQl/Model/Cart/QuoteAddressFactory.php | 8 ++++++-- .../Quote/Customer/SetBillingAddressOnCartTest.php | 8 ++++---- .../Quote/Customer/SetShippingAddressOnCartTest.php | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 0fca8a19aa03..2b0a903a9725 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -11,6 +11,7 @@ use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; use Magento\Directory\Api\CountryInformationAcquirerInterface; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; @@ -58,7 +59,6 @@ public function __construct( $this->getCustomerAddress = $getCustomerAddress; $this->addressHelper = $addressHelper; $this->countryInformationAcquirer = $countryInformationAcquirer; - $this->countryInformationAcquirer = $countryInformationAcquirer; } /** @@ -78,7 +78,11 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress } if ($addressInput['country_id'] && isset($addressInput['region'])) { - $countryInformation = $this->countryInformationAcquirer->getCountryInfo($addressInput['country_id']); + try { + $countryInformation = $this->countryInformationAcquirer->getCountryInfo($addressInput['country_id']); + } catch (NoSuchEntityException $e) { + throw new GraphQlInputException(__('The country isn\'t available.')); + } $availableRegions = $countryInformation->getAvailableRegions(); if (null !== $availableRegions) { $addressInput['region_code'] = $addressInput['region']; 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 cca126e25896..da8af077866e 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 @@ -798,7 +798,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -865,7 +865,7 @@ public function testSetNewBillingAddressWithNotSaveInAddressBook() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -933,7 +933,7 @@ public function testWithInvalidBillingAddressInput() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "USS" telephone: "88776655" @@ -960,7 +960,7 @@ public function testWithInvalidBillingAddressInput() } } QUERY; - $this->expectExceptionMessage('The address failed to save. Verify the address and try again.'); + $this->expectExceptionMessage('The country isn\'t available.'); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index 2d380785b47f..9e03c3932cc8 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -780,7 +780,7 @@ public function testWithInvalidShippingAddressesInput() } } QUERY; - $this->expectExceptionMessage('The address failed to save. Verify the address and try again.'); + $this->expectExceptionMessage('The country isn\'t available.'); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } @@ -806,7 +806,7 @@ public function testSetNewShippingAddressWithSaveInAddressBook() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" @@ -877,7 +877,7 @@ public function testSetNewShippingAddressWithNotSaveInAddressBook() company: "test company" street: ["test street 1", "test street 2"] city: "test city" - region: "test region" + region: "AZ" postcode: "887766" country_code: "US" telephone: "88776655" From 222ec5f3706dcca910ff3af44630f4cfee002e62 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 25 Oct 2019 14:42:41 -0500 Subject: [PATCH 9/9] magento/graphql-ce#961: ShippingAddressInput.postcode: String, is not required by Schema --- .../SaveQuoteAddressToCustomerAddressBook.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php index 5c773d44e6a1..c87101156327 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php @@ -12,6 +12,7 @@ use Magento\Customer\Api\Data\AddressInterfaceFactory; use Magento\Customer\Api\Data\RegionInterface; use Magento\Customer\Api\Data\RegionInterfaceFactory; +use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Quote\Model\Quote\Address as QuoteAddress; @@ -89,8 +90,15 @@ public function execute(QuoteAddress $quoteAddress, int $customerId): void $customerAddress->setRegion($region); $this->addressRepository->save($customerAddress); - } catch (LocalizedException $e) { - throw new GraphQlInputException(__($e->getMessage()), $e); + } catch (InputException $inputException) { + $graphQlInputException = new GraphQlInputException(__($inputException->getMessage())); + $errors = $inputException->getErrors(); + foreach ($errors as $error) { + $graphQlInputException->addError(new GraphQlInputException(__($error->getMessage()))); + } + throw $graphQlInputException; + } catch (LocalizedException $exception) { + throw new GraphQlInputException(__($exception->getMessage()), $exception); } } }