Skip to content

Commit 1ad527b

Browse files
committed
MAGETWO-89039: Allow Validation of customer address attributes to include spaces
1 parent a392746 commit 1ad527b

File tree

10 files changed

+420
-130
lines changed

10 files changed

+420
-130
lines changed

app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
namespace Magento\Checkout\Block\Checkout;
77

88
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
9+
use Magento\Customer\Api\Data\CustomerInterface;
910
use Magento\Customer\Helper\Address as AddressHelper;
1011
use Magento\Customer\Model\Session;
1112
use Magento\Directory\Helper\Data as DirectoryHelper;
1213

14+
/**
15+
* Fields attribute merger.
16+
*/
1317
class AttributeMerger
1418
{
1519
/**
@@ -46,6 +50,7 @@ class AttributeMerger
4650
'alpha' => 'validate-alpha',
4751
'numeric' => 'validate-number',
4852
'alphanumeric' => 'validate-alphanum',
53+
'alphanum-with-spaces' => 'validate-alphanum-with-spaces',
4954
'url' => 'validate-url',
5055
'email' => 'email2',
5156
'length' => 'validate-length',
@@ -67,7 +72,7 @@ class AttributeMerger
6772
private $customerRepository;
6873

6974
/**
70-
* @var \Magento\Customer\Api\Data\CustomerInterface
75+
* @var CustomerInterface
7176
*/
7277
private $customer;
7378

@@ -309,6 +314,8 @@ protected function getMultilineFieldConfig($attributeCode, array $attributeConfi
309314
}
310315

311316
/**
317+
* Returns default attribute value.
318+
*
312319
* @param string $attributeCode
313320
* @return null|string
314321
*/
@@ -346,7 +353,9 @@ protected function getDefaultValue($attributeCode)
346353
}
347354

348355
/**
349-
* @return \Magento\Customer\Api\Data\CustomerInterface|null
356+
* Returns logged customer.
357+
*
358+
* @return CustomerInterface|null
350359
*/
351360
protected function getCustomer()
352361
{
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Test\Unit\Block\Checkout;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
11+
use Magento\Customer\Helper\Address as AddressHelper;
12+
use Magento\Customer\Model\Session as CustomerSession;
13+
use Magento\Directory\Helper\Data as DirectoryHelper;
14+
use Magento\Checkout\Block\Checkout\AttributeMerger;
15+
16+
class AttributeMergerTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var CustomerRepository
20+
*/
21+
private $customerRepositoryMock;
22+
23+
/**
24+
* @var CustomerSession
25+
*/
26+
private $customerSessionMock;
27+
28+
/**
29+
* @var AddressHelper
30+
*/
31+
private $addressHelperMock;
32+
33+
/**
34+
* @var DirectoryHelper
35+
*/
36+
private $directoryHelperMock;
37+
38+
/**
39+
* @var AttributeMerger
40+
*/
41+
private $attributeMerger;
42+
43+
protected function setUp()
44+
{
45+
46+
$this->customerRepositoryMock = $this->createMock(CustomerRepository::class);
47+
$this->customerSessionMock = $this->createMock(CustomerSession::class);
48+
$this->addressHelperMock = $this->createMock(AddressHelper::class);
49+
$this->directoryHelperMock = $this->createMock(DirectoryHelper::class);
50+
51+
$this->attributeMerger = new AttributeMerger(
52+
$this->addressHelperMock,
53+
$this->customerSessionMock,
54+
$this->customerRepositoryMock,
55+
$this->directoryHelperMock
56+
);
57+
}
58+
59+
/**
60+
* Tests of element attributes merging.
61+
*
62+
* @param string $validationRule
63+
* @param string $expectedValidation
64+
* @return void
65+
* @dataProvider validationRulesDataProvider
66+
*/
67+
public function testMerge($validationRule, $expectedValidation)
68+
{
69+
$elements = [
70+
'field' => [
71+
'visible' => true,
72+
'formElement' => 'input',
73+
'label' => __('City'),
74+
'value' => null,
75+
'sortOrder' => 1,
76+
'validation' => [
77+
'input_validation' => $validationRule,
78+
],
79+
]
80+
];
81+
82+
$actualResult = $this->attributeMerger->merge(
83+
$elements,
84+
'provider',
85+
'dataScope',
86+
[
87+
'field' =>
88+
[
89+
'validation' => ['length' => true],
90+
]
91+
]
92+
);
93+
94+
$expectedResult = [
95+
$expectedValidation => true,
96+
'length' => true,
97+
];
98+
99+
$this->assertEquals($expectedResult, $actualResult['field']['validation']);
100+
}
101+
102+
/**
103+
* Provides possible validation types.
104+
*
105+
* @return array
106+
*/
107+
public function validationRulesDataProvider()
108+
{
109+
return [
110+
['alpha', 'validate-alpha'],
111+
['numeric', 'validate-number'],
112+
['alphanumeric', 'validate-alphanum'],
113+
['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
114+
['url', 'validate-url'],
115+
['email', 'email2'],
116+
['length', 'validate-length'],
117+
];
118+
}
119+
}

app/code/Magento/Customer/Model/Metadata/Form/AbstractData.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Form Element Abstract Data Model
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
@@ -14,6 +12,8 @@
1412
use Magento\Framework\Validator\EmailAddress;
1513

1614
/**
15+
* Form Element Abstract Data Model.
16+
*
1717
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1818
*/
1919
abstract class AbstractData
@@ -138,7 +138,8 @@ public function setRequestScope($scope)
138138
}
139139

