Skip to content

Commit 580dcf1

Browse files
committed
update city, street, phone number validation and unit test
1 parent 6a458bd commit 580dcf1

File tree

6 files changed

+318
-17
lines changed

6 files changed

+318
-17
lines changed

app/code/Magento/Customer/Model/Validator/City.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ class City extends AbstractValidator
2020
*
2121
* \p{L}: Unicode letters.
2222
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
23-
* ,: Comma.
24-
* \-: Hyphen.
25-
* \.: Period.
26-
* `'’: Single quotes, both regular and right single quotation marks.
27-
* &: Ampersand.
23+
* ': Apostrophe mark.
2824
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
29-
* \d: Digits (0-9).
3025
*/
31-
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\,\-\.\'’`&\s\d]){1,255}+/u';
26+
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\s\-\']{1,100})/u';
3227

3328
/**
3429
* Validate city fields.
@@ -40,7 +35,7 @@ public function isValid($customer)
4035
{
4136
if (!$this->isValidCity($customer->getCity())) {
4237
parent::_addMessages([[
43-
'city' => 'City is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits'
38+
'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces"
4439
]]);
4540
}
4641

app/code/Magento/Customer/Model/Validator/Street.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class Street extends AbstractValidator
2121
* \p{L}: Unicode letters.
2222
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
2323
* ,: Comma.
24-
* \-: Hyphen.
25-
* \.: Period.
24+
* -: Hyphen.
25+
* .: Period.
2626
* `'’: Single quotes, both regular and right single quotation marks.
2727
* &: Ampersand.
2828
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
2929
* \d: Digits (0-9).
3030
*/
31-
private const PATTERN_STREET = '/(?:[\p{L}\p{M}\,\-\.\'’`&\s\d]){1,255}+/u';
31+
private const PATTERN_STREET = "/(?:[\p{L}\p{M},-.'’`&\s\d]){1,255}+/u";
3232

