Skip to content

Commit c5c662b

Browse files
committed
Merge remote-tracking branch '38345/fix-38331' into community_prs_april
2 parents 8899db8 + a50615a commit c5c662b

File tree

7 files changed

+517
-0
lines changed

7 files changed

+517
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer city fields validator.
15+
*/
16+
class City extends AbstractValidator
17+
{
18+
/**
19+
* Allowed characters:
20+
*
21+
* \p{L}: Unicode letters.
22+
* \p{M}: Unicode marks (diacritic marks, accents, etc.).
23+
* ': Apostrophe mark.
24+
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
25+
*/
26+
private const PATTERN_CITY = '/(?:[\p{L}\p{M}\s\-\']{1,100})/u';
27+
28+
/**
29+
* Validate city fields.
30+
*
31+
* @param Customer $customer
32+
* @return bool
33+
*/
34+
public function isValid($customer)
35+
{
36+
if (!$this->isValidCity($customer->getCity())) {
37+
parent::_addMessages([[
38+
'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces"
39+
]]);
40+
}
41+
42+
return count($this->_messages) == 0;
43+
}
44+
45+
/**
46+
* Check if city field is valid.
47+
*
48+
* @param string|null $cityValue
49+
* @return bool
50+
*/
51+
private function isValidCity($cityValue)
52+
{
53+
if ($cityValue != null) {
54+
if (preg_match(self::PATTERN_CITY, $cityValue, $matches)) {
55+
return $matches[0] == $cityValue;
56+
}
57+
}
58+
59+
return true;
60+
}
61+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer street fields validator.
15+
*/
16+
class Street extends AbstractValidator
17+
{
18+
/**
19+
* Allowed characters:
20+
*
21+
* \p{L}: Unicode letters.
22+
* \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.
28+
* \s: Whitespace characters (spaces, tabs, newlines, etc.).
29+
* \d: Digits (0-9).
30+
*/
31+
private const PATTERN_STREET = "/(?:[\p{L}\p{M}\"[],-.'’`&\s\d]){1,255}+/u";
32+
33+
/**
34+
* Validate street fields.
35+
*
36+
* @param Customer $customer
37+
* @return bool
38+
*/
39+
public function isValid($customer)
40+
{
41+
foreach ($customer->getStreet() as $street) {
42+
if (!$this->isValidStreet($street)) {
43+
parent::_addMessages([[
44+
'street' => "Invalid Street Address. Please use A-Z, a-z, 0-9, , - . ' ’ ` & spaces"
45+
]]);
46+
}
47+
}
48+
49+
return count($this->_messages) == 0;
50+
}
51+
52+
/**
53+
* Check if street field is valid.
54+
*
55+
* @param string|null $streetValue
56+
* @return bool
57+
*/
58+
private function isValidStreet($streetValue)
59+
{
60+
if ($streetValue != null) {
61+
if (preg_match(self::PATTERN_STREET, $streetValue, $matches)) {
62+
return $matches[0] == $streetValue;
63+
}
64+
}
65+
66+
return true;
67+
}
68+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Model\Validator;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
13+
/**
14+
* Customer telephone fields validator.
15+
*/
16+
class Telephone extends AbstractValidator
17+
{
18+
/**
19+
* Allowed char:
20+
*
21+
* \() :Matches open and close parentheses
22+
* \+: Matches the plus sign.
23+
* \-: Matches the hyphen.
24+
* \d: Digits (0-9).
25+
*/
26+
private const PATTERN_TELEPHONE = '/(?:[\d\s\+\-\()]{1,20})/u';
27+
28+
/**
29+
* Validate telephone fields.
30+
*
31+
* @param Customer $customer
32+
* @return bool
33+
*/
34+
public function isValid($customer)
35+
{
36+
if (!$this->isValidTelephone($customer->getTelephone())) {
37+
parent::_addMessages([[
38+
'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ) and space."
39+
]]);
40+
}
41+
42+
return count($this->_messages) == 0;
43+
}
44+
45+
/**
46+
* Check if telephone field is valid.
47+
*
48+
* @param string|null $telephoneValue
49+
* @return bool
50+
*/
51+
private function isValidTelephone($telephoneValue)
52+
{
53+
if ($telephoneValue != null) {
54+
if (preg_match(self::PATTERN_TELEPHONE, (string) $telephoneValue, $matches)) {
55+
return $matches[0] == $telephoneValue;
56+
}
57+
}
58+
59+
return true;
60+
}
61+
}
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+
}

0 commit comments

Comments
 (0)