Skip to content

Commit

Permalink
graphQl-784: added validation for the lowercase country id
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Boyko committed Jul 24, 2019
1 parent 7ffabd0 commit e4c0752
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function __construct(
public function createBasedOnInputData(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
if ($addressInput['country_id'] && !ctype_upper($addressInput['country_code'] )) {
throw new GraphQlInputException(
__('"Country Code" cannot contain lowercase characters.')
);
}

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


$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}

if (null === $customerAddressId) {
$addressInput['country_code'] = strtoupper($addressInput['country_code']);
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,50 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @expectedException \Exception
* @expectedExceptionMessage "Country Code" cannot contain lowercase characters.
*/
public function testSetNewBillingAddressWithLowercaseCountryCode()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = <<<QUERY
mutation {
setBillingAddressOnCart(
input: {
cart_id: "$maskedQuoteId"
billing_address: {
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "us"
telephone: "88776655"
save_in_address_book: false
}
}
}
) {
cart {
billing_address {
firstname
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
* Verify the all the whitelisted fields for a New Address Object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,52 @@ public function testSetShippingAddressWithLowerCaseCountry()
$this->assertEquals('CA', $address['region']['code']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @expectedException \Exception
* @expectedExceptionMessage "Country Code" cannot contain lowercase characters.
*/
public function testSetNewShippingAddressOnCartWithLowercaseCountryCode()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = <<<QUERY
mutation {
setShippingAddressesOnCart(
input: {
cart_id: "$maskedQuoteId"
shipping_addresses: [
{
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "us"
telephone: "88776655"
save_in_address_book: false
}
}
]
}
) {
cart {
shipping_addresses {
firstname
}
}
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
* Verify the all the whitelisted fields for a New Address Object
*
Expand Down

0 comments on commit e4c0752

Please sign in to comment.