From 676cd1b048aaef37a2716d8c658a81cf2d89afc7 Mon Sep 17 00:00:00 2001 From: Oscar Recio Date: Sun, 27 Oct 2019 12:37:21 +0100 Subject: [PATCH 1/3] #1029 Add Postcode as required depending of the country Mutation createCustomerAddress --- .../Address/CreateCustomerAddress.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php index 9637b3e555b8..b9a9b4979682 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php @@ -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 @@ -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; } /** @@ -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 (!in_array($addressData['country_id'], $this->directoryData->getCountriesWithOptionalZip()) + && (!isset($addressData['postcode']) || empty($addressData['postcode'])) + ) { + $errorInput[] = 'postcode'; + } + foreach ($attributes as $attributeName => $attributeInfo) { if ($attributeInfo->getIsRequired() && (!isset($addressData[$attributeName]) || empty($addressData[$attributeName])) From 127835e3d9cb4c51bab70291e57a37a12b505d4c Mon Sep 17 00:00:00 2001 From: Oscar Recio Date: Sun, 27 Oct 2019 14:26:59 +0100 Subject: [PATCH 2/3] Add depency with module-directory and add better method to get is zip code is optional --- .../Model/Customer/Address/CreateCustomerAddress.php | 2 +- app/code/Magento/CustomerGraphQl/composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php index b9a9b4979682..37230df202a6 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php @@ -111,7 +111,7 @@ public function validateData(array $addressData): void $errorInput = []; //Add error for empty postcode with country with no optional ZIP - if (!in_array($addressData['country_id'], $this->directoryData->getCountriesWithOptionalZip()) + if (!$this->directoryData->isZipCodeOptional($addressData['country_id']) && (!isset($addressData['postcode']) || empty($addressData['postcode'])) ) { $errorInput[] = 'postcode'; diff --git a/app/code/Magento/CustomerGraphQl/composer.json b/app/code/Magento/CustomerGraphQl/composer.json index 911624da8fe5..70a98f728b69 100644 --- a/app/code/Magento/CustomerGraphQl/composer.json +++ b/app/code/Magento/CustomerGraphQl/composer.json @@ -4,7 +4,6 @@ "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": "*", @@ -12,7 +11,8 @@ "magento/module-newsletter": "*", "magento/module-integration": "*", "magento/module-store": "*", - "magento/framework": "*" + "magento/framework": "*", + "magento/module-directory": "*" }, "license": [ "OSL-3.0", From 891e5a4e91ce5ef261cb22c457210322bf4ddcd9 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 8 Nov 2019 13:34:24 -0600 Subject: [PATCH 3/3] magento/graphql-ce#1029: Add Postcode as required depending of the country Mutation createCustomerAddress - added test --- .../Customer/CreateCustomerAddressTest.php | 61 +++++++++++++++++++ 1 file changed, 61 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 408a254f65f2..5eed8576db0d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -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 + = <<graphQlMutation( + $mutation, + [], + '', + $this->getCustomerAuthHeaders($userName, $password) + ); + $this->assertNotEmpty($response['createCustomerAddress']['id']); + } + /** * Create new address with invalid input *