Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema #969

Merged
merged 13 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
27 changes: 25 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

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\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
Expand All @@ -36,36 +38,57 @@ class QuoteAddressFactory
*/
private $addressHelper;

/**
* @var CountryInformationAcquirerInterface
*/
private $countryInformationAcquirer;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
* @param GetCustomerAddress $getCustomerAddress
* @param AddressHelper $addressHelper
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory,
GetCustomerAddress $getCustomerAddress,
AddressHelper $addressHelper
AddressHelper $addressHelper,
CountryInformationAcquirerInterface $countryInformationAcquirer
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomerAddress = $getCustomerAddress;
$this->addressHelper = $addressHelper;
$this->countryInformationAcquirer = $countryInformationAcquirer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not care about BC in GraphQL modules. Only the schema is out API.
https://github.com/magento/graphql-ce/wiki/Approach-to-Implementation#backward-compatibility-policy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

}

/**
* 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 ($addressInput['country_id'] && isset($addressInput['region'])) {
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'];
}
}

$maxAllowedLineCount = $this->addressHelper->getStreetLines();
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
Copy link
Contributor

@TomashKhamlai TomashKhamlai Oct 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that $addressInput['street'] can be empty and this should be checked here or somewhere else before these lines can be executed.

throw new GraphQlInputException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ private function createBillingAddress(
(int)$context->getUserId()
);
}
$errors = $billingAddress->validate();

if (true !== $errors) {
$e = new GraphQlInputException(__('Billing address errors'));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException($error));
}
throw $e;
}

return $billingAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
Expand Down Expand Up @@ -52,6 +53,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s

$shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput);

$errors = $shippingAddress->validate();

if (true !== $errors) {
$e = new GraphQlInputException(__('Shipping address errors'));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException($error));
}
throw $e;
}
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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"
Expand Down Expand Up @@ -154,7 +154,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"
Expand Down Expand Up @@ -350,7 +350,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"
Expand Down Expand Up @@ -431,7 +431,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"
Expand Down Expand Up @@ -624,7 +624,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
QUERY;

$this->expectExceptionMessage($message);
$this->graphQlMutation($query);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
Expand All @@ -641,6 +641,38 @@ 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.'
],
'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.'
]
];
}
Expand All @@ -667,7 +699,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"
Expand Down Expand Up @@ -709,7 +741,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"
Expand Down Expand Up @@ -766,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"
Expand Down Expand Up @@ -833,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"
Expand Down Expand Up @@ -901,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"
Expand All @@ -928,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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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"
Expand Down Expand Up @@ -164,7 +164,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"
Expand Down Expand Up @@ -328,7 +328,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"
Expand Down Expand Up @@ -479,7 +479,39 @@ 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.'
],
'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.'
],
];
}

Expand Down Expand Up @@ -509,7 +541,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"
Expand All @@ -522,7 +554,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"
Expand Down Expand Up @@ -565,7 +597,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"
Expand Down Expand Up @@ -748,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());
}

Expand All @@ -774,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"
Expand Down Expand Up @@ -845,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"
Expand Down
Loading