140140
/**
141-
* Set scope visibility
141+
* Set scope visibility.
142+
*
142143
* Search value only in scope or search value in scope and global
143144
*
144145
* @param boolean $flag
@@ -283,9 +284,13 @@ protected function _validateInputRule($value)
283284
);
284285

285286
if (!is_null($inputValidation)) {
287+
$allowWhiteSpace = false;
286288
switch ($inputValidation) {
289+
case 'alphanum-with-spaces':
290+
$allowWhiteSpace = true;
291+
// continue to alphanumeric validation
287292
case 'alphanumeric':
288-
$validator = new \Zend_Validate_Alnum(true);
293+
$validator = new \Zend_Validate_Alnum($allowWhiteSpace);
289294
$validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
290295
$validator->setMessage(
291296
__('"%1" contains non-alphabetic or non-numeric characters.', $label),

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/AbstractDataTest.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ public function applyOutputFilterDataProvider()
205205
}
206206

207207
/**
208+
* Tests input validation rules.
209+
*
208210
* @param null|string $value
209211
* @param null|string $label
210212
* @param null|string $inputValidation
@@ -217,25 +219,18 @@ public function testValidateInputRule($value, $label, $inputValidation, $expecte
217219
->disableOriginalConstructor()
218220
->setMethods(['getName', 'getValue'])
219221
->getMockForAbstractClass();
220-
$validationRule->expects($this->any())
221-
->method('getName')
222-
->will($this->returnValue('input_validation'));
223-
$validationRule->expects($this->any())
224-
->method('getValue')
225-
->will($this->returnValue($inputValidation));
226-
227-
$this->_attributeMock->expects($this->any())->method('getStoreLabel')->will($this->returnValue($label));
228-
$this->_attributeMock->expects(
229-
$this->any()
230-
)->method(
231-
'getValidationRules'
232-
)->will(
233-
$this->returnValue(
234-
[
235-
$validationRule,
236-
]
237-
)
238-
);
222+
223+
$validationRule->method('getName')
224+
->willReturn('input_validation');
225+
226+
$validationRule->method('getValue')
227+
->willReturn($inputValidation);
228+
229+
$this->_attributeMock->method('getStoreLabel')
230+
->willReturn($label);
231+
232+
$this->_attributeMock->method('getValidationRules')
233+
->willReturn([$validationRule]);
239234

240235
$this->assertEquals($expectedOutput, $this->_model->validateInputRule($value));
241236
}
@@ -256,6 +251,16 @@ public function validateInputRuleDataProvider()
256251
\Zend_Validate_Alnum::NOT_ALNUM => '"mylabel" contains non-alphabetic or non-numeric characters.'
257252
]
258253
],
254+
[
255+
'abc qaz',
256+
'mylabel',
257+
'alphanumeric',
258+
[
259+
\Zend_Validate_Alnum::NOT_ALNUM => '"mylabel" contains non-alphabetic or non-numeric characters.'
260+
]
261+
],
262+
['abcqaz', 'mylabel', 'alphanumeric', true],
263+
['abc qaz', 'mylabel', 'alphanum-with-spaces', true],
259264
[
260265
'!@#$',
261266
'mylabel',

app/code/Magento/Customer/Test/Unit/Ui/Component/Listing/Column/ValidationRulesTest.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,32 @@ class ValidationRulesTest extends \PHPUnit\Framework\TestCase
1818

1919
protected function setUp()
2020
{
21-
$this->validationRules = $this->getMockBuilder(
22-
\Magento\Customer\Ui\Component\Listing\Column\ValidationRules::class
23-
)
24-
->disableOriginalConstructor()
25-
->getMock();
26-
2721
$this->validationRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
2822
->disableOriginalConstructor()
2923
->getMock();
3024

3125
$this->validationRules = new ValidationRules();
3226
}
3327

34-
public function testGetValidationRules()
28+
/**
29+
* Tests input validation rules.
30+
*
31+
* @param string $validationRule
32+
* @param string $validationClass
33+
* @return void
34+
* @dataProvider validationRulesDataProvider
35+
*/
36+
public function testGetValidationRules($validationRule, $validationClass)
3537
{
3638
$expectsRules = [
3739
'required-entry' => true,
38-
'validate-number' => true,
40+
$validationClass => true,
3941
];
40-
$this->validationRule->expects($this->atLeastOnce())
41-
->method('getName')
42+
$this->validationRule->method('getName')
4243
->willReturn('input_validation');
43-
$this->validationRule->expects($this->atLeastOnce())
44-
->method('getValue')
45-
->willReturn('numeric');
44+
45+
$this->validationRule->method('getValue')
46+
->willReturn($validationRule);
4647

4748
$this->assertEquals(
4849
$expectsRules,
@@ -66,4 +67,21 @@ public function testGetValidationRulesWithOnlyRequiredRule()
6667
$this->validationRules->getValidationRules(true, [])
6768
);
6869
}
70+
71+
/**
72+
* Provides possible validation rules.
73+
*
74+
* @return array
75+
*/
76+
public function validationRulesDataProvider()
77+
{
78+
return [
79+
['alpha', 'validate-alpha'],
80+
['numeric', 'validate-number'],
81+
['alphanumeric', 'validate-alphanum'],
82+
['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
83+
['url', 'validate-url'],
84+
['email', 'validate-email'],
85+
];
86+
}
6987
}

