Skip to content

Commit 71eb365

Browse files
committed
magento graphql-ce#961 ShippingAddressInput postcode String, is not required by Schema
1 parent 78c1cfd commit 71eb365

File tree

3 files changed

+173
-20
lines changed

3 files changed

+173
-20
lines changed

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
use Magento\Customer\Helper\Address as AddressHelper;
1111
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
12-
use Magento\Directory\Api\CountryInformationAcquirerInterface;
1312
use Magento\Framework\Exception\LocalizedException;
14-
use Magento\Framework\Exception\NoSuchEntityException;
1513
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1614
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1715
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1816
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1917
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;
2021

2122
/**
2223
* Create QuoteAddress
@@ -39,26 +40,42 @@ class QuoteAddressFactory
3940
private $addressHelper;
4041

4142
/**
42-
* @var CountryInformationAcquirerInterface
43+
* @var RegionCollectionFactory
4344
*/
44-
private $countryInformationAcquirer;
45+
private $regionCollectionFactory;
46+
47+
/**
48+
* @var CountryHelper
49+
*/
50+
private $countryHelper;
51+
52+
/**
53+
* @var AllowedCountries
54+
*/
55+
private $allowedCountries;
4556

4657
/**
4758
* @param BaseQuoteAddressFactory $quoteAddressFactory
4859
* @param GetCustomerAddress $getCustomerAddress
4960
* @param AddressHelper $addressHelper
50-
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
61+
* @param RegionCollectionFactory $regionCollectionFactory
62+
* @param CountryHelper $countryHelper
63+
* @param AllowedCountries $allowedCountries
5164
*/
5265
public function __construct(
5366
BaseQuoteAddressFactory $quoteAddressFactory,
5467
GetCustomerAddress $getCustomerAddress,
5568
AddressHelper $addressHelper,
56-
CountryInformationAcquirerInterface $countryInformationAcquirer
69+
RegionCollectionFactory $regionCollectionFactory,
70+
CountryHelper $countryHelper,
71+
AllowedCountries $allowedCountries
5772
) {
5873
$this->quoteAddressFactory = $quoteAddressFactory;
5974
$this->getCustomerAddress = $getCustomerAddress;
6075
$this->addressHelper = $addressHelper;
61-
$this->countryInformationAcquirer = $countryInformationAcquirer;
76+
$this->regionCollectionFactory = $regionCollectionFactory;
77+
$this->countryHelper = $countryHelper;
78+
$this->allowedCountries = $allowedCountries;
6279
}
6380

6481
/**
@@ -77,18 +94,22 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
7794
$addressInput['country_id'] = $addressInput['country_code'];
7895
}
7996

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'];
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+
);
89111
}
90112
}
91-
92113
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
93114
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
94115
throw new GraphQlInputException(

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,23 @@ public function dataProviderSetWithoutRequiredParameters(): array
693693
}',
694694
'"postcode" is required. Enter and try again.
695695
"regionId" is required. Enter and try again.'
696-
]
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+
],
697713
];
698714
}
699715

@@ -980,10 +996,59 @@ public function testWithInvalidBillingAddressInput()
980996
}
981997
}
982998
QUERY;
983-
$this->expectExceptionMessage('The country isn\'t available.');
999+
$this->expectExceptionMessage('Country is not available');
9841000
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
9851001
}
9861002

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+
9871052
/**
9881053
* Verify the all the whitelisted fields for a New Address Object
9891054
*

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,22 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
512512
'"postcode" is required. Enter and try again.
513513
"regionId" is required. Enter and try again.'
514514
],
515+
'wrong_required_region' => [
516+
'cart_id: "cart_id_value"
517+
shipping_addresses: [{
518+
address: {
519+
firstname: "test firstname"
520+
lastname: "test lastname"
521+
company: "test company"
522+
street: ["test street 1", "test street 2"]
523+
region: "wrong region"
524+
city: "test city"
525+
country_code: "US"
526+
telephone: "88776655"
527+
}
528+
}]',
529+
'Region is not available for the selected country'
530+
],
515531
];
516532
}
517533

@@ -780,10 +796,61 @@ public function testWithInvalidShippingAddressesInput()
780796
}
781797
}
782798
QUERY;
783-
$this->expectExceptionMessage('The country isn\'t available.');
799+
$this->expectExceptionMessage('Country is not available');
784800
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
785801
}
786802

803+
/**
804+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
805+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
806+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
807+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
808+
*/
809+
public function testSetShippingAddressesWithNotRequiredRegion()
810+
{
811+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
812+
813+
$query = <<<QUERY
814+
mutation {
815+
setShippingAddressesOnCart(
816+
input: {
817+
cart_id: "$maskedQuoteId"
818+
shipping_addresses: [
819+
{
820+
address: {
821+
firstname: "Vasyl"
822+
lastname: "Doe"
823+
street: ["1 Svobody"]
824+
city: "Lviv"
825+
region: "Lviv"
826+
postcode: "00000"
827+
country_code: "UA"
828+
telephone: "555-555-55-55"
829+
}
830+
}
831+
]
832+
}
833+
) {
834+
cart {
835+
shipping_addresses {
836+
region {
837+
label
838+
}
839+
country {
840+
code
841+
}
842+
}
843+
}
844+
}
845+
}
846+
QUERY;
847+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
848+
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
849+
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
850+
self::assertEquals('UA', $cartResponse['shipping_addresses'][0]['country']['code']);
851+
self::assertEquals('Lviv', $cartResponse['shipping_addresses'][0]['region']['label']);
852+
}
853+
787854
/**
788855
* @magentoApiDataFixture Magento/Customer/_files/customer.php
789856
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php

0 commit comments

Comments
 (0)