Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - GraphQL
Browse files Browse the repository at this point in the history
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
  • Loading branch information
magento-engcom-team authored Nov 14, 2019
2 parents 9fb7790 + 74999e8 commit 3397b2b
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Directory\Helper\Data as DirectoryData;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Api\DataObjectHelper;

/**
* Create customer address
Expand All @@ -38,23 +39,30 @@ class CreateCustomerAddress
* @var DataObjectHelper
*/
private $dataObjectHelper;
/**
* @var DirectoryData
*/
private $directoryData;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param DataObjectHelper $dataObjectHelper
* @param DirectoryData $directoryData
*/
public function __construct(
GetAllowedAddressAttributes $getAllowedAddressAttributes,
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
DataObjectHelper $dataObjectHelper
DataObjectHelper $dataObjectHelper,
DirectoryData $directoryData
) {
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->directoryData = $directoryData;
}

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

//Add error for empty postcode with country with no optional ZIP
if (!$this->directoryData->isZipCodeOptional($addressData['country_id'])
&& (!isset($addressData['postcode']) || empty($addressData['postcode']))
) {
$errorInput[] = 'postcode';
}

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public function resolve(
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

if (empty($args['id'])) {
throw new GraphQlInputException(__('Address "id" value should be specified'));
}

$address = $this->getCustomerAddress->execute((int)$args['id'], $context->getUserId());
$this->deleteCustomerAddress->execute($address);
return true;
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/module-customer": "*",
"magento/module-authorization": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-graph-ql": "*",
"magento/module-newsletter": "*",
"magento/module-integration": "*",
"magento/module-store": "*",
"magento/framework": "*"
"magento/framework": "*",
"magento/module-directory": "*"
},
"license": [
"OSL-3.0",
Expand Down
30 changes: 8 additions & 22 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Message\AbstractMessage;
use Magento\Framework\Message\MessageInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;

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

if ($cart->getData('has_error')) {
throw new GraphQlInputException(
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
);
$e = new GraphQlInputException(__('Shopping cart errors'));
$errors = $cart->getErrors();
foreach ($errors as $error) {
/** @var MessageInterface $error */
$e->addError(new GraphQlInputException(__($error->getText())));
}
throw $e;
}

$this->cartRepository->save($cart);
}

/**
* Collecting cart errors
*
* @param Quote $cart
* @return string
*/
private function getCartErrors(Quote $cart): string
{
$errorMessages = [];

/** @var AbstractMessage $error */
foreach ($cart->getErrors() as $error) {
$errorMessages[] = $error->getText();
}

return implode(PHP_EOL, $errorMessages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public function execute(Quote $cart, array $cartItemData): void
}

if (is_string($result)) {
throw new GraphQlInputException(__($result));
$e = new GraphQlInputException(__('Cannot add product to cart'));
$errors = array_unique(explode("\n", $result));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException(__($error)));
}
throw $e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
*/
private $customerRepository;

protected function setUp()
protected function setUp(): void
{
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
$this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CreateCustomerAddressTest extends GraphQlAbstract
*/
private $addressRepository;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

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

/**
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
* @magentoConfigFixture default_store general/country/optional_zip_countries UA
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testCreateCustomerAddressWithOptionalZipCode()
{
$newAddress = [
'country_code' => 'UA',
'street' => ['Line 1 Street', 'Line 2'],
'company' => 'Company name',
'telephone' => '123456789',
'fax' => '123123123',
'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: {
country_code: {$newAddress['country_code']}
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
company: "{$newAddress['company']}"
telephone: "{$newAddress['telephone']}"
fax: "{$newAddress['fax']}"
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';

$response = $this->graphQlMutation(
$mutation,
[],
'',
$this->getCustomerAuthHeaders($userName, $password)
);
$this->assertNotEmpty($response['createCustomerAddress']['id']);
}

/**
* Create new address with invalid input
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for create customer functionallity
* Test for create customer functionality
*/
class CreateCustomerTest extends GraphQlAbstract
{
Expand All @@ -27,7 +27,7 @@ class CreateCustomerTest extends GraphQlAbstract
*/
private $customerRepository;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

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

public function tearDown()
/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage A customer with the same email address already exists in an associated website.
*/
public function testCreateCustomerIfCustomerWithProvidedEmailAlreadyExists()
{
$existedEmail = 'customer@example.com';
$password = 'test123#';
$firstname = 'John';
$lastname = 'Smith';

$query = <<<QUERY
mutation {
createCustomer(
input: {
email: "{$existedEmail}"
password: "{$password}"
firstname: "{$firstname}"
lastname: "{$lastname}"
}
) {
customer {
firstname
lastname
email
}
}
}
QUERY;
$this->graphQlMutation($query);
}

public function tearDown(): void
{
$newEmail = 'new_customer@example.com';
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DeleteCustomerAddressTest extends GraphQlAbstract
*/
private $lockCustomer;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GetAddressesTest extends GraphQlAbstract
*/
private $lockCustomer;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GetCustomerTest extends GraphQlAbstract
*/
private $customerRepository;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SubscriptionStatusTest extends GraphQlAbstract
*/
private $subscriberFactory;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

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

protected function tearDown()
protected function tearDown(): void
{
parent::tearDown();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class UpdateCustomerAddressTest extends GraphQlAbstract
*/
private $lockCustomer;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UpdateCustomerTest extends GraphQlAbstract
*/
private $lockCustomer;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
*/
private $getMaskedQuoteIdByReservedOrderId;

protected function setUp()
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
Expand Down
Loading

0 comments on commit 3397b2b

Please sign in to comment.