app/code/Magento/Customer/Ui/Component/Listing/Column/ValidationRules.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
use Magento\Customer\Api\Data\ValidationRuleInterface;
99

10+
/**
11+
* Provides validation classes according to corresponding rules.
12+
*/
1013
class ValidationRules
1114
{
1215
/**
@@ -16,6 +19,7 @@ class ValidationRules
1619
'alpha' => 'validate-alpha',
1720
'numeric' => 'validate-number',
1821
'alphanumeric' => 'validate-alphanum',
22+
'alphanum-with-spaces' => 'validate-alphanum-with-spaces',
1923
'url' => 'validate-url',
2024
'email' => 'validate-email',
2125
];

app/code/Magento/Eav/Model/Attribute/Data/AbstractData.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ public function setRequestScope($scope)
144144
}
145145

146146
/**
147-
* Set scope visibility
147+
* Set scope visibility.
148+
*
148149
* Search value only in scope or search value in scope and global
149150
*
150151
* @param bool $flag
@@ -313,9 +314,13 @@ protected function _validateInputRule($value)
313314

314315
if (!empty($validateRules['input_validation'])) {
315316
$label = $this->getAttribute()->getStoreLabel();
317+
$allowWhiteSpace = false;
316318
switch ($validateRules['input_validation']) {
319+
case 'alphanum-with-spaces':
320+
$allowWhiteSpace = true;
321+
// continue to alphanumeric validation
317322
case 'alphanumeric':
318-
$validator = new \Zend_Validate_Alnum(true);
323+
$validator = new \Zend_Validate_Alnum($allowWhiteSpace);
319324
$validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
320325
$validator->setMessage(
321326
__('"%1" contains non-alphabetic or non-numeric characters.', $label),

0 commit comments

Comments
 (0)