From e09b1aa5d51fd89f386791758f83cd20a004f545 Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Tue, 3 Jul 2018 12:16:57 -0500 Subject: [PATCH 1/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- app/code/Magento/Ui/view/base/web/js/form/element/region.js | 1 + .../Magento/Ui/view/base/web/js/lib/validation/rules.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/code/Magento/Ui/view/base/web/js/form/element/region.js b/app/code/Magento/Ui/view/base/web/js/form/element/region.js index 0edb4c1966b5..fec69bf8558b 100644 --- a/app/code/Magento/Ui/view/base/web/js/form/element/region.js +++ b/app/code/Magento/Ui/view/base/web/js/form/element/region.js @@ -57,6 +57,7 @@ define([ registry.get(this.customName, function (input) { isRegionRequired = !!option['is_region_required']; input.validation['required-entry'] = isRegionRequired; + input.validation['validate-not-number-first'] = true; input.required(isRegionRequired); }); } diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 41e2703f4ed1..14277499ec6c 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -755,6 +755,12 @@ define([ }, $.mage.__('Please use only letters (a-z or A-Z) or numbers (0-9) in this field. No spaces or other characters are allowed.')//eslint-disable-line max-len ], + 'validate-not-number-first': [ + function (value) { + return utils.isEmptyNoTrim(value) || /^[^0-9].*$$/.test(value); + }, + $.mage.__('First character must be letter.')//eslint-disable-line max-len + ], 'validate-date': [ function (value, params, additionalParams) { var test = moment(value, additionalParams.dateFormat); From 50e1f0a1256c11a06d6cb62c42763e308dfab7db Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Tue, 3 Jul 2018 12:19:43 -0500 Subject: [PATCH 2/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 14277499ec6c..2a19b6ad7e12 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -757,7 +757,7 @@ define([ ], 'validate-not-number-first': [ function (value) { - return utils.isEmptyNoTrim(value) || /^[^0-9].*$$/.test(value); + return utils.isEmptyNoTrim(value) || /^[^0-9].*$/.test(value); }, $.mage.__('First character must be letter.')//eslint-disable-line max-len ], From 9f0fda689dd3df51bcc3bacd730ed73a0385e72e Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Mon, 9 Jul 2018 13:22:22 -0500 Subject: [PATCH 3/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- .../Magento/Customer/Model/Address/Validator/Country.php | 6 +++--- .../Test/Unit/Model/Address/Validator/CountryTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Customer/Model/Address/Validator/Country.php b/app/code/Magento/Customer/Model/Address/Validator/Country.php index ff1020eba70e..832a893eb720 100644 --- a/app/code/Magento/Customer/Model/Address/Validator/Country.php +++ b/app/code/Magento/Customer/Model/Address/Validator/Country.php @@ -95,8 +95,8 @@ private function validateRegion(AbstractAddress $address) $countryId = $address->getCountryId(); $countryModel = $address->getCountryModel(); $regionCollection = $countryModel->getRegionCollection(); - $region = $address->getRegion(); - $regionId = (string)$address->getRegionId(); + $region = $address->getData('region'); + $regionId = (string)$address->getData('region_id'); $allowedRegions = $regionCollection->getAllIds(); $isRegionRequired = $this->directoryData->isRegionRequired($countryId); if ($isRegionRequired && empty($allowedRegions) && !\Zend_Validate::is($region, 'NotEmpty')) { @@ -107,7 +107,7 @@ private function validateRegion(AbstractAddress $address) //If country actually has regions and requires you to //select one then it must be selected. $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'regionId']); - } elseif ($regionId && !in_array($regionId, $allowedRegions, true)) { + } elseif ($allowedRegions && $regionId && !in_array($regionId, $allowedRegions, true)) { //If a region is selected then checking if it exists. $errors[] = __( 'Invalid value of "%value" provided for the %fieldName field.', diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php index 9ab35f2d301d..c060d698b7f7 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php @@ -162,7 +162,7 @@ public function validateDataProvider() array_merge($data, ['country_id' => $countryId, 'region_id' => 2]), [$countryId++], [1], - ['Invalid value of "2" provided for the regionId field.'], + [], ], 'validated' => [ array_merge($data, ['country_id' => $countryId]), From e0a8abe7657c43f93aaf0c47775bdad5412bf73c Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Mon, 9 Jul 2018 15:38:58 -0500 Subject: [PATCH 4/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- .../Magento/Customer/Model/Address/Validator/Country.php | 4 ++-- .../Test/Unit/Model/Address/Validator/CountryTest.php | 8 +++++++- .../Magento/Ui/view/base/web/js/lib/validation/rules.js | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Customer/Model/Address/Validator/Country.php b/app/code/Magento/Customer/Model/Address/Validator/Country.php index 832a893eb720..1b618b4ae069 100644 --- a/app/code/Magento/Customer/Model/Address/Validator/Country.php +++ b/app/code/Magento/Customer/Model/Address/Validator/Country.php @@ -95,8 +95,8 @@ private function validateRegion(AbstractAddress $address) $countryId = $address->getCountryId(); $countryModel = $address->getCountryModel(); $regionCollection = $countryModel->getRegionCollection(); - $region = $address->getData('region'); - $regionId = (string)$address->getData('region_id'); + $region = $address->getRegion(); + $regionId = (string)$address->getRegionId(); $allowedRegions = $regionCollection->getAllIds(); $isRegionRequired = $this->directoryData->isRegionRequired($countryId); if ($isRegionRequired && empty($allowedRegions) && !\Zend_Validate::is($region, 'NotEmpty')) { diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php index c060d698b7f7..d8148543a55d 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/Validator/CountryTest.php @@ -161,8 +161,14 @@ public function validateDataProvider() 'region_id2' => [ array_merge($data, ['country_id' => $countryId, 'region_id' => 2]), [$countryId++], - [1], [], + [], + ], + 'region_id3' => [ + array_merge($data, ['country_id' => $countryId, 'region_id' => 2]), + [$countryId++], + [1, 3], + ['Invalid value of "2" provided for the regionId field.'], ], 'validated' => [ array_merge($data, ['country_id' => $countryId]), diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 2a19b6ad7e12..7aa9ead484b7 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -757,7 +757,7 @@ define([ ], 'validate-not-number-first': [ function (value) { - return utils.isEmptyNoTrim(value) || /^[^0-9].*$/.test(value); + return utils.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value); }, $.mage.__('First character must be letter.')//eslint-disable-line max-len ], From 6bc76e0985c8ae60915c2535dadd83587099f8db Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Mon, 9 Jul 2018 15:54:49 -0500 Subject: [PATCH 5/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- app/code/Magento/Customer/Model/Address/Validator/Country.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/code/Magento/Customer/Model/Address/Validator/Country.php b/app/code/Magento/Customer/Model/Address/Validator/Country.php index 1b618b4ae069..d7fb51dbbd44 100644 --- a/app/code/Magento/Customer/Model/Address/Validator/Country.php +++ b/app/code/Magento/Customer/Model/Address/Validator/Country.php @@ -88,6 +88,8 @@ private function validateCountry(AbstractAddress $address) * * @param AbstractAddress $address * @return array + * @throws \Zend_Validate_Exception + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function validateRegion(AbstractAddress $address) { From 14e89c27eeb237e3c821d75a67336c08d546efc7 Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Tue, 10 Jul 2018 14:35:57 -0500 Subject: [PATCH 6/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- .../Customer/view/frontend/templates/address/edit.phtml | 2 +- .../Magento/Ui/view/base/web/js/lib/validation/rules.js | 2 +- lib/web/mage/validation.js | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml b/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml index 1f1f07850452..6a129a3aa4b4 100644 --- a/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/address/edit.phtml @@ -112,7 +112,7 @@ name="region" value="escapeHtmlAttr($block->getRegion()) ?>" title="escapeHtmlAttr(__('State/Province')) ?>" - class="input-text escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>"getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/> + class="input-text validate-not-number-first escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>"getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/>
diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 7aa9ead484b7..675c7214edc5 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -759,7 +759,7 @@ define([ function (value) { return utils.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value); }, - $.mage.__('First character must be letter.')//eslint-disable-line max-len + $.mage.__('First character must be letter.') ], 'validate-date': [ function (value, params, additionalParams) { diff --git a/lib/web/mage/validation.js b/lib/web/mage/validation.js index 6258b3c62737..ba06c112d510 100644 --- a/lib/web/mage/validation.js +++ b/lib/web/mage/validation.js @@ -990,6 +990,12 @@ }, $.mage.__('Please use only letters (a-z or A-Z) or numbers (0-9) in this field. No spaces or other characters are allowed.') //eslint-disable-line max-len ], + 'validate-not-number-first': [ + function (value) { + return $.mage.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value); + }, + $.mage.__('First character must be letter.') + ], 'validate-date': [ function (value, params, additionalParams) { var test = moment(value, additionalParams.dateFormat); From 660a4dbe7b9ca5db210bdf90cfe4e58963c13329 Mon Sep 17 00:00:00 2001 From: Roman Ganin Date: Wed, 11 Jul 2018 10:55:41 -0500 Subject: [PATCH 7/8] MAGETWO-91465: Once integer is stored for State/Province field, it can not be changed to alphanumeric --- app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js | 2 +- lib/web/mage/validation.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js index 675c7214edc5..c63b4588d2d1 100644 --- a/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js +++ b/app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js @@ -757,7 +757,7 @@ define([ ], 'validate-not-number-first': [ function (value) { - return utils.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value); + return utils.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value.trim()); }, $.mage.__('First character must be letter.') ], diff --git a/lib/web/mage/validation.js b/lib/web/mage/validation.js index ba06c112d510..d08819ebe94a 100644 --- a/lib/web/mage/validation.js +++ b/lib/web/mage/validation.js @@ -992,7 +992,7 @@ ], 'validate-not-number-first': [ function (value) { - return $.mage.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value); + return $.mage.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value.trim()); }, $.mage.__('First character must be letter.') ], From 2099950f7a583c65e80a8ecd6e2d49a35f52f7b1 Mon Sep 17 00:00:00 2001 From: Devagouda Date: Thu, 12 Jul 2018 11:46:35 -0500 Subject: [PATCH 8/8] MAGETWO-91465:Once integer is stored for State/Province field, it can not be changed to alphanumeric -Added functional test to cover bug fix --- .../Mftf/Section/CheckoutShippingSection.xml | 1 + ...ldShouldNotAcceptJustIntegerValuesTest.xml | 52 +++++++++++++++++++ .../Customer/Test/Mftf/Data/AddressData.xml | 4 ++ 3 files changed, 57 insertions(+) create mode 100644 app/code/Magento/Checkout/Test/Mftf/Test/AddressStateFieldShouldNotAcceptJustIntegerValuesTest.xml diff --git a/app/code/Magento/Checkout/Test/Mftf/Section/CheckoutShippingSection.xml b/app/code/Magento/Checkout/Test/Mftf/Section/CheckoutShippingSection.xml index c20309814d51..136658cc5910 100644 --- a/app/code/Magento/Checkout/Test/Mftf/Section/CheckoutShippingSection.xml +++ b/app/code/Magento/Checkout/Test/Mftf/Section/CheckoutShippingSection.xml @@ -31,5 +31,6 @@ + diff --git a/app/code/Magento/Checkout/Test/Mftf/Test/AddressStateFieldShouldNotAcceptJustIntegerValuesTest.xml b/app/code/Magento/Checkout/Test/Mftf/Test/AddressStateFieldShouldNotAcceptJustIntegerValuesTest.xml new file mode 100644 index 000000000000..add1a1b1cf9b --- /dev/null +++ b/app/code/Magento/Checkout/Test/Mftf/Test/AddressStateFieldShouldNotAcceptJustIntegerValuesTest.xml @@ -0,0 +1,52 @@ + + + + + + + + + + <description value="Address State field should not allow just integer values"/> + <severity value="MAJOR"/> + <testCaseId value="MAGETWO-93203"/> + <group value="checkout"/> + </annotations> + <before> + <createData entity="_defaultCategory" stepKey="createCategory"/> + <createData entity="ApiSimpleProduct" stepKey="createProduct"> + <requiredEntity createDataKey="createCategory"/> + </createData> + </before> + <after> + <deleteData createDataKey="createCategory" stepKey="deleteCategory"/> + <deleteData createDataKey="createProduct" stepKey="deleteProduct"/> + </after> + + <amOnPage url="{{StorefrontCategoryPage.url($$createCategory.name$$)}}" stepKey="onCategoryPage"/> + <waitForPageLoad stepKey="waitForPageLoad1"/> + <moveMouseOver selector="{{StorefrontCategoryMainSection.ProductItemInfo}}" stepKey="hoverProduct"/> + <click selector="{{StorefrontCategoryMainSection.AddToCartBtn}}" stepKey="addToCart"/> + <waitForElementVisible selector="{{StorefrontCategoryMainSection.SuccessMsg}}" time="30" stepKey="waitForProductAdded"/> + <see selector="{{StorefrontCategoryMainSection.SuccessMsg}}" userInput="You added $$createProduct.name$$ to your shopping cart." stepKey="seeAddedToCartMessage"/> + <see selector="{{StorefrontMinicartSection.quantity}}" userInput="1" stepKey="seeCartQuantity"/> + <actionGroup ref="GoToCheckoutFromMinicartActionGroup" stepKey="guestGoToCheckoutFromMinicart" /> + <selectOption stepKey="selectCounty" selector="{{CheckoutShippingSection.country}}" userInput="{{UK_Address.country_id}}"/> + <waitForPageLoad stepKey="waitFormToReload"/> + <fillField selector="{{CheckoutShippingSection.stateInput}}" userInput="1" stepKey="enterStateAsIntegerValue"/> + <waitForPageLoad stepKey="waitforFormValidation"/> + <see userInput="First character must be letter." stepKey="seeTheErrorMessageDisplayed"/> + <fillField selector="{{CheckoutShippingSection.stateInput}}" userInput=" 1" stepKey="enterStateAsIntegerValue1"/> + <waitForPageLoad stepKey="waitforFormValidation1"/> + <see userInput="First character must be letter." stepKey="seeTheErrorMessageDisplayed1"/> + <fillField selector="{{CheckoutShippingSection.stateInput}}" userInput="ABC1" stepKey="enterStateAsIntegerValue2"/> + <waitForPageLoad stepKey="waitforFormValidation2"/> + <dontSee userInput="First character must be letter." stepKey="seeTheErrorMessageIsNotDisplayed"/> + </test> +</tests> diff --git a/app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml b/app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml index 19194ae2e542..a1f0277ec40e 100644 --- a/app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml +++ b/app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml @@ -83,4 +83,8 @@ <data key="default_shipping">Yes</data> <requiredEntity type="region">RegionCA</requiredEntity> </entity> + <!--If required other field can be added to UK_Address entity, dont modify any existing data--> + <entity name="UK_Address" type="address"> + <data key="country_id">GB</data> + </entity> </entities>