Skip to content

Commit 3397b2b

Browse files
🔃 [Magento Community Engineering] Community Contributions - GraphQL
Accepted Community Pull Requests: - magento/graphql-ce#1059: magento/graphql-ce#: deleteCustomerAddress. [Test coverage] Address "id" value should be specified. (by @atwixfirster) - magento/graphql-ce#1056: Refactor Magento\GraphQl\Quote\Customer\GetCustomerCartTest (by @TomashKhamlai) - magento/graphql-ce#1053: magento/devdocs#: createCustomer. Test coverage. Case: create new customer with the email of already existent user (by @atwixfirster) - magento/graphql-ce#1052: magento/graphql-ce# Remove redundant logic in createCustomer mutation (by @atwixfirster) - magento/graphql-ce#1031: #1029 Add Postcode as required depending of the country Mutation createCustomerAddress (by @osrecio) - magento/graphql-ce#1023: Filter error array by unique values (by @paul-stolk-webdiensten) - magento/graphql-ce#1051: magento/graphql-ce#: Add missed annotation blocks to Magento\GraphQl\Customer\CreateCustomerTest (by @atwixfirster) Fixed GitHub Issues: - magento/graphql-ce#1029: Postcode is not listed in the array of errors (reported by @TomashKhamlai) has been fixed in magento/graphql-ce#1031 by @osrecio in 2.3-develop branch Related commits: 1. 676cd1b 2. 127835e 3. 891e5a4 - magento/graphql-ce#416: graphql-input provides to Customer as many errors as appeared instead of last one like on Magento Storefront (reported by @TomashKhamlai) has been fixed in magento/graphql-ce#1023 by @paul-stolk-webdiensten in 2.3-develop branch Related commits: 1. 18fb482 2. de92f12 3. 4a89312 4. cbeb99c
2 parents 9fb7790 + 74999e8 commit 3397b2b

16 files changed

+147
-83
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Customer\Api\Data\AddressInterface;
1212
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Directory\Helper\Data as DirectoryData;
14+
use Magento\Framework\Api\DataObjectHelper;
1315
use Magento\Framework\Exception\LocalizedException;
1416
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15-
use Magento\Framework\Api\DataObjectHelper;
1617

1718
/**
1819
* Create customer address
@@ -38,23 +39,30 @@ class CreateCustomerAddress
3839
* @var DataObjectHelper
3940
*/
4041
private $dataObjectHelper;
42+
/**
43+
* @var DirectoryData
44+
*/
45+
private $directoryData;
4146

4247
/**
4348
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
4449
* @param AddressInterfaceFactory $addressFactory
4550
* @param AddressRepositoryInterface $addressRepository
4651
* @param DataObjectHelper $dataObjectHelper
52+
* @param DirectoryData $directoryData
4753
*/
4854
public function __construct(
4955
GetAllowedAddressAttributes $getAllowedAddressAttributes,
5056
AddressInterfaceFactory $addressFactory,
5157
AddressRepositoryInterface $addressRepository,
52-
DataObjectHelper $dataObjectHelper
58+
DataObjectHelper $dataObjectHelper,
59+
DirectoryData $directoryData
5360
) {
5461
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
5562
$this->addressFactory = $addressFactory;
5663
$this->addressRepository = $addressRepository;
5764
$this->dataObjectHelper = $dataObjectHelper;
65+
$this->directoryData = $directoryData;
5866
}
5967

