Skip to content

Commit e2dca09

Browse files
authored
ENGCOM-6170: graphQl-961: ShippingAddressInput.postcode: String, is not required by Schema #969
2 parents 67f525d + d8ea43d commit e2dca09

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
@@ -132,6 +132,15 @@ private function createBillingAddress(
132132
(int)$context->getUserId()
133133
);
134134
}
135+
$errors = $billingAddress->validate();
136+
137+
if (true !== $errors) {
138+
$e = new GraphQlInputException(__('Billing address errors'));
139+
foreach ($errors as $error) {
140+
$e->addError(new GraphQlInputException($error));
141+
}
142+
throw $e;
143+
}
135144

136145
return $billingAddress;
137146
}

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"
@@ -174,7 +174,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
174174
company: "test company"
175175
street: ["test street 1", "test street 2"]
176176
city: "test city"
177-
region: "test region"
177+
region: "AZ"
178178
postcode: "887766"
179179
country_code: "US"
180180
telephone: "88776655"
@@ -370,7 +370,7 @@ public function testSetNewBillingAddressAndFromAddressBookAtSameTime()
370370
company: "test company"
371371
street: ["test street 1", "test street 2"]
372372
city: "test city"
373-
region: "test region"
373+
region: "AZ"
374374
postcode: "887766"
375375
country_code: "US"
376376
telephone: "88776655"
@@ -451,7 +451,7 @@ public function testSetNewBillingAddressWithSameAsShippingAndMultishipping()
451451
company: "test company"
452452
street: ["test street 1", "test street 2"]
453453
city: "test city"
454-
region: "test region"
454+
region: "AZ"
455455
postcode: "887766"
456456
country_code: "US"
457457
telephone: "88776655"
@@ -644,7 +644,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
644644
QUERY;
645645

646646
$this->expectExceptionMessage($message);
647-
$this->graphQlMutation($query);
647+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
648648
}
649649

650650
/**
@@ -661,6 +661,38 @@ public function dataProviderSetWithoutRequiredParameters(): array
661661
'missed_cart_id' => [
662662
'billing_address: {}',
663663
'Required parameter "cart_id" is missing'
664+
],
665+
'missed_region' => [
666+
'cart_id: "cart_id_value"
667+
billing_address: {
668+
address: {
669+
firstname: "test firstname"
670+
lastname: "test lastname"
671+
company: "test company"
672+
street: ["test street 1", "test street 2"]
673+
city: "test city"
674+
postcode: "887766"
675+
country_code: "US"
676+
telephone: "88776655"
677+
}
678+
}',
679+
'"regionId" is required. Enter and try again.'
680+
],
681+
'missed_multiple_fields' => [
682+
'cart_id: "cart_id_value"
683+
billing_address: {
684+
address: {
685+
firstname: "test firstname"
686+
lastname: "test lastname"
687+
company: "test company"
688+
street: ["test street 1", "test street 2"]
689+
city: "test city"
690+
country_code: "US"
691+
telephone: "88776655"
692+
}
693+
}',
694+
'"postcode" is required. Enter and try again.
695+
"regionId" is required. Enter and try again.'
664696
]
665697
];
666698
}
@@ -687,7 +719,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
687719
company: "test company"
688720
street: ["test street 1", "test street 2", "test street 3"]
689721
city: "test city"
690-
region: "test region"
722+
region: "AZ"
691723
postcode: "887766"
692724
country_code: "US"
693725
telephone: "88776655"
@@ -729,7 +761,7 @@ public function testSetBillingAddressWithLowerCaseCountry()
729761
company: "test company"
730762
street: ["test street 1", "test street 2"]
731763
city: "test city"
732-
region: "test region"
764+
region: "AZ"
733765
postcode: "887766"
734766
country_code: "us"
735767
telephone: "88776655"
@@ -786,7 +818,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
786818
company: "test company"
787819
street: ["test street 1", "test street 2"]
788820
city: "test city"
789-
region: "test region"
821+
region: "AZ"
790822
postcode: "887766"
791823
country_code: "US"
792824
telephone: "88776655"
@@ -853,7 +885,7 @@ public function testSetNewBillingAddressWithNotSaveInAddressBook()
853885
company: "test company"
854886
street: ["test street 1", "test street 2"]
855887
city: "test city"
856-
region: "test region"
888+
region: "AZ"
857889
postcode: "887766"
858890
country_code: "US"
859891
telephone: "88776655"
@@ -921,7 +953,7 @@ public function testWithInvalidBillingAddressInput()
921953
company: "test company"
922954
street: ["test street 1", "test street 2"]
923955
city: "test city"
924-
region: "test region"
956+
region: "AZ"
925957
postcode: "887766"
926958
country_code: "USS"
927959
telephone: "88776655"
@@ -948,7 +980,7 @@ public function testWithInvalidBillingAddressInput()
948980
}
949981
}
950982
QUERY;
951-
$this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
983+
$this->expectExceptionMessage('The country isn\'t available.');
952984
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
953985
}
954986

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)