Skip to content

Commit d8ea43d

Browse files
ENGCOM-6170: graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema #969
- Merge Pull Request magento/graphql-ce#969 from kisroman/graphql-ce:graphql-961 - Merged commits: 1. 39c9c80 2. 18a3260 3. c6cfe10 4. 18784c2 5. 694dda4 6. f8b47b3 7. 2555933 8. e9a701f 9. 9ced73c 10. 2e4cd74 11. b72a5c6 12. 1f03323 13. 222ec5f
2 parents 6f1c7e3 + 222ec5f commit d8ea43d

File tree

8 files changed

+152
-38
lines changed

8 files changed

+152
-38
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1313
use Magento\Customer\Api\Data\RegionInterface;
1414
use Magento\Customer\Api\Data\RegionInterfaceFactory;
15+
use Magento\Framework\Exception\InputException;
1516
use Magento\Framework\Exception\LocalizedException;
1617
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1718
use Magento\Quote\Model\Quote\Address as QuoteAddress;
@@ -89,8 +90,15 @@ public function execute(QuoteAddress $quoteAddress, int $customerId): void
8990
$customerAddress->setRegion($region);
9091

9192
$this->addressRepository->save($customerAddress);
92-
} catch (LocalizedException $e) {
93-
throw new GraphQlInputException(__($e->getMessage()), $e);
93+
} catch (InputException $inputException) {
94+
$graphQlInputException = new GraphQlInputException(__($inputException->getMessage()));
95+
$errors = $inputException->getErrors();
96+
foreach ($errors as $error) {
97+
$graphQlInputException->addError(new GraphQlInputException(__($error->getMessage())));
98+
}
99+
throw $graphQlInputException;
100+
} catch (LocalizedException $exception) {
101+
throw new GraphQlInputException(__($exception->getMessage()), $exception);
94102
}
95103
}
96104
}

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
use Magento\Customer\Helper\Address as AddressHelper;
1111
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
12+
use Magento\Directory\Api\CountryInformationAcquirerInterface;
1213
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
1315
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1416
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1517
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -36,36 +38,57 @@ class QuoteAddressFactory
3638
*/
3739
private $addressHelper;
3840

41+
/**
42+
* @var CountryInformationAcquirerInterface
43+
*/
44+
private $countryInformationAcquirer;
45+
3946
/**
4047
* @param BaseQuoteAddressFactory $quoteAddressFactory
4148
* @param GetCustomerAddress $getCustomerAddress
4249
* @param AddressHelper $addressHelper
50+
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
4351
*/
4452
public function __construct(
4553
BaseQuoteAddressFactory $quoteAddressFactory,
4654
GetCustomerAddress $getCustomerAddress,
47-
AddressHelper $addressHelper
55+
AddressHelper $addressHelper,
56+
CountryInformationAcquirerInterface $countryInformationAcquirer
4857
) {
4958
$this->quoteAddressFactory = $quoteAddressFactory;
5059
$this->getCustomerAddress = $getCustomerAddress;
5160
$this->addressHelper = $addressHelper;
61+
$this->countryInformationAcquirer = $countryInformationAcquirer;
5262
}
5363