6068
/**
@@ -102,6 +110,13 @@ public function validateData(array $addressData): void
102110
$attributes = $this->getAllowedAddressAttributes->execute();
103111
$errorInput = [];
104112

113+
//Add error for empty postcode with country with no optional ZIP
114+
if (!$this->directoryData->isZipCodeOptional($addressData['country_id'])
115+
&& (!isset($addressData['postcode']) || empty($addressData['postcode']))
116+
) {
117+
$errorInput[] = 'postcode';
118+
}
119+
105120
foreach ($attributes as $attributeName => $attributeInfo) {
106121
if ($attributeInfo->getIsRequired()
107122
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))

app/code/Magento/CustomerGraphQl/Model/Resolver/DeleteCustomerAddress.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ public function resolve(
5858
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
5959
}
6060

61-
if (empty($args['id'])) {
62-
throw new GraphQlInputException(__('Address "id" value should be specified'));
63-
}
64-
6561
$address = $this->getCustomerAddress->execute((int)$args['id'], $context->getUserId());
6662
$this->deleteCustomerAddress->execute($address);
6763
return true;

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0||~7.3.0",
7-
"magento/module-customer": "*",
87
"magento/module-authorization": "*",
98
"magento/module-customer": "*",
109
"magento/module-eav": "*",
1110
"magento/module-graph-ql": "*",
1211
"magento/module-newsletter": "*",
1312
"magento/module-integration": "*",
1413
"magento/module-store": "*",
15-
"magento/framework": "*"
14+
"magento/framework": "*",
15+
"magento/module-directory": "*"
1616
},
1717
"license": [
1818
"OSL-3.0",

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11-
use Magento\Framework\Message\AbstractMessage;
11+
use Magento\Framework\Message\MessageInterface;
1212
use Magento\Quote\Api\CartRepositoryInterface;
1313
use Magento\Quote\Model\Quote;
1414

@@ -55,29 +55,15 @@ public function execute(Quote $cart, array $cartItems): void
5555
}
5656

5757
if ($cart->getData('has_error')) {
58-
throw new GraphQlInputException(
59-
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
60-
);
58+
$e = new GraphQlInputException(__('Shopping cart errors'));
59+
$errors = $cart->getErrors();
60+
foreach ($errors as $error) {
61+
/** @var MessageInterface $error */
62+
$e->addError(new GraphQlInputException(__($error->getText())));
63+
}
64+
throw $e;
6165
}
6266

6367
$this->cartRepository->save($cart);
6468
}
65-
66-
/**
67-
* Collecting cart errors
68-
*
69-
* @param Quote $cart
70-
* @return string
71-
*/
72-
private function getCartErrors(Quote $cart): string
73-
{
74-
$errorMessages = [];
75-
76-
/** @var AbstractMessage $error */
77-
foreach ($cart->getErrors() as $error) {
78-
$errorMessages[] = $error->getText();
79-
}
80-
81-
return implode(PHP_EOL, $errorMessages);
82-
}
8369
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function execute(Quote $cart, array $cartItemData): void
7272
}
7373

7474
if (is_string($result)) {
75-
throw new GraphQlInputException(__($result));
75+
$e = new GraphQlInputException(__('Cannot add product to cart'));
76+
$errors = array_unique(explode("\n", $result));
77+
foreach ($errors as $error) {
78+
$e->addError(new GraphQlInputException(__($error)));
79+
}
80+
throw $e;
7681
}
7782
}
7883

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
4848
*/
4949
private $customerRepository;
5050

