Skip to content

Commit

Permalink
ENGCOM-6255: #1029 Add Postcode as required depending of the country …
Browse files Browse the repository at this point in the history
…Mutation createCustomerAddress #1031

 - Merge Pull Request magento/graphql-ce#1031 from osrecio/graphql-ce:1029-postcode-required-customerCreate
 - Merged commits:
   1. 676cd1b
   2. 127835e
   3. 891e5a4
  • Loading branch information
magento-engcom-team committed Nov 8, 2019
2 parents 4e3da15 + 891e5a4 commit 0cd1367
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 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
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0cd1367

Please sign in to comment.