Skip to content

Commit ac6d9b1

Browse files
ENGCOM-4710: graphQl-512: added street lines validation #562
- Merge Pull Request magento/graphql-ce#562 from magento/graphql-ce:graphQl-512-address.street-validation - Merged commits: 1. 2d0eecc 2. 27f7725 3. c11b10b 4. 722afe3
2 parents 7e6e098 + 722afe3 commit ac6d9b1

File tree

6 files changed

+263
-1
lines changed

6 files changed

+263
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Customer\Helper\Address as AddressHelper;
1011
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1112
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
@@ -30,28 +31,44 @@ class QuoteAddressFactory
3031
*/
3132
private $getCustomerAddress;
3233

34+
/**
35+
* @var AddressHelper
36+
*/
37+
private $addressHelper;
38+
3339
/**
3440
* @param BaseQuoteAddressFactory $quoteAddressFactory
3541
* @param GetCustomerAddress $getCustomerAddress
42+
* @param AddressHelper $addressHelper
3643
*/
3744
public function __construct(
3845
BaseQuoteAddressFactory $quoteAddressFactory,
39-
GetCustomerAddress $getCustomerAddress
46+
GetCustomerAddress $getCustomerAddress,
47+
AddressHelper $addressHelper
4048
) {
4149
$this->quoteAddressFactory = $quoteAddressFactory;
4250
$this->getCustomerAddress = $getCustomerAddress;
51+
$this->addressHelper = $addressHelper;
4352
}
4453

4554
/**
4655
* Create QuoteAddress based on input data
4756
*
4857
* @param array $addressInput
4958
* @return QuoteAddress
59+
* @throws GraphQlInputException
5060
*/
5161
public function createBasedOnInputData(array $addressInput): QuoteAddress
5262
{
5363
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
5464

65+
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
66+
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
67+
throw new GraphQlInputException(
68+
__('"Street Address" cannot contain more than %1 lines.', $maxAllowedLineCount)
69+
);
70+
}
71+
5572
$quoteAddress = $this->quoteAddressFactory->create();
5673
$quoteAddress->addData($addressInput);
5774
return $quoteAddress;

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414
use Magento\Integration\Api\CustomerTokenServiceInterface;
1515

16+
/**
17+
* Create customer address tests
18+
*/
1619
class CreateCustomerAddressTest extends GraphQlAbstract
1720
{
1821
/**
@@ -198,6 +201,72 @@ public function testCreateCustomerAddressWithMissingAttribute()
198201
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
199202
}
200203

204+
/**
205+
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
206+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
207+
*/
208+
public function testCreateCustomerAddressWithRedundantStreetLine()
209+
{
210+
$newAddress = [
211+
'region' => [
212+
'region' => 'Arizona',
213+
'region_id' => 4,
214+
'region_code' => 'AZ'
215+
],
216+
'country_id' => 'US',
217+
'street' => ['Line 1 Street', 'Line 2', 'Line 3'],
218+
'company' => 'Company name',
219+
'telephone' => '123456789',
220+
'fax' => '123123123',
221+
'postcode' => '7777',
222+
'city' => 'City Name',
223+
'firstname' => 'Adam',
224+
'lastname' => 'Phillis',
225+
'middlename' => 'A',
226+
'prefix' => 'Mr.',
227+
'suffix' => 'Jr.',
228+
'vat_id' => '1',
229+
'default_shipping' => true,
230+
'default_billing' => false
231+
];
232+
233+
$mutation
234+
= <<<MUTATION
235+
mutation {
236+
createCustomerAddress(input: {
237+
region: {
238+
region: "{$newAddress['region']['region']}"
239+
region_id: {$newAddress['region']['region_id']}
240+
region_code: "{$newAddress['region']['region_code']}"
241+
}
242+
country_id: {$newAddress['country_id']}
243+
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}","{$newAddress['street'][2]}"]
244+
company: "{$newAddress['company']}"
245+
telephone: "{$newAddress['telephone']}"
246+
fax: "{$newAddress['fax']}"
247+
postcode: "{$newAddress['postcode']}"
248+
city: "{$newAddress['city']}"
249+
firstname: "{$newAddress['firstname']}"
250+
lastname: "{$newAddress['lastname']}"
251+
middlename: "{$newAddress['middlename']}"
252+
prefix: "{$newAddress['prefix']}"
253+
suffix: "{$newAddress['suffix']}"
254+
vat_id: "{$newAddress['vat_id']}"
255+
default_shipping: true
256+
default_billing: false
257+
}) {
258+
id
259+
}
260+
}
261+
MUTATION;
262+
263+
$userName = 'customer@example.com';
264+
$password = 'password';
265+
266+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
267+
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
268+
}
269+
201270
/**
202271
* Verify the fields for Customer address
203272
*

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,49 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
493493
$this->graphQlQuery($query);
494494
}
495495

496+
/**
497+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
498+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
499+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
500+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
501+
*/
502+
public function testSetNewBillingAddressWithRedundantStreetLine()
503+
{
504+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
505+
506+
$query = <<<QUERY
507+
mutation {
508+
setBillingAddressOnCart(
509+
input: {
510+
cart_id: "$maskedQuoteId"
511+
billing_address: {
512+
address: {
513+
firstname: "test firstname"
514+
lastname: "test lastname"
515+
company: "test company"
516+
street: ["test street 1", "test street 2", "test street 3"]
517+
city: "test city"
518+
region: "test region"
519+
postcode: "887766"
520+
country_code: "US"
521+
telephone: "88776655"
522+
save_in_address_book: false
523+
}
524+
}
525+
}
526+
) {
527+
cart {
528+
billing_address {
529+
firstname
530+
}
531+
}
532+
}
533+
}
534+
QUERY;
535+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
536+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
537+
}
538+
496539
/**
497540
* @return array
498541
*/

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
@@ -486,6 +486,52 @@ public function testSetMultipleNewShippingAddresses()
486486
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
487487
}
488488

