diff --git a/app/code/Magento/Customer/Model/Validator/City.php b/app/code/Magento/Customer/Model/Validator/City.php new file mode 100644 index 0000000000000..0b53551dfd88f --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/City.php @@ -0,0 +1,61 @@ +isValidCity($customer->getCity())) { + parent::_addMessages([[ + 'city' => "Invalid City. Please use A-Z, a-z, 0-9, -, ', spaces" + ]]); + } + + return count($this->_messages) == 0; + } + + /** + * Check if city field is valid. + * + * @param string|null $cityValue + * @return bool + */ + private function isValidCity($cityValue) + { + if ($cityValue != null) { + if (preg_match(self::PATTERN_CITY, $cityValue, $matches)) { + return $matches[0] == $cityValue; + } + } + + return true; + } +} diff --git a/app/code/Magento/Customer/Model/Validator/Street.php b/app/code/Magento/Customer/Model/Validator/Street.php new file mode 100644 index 0000000000000..7de57d0ed32ef --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/Street.php @@ -0,0 +1,68 @@ +getStreet() as $street) { + if (!$this->isValidStreet($street)) { + parent::_addMessages([[ + 'street' => "Invalid Street Address. Please use A-Z, a-z, 0-9, , - . ' ’ ` & spaces" + ]]); + } + } + + return count($this->_messages) == 0; + } + + /** + * Check if street field is valid. + * + * @param string|null $streetValue + * @return bool + */ + private function isValidStreet($streetValue) + { + if ($streetValue != null) { + if (preg_match(self::PATTERN_STREET, $streetValue, $matches)) { + return $matches[0] == $streetValue; + } + } + + return true; + } +} diff --git a/app/code/Magento/Customer/Model/Validator/Telephone.php b/app/code/Magento/Customer/Model/Validator/Telephone.php new file mode 100644 index 0000000000000..0c85cb51f7e3d --- /dev/null +++ b/app/code/Magento/Customer/Model/Validator/Telephone.php @@ -0,0 +1,61 @@ +isValidTelephone($customer->getTelephone())) { + parent::_addMessages([[ + 'telephone' => "Invalid Phone Number. Please use 0-9, +, -, (, ) and space." + ]]); + } + + return count($this->_messages) == 0; + } + + /** + * Check if telephone field is valid. + * + * @param string|null $telephoneValue + * @return bool + */ + private function isValidTelephone($telephoneValue) + { + if ($telephoneValue != null) { + if (preg_match(self::PATTERN_TELEPHONE, (string) $telephoneValue, $matches)) { + return $matches[0] == $telephoneValue; + } + } + + return true; + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php new file mode 100644 index 0000000000000..9c15427154fea --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/CityTest.php @@ -0,0 +1,85 @@ +nameValidator = new City; + $this->customerMock = $this + ->getMockBuilder(Customer::class) + ->disableOriginalConstructor() + ->addMethods(['getCity']) + ->getMock(); + } + + /** + * Test for allowed apostrophe and other punctuation characters in customer names + * + * @param string $city + * @param string $message + * @return void + * @dataProvider expectedPunctuationInNamesDataProvider + */ + public function testValidateCorrectPunctuationInNames( + string $city, + string $message + ) { + $this->customerMock->expects($this->once())->method('getCity')->willReturn($city); + + $isValid = $this->nameValidator->isValid($this->customerMock); + $this->assertTrue($isValid, $message); + } + + /** + * @return array + */ + public function expectedPunctuationInNamesDataProvider(): array + { + return [ + [ + 'city' => 'Москва', + 'message' => 'Unicode letters must be allowed in city' + ], + [ + 'city' => 'Мо́сква', + 'message' => 'Unicode marks must be allowed in city' + ], + [ + 'city' => ' Moscow \'', + 'message' => 'Apostrophe characters must be allowed in city' + ], + [ + 'city' => ' Moscow Moscow', + 'message' => 'Whitespace characters must be allowed in city' + ] + ]; + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php new file mode 100644 index 0000000000000..6d40bec460b3e --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/StreetTest.php @@ -0,0 +1,133 @@ +nameValidator = new Street; + $this->customerMock = $this + ->getMockBuilder(Customer::class) + ->disableOriginalConstructor() + ->addMethods(['getStreet']) + ->getMock(); + } + + /** + * Test for allowed apostrophe and other punctuation characters in customer names + * + * @param array $street + * @param string $message + * @return void + * @dataProvider expectedPunctuationInNamesDataProvider + */ + public function testValidateCorrectPunctuationInNames( + array $street, + string $message + ) { + $this->customerMock->expects($this->once())->method('getStreet')->willReturn($street); + + $isValid = $this->nameValidator->isValid($this->customerMock); + $this->assertTrue($isValid, $message); + } + + /** + * @return array + */ + public function expectedPunctuationInNamesDataProvider(): array + { + return [ + [ + 'street' => [ + "123 Rue de l'Étoile", + "Ville d'Ölives, Çôte d'Azur", + "Çôte d'Azur" + ], + 'message' => 'Unicode marks and Unicode letters must be allowed in street' + ], + [ + 'street' => [ + '876 Elm Way, Redwood Lodge', + '456 Pine Street, Serenity Cottage', + '321 Birch Boulevard, Willow Retreat' + ], + 'message' => 'Comma must be allowed in street' + ], + [ + 'street' => [ + '321 Birch Boulevard-Retreat', + '234 Spruce Place-Residence', + '456 Pine Street-Haven' + ], + 'message' => 'Hyphen must be allowed in street' + ], + [ + 'street' => [ + '1234 Elm St.', + 'Main. Street', + '1234 Elm St' + ], + 'message' => 'Period must be allowed in street' + ], + [ + 'street' => [ + 'O\'Connell Street', + 'O`Connell Street', + '321 Birch Boulevard ’Willow Retreat’' + ], + 'message' => 'quotes must be allowed in street' + ], + [ + 'street' => [ + '123 Main Street & Elm Avenue', + '456 Pine Street & Maple Avenue', + '789 Oak Lane & Cedar Road' + ], + 'message' => 'Ampersand must be allowed in street' + ], + [ + 'street' => [ + 'Oak Lane Space', + 'Birch Boulevard Space', + 'Spruce Place' + ], + 'message' => 'Whitespace must be allowed in street' + ], + [ + 'street' => [ + '234 Spruce Place', + '321 Birch Boulevard', + '876 Elm Way' + ], + 'message' => 'Digits must be allowed in street' + ] + ]; + } +} diff --git a/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php b/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php new file mode 100644 index 0000000000000..47a9d6da18831 --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Validator/TelephoneTest.php @@ -0,0 +1,85 @@ +nameValidator = new Telephone; + $this->customerMock = $this + ->getMockBuilder(Customer::class) + ->disableOriginalConstructor() + ->addMethods(['getTelephone']) + ->getMock(); + } + + /** + * Test for allowed apostrophe and other punctuation characters in customer names + * + * @param string $telephone + * @param string $message + * @return void + * @dataProvider expectedPunctuationInNamesDataProvider + */ + public function testValidateCorrectPunctuationInNames( + string $telephone, + string $message + ) { + $this->customerMock->expects($this->once())->method('getTelephone')->willReturn($telephone); + + $isValid = $this->nameValidator->isValid($this->customerMock); + $this->assertTrue($isValid, $message); + } + + /** + * @return array + */ + public function expectedPunctuationInNamesDataProvider(): array + { + return [ + [ + 'telephone' => '(1)99887766', + 'message' => 'parentheses must be allowed in telephone' + ], + [ + 'telephone' => '+6255554444', + 'message' => 'plus sign be allowed in telephone' + ], + [ + 'telephone' => '555-555-555', + 'message' => 'hyphen must be allowed in telephone' + ], + [ + 'telephone' => '123456789', + 'message' => 'Digits (numbers) must be allowed in telephone' + ] + ]; + } +} diff --git a/app/code/Magento/Customer/etc/validation.xml b/app/code/Magento/Customer/etc/validation.xml index bac6e54afa7b5..7fd6cfeb79472 100644 --- a/app/code/Magento/Customer/etc/validation.xml +++ b/app/code/Magento/Customer/etc/validation.xml @@ -46,11 +46,35 @@ + + + + + + + + + + + + + + + + + + + + + + + +