Skip to content

Commit e4c0752

Browse files
author
Vitaliy Boyko
committed
graphQl-784: added validation for the lowercase country id
1 parent 7ffabd0 commit e4c0752

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function __construct(
6161
public function createBasedOnInputData(array $addressInput): QuoteAddress
6262
{
6363
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
64+
if ($addressInput['country_id'] && !ctype_upper($addressInput['country_code'] )) {
65+
throw new GraphQlInputException(
66+
__('"Country Code" cannot contain lowercase characters.')
67+
);
68+
}
6469

6570
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
6671
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
@@ -69,6 +74,7 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
6974
);
7075
}
7176

77+
7278
$quoteAddress = $this->quoteAddressFactory->create();
7379
$quoteAddress->addData($addressInput);
7480
return $quoteAddress;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6666
}
6767

6868
if (null === $customerAddressId) {
69-
$addressInput['country_code'] = strtoupper($addressInput['country_code']);
7069
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
7170
} else {
7271
if (false === $context->getExtensionAttributes()->getIsCustomer()) {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,50 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
637637
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
638638
}
639639

640+
/**
641+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
642+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
643+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
644+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
645+
* @expectedException \Exception
646+
* @expectedExceptionMessage "Country Code" cannot contain lowercase characters.
647+
*/
648+
public function testSetNewBillingAddressWithLowercaseCountryCode()
649+
{
650+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
651+
652+
$query = <<<QUERY
653+
mutation {
654+
setBillingAddressOnCart(
655+
input: {
656+
cart_id: "$maskedQuoteId"
657+
billing_address: {
658+
address: {
659+
firstname: "test firstname"
660+
lastname: "test lastname"
661+
company: "test company"
662+
street: ["test street 1", "test street 2"]
663+
city: "test city"
664+
region: "test region"
665+
postcode: "887766"
666+
country_code: "us"
667+
telephone: "88776655"
668+
save_in_address_book: false
669+
}
670+
}
671+
}
672+
) {
673+
cart {
674+
billing_address {
675+
firstname
676+
}
677+
}
678+
}
679+
}
680+
QUERY;
681+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
682+
}
683+
640684
/**
641685
* Verify the all the whitelisted fields for a New Address Object
642686
*

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,52 @@ public function testSetShippingAddressWithLowerCaseCountry()
655655
$this->assertEquals('CA', $address['region']['code']);
656656
}
657657

658+
/**
659+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
660+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
661+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
662+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
663+
* @expectedException \Exception
664+
* @expectedExceptionMessage "Country Code" cannot contain lowercase characters.
665+
*/
666+
public function testSetNewShippingAddressOnCartWithLowercaseCountryCode()
667+
{
668+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
669+
670+
$query = <<<QUERY
671+
mutation {
672+
setShippingAddressesOnCart(
673+
input: {
674+
cart_id: "$maskedQuoteId"
675+
shipping_addresses: [
676+
{
677+
address: {
678+
firstname: "test firstname"
679+
lastname: "test lastname"
680+
company: "test company"
681+
street: ["test street 1", "test street 2"]
682+
city: "test city"
683+
region: "test region"
684+
postcode: "887766"
685+
country_code: "us"
686+
telephone: "88776655"
687+
save_in_address_book: false
688+
}
689+
}
690+
]
691+
}
692+
) {
693+
cart {
694+
shipping_addresses {
695+
firstname
696+
}
697+
}
698+
}
699+
}
700+
QUERY;
701+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
702+
}
703+
658704
/**
659705
* Verify the all the whitelisted fields for a New Address Object
660706
*

0 commit comments

Comments
 (0)