51-
protected function setUp()
51+
protected function setUp(): void
5252
{
5353
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
5454
$this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CreateCustomerAddressTest extends GraphQlAbstract
2929
*/
3030
private $addressRepository;
3131

32-
protected function setUp()
32+
protected function setUp(): void
3333
{
3434
parent::setUp();
3535

@@ -358,6 +358,67 @@ public function testCreateCustomerAddressWithRedundantStreetLine()
358358
$this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
359359
}
360360

361+
/**
362+
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
363+
* @magentoConfigFixture default_store general/country/optional_zip_countries UA
364+
*
365+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
366+
*/
367+
public function testCreateCustomerAddressWithOptionalZipCode()
368+
{
369+
$newAddress = [
370+
'country_code' => 'UA',
371+
'street' => ['Line 1 Street', 'Line 2'],
372+
'company' => 'Company name',
373+
'telephone' => '123456789',
374+
'fax' => '123123123',
375+
'city' => 'City Name',
376+
'firstname' => 'Adam',
377+
'lastname' => 'Phillis',
378+
'middlename' => 'A',
379+
'prefix' => 'Mr.',
380+
'suffix' => 'Jr.',
381+
'vat_id' => '1',
382+
'default_shipping' => true,
383+
'default_billing' => false
384+
];
385+
386+
$mutation
387+
= <<<MUTATION
388+
mutation {
389+
createCustomerAddress(input: {
390+
country_code: {$newAddress['country_code']}
391+
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
392+
company: "{$newAddress['company']}"
393+
telephone: "{$newAddress['telephone']}"
394+
fax: "{$newAddress['fax']}"
395+
city: "{$newAddress['city']}"
396+
firstname: "{$newAddress['firstname']}"
397+
lastname: "{$newAddress['lastname']}"
398+
middlename: "{$newAddress['middlename']}"
399+
prefix: "{$newAddress['prefix']}"
400+
suffix: "{$newAddress['suffix']}"
401+
vat_id: "{$newAddress['vat_id']}"
402+
default_shipping: true
403+
default_billing: false
404+
}) {
405+
id
406+
}
407+
}
408+
MUTATION;
409+
410+
$userName = '[email protected]';
411+
$password = 'password';
412+
413+
$response = $this->graphQlMutation(
414+
$mutation,
415+
[],
416+
'',
417+
$this->getCustomerAuthHeaders($userName, $password)
418+
);
419+
$this->assertNotEmpty($response['createCustomerAddress']['id']);
420+
}
421+
361422
/**
362423
* Create new address with invalid input
363424
*

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414

1515
/**
16-
* Test for create customer functionallity
16+
* Test for create customer functionality
1717
*/
1818
class CreateCustomerTest extends GraphQlAbstract
1919
{
@@ -27,7 +27,7 @@ class CreateCustomerTest extends GraphQlAbstract
2727
*/
2828
private $customerRepository;
2929

30-
protected function setUp()
30+
protected function setUp(): void
3131
{
3232
parent::setUp();
3333

@@ -308,7 +308,40 @@ public function testCreateCustomerSubscribed()
308308
$this->assertEquals(false, $response['createCustomer']['customer']['is_subscribed']);
309309
}
310310

311-
public function tearDown()
311+
/**
312+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
313+
* @expectedException \Exception
314+
* @expectedExceptionMessage A customer with the same email address already exists in an associated website.
315+
*/
316+
public function testCreateCustomerIfCustomerWithProvidedEmailAlreadyExists()
317+
{
318+
$existedEmail = '[email protected]';
319+
$password = 'test123#';
320+
$firstname = 'John';
321+
$lastname = 'Smith';
322+
323+
$query = <<<QUERY
324+
mutation {
325+
createCustomer(
326+
input: {
327+
email: "{$existedEmail}"
328+
password: "{$password}"
329+
firstname: "{$firstname}"
330+
lastname: "{$lastname}"
331+
}
332+
) {
333+
customer {
334+
firstname
335+
lastname
336+
email
337+
}
338+
}
339+
}
340+
QUERY;
341+
$this->graphQlMutation($query);
342+
}
343+
344+
public function tearDown(): void
312345
{
313346
$newEmail = '[email protected]';
314347
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DeleteCustomerAddressTest extends GraphQlAbstract
3939
*/
4040
private $lockCustomer;
4141

42-
protected function setUp()
42+
protected function setUp(): void
4343
{
4444
parent::setUp();
4545

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class GetAddressesTest extends GraphQlAbstract
3131
*/
3232
private $lockCustomer;
3333

34-
protected function setUp()
34+
protected function setUp(): void
3535
{
3636
parent::setUp();
3737

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GetCustomerTest extends GraphQlAbstract
3636
*/
3737
private $customerRepository;
3838

39-
protected function setUp()
39+
protected function setUp(): void
4040
{
4141
parent::setUp();
4242

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SubscriptionStatusTest extends GraphQlAbstract
2929
*/
3030
private $subscriberFactory;
3131

32-
protected function setUp()
32+
protected function setUp(): void
3333
{
3434
parent::setUp();
3535

@@ -162,7 +162,7 @@ private function getHeaderMap(string $email, string $password): array
162162
return ['Authorization' => 'Bearer ' . $customerToken];
163163
}
164164

165-
protected function tearDown()
165+
protected function tearDown(): void
166166
{
167167
parent::tearDown();
168168

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class UpdateCustomerAddressTest extends GraphQlAbstract
4040
*/
4141
private $lockCustomer;
4242

43-
protected function setUp()
43+
protected function setUp(): void
4444
{
4545
parent::setUp();
4646

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class UpdateCustomerTest extends GraphQlAbstract
3333
*/
3434
private $lockCustomer;
3535

36-
protected function setUp()
36+
protected function setUp(): void
3737
{
3838
parent::setUp();
3939

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
2929
*/
3030
private $getMaskedQuoteIdByReservedOrderId;
3131

32-
protected function setUp()
32+
protected function setUp(): void
3333
{
3434
$objectManager = Bootstrap::getObjectManager();
3535
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);

0 commit comments

Comments
 (0)