489+
/**
490+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
491+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
492+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
493+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
494+
*/
495+
public function testSetNewShippingAddressOnCartWithRedundantStreetLine()
496+
{
497+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
498+
499+
$query = <<<QUERY
500+
mutation {
501+
setShippingAddressesOnCart(
502+
input: {
503+
cart_id: "$maskedQuoteId"
504+
shipping_addresses: [
505+
{
506+
address: {
507+
firstname: "test firstname"
508+
lastname: "test lastname"
509+
company: "test company"
510+
street: ["test street 1", "test street 2", "test street 3"]
511+
city: "test city"
512+
region: "test region"
513+
postcode: "887766"
514+
country_code: "US"
515+
telephone: "88776655"
516+
save_in_address_book: false
517+
}
518+
}
519+
]
520+
}
521+
) {
522+
cart {
523+
shipping_addresses {
524+
firstname
525+
}
526+
}
527+
}
528+
}
529+
QUERY;
530+
531+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
532+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
533+
}
534+
489535
/**
490536
* Verify the all the whitelisted fields for a New Address Object
491537
*

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,49 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
313313
$this->graphQlQuery($query);
314314
}
315315

316+
/**
317+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
318+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
319+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
320+
*/
321+
public function testSetNewBillingAddressRedundantStreetLine()
322+
{
323+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
324+
325+
$query = <<<QUERY
326+
mutation {
327+
setBillingAddressOnCart(
328+
input: {
329+
cart_id: "$maskedQuoteId"
330+
billing_address: {
331+
address: {
332+
firstname: "test firstname"
333+
lastname: "test lastname"
334+
company: "test company"
335+
street: ["test street 1", "test street 2", "test street 3"]
336+
city: "test city"
337+
region: "test region"
338+
postcode: "887766"
339+
country_code: "US"
340+
telephone: "88776655"
341+
save_in_address_book: false
342+
}
343+
}
344+
}
345+
) {
346+
cart {
347+
billing_address {
348+
firstname
349+
}
350+
}
351+
}
352+
}
353+
QUERY;
354+
355+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
356+
$this->graphQlQuery($query);
357+
}
358+
316359
/**
317360
* @return array
318361
*/

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,50 @@ public function testSetNewShippingAddressWithMissedRequiredParameters(string $in
246246
$this->graphQlQuery($query);
247247
}
248248

249+
/**
250+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
251+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
252+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
253+
*/
254+
public function testSetNewShippingAddressOnCartWithRedundantStreetLine()
255+
{
256+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
257+
258+
$query = <<<QUERY
259+
mutation {
260+
setShippingAddressesOnCart(
261+
input: {
262+
cart_id: "$maskedQuoteId"
263+
shipping_addresses: [
264+
{
265+
address: {
266+
firstname: "test firstname"
267+
lastname: "test lastname"
268+
company: "test company"
269+
street: ["test street 1", "test street 2", "test street 3"]
270+
city: "test city"
271+
region: "test region"
272+
postcode: "887766"
273+
country_code: "US"
274+
telephone: "88776655"
275+
save_in_address_book: false
276+
}
277+
}
278+
]
279+
}
280+
) {
281+
cart {
282+
shipping_addresses {
283+
firstname
284+
}
285+
}
286+
}
287+
}
288+
QUERY;
289+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
290+
$this->graphQlQuery($query);
291+
}
292+
249293
/**
250294
* @return array
251295
*/

0 commit comments

Comments
 (0)