Skip to content

Commit c0f9fb7

Browse files
committed
Merge branch '2.3-develop-graphql-prs' of github.com:magento-engcom/magento2ce into fix-nightly-fails-with-msi
2 parents 89cf52c + 5459ada commit c0f9fb7

File tree

10 files changed

+313
-46
lines changed

10 files changed

+313
-46
lines changed

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

+10-2
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

+46-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1616
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1717
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
18+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
19+
use Magento\Directory\Helper\Data as CountryHelper;
20+
use Magento\Directory\Model\AllowedCountries;
1821

1922
/**
2023
* Create QuoteAddress
@@ -36,36 +39,77 @@ class QuoteAddressFactory
3639
*/
3740
private $addressHelper;
3841

42+
/**
43+
* @var RegionCollectionFactory
44+
*/
45+
private $regionCollectionFactory;
46+
47+
/**
48+
* @var CountryHelper
49+
*/
50+
private $countryHelper;
51+
52+
/**
53+
* @var AllowedCountries
54+
*/
55+
private $allowedCountries;
56+
3957
/**
4058
* @param BaseQuoteAddressFactory $quoteAddressFactory
4159
* @param GetCustomerAddress $getCustomerAddress
4260
* @param AddressHelper $addressHelper
61+
* @param RegionCollectionFactory $regionCollectionFactory
62+
* @param CountryHelper $countryHelper
63+
* @param AllowedCountries $allowedCountries
4364
*/
4465
public function __construct(
4566
BaseQuoteAddressFactory $quoteAddressFactory,
4667
GetCustomerAddress $getCustomerAddress,
47-
AddressHelper $addressHelper
68+
AddressHelper $addressHelper,
69+
RegionCollectionFactory $regionCollectionFactory,
70+
CountryHelper $countryHelper,
71+
AllowedCountries $allowedCountries
4872
) {
4973
$this->quoteAddressFactory = $quoteAddressFactory;
5074
$this->getCustomerAddress = $getCustomerAddress;
5175
$this->addressHelper = $addressHelper;
76+
$this->regionCollectionFactory = $regionCollectionFactory;
77+
$this->countryHelper = $countryHelper;
78+
$this->allowedCountries = $allowedCountries;
5279
}
5380

5481
/**
5582
* Create QuoteAddress based on input data
5683
*
5784
* @param array $addressInput
85+
*
5886
* @return QuoteAddress
5987
* @throws GraphQlInputException
6088
*/
6189
public function createBasedOnInputData(array $addressInput): QuoteAddress
6290
{
6391
$addressInput['country_id'] = '';
64-
if ($addressInput['country_code']) {
92+
if (isset($addressInput['country_code']) && $addressInput['country_code']) {
6593
$addressInput['country_code'] = strtoupper($addressInput['country_code']);
6694
$addressInput['country_id'] = $addressInput['country_code'];
6795
}
6896

97+
$allowedCountries = $this->allowedCountries->getAllowedCountries();
98+
if (!in_array($addressInput['country_code'], $allowedCountries, true)) {
99+
throw new GraphQlInputException(__('Country is not available'));
100+
}
101+
$isRegionRequired = $this->countryHelper->isRegionRequired($addressInput['country_code']);
102+
if ($isRegionRequired && !empty($addressInput['region'])) {
103+
$regionCollection = $this->regionCollectionFactory
104+
->create()
105+
->addRegionCodeFilter($addressInput['region'])
106+
->addCountryFilter($addressInput['country_code']);
107+
if ($regionCollection->getSize() === 0) {
108+
throw new GraphQlInputException(
109+
__('Region is not available for the selected country')
110+
);
111+
}
112+
}
69113
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
70114
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
71115
throw new GraphQlInputException(

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

+9
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

+10
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

+109-12
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,7 +661,55 @@ public function dataProviderSetWithoutRequiredParameters(): array
661661
'missed_cart_id' => [
662662
'billing_address: {}',
663663
'Required parameter "cart_id" is missing'
664-
]
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.'
696+
],
697+
'wrong_required_region' => [
698+
'cart_id: "cart_id_value"
699+
billing_address: {
700+
address: {
701+
firstname: "test firstname"
702+
lastname: "test lastname"
703+
company: "test company"
704+
street: ["test street 1", "test street 2"]
705+
region: "wrong region"
706+
city: "test city"
707+
country_code: "US"
708+
telephone: "88776655"
709+
}
710+
}',
711+
'Region is not available for the selected country'
712+
],
665713
];
666714
}
667715