3333
/**
3434
* Validate street fields.
@@ -40,7 +40,9 @@ public function isValid($customer)
4040
{
4141
foreach ($customer->getStreet() as $street) {
4242
if (!$this->isValidStreet($street)) {
43-
parent::_addMessages([['street' => 'Street is not valid! Allowed chars: Unicode letters, Unicode marks, Comma, Hyphen, Period, Single quotes, both regular and right single quotation marks, Ampersand, Whitespace characters, Digits']]);
43+
parent::_addMessages([[
44+
'street' => "Invalid Street Address. Please use A-Z, a-z, 0-9, , - . ' ’ ` & spaces"
45+
]]);
4446
}
4547
}
4648

app/code/Magento/Customer/Model/Validator/Telephone.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ class Telephone extends AbstractValidator
1818
/**
1919
* Allowed char:
2020
*
21-
* \(\) :Matches open and close parentheses
22-
* \s: Matches any whitespace character.
21+
* \() :Matches open and close parentheses
2322
* \+: Matches the plus sign.
2423
* \-: Matches the hyphen.
2524
* \d: Digits (0-9).
2625
*/
27-
private const PATTERN_TELEPHONE = '/(?:[\(\)\+\-\s\d]){1,255}+/u';
28-
26+
private const PATTERN_TELEPHONE = '/(?:[\d\+\-\()]{1,20})/u';
27+
2928
/**
3029
* Validate telephone fields.
3130
*
@@ -35,7 +34,9 @@ class Telephone extends AbstractValidator
3534
public function isValid($customer)
3635
{
3736
if (!$this->isValidTelephone($customer->getTelephone())) {
38-
parent::_addMessages([['telephone' => 'Telephone is not valid! Allowed chars: Matches open and close parentheses, Matches any whitespace character, Matches the plus sign, Matches the hyphen, Digits (0-9)']]);
37+
parent::_addMessages([[
38+
'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ). space not allowed."
39+
]]);
3940
}
4041

4142
return count($this->_messages) == 0;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Customer\Test\Unit\Model\Validator;
9+
10+
use Magento\Customer\Model\Validator\City;
11+
use Magento\Customer\Model\Customer;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Customer city validator tests
17+
*/
18+
class CityTest extends TestCase
19+
{
20+
/**
21+
* @var City
22+
*/
23+
private City $nameValidator;
24+
25+
/**
26+
* @var Customer|MockObject
27+
*/
28+
private MockObject $customerMock;
29+
30+
/**
31+
* @return void
32+
*/
33+
protected function setUp(): void
34+
{
35+
$this->nameValidator = new City;
36+
$this->customerMock = $this
37+
->getMockBuilder(Customer::class)
38+
->disableOriginalConstructor()
39+
->addMethods(['getCity'])
40+
->getMock();
41+
}
42+
43+
/**
44+
* Test for allowed apostrophe and other punctuation characters in customer names
45+
*
46+
* @param string $city
47+
* @param string $message
48+
* @return void
49+
* @dataProvider expectedPunctuationInNamesDataProvider
50+
*/
51+
public function testValidateCorrectPunctuationInNames(
52+
string $city,
53+
string $message
54+
) {
55+
$this->customerMock->expects($this->once())->method('getCity')->willReturn($city);
56+
57+
$isValid = $this->nameValidator->isValid($this->customerMock);
58+
$this->assertTrue($isValid, $message);
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function expectedPunctuationInNamesDataProvider(): array
65+
{
66+
return [
67+
[
68+
'city' => 'Москва',
69+
'message' => 'Unicode letters must be allowed in city'
70+
],
71+
[
72+
'city' => 'Мо́сква',
73+
'message' => 'Unicode marks must be allowed in city'
74+
],
75+
[
76+
'city' => ' Moscow \'',
77+
'message' => 'Apostrophe characters must be allowed in city'
78+
],
79+
[
80+
'city' => ' Moscow Moscow',
81+
'message' => 'Whitespace characters must be allowed in city'
82+
]
83+
];
84+
}
85+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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\Customer\Test\Unit\Model\Validator;
9+
10+
use Magento\Customer\Model\Validator\Street;
11+
use Magento\Customer\Model\Customer;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Customer street validator tests
17+
*/
18+
class StreetTest extends TestCase
19+
{
20+
/**
21+
* @var Street
22+
*/
23+
private Street $nameValidator;
24+
25+
/**
26+
* @var Customer|MockObject
27+
*/
28+
private MockObject $customerMock;
29+
30+
/**
31+
* @return void
32+
*/
33+
protected function setUp(): void
34+
{
35+
$this->nameValidator = new Street;
36+
$this->customerMock = $this
37+
->getMockBuilder(Customer::class)
38+
->disableOriginalConstructor()
39+
->addMethods(['getStreet'])
40+
->getMock();
41+
}
42+
43+
/**
44+
* Test for allowed apostrophe and other punctuation characters in customer names
45+
*
46+
* @param array $street
47+
* @param string $message
48+
* @return void
49+
* @dataProvider expectedPunctuationInNamesDataProvider
50+
*/
51+
public function testValidateCorrectPunctuationInNames(
52+
array $street,
53+
string $message
54+
) {
55+
$this->customerMock->expects($this->once())->method('getStreet')->willReturn($street);
56+
57+
$isValid = $this->nameValidator->isValid($this->customerMock);
58+
$this->assertTrue($isValid, $message);
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function expectedPunctuationInNamesDataProvider(): array
65+
{
66+
return [
67+
[
68+
'street' => [
69+
"123 Rue de l'Étoile",
70+
"Ville d'Ölives, Çôte d'Azur",
71+
"Çôte d'Azur"
72+
],
73+
'message' => 'Unicode marks and Unicode letters must be allowed in street'
74+
],
75+
[
76+
'street' => [
77+
'876 Elm Way, Redwood Lodge',
78+
'456 Pine Street, Serenity Cottage',
79+
'321 Birch Boulevard, Willow Retreat'
80+
],
81+
'message' => 'Comma must be allowed in street'
82+
],
83+
[
84+
'street' => [
85+
'321 Birch Boulevard-Retreat',
86+
'234 Spruce Place-Residence',
87+
'456 Pine Street-Haven'
88+
],
89+
'message' => 'Hyphen must be allowed in street'
90+
],
91+
[
92+
'street' => [
93+
'1234 Elm St.',
94+
'Main. Street',
95+
'1234 Elm St'
96+
],
97+
'message' => 'Period must be allowed in street'
98+
],
99+
[
100+
'street' => [
101+
'O\'Connell Street',
102+
'O`Connell Street',
103+
'321 Birch Boulevard ’Willow Retreat’'
104+
],
105+
'message' => 'quotes must be allowed in street'
106+
],
107+
[
108+
'street' => [
109+
'123 Main Street & Elm Avenue',
110+
'456 Pine Street & Maple Avenue',
111+
'789 Oak Lane & Cedar Road'
112+
],
113+
'message' => 'Ampersand must be allowed in street'
114+
],
115+
[
116+
'street' => [
117+
'Oak Lane Space',
118+
'Birch Boulevard Space',
119+
'Spruce Place'
120+
],
121+
'message' => 'Whitespace must be allowed in street'
122+
],
123+
[
124+
'street' => [
125+
'234 Spruce Place',
126+
'321 Birch Boulevard',
127+
'876 Elm Way'
128+
],
129+
'message' => 'Digits must be allowed in street'
130+
]
131+
];
132+
}
133+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Customer\Test\Unit\Model\Validator;
9+
10+
use Magento\Customer\Model\Validator\Telephone;
11+
use Magento\Customer\Model\Customer;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Customer telephone validator tests
17+
*/
18+
class TelephoneTest extends TestCase
19+
{
20+
/**
21+
* @var Telephone
22+
*/
23+
private Telephone $nameValidator;
24+
25+
/**
26+
* @var Customer|MockObject
27+
*/
28+
private MockObject $customerMock;
29+
30+
/**
31+
* @return void
32+
*/
33+
protected function setUp(): void
34+
{
35+
$this->nameValidator = new Telephone;
36+
$this->customerMock = $this
37+
->getMockBuilder(Customer::class)
38+
->disableOriginalConstructor()
39+
->addMethods(['getTelephone'])
40+
->getMock();
41+
}
42+
43+
/**
44+
* Test for allowed apostrophe and other punctuation characters in customer names
45+
*
46+
* @param string $telephone
47+
* @param string $message
48+
* @return void
49+
* @dataProvider expectedPunctuationInNamesDataProvider
50+
*/
51+
public function testValidateCorrectPunctuationInNames(
52+
string $telephone,
53+
string $message
54+
) {
55+
$this->customerMock->expects($this->once())->method('getTelephone')->willReturn($telephone);
56+
57+
$isValid = $this->nameValidator->isValid($this->customerMock);
58+
$this->assertTrue($isValid, $message);
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function expectedPunctuationInNamesDataProvider(): array
65+
{
66+
return [
67+
[
68+
'telephone' => '(1)99887766',
69+
'message' => 'parentheses must be allowed in telephone'
70+
],
71+
[
72+
'telephone' => '+6255554444',
73+
'message' => 'plus sign be allowed in telephone'
74+
],
75+
[
76+
'telephone' => '555-555-555',
77+
'message' => 'hyphen must be allowed in telephone'
78+
],
79+
[
80+
'telephone' => '123456789',
81+
'message' => 'Digits (numbers) must be allowed in telephone'
82+
]
83+
];
84+
}
85+
}

0 commit comments

Comments
 (0)