Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
ENGCOM-4710: graphQl-512: added street lines validation #562
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav authored Apr 8, 2019
2 parents 2393a36 + ac6d9b1 commit de85a5d
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Helper\Address as AddressHelper;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
Expand All @@ -30,28 +31,44 @@ class QuoteAddressFactory
*/
private $getCustomerAddress;

/**
* @var AddressHelper
*/
private $addressHelper;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
* @param GetCustomerAddress $getCustomerAddress
* @param AddressHelper $addressHelper
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory,
GetCustomerAddress $getCustomerAddress
GetCustomerAddress $getCustomerAddress,
AddressHelper $addressHelper
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomerAddress = $getCustomerAddress;
$this->addressHelper = $addressHelper;
}

/**
* Create QuoteAddress based on input data
*
* @param array $addressInput
* @return QuoteAddress
* @throws GraphQlInputException
*/
public function createBasedOnInputData(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';

$maxAllowedLineCount = $this->addressHelper->getStreetLines();
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
throw new GraphQlInputException(
__('"Street Address" cannot contain more than %1 lines.', $maxAllowedLineCount)
);
}

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Integration\Api\CustomerTokenServiceInterface;

/**
* Create customer address tests
*/
class CreateCustomerAddressTest extends GraphQlAbstract
{
/**
Expand Down Expand Up @@ -198,6 +201,72 @@ public function testCreateCustomerAddressWithMissingAttribute()
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testCreateCustomerAddressWithRedundantStreetLine()
{
$newAddress = [
'region' => [
'region' => 'Arizona',
'region_id' => 4,
'region_code' => 'AZ'
],
'country_id' => 'US',
'street' => ['Line 1 Street', 'Line 2', 'Line 3'],
'company' => 'Company name',
'telephone' => '123456789',
'fax' => '123123123',
'postcode' => '7777',
'city' => 'City Name',
'firstname' => 'Adam',
'lastname' => 'Phillis',
'middlename' => 'A',
'prefix' => 'Mr.',
'suffix' => 'Jr.',
'vat_id' => '1',
'default_shipping' => true,
'default_billing' => false
];

$mutation
= <<<MUTATION
mutation {
createCustomerAddress(input: {
region: {
region: "{$newAddress['region']['region']}"
region_id: {$newAddress['region']['region_id']}
region_code: "{$newAddress['region']['region_code']}"
}
country_id: {$newAddress['country_id']}
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}","{$newAddress['street'][2]}"]
company: "{$newAddress['company']}"
telephone: "{$newAddress['telephone']}"
fax: "{$newAddress['fax']}"
postcode: "{$newAddress['postcode']}"
city: "{$newAddress['city']}"
firstname: "{$newAddress['firstname']}"
lastname: "{$newAddress['lastname']}"
middlename: "{$newAddress['middlename']}"
prefix: "{$newAddress['prefix']}"
suffix: "{$newAddress['suffix']}"
vat_id: "{$newAddress['vat_id']}"
default_shipping: true
default_billing: false
}) {
id
}
}
MUTATION;

$userName = 'customer@example.com';
$password = 'password';

self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
}

/**
* Verify the fields for Customer address
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,49 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
$this->graphQlQuery($query);
}

/**
* @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
*/
public function testSetNewBillingAddressWithRedundantStreetLine()
{
$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", "test street 3"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: false
}
}
}
) {
cart {
billing_address {
firstname
}
}
}
}
QUERY;
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,52 @@ public function testSetMultipleNewShippingAddresses()
$this->graphQlQuery($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
*/
public function testSetNewShippingAddressOnCartWithRedundantStreetLine()
{
$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", "test street 3"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: false
}
}
]
}
) {
cart {
shipping_addresses {
firstname
}
}
}
}
QUERY;

self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($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 @@ -313,6 +313,49 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetNewBillingAddressRedundantStreetLine()
{
$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", "test street 3"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: false
}
}
}
) {
cart {
billing_address {
firstname
}
}
}
}
QUERY;

self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($query);
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,50 @@ public function testSetNewShippingAddressWithMissedRequiredParameters(string $in
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetNewShippingAddressOnCartWithRedundantStreetLine()
{
$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", "test street 3"]
city: "test city"
region: "test region"
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: false
}
}
]
}
) {
cart {
shipping_addresses {
firstname
}
}
}
}
QUERY;
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($query);
}

/**
* @return array
*/
Expand Down

0 comments on commit de85a5d

Please sign in to comment.