Skip to content

Commit f4daa73

Browse files
committed
MAGETWO-95935: Allow Validation of customer address attributes to include spaces
1 parent c4fb1ee commit f4daa73

File tree

10 files changed

+451
-158
lines changed

10 files changed

+451
-158
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
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;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
1215

16+
/**
17+
* Fields attribute merger.
18+
*/
1319
class AttributeMerger
1420
{
1521
/**
@@ -46,6 +52,7 @@ class AttributeMerger
4652
'alpha' => 'validate-alpha',
4753
'numeric' => 'validate-number',
4854
'alphanumeric' => 'validate-alphanum',
55+
'alphanum-with-spaces' => 'validate-alphanum-with-spaces',
4956
'url' => 'validate-url',
5057
'email' => 'email2',
5158
'length' => 'validate-length',
@@ -67,7 +74,7 @@ class AttributeMerger
6774
private $customerRepository;
6875

6976
/**
70-
* @var \Magento\Customer\Api\Data\CustomerInterface
77+
* @var CustomerInterface
7178
*/
7279
private $customer;
7380

@@ -309,10 +316,14 @@ protected function getMultilineFieldConfig($attributeCode, array $attributeConfi
309316
}
310317

311318
/**
319+
* Returns default attribute value.
320+
*
312321
* @param string $attributeCode
322+
* @throws NoSuchEntityException
323+
* @throws LocalizedException
313324
* @return null|string
314325
*/
315-
protected function getDefaultValue($attributeCode)
326+
protected function getDefaultValue($attributeCode): ?string
316327
{
317328
if ($attributeCode === 'country_id') {
318329
return $this->directoryHelper->getDefaultCountry();
@@ -346,9 +357,13 @@ protected function getDefaultValue($attributeCode)
346357
}
347358

348359
/**
349-
* @return \Magento\Customer\Api\Data\CustomerInterface|null
360+
* Returns logged customer.
361+
*
362+
* @throws NoSuchEntityException
363+
* @throws LocalizedException
364+
* @return CustomerInterface|null
350365
*/
351-
protected function getCustomer()
366+
protected function getCustomer(): ?CustomerInterface
352367
{
353368
if (!$this->customer) {
354369
if ($this->customerSession->isLoggedIn()) {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
use PHPUnit\Framework\TestCase;
16+
17+
class AttributeMergerTest extends TestCase
18+
{
19+
/**
20+
* @var CustomerRepository
21+
*/
22+
private $customerRepository;
23+
24+
/**
25+
* @var CustomerSession
26+
*/
27+
private $customerSession;
28+
29+
/**
30+
* @var AddressHelper
31+
*/
32+
private $addressHelper;
33+
34+
/**
35+
* @var DirectoryHelper
36+
*/
37+
private $directoryHelper;
38+
39+
/**
40+
* @var AttributeMerger
41+
*/
42+
private $attributeMerger;
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
protected function setUp()
48+
{
49+
50+
$this->customerRepository = $this->createMock(CustomerRepository::class);
51+
$this->customerSession = $this->createMock(CustomerSession::class);
52+
$this->addressHelper = $this->createMock(AddressHelper::class);
53+
$this->directoryHelper = $this->createMock(DirectoryHelper::class);
54+
55+
$this->attributeMerger = new AttributeMerger(
56+
$this->addressHelper,
57+
$this->customerSession,
58+
$this->customerRepository,
59+
$this->directoryHelper
60+
);
61+
}
62+
63+
/**
64+
* Tests of element attributes merging.
65+
*
66+
* @param String $validationRule - validation rule.
67+
* @param String $expectedValidation - expected mapped validation.
68+
* @dataProvider validationRulesDataProvider
69+
*/
70+
public function testMerge(String $validationRule, String $expectedValidation): void
71+
{
72+
$elements = [
73+
'field' => [
74+
'visible' => true,
75+
'formElement' => 'input',
76+
'label' => __('City'),
77+
'value' => null,
78+
'sortOrder' => 1,
79+
'validation' => [
80+
'input_validation' => $validationRule
81+
],
82+
]
83+
];
84+
85+
$actualResult = $this->attributeMerger->merge(
86+
$elements,
87+
'provider',
88+
'dataScope',
89+
['field' =>
90+
[
91+
'validation' => ['length' => true]
92+
]
93+
]
94+
);
95+
96+
$expectedResult = [
97+
$expectedValidation => true,
98+
'length' => true
99+
];
100+
101+
self::assertEquals($expectedResult, $actualResult['field']['validation']);
102+
}
103+
104+
/**
105+
* Provides possible validation types.
106+
*
107+
* @return array
108+
*/
109+
public function validationRulesDataProvider(): array
110+
{
111+
return [
112+
['alpha', 'validate-alpha'],
113+
['numeric', 'validate-number'],
114+
['alphanumeric', 'validate-alphanum'],
115+
['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
116+
['url', 'validate-url'],
117+
['email', 'email2'],
118+
['length', 'validate-length']
119+
];
120+
}
121+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Framework\Validator\EmailAddress;
1313

1414
/**
15+
* Form Element Abstract Data Model
16+
*
1517
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1618
*/
1719
abstract class AbstractData
@@ -137,6 +139,7 @@ public function setRequestScope($scope)
137139

138140
/**
139141
* Set scope visibility
142+
*
140143
* Search value only in scope or search value in scope and global
141144
*
142145
* @param boolean $flag
@@ -281,9 +284,14 @@ protected function _validateInputRule($value)
281284
);
282285

283286
if ($inputValidation !== null) {
287+
$allowWhiteSpace = false;
288+
284289
switch ($inputValidation) {
290+
case 'alphanum-with-spaces':
291+
$allowWhiteSpace = true;
292+
// Continue to alphanumeric validation
285293
case 'alphanumeric':
286-
$validator = new \Zend_Validate_Alnum(true);
294+
$validator = new \Zend_Validate_Alnum($allowWhiteSpace);
287295
$validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
288296
$validator->setMessage(
289297
__('"%1" contains non-alphabetic or non-numeric characters.', $label),

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -205,37 +205,34 @@ 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
211213
* @param bool|array $expectedOutput
212214
* @dataProvider validateInputRuleDataProvider
213215
*/
214-
public function testValidateInputRule($value, $label, $inputValidation, $expectedOutput)
216+
public function testValidateInputRule($value, $label, $inputValidation, $expectedOutput): void
215217
{
216218
$validationRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
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
230+
->method('getStoreLabel')
231+
->willReturn($label);
232+
233+
$this->_attributeMock
234+
->method('getValidationRules')
235+
->willReturn([$validationRule]);
239236

240237
$this->assertEquals($expectedOutput, $this->_model->validateInputRule($value));
241238
}
@@ -256,6 +253,16 @@ public function validateInputRuleDataProvider()
256253
\Zend_Validate_Alnum::NOT_ALNUM => '"mylabel" contains non-alphabetic or non-numeric characters.'
257254
]
258255
],
256+
[
257+
'abc qaz',
258+
'mylabel',
259+
'alphanumeric',
260+
[
261+
\Zend_Validate_Alnum::NOT_ALNUM => '"mylabel" contains non-alphabetic or non-numeric characters.'
262+
]
263+
],
264+
['abcqaz', 'mylabel', 'alphanumeric', true],
265+
['abc qaz', 'mylabel', 'alphanum-with-spaces', true],
259266
[
260267
'!@#$',
261268
'mylabel',

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@ 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 - provided input validation rules
32+
* @param String $validationClass - expected input validation class
33+
* @dataProvider validationRulesDataProvider
34+
*/
35+
public function testGetValidationRules(String $validationRule, String $validationClass): void
3536
{
3637
$expectsRules = [
3738
'required-entry' => true,
38-
'validate-number' => true,
39+
$validationClass => true,
3940
];
40-
$this->validationRule->expects($this->atLeastOnce())
41-
->method('getName')
41+
$this->validationRule->method('getName')
4242
->willReturn('input_validation');
43-
$this->validationRule->expects($this->atLeastOnce())
44-
->method('getValue')
45-
->willReturn('numeric');
4643

47-
$this->assertEquals(
44+
$this->validationRule->method('getValue')
45+
->willReturn($validationRule);
46+
47+
self::assertEquals(
4848
$expectsRules,
4949
$this->validationRules->getValidationRules(
5050
true,
@@ -56,6 +56,23 @@ public function testGetValidationRules()
5656
);
5757
}
5858

59+
/**
60+
* Provides possible validation rules.
61+
*
62+
* @return array
63+
*/
64+
public function validationRulesDataProvider(): array
65+
{
66+
return [
67+
['alpha', 'validate-alpha'],
68+
['numeric', 'validate-number'],
69+
['alphanumeric', 'validate-alphanum'],
70+
['alphanum-with-spaces', 'validate-alphanum-with-spaces'],
71+
['url', 'validate-url'],
72+
['email', 'validate-email']
73+
];
74+
}
75+
5976
public function testGetValidationRulesWithOnlyRequiredRule()
6077
{
6178
$expectsRules = [

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
];

0 commit comments

Comments
 (0)