Skip to content

Commit

Permalink
graphQl-512: added street lines validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Boyko committed Apr 3, 2019
1 parent 4a28abb commit 2d0eecc
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\QuoteAddress;

use Magento\Customer\Helper\Address as AddressHelper;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Validate Quote Address
*/
class Validator
{
/**
* @var AddressHelper
*/
private $addressHelper;

/**
* @param AddressHelper $addressHelper
*/
public function __construct(AddressHelper $addressHelper)
{
$this->addressHelper = $addressHelper;
}

/**
* Additional Quote Address validation for the GraphQl endpoint
*
* @param QuoteAddress $quoteAddress
* @throws GraphQlInputException
*/
public function validate(QuoteAddress $quoteAddress)
{
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
if (is_array($quoteAddress->getStreet()) && count($quoteAddress->getStreet()) > $maxAllowedLineCount) {
throw new GraphQlInputException(__('"Street Address" cannot contain more than %1 lines.', $maxAllowedLineCount));
}
}
}
14 changes: 13 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
use Magento\QuoteGraphQl\Model\Cart\QuoteAddress\Validator;

/**
* Create QuoteAddress
Expand All @@ -22,14 +23,21 @@ class QuoteAddressFactory
* @var BaseQuoteAddressFactory
*/
private $quoteAddressFactory;
/**
* @var Validator
*/
private $quoteAddressValidator;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
* @param Validator $quoteAddressValidator
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory
BaseQuoteAddressFactory $quoteAddressFactory,
Validator $quoteAddressValidator
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->quoteAddressValidator = $quoteAddressValidator;
}

/**
Expand All @@ -44,6 +52,8 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
$this->quoteAddressValidator->validate($quoteAddress);

return $quoteAddress;
}

Expand All @@ -62,6 +72,8 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress):
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
$this->quoteAddressValidator->validate($quoteAddress);

return $quoteAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,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/Catalog/_files/product_simple.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/Catalog/_files/product_simple.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/Catalog/_files/product_simple.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/Catalog/_files/product_simple.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 2d0eecc

Please sign in to comment.