5464
/**
5565
* Create QuoteAddress based on input data
5666
*
5767
* @param array $addressInput
68+
*
5869
* @return QuoteAddress
5970
* @throws GraphQlInputException
6071
*/
6172
public function createBasedOnInputData(array $addressInput): QuoteAddress
6273
{
6374
$addressInput['country_id'] = '';
64-
if ($addressInput['country_code']) {
75+
if (isset($addressInput['country_code']) && $addressInput['country_code']) {
6576
$addressInput['country_code'] = strtoupper($addressInput['country_code']);
6677
$addressInput['country_id'] = $addressInput['country_code'];
6778
}
6879

80+
if ($addressInput['country_id'] && isset($addressInput['region'])) {
81+
try {
82+
$countryInformation = $this->countryInformationAcquirer->getCountryInfo($addressInput['country_id']);
83+
} catch (NoSuchEntityException $e) {
84+
throw new GraphQlInputException(__('The country isn\'t available.'));
85+
}
86+
$availableRegions = $countryInformation->getAvailableRegions();
87+
if (null !== $availableRegions) {
88+
$addressInput['region_code'] = $addressInput['region'];
89+
}
90+
}
91+
6992
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
7093
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
7194
throw new GraphQlInputException(

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ private function createBillingAddress(
129129
(int)$context->getUserId()
130130
);
131131
}
132+
$errors = $billingAddress->validate();
133+
134+
if (true !== $errors) {
135+
$e = new GraphQlInputException(__('Billing address errors'));
136+
foreach ($errors as $error) {
137+
$e->addError(new GraphQlInputException($error));
138+
}
139+
throw $e;
140+
}
132141

133142
return $billingAddress;
134143
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1111
use Magento\GraphQl\Model\Query\ContextInterface;
1212
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\Quote\Model\Quote\Address;
1314

1415
/**
1516
* Set single shipping address for a specified shopping cart
@@ -52,6 +53,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
5253

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

56+
$errors = $shippingAddress->validate();
57+
58+
if (true !== $errors) {
59+
$e = new GraphQlInputException(__('Shipping address errors'));
60+
foreach ($errors as $error) {
61+
$e->addError(new GraphQlInputException($error));
62+
}
63+
throw $e;
64+
}
5565
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
5666
}
5767
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testSetNewBillingAddress()
9696
company: "test company"
9797
street: ["test street 1", "test street 2"]
9898
city: "test city"
99-
region: "test region"
99+
region: "AZ"
100100
postcode: "887766"
101101
country_code: "US"
102102
telephone: "88776655"
@@ -154,7 +154,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
154154
company: "test company"
155155
street: ["test street 1", "test street 2"]
156156
city: "test city"
157-
region: "test region"
157+
region: "AZ"
158158
postcode: "887766"
159159
country_code: "US"
160160
telephone: "88776655"
@@ -350,7 +350,7 @@ public function testSetNewBillingAddressAndFromAddressBookAtSameTime()
350350
company: "test company"
351351
street: ["test street 1", "test street 2"]
352352
city: "test city"
353-
region: "test region"
353+
region: "AZ"
354354
postcode: "887766"
355355
country_code: "US"
356356
telephone: "88776655"
@@ -431,7 +431,7 @@ public function testSetNewBillingAddressWithUseForShippingAndMultishipping()
431431
company: "test company"
432432
street: ["test street 1", "test street 2"]
433433
city: "test city"
434-
region: "test region"
434+
region: "AZ"
435435
postcode: "887766"
436436
country_code: "US"
437437
telephone: "88776655"
@@ -624,7 +624,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
624624
QUERY;
625625

626626
$this->expectExceptionMessage($message);
627-
$this->graphQlMutation($query);
627+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
628628
}
629629

630630
/**
@@ -641,6 +641,38 @@ public function dataProviderSetWithoutRequiredParameters(): array
641641
'missed_cart_id' => [
642642
'billing_address: {}',
643643
'Required parameter "cart_id" is missing'
644+
],
645+
'missed_region' => [
646+
'cart_id: "cart_id_value"
647+
billing_address: {
648+
address: {
649+
firstname: "test firstname"
650+
lastname: "test lastname"
651+
company: "test company"
652+
street: ["test street 1", "test street 2"]
653+
city: "test city"
654+
postcode: "887766"
655+
country_code: "US"
656+
telephone: "88776655"
657+
}
658+
}',
659+
'"regionId" is required. Enter and try again.'
660+
],
661+
'missed_multiple_fields' => [
662+
'cart_id: "cart_id_value"
663+
billing_address: {
664+
address: {
665+
firstname: "test firstname"
666+
lastname: "test lastname"
667+
company: "test company"
668+
street: ["test street 1", "test street 2"]
669+
city: "test city"
670+
country_code: "US"
671+
telephone: "88776655"
672+
}
673+
}',
674+
'"postcode" is required. Enter and try again.
675+
"regionId" is required. Enter and try again.'
644676
]
645677
];
646678
}
@@ -667,7 +699,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
667699
company: "test company"
668700
street: ["test street 1", "test street 2", "test street 3"]
669701
city: "test city"
670-
region: "test region"
702+
region: "AZ"
671703
postcode: "887766"
672704
country_code: "US"
673705
telephone: "88776655"
@@ -709,7 +741,7 @@ public function testSetBillingAddressWithLowerCaseCountry()
709741
company: "test company"
710742
street: ["test street 1", "test street 2"]
711743
city: "test city"
712-
region: "test region"
744+
region: "AZ"
713745
postcode: "887766"
714746
country_code: "us"
715747
telephone: "88776655"
@@ -766,7 +798,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
766798
company: "test company"
767799
street: ["test street 1", "test street 2"]
768800
city: "test city"
769-
region: "test region"
801+
region: "AZ"
770802
postcode: "887766"
771803
country_code: "US"
772804
telephone: "88776655"
@@ -833,7 +865,7 @@ public function testSetNewBillingAddressWithNotSaveInAddressBook()
833865
company: "test company"
834866
street: ["test street 1", "test street 2"]
835867
city: "test city"
836-
region: "test region"
868+
region: "AZ"
837869
postcode: "887766"
838870
country_code: "US"
839871
telephone: "88776655"
@@ -901,7 +933,7 @@ public function testWithInvalidBillingAddressInput()
901933
company: "test company"
902934
street: ["test street 1", "test street 2"]
903935
city: "test city"
904-
region: "test region"
936+
region: "AZ"
905937
postcode: "887766"
906938
country_code: "USS"
907939
telephone: "88776655"
@@ -928,7 +960,7 @@ public function testWithInvalidBillingAddressInput()
928960
}
929961
}
930962
QUERY;
931-
$this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
963+
$this->expectExceptionMessage('The country isn\'t available.');
932964
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
933965
}
934966

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct()
9999
company: "test company"
100100
street: ["test street 1", "test street 2"]
101101
city: "test city"
102-
region: "test region"
102+
region: "AZ"
103103
postcode: "887766"
104104
country_code: "US"
105105
telephone: "88776655"
@@ -164,7 +164,7 @@ public function testSetNewShippingAddressOnCartWithVirtualProduct()
164164
company: "test company"
165165
street: ["test street 1", "test street 2"]
166166
city: "test city"
167-
region: "test region"
167+
region: "AZ"
168168
postcode: "887766"
169169
country_code: "US"
170170
telephone: "88776655"
@@ -328,7 +328,7 @@ public function testSetNewShippingAddressAndFromAddressBookAtSameTime()
328328
company: "test company"
329329
street: ["test street 1", "test street 2"]
330330
city: "test city"
331-
region: "test region"
331+
region: "AZ"
332332
postcode: "887766"
333333
country_code: "US"
334334
telephone: "88776655"
@@ -479,7 +479,39 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
479479
'missed_cart_id' => [
480480
'shipping_addresses: {}',
481481
'Required parameter "cart_id" is missing'
482-
]
482+
],
483+
'missed_region' => [
484+
'cart_id: "cart_id_value"
485+
shipping_addresses: [{
486+
address: {
487+
firstname: "test firstname"
488+
lastname: "test lastname"
489+
company: "test company"
490+
street: ["test street 1", "test street 2"]
491+
city: "test city"
492+
postcode: "887766"
493+
country_code: "US"
494+
telephone: "88776655"
495+
}
496+
}]',
497+
'"regionId" is required. Enter and try again.'
498+
],
499+
'missed_multiple_fields' => [
500+
'cart_id: "cart_id_value"
501+
shipping_addresses: [{
502+
address: {
503+
firstname: "test firstname"
504+
lastname: "test lastname"
505+
company: "test company"
506+
street: ["test street 1", "test street 2"]
507+
city: "test city"
508+
country_code: "US"
509+
telephone: "88776655"
510+
}
511+
}]',
512+
'"postcode" is required. Enter and try again.
513+
"regionId" is required. Enter and try again.'
514+
],
483515
];
484516
}
485517

@@ -509,7 +541,7 @@ public function testSetMultipleNewShippingAddresses()
509541
company: "test company"
510542
street: ["test street 1", "test street 2"]
511543
city: "test city"
512-
region: "test region"
544+
region: "AZ"
513545
postcode: "887766"
514546
country_code: "US"
515547
telephone: "88776655"
@@ -522,7 +554,7 @@ public function testSetMultipleNewShippingAddresses()
522554
company: "test company 2"
523555
street: ["test street 1", "test street 2"]
524556
city: "test city"
525-
region: "test region"
557+
region: "AZ"
526558
postcode: "887766"
527559
country_code: "US"
528560
telephone: "88776655"
@@ -565,7 +597,7 @@ public function testSetNewShippingAddressOnCartWithRedundantStreetLine()
565597
company: "test company"
566598
street: ["test street 1", "test street 2", "test street 3"]
567599
city: "test city"
568-
region: "test region"
600+
region: "AZ"
569601
postcode: "887766"
570602
country_code: "US"
571603
telephone: "88776655"
@@ -748,7 +780,7 @@ public function testWithInvalidShippingAddressesInput()
748780
}
749781
}
750782
QUERY;
751-
$this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
783+
$this->expectExceptionMessage('The country isn\'t available.');
752784
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
753785
}
754786

@@ -774,7 +806,7 @@ public function testSetNewShippingAddressWithSaveInAddressBook()
774806
company: "test company"
775807
street: ["test street 1", "test street 2"]
776808
city: "test city"
777-
region: "test region"
809+
region: "AZ"
778810
postcode: "887766"
779811
country_code: "US"
780812
telephone: "88776655"
@@ -845,7 +877,7 @@ public function testSetNewShippingAddressWithNotSaveInAddressBook()
845877
company: "test company"
846878
street: ["test street 1", "test street 2"]
847879
city: "test city"
848-
region: "test region"
880+
region: "AZ"
849881
postcode: "887766"
850882
country_code: "US"
851883
telephone: "88776655"

0 commit comments

Comments
 (0)