From 2d0eecc3e3cab0bd1bf0f2c70d3ff097711d6304 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 3 Apr 2019 15:16:03 +0300 Subject: [PATCH 1/3] graphQl-512: added street lines validation --- .../Model/Cart/QuoteAddress/Validator.php | 45 +++++++++++++ .../Model/Cart/QuoteAddressFactory.php | 14 +++- .../Customer/CreateCustomerAddressTest.php | 66 +++++++++++++++++++ .../Customer/SetBillingAddressOnCartTest.php | 43 ++++++++++++ .../Customer/SetShippingAddressOnCartTest.php | 46 +++++++++++++ .../Guest/SetBillingAddressOnCartTest.php | 43 ++++++++++++ .../Guest/SetShippingAddressOnCartTest.php | 44 +++++++++++++ 7 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php new file mode 100644 index 000000000000..8cf2478d8f8b --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php @@ -0,0 +1,45 @@ +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)); + } + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 76bdc7461113..734843e1be09 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -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 @@ -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; } /** @@ -44,6 +52,8 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress $quoteAddress = $this->quoteAddressFactory->create(); $quoteAddress->addData($addressInput); + $this->quoteAddressValidator->validate($quoteAddress); + return $quoteAddress; } @@ -62,6 +72,8 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): } catch (LocalizedException $e) { throw new GraphQlInputException(__($e->getMessage()), $e); } + $this->quoteAddressValidator->validate($quoteAddress); + return $quoteAddress; } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php index 602d969924fb..4ea19f08e3c1 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -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 + = <<graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); + } + /** * Verify the fields for Customer address * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php index 88e7b93dd1d0..2ee5bb6708f4 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php @@ -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 = <<graphQlQuery($query, [], '', $this->getHeaderMap()); + } + /** * @return array */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index 5ff29d20b34d..f765851f2372 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -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 = <<graphQlQuery($query, [], '', $this->getHeaderMap()); + } + /** * Verify the all the whitelisted fields for a New Address Object * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php index ae0a1a0e822a..5fe0ae05903c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php @@ -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 = <<graphQlQuery($query); + } + /** * @return array */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php index e21d9ed64d49..f86b62149720 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php @@ -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 = <<graphQlQuery($query); + } + /** * @return array */ From c11b10bbfeccd8f142cec31495d944bd7f94b9c5 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Mon, 8 Apr 2019 13:31:29 -0500 Subject: [PATCH 2/3] GraphQL-512: 'address.street' needs refactoring to support as many lines as defined by admin --- .../Model/Cart/QuoteAddress/Validator.php | 45 ------------------- .../Model/Cart/QuoteAddressFactory.php | 19 +++++++- .../Customer/SetBillingAddressOnCartTest.php | 2 +- .../Customer/SetShippingAddressOnCartTest.php | 2 +- .../Guest/SetBillingAddressOnCartTest.php | 2 +- .../Guest/SetShippingAddressOnCartTest.php | 2 +- 6 files changed, 22 insertions(+), 50 deletions(-) delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php deleted file mode 100644 index 8cf2478d8f8b..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddress/Validator.php +++ /dev/null @@ -1,45 +0,0 @@ -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)); - } - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php index 582055bc6e13..13d6a1d3dce7 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php @@ -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; @@ -30,16 +31,24 @@ 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; } /** @@ -47,11 +56,19 @@ public function __construct( * * @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; diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php index f5bc6276b10e..b7b6fb585166 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php @@ -495,7 +495,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st /** * @magentoApiDataFixture Magento/Customer/_files/customer.php - * @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php index f731ff5b89db..0756ba6aeaf2 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php @@ -488,7 +488,7 @@ public function testSetMultipleNewShippingAddresses() /** * @magentoApiDataFixture Magento/Customer/_files/customer.php - * @magentoApiDataFixture Magento/Catalog/_files/product_simple.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 */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php index 421dbeb813a4..d253b6f39b0d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetBillingAddressOnCartTest.php @@ -314,7 +314,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st } /** - * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @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 */ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php index 38a835ba8923..a6981e11799f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/SetShippingAddressOnCartTest.php @@ -247,7 +247,7 @@ public function testSetNewShippingAddressWithMissedRequiredParameters(string $in } /** - * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @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 */ From 722afe3c9fe5c44783a54ae1b9d7feac33542d07 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Mon, 8 Apr 2019 15:08:33 -0500 Subject: [PATCH 3/3] GraphQL-512: 'address.street' needs refactoring to support as many lines as defined by admin --- .../Magento/GraphQl/Customer/CreateCustomerAddressTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php index 4ea19f08e3c1..264a2c7de478 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -13,6 +13,9 @@ use Magento\TestFramework\TestCase\GraphQlAbstract; use Magento\Integration\Api\CustomerTokenServiceInterface; +/** + * Create customer address tests + */ class CreateCustomerAddressTest extends GraphQlAbstract { /**