Skip to content

Commit 5b87891

Browse files
committed
MAGETWO-90029: Once integer is stored for State/Province field, it can not be changed to alphanumeric
1 parent cc4b174 commit 5b87891

File tree

8 files changed

+101
-9
lines changed

8 files changed

+101
-9
lines changed

app/code/Magento/Checkout/Test/Mftf/Section/CheckoutShippingSection.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
<element name="city" type="input" selector="input[name=city]"/>
1818
<element name="region" type="select" selector="select[name=region_id]"/>
1919
<element name="postcode" type="input" selector="input[name=postcode]"/>
20+
<element name="country" type="select" selector="select[name=country_id]"/>
2021
<element name="telephone" type="input" selector="input[name=telephone]"/>
2122
<element name="next" type="button" selector="button.button.action.continue.primary"/>
2223
<element name="firstShippingMethod" type="radio" selector="#checkout-shipping-method-load input[type='radio']"/>
2324
<element name="selectedShippingAddress" type="text" selector=".shipping-address-item.selected-item"/>
2425
<element name="newAddressButton" type="button" selector="#checkout-step-shipping button"/>
2526
<element name="next" type="button" selector="[data-role='opc-continue']"/>
27+
<element name="stateInput" type="input" selector="input[name=region]"/>
2628
</section>
2729
</sections>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="../../../../../../../dev/tests/acceptance/vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Test/etc/testSchema.xsd">
11+
<test name="AddressStateFieldShouldNotAcceptJustIntegerValuesTest">
12+
<annotations>
13+
<features value="Checkout"/>
14+
<title value="Guest Checkout"/>
15+
<description value="Address State field should not allow just integer values"/>
16+
<severity value="MAJOR"/>
17+
<testCaseId value="MAGETWO-93291"/>
18+
<group value="checkout"/>
19+
</annotations>
20+
<before>
21+
<createData entity="_defaultCategory" stepKey="createCategory"/>
22+
<createData entity="ApiSimpleProduct" stepKey="createProduct">
23+
<requiredEntity createDataKey="createCategory"/>
24+
</createData>
25+
</before>
26+
<after>
27+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
28+
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/>
29+
</after>
30+
31+
<amOnPage url="{{StorefrontCategoryPage.url($$createCategory.name$$)}}" stepKey="onCategoryPage"/>
32+
<waitForPageLoad stepKey="waitForPageLoad1"/>
33+
<moveMouseOver selector="{{StorefrontCategoryMainSection.ProductItemInfo}}" stepKey="hoverProduct"/>
34+
<click selector="{{StorefrontCategoryMainSection.AddToCartBtn}}" stepKey="addToCart"/>
35+
<waitForElementVisible selector="{{StorefrontCategoryMainSection.SuccessMsg}}" time="30" stepKey="waitForProductAdded"/>
36+
<see selector="{{StorefrontCategoryMainSection.SuccessMsg}}" userInput="You added $$createProduct.name$$ to your shopping cart." stepKey="seeAddedToCartMessage"/>
37+
<see selector="{{StorefrontMinicartSection.quantity}}" userInput="1" stepKey="seeCartQuantity"/>
38+
<actionGroup ref="GoToCheckoutFromMinicartActionGroup" stepKey="guestGoToCheckoutFromMinicart" />
39+
<selectOption selector="{{CheckoutShippingSection.country}}" userInput="{{UK_Address.country_id}}" stepKey="selectCounty"/>
40+
<waitForPageLoad stepKey="waitFormToReload"/>
41+
<fillField selector="{{CheckoutShippingSection.stateInput}}" userInput="1" stepKey="enterStateAsIntegerValue"/>
42+
<waitForPageLoad stepKey="waitforFormValidation"/>
43+
<see userInput="First character must be letter." stepKey="seeTheErrorMessageDisplayed"/>
44+
<fillField selector="{{CheckoutShippingSection.stateInput}}" userInput=" 1" stepKey="enterStateAsIntegerValue1"/>
45+
<waitForPageLoad stepKey="waitforFormValidation1"/>
46+
<see userInput="First character must be letter." stepKey="seeTheErrorMessageDisplayed1"/>
47+
<fillField selector="{{CheckoutShippingSection.stateInput}}" userInput="ABC1" stepKey="enterStateAsIntegerValue2"/>
48+
<waitForPageLoad stepKey="waitforFormValidation2"/>
49+
<dontSee userInput="First character must be letter." stepKey="seeTheErrorMessageIsNotDisplayed"/>
50+
</test>
51+
</tests>

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ public function validate()
648648
//If country actually has regions and requires you to
649649
//select one then it must be selected.
650650
$errors[] = __('%fieldName is a required field.', ['fieldName' => 'regionId']);
651-
} elseif ($regionId && !in_array($regionId, $allowedRegions, true)) {
651+
} elseif ($allowedRegions && $regionId && !in_array($regionId, $allowedRegions, true)) {
652652
//If a region is selected then checking if it exists.
653653
$errors[] = __(
654654
'Invalid value of "%value" provided for the %fieldName field.',

app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@
4949
<data key="default_shipping">Yes</data>
5050
<requiredEntity type="region">RegionTX</requiredEntity>
5151
</entity>
52+
<!--If required other field can be added to UK_Address entity, don't modify any existing data-->
53+
<entity name="UK_Address" type="address">
54+
<data key="country_id">GB</data>
55+
</entity>
5256
</entities>

app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ public function testValidate(array $data, $expected)
311311
->will($this->returnValue([]));
312312

313313
$this->directoryDataMock->expects($this->any())
314-
->method('isRegionRequired');
314+
->method('isRegionRequired')
315+
->willReturn($data['region_required']);
315316

316317
$countryCollectionMock = $this->getMockBuilder(\Magento\Directory\Model\ResourceModel\Country\Collection::class)
317318
->disableOriginalConstructor()
@@ -326,14 +327,18 @@ public function testValidate(array $data, $expected)
326327

327328
$regionModelMock = $this->getMockBuilder(\Magento\Directory\Model\Region::class)
328329
->disableOriginalConstructor()
329-
->setMethods(['getCountryId', 'getName', 'load'])
330+
->setMethods(['getCountryId', 'getName', 'load', 'loadByCode'])
330331
->getMock();
331332

332333
$this->regionFactoryMock->expects($this->any())->method('create')->willReturn($regionModelMock);
333334

334335
$regionModelMock->expects($this->any())->method('load')->with($data['region_id'])->willReturnSelf();
335336
$regionModelMock->expects($this->any())->method('getCountryId')->willReturn($countryId);
336-
$regionModelMock->expects($this->any())->method('getName')->willReturn('RegionName');
337+
$regionModelMock->expects($this->any())->method('getName')->willReturn($data['region']);
338+
$regionModelMock->expects($this->any())
339+
->method('loadByCode')
340+
->with($data['region'], $countryId)
341+
->willReturnSelf();
337342

338343
$countryModelMock = $this->getMockBuilder(\Magento\Directory\Model\Country::class)
339344
->disableOriginalConstructor()
@@ -352,7 +357,7 @@ public function testValidate(array $data, $expected)
352357
->setMethods(['getAllIds'])
353358
->getMock();
354359
$countryModelMock->expects($this->any())->method('getRegionCollection')->willReturn($regionCollectionMock);
355-
$regionCollectionMock->expects($this->any())->method('getAllIds')->willReturn(['1']);
360+
$regionCollectionMock->expects($this->any())->method('getAllIds')->willReturn($data['allowed_regions']);
356361

357362
foreach ($data as $key => $value) {
358363
$this->model->setData($key, $value);
@@ -377,8 +382,11 @@ public function validateDataProvider()
377382
'country_id' => $countryId,
378383
'postcode' => 07201,
379384
'region_id' => 1,
385+
'region' => 'RegionName',
386+
'region_required' => false,
380387
'company' => 'Magento',
381-
'fax' => '222-22-22'
388+
'fax' => '222-22-22',
389+
'allowed_regions' => ['1'],
382390
];
383391
return [
384392
'firstname' => [
@@ -405,8 +413,28 @@ public function validateDataProvider()
405413
array_merge(array_diff_key($data, ['postcode' => '']), ['country_id' => $countryId++]),
406414
['postcode is a required field.'],
407415
],
408-
'region_id' => [
409-
array_merge($data, ['country_id' => $countryId++, 'region_id' => 2]),
416+
'region' => [
417+
array_merge(
418+
$data,
419+
[
420+
'region_required' => true,
421+
'country_id' => $countryId++,
422+
'allowed_regions' => [],
423+
'region' => '',
424+
]
425+
),
426+
['region is a required field.'],
427+
],
428+
'region_id1' => [
429+
array_merge($data, ['country_id' => $countryId, 'region_required' => true, 'region_id' => '']),
430+
['regionId is a required field.'],
431+
],
432+
'region_id2' => [
433+
array_merge($data, ['country_id' => $countryId, 'region_id' => 2, 'allowed_regions' => []]),
434+
true,
435+
],
436+
'region_id3' => [
437+
array_merge($data, ['country_id' => $countryId, 'region_id' => 2, 'allowed_regions' => [1, 3]]),
410438
['Invalid value of "2" provided for the regionId field.'],
411439
],
412440
'country_id' => [

app/code/Magento/Customer/view/frontend/templates/address/edit.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
name="region"
113113
value="<?= $block->escapeHtmlAttr($block->getRegion()) ?>"
114114
title="<?= $block->escapeHtmlAttr(__('State/Province')) ?>"
115-
class="input-text <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>"<?= !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/>
115+
class="input-text validate-not-number-first <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>"<?= !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/>
116116
</div>
117117
</div>
118118
<div class="field zip required">

app/code/Magento/Ui/view/base/web/js/form/element/region.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ define([
5757
registry.get(this.customName, function (input) {
5858
isRegionRequired = !!option['is_region_required'];
5959
input.validation['required-entry'] = isRegionRequired;
60+
input.validation['validate-not-number-first'] = true;
6061
input.required(isRegionRequired);
6162
});
6263
}

app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,12 @@ define([
754754
},
755755
$.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
756756
],
757+
'validate-not-number-first': [
758+
function (value) {
759+
return utils.isEmptyNoTrim(value) || /^[^0-9-\.].*$/.test(value.trim());
760+
},
761+
$.mage.__('First character must be letter.')
762+
],
757763
'validate-date': [
758764
function (value, params, additionalParams) {
759765
var test = moment(value, additionalParams.dateFormat);

0 commit comments

Comments
 (0)