@@ -687,7 +735,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
687735
company: "test company"
688736
street: ["test street 1", "test street 2", "test street 3"]
689737
city: "test city"
690-
region: "test region"
738+
region: "AZ"
691739
postcode: "887766"
692740
country_code: "US"
693741
telephone: "88776655"
@@ -729,7 +777,7 @@ public function testSetBillingAddressWithLowerCaseCountry()
729777
company: "test company"
730778
street: ["test street 1", "test street 2"]
731779
city: "test city"
732-
region: "test region"
780+
region: "AZ"
733781
postcode: "887766"
734782
country_code: "us"
735783
telephone: "88776655"
@@ -786,7 +834,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
786834
company: "test company"
787835
street: ["test street 1", "test street 2"]
788836
city: "test city"
789-
region: "test region"
837+
region: "AZ"
790838
postcode: "887766"
791839
country_code: "US"
792840
telephone: "88776655"
@@ -853,7 +901,7 @@ public function testSetNewBillingAddressWithNotSaveInAddressBook()
853901
company: "test company"
854902
street: ["test street 1", "test street 2"]
855903
city: "test city"
856-
region: "test region"
904+
region: "AZ"
857905
postcode: "887766"
858906
country_code: "US"
859907
telephone: "88776655"
@@ -921,7 +969,7 @@ public function testWithInvalidBillingAddressInput()
921969
company: "test company"
922970
street: ["test street 1", "test street 2"]
923971
city: "test city"
924-
region: "test region"
972+
region: "AZ"
925973
postcode: "887766"
926974
country_code: "USS"
927975
telephone: "88776655"
@@ -948,10 +996,59 @@ public function testWithInvalidBillingAddressInput()
948996
}
949997
}
950998
QUERY;
951-
$this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
999+
$this->expectExceptionMessage('Country is not available');
9521000
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
9531001
}
9541002

1003+
/**
1004+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
1005+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
1006+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
1007+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
1008+
*/
1009+
public function testSetShippingAddressesWithNotRequiredRegion()
1010+
{
1011+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
1012+
1013+
$query = <<<QUERY
1014+
mutation {
1015+
setBillingAddressOnCart(
1016+
input: {
1017+
cart_id: "$maskedQuoteId"
1018+
billing_address: {
1019+
address: {
1020+
firstname: "Vasyl"
1021+
lastname: "Doe"
1022+
street: ["1 Svobody"]
1023+
city: "Lviv"
1024+
region: "Lviv"
1025+
postcode: "00000"
1026+
country_code: "UA"
1027+
telephone: "555-555-55-55"
1028+
}
1029+
}
1030+
}
1031+
) {
1032+
cart {
1033+
billing_address {
1034+
region {
1035+
label
1036+
}
1037+
country {
1038+
code
1039+
}
1040+
}
1041+
}
1042+
}
1043+
}
1044+
QUERY;
1045+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
1046+
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
1047+
$cartResponse = $response['setBillingAddressOnCart']['cart'];
1048+
self::assertEquals('UA', $cartResponse['billing_address']['country']['code']);
1049+
self::assertEquals('Lviv', $cartResponse['billing_address']['region']['label']);
1050+
}
1051+
9551052
/**
9561053
* Verify the all the whitelisted fields for a New Address Object
9571054
*

0 commit comments

Comments
 (0)