Skip to content

Replace Zend_Validation #4612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 58 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
aec1a31
added respect/validation
sreichel Feb 5, 2025
6111f8c
replaced Zend_Validate::
sreichel Feb 5, 2025
e3fa261
replaced Zend_Validate_File_Extension
sreichel Feb 6, 2025
4fed24d
doc
sreichel Feb 6, 2025
89bcb2b
phpstan
sreichel Feb 7, 2025
ed884ad
test symfony
sreichel Feb 8, 2025
54f5892
Merge branch 'main' into validation
sreichel Mar 29, 2025
c323535
use symfony (not tested)
sreichel Mar 30, 2025
9cb98e5
phpstan baseline
sreichel Mar 30, 2025
69903cf
Merge branch 'main' into validation
sreichel Apr 6, 2025
65cd4ae
some fixes
sreichel Apr 6, 2025
d7c6049
some updates
sreichel Apr 6, 2025
0f6e197
added test
sreichel Apr 6, 2025
b6c03ae
added test
sreichel Apr 6, 2025
3583b5f
Merge remote-tracking branch 'origin/validation' into validation
sreichel Apr 7, 2025
4fe1c54
fix file cannot be empty
sreichel Apr 9, 2025
0411938
renaming
sreichel Apr 9, 2025
6f91392
Merge branch 'main' into validation
sreichel Apr 9, 2025
37dd0ef
update
sreichel Apr 9, 2025
3e7be18
update
sreichel Apr 9, 2025
00f3c49
test
sreichel Apr 9, 2025
9b6948a
updated test
sreichel Apr 10, 2025
f00f13b
updated test
sreichel Apr 10, 2025
fd8f1bb
updated test
sreichel Apr 10, 2025
0c59f9d
updated test
sreichel Apr 10, 2025
793e310
update
sreichel Apr 10, 2025
cd473a5
updated test
sreichel Apr 10, 2025
478a2ad
typo
sreichel Apr 10, 2025
e3207a6
added test
sreichel Apr 11, 2025
b1606bc
update
sreichel Apr 12, 2025
2869035
fix + test
sreichel Apr 14, 2025
7df9000
update
sreichel Apr 14, 2025
dcbfdcf
skip valid data, to not send email
sreichel Apr 14, 2025
616d488
update
sreichel Apr 14, 2025
1de9167
updated tests
sreichel Apr 14, 2025
d6752f0
rector
sreichel Apr 14, 2025
3df9d26
rector
sreichel Apr 14, 2025
82c5d79
updated tests
sreichel Apr 14, 2025
70c3f7d
updated tests -. fix
sreichel Apr 14, 2025
7786276
updated tests
sreichel Apr 14, 2025
aad34d6
split unit tests
sreichel Apr 14, 2025
d54e5de
test
sreichel Apr 14, 2025
b13ea95
fix later
sreichel Apr 14, 2025
dc6a5cd
updated tests
sreichel Apr 14, 2025
47a37ad
typo
sreichel Apr 14, 2025
df9ce5f
updated tests
sreichel Apr 15, 2025
ec9aa27
baseline phpunit
sreichel Apr 15, 2025
b91ffdd
Merge branch 'main' into validation
sreichel Apr 15, 2025
f718560
merge tests from main
sreichel May 7, 2025
d521531
Merge branch 'main' into validation
sreichel May 7, 2025
f6d55e2
Merge branch 'main' into validation
sreichel May 7, 2025
198a279
simplified for now
sreichel May 7, 2025
cc86b05
phpstan
sreichel May 7, 2025
39e5606
Merge branch 'main' into validation
sreichel Jun 15, 2025
16fe79d
Merge branch 'main' into validation
sreichel Jul 21, 2025
7932e12
composer update
sreichel Jul 21, 2025
ff3323f
updated tests
sreichel Jul 21, 2025
bdafcbd
updated tests
sreichel Jul 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// PHP84: Adds or removes ? before single type declarations or |null at the end of union types when parameters have a default null value.
'nullable_type_declaration_for_default_null_value' => true,
// Calls to PHPUnit\Framework\TestCase static methods must all be of the same type, either $this->, self:: or static::
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'static'],
// Convert double quotes to single quotes for simple strings.
'single_quote' => true,
// Arguments lists, array destructuring lists, arrays that are multi-line, match-lines and parameters lists must have a trailing comma.
Expand Down
3 changes: 3 additions & 0 deletions .rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rector\Php82\Rector as Php82;
use Rector\Php83\Rector as Php83;
use Rector\Php84\Rector as Php84;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\TypeDeclaration\Rector as TypeDeclaration;

try {
Expand Down Expand Up @@ -42,6 +43,8 @@
Php80\Class_\ClassPropertyAssignToConstructorPromotionRector::class, # todo: wait for php80
Php80\Class_\StringableForToStringRector::class, # todo: wait for php80
TypeDeclaration\ClassMethod\ReturnNeverTypeRector::class,
# use static methods
PreferPHPUnitThisCallRector::class,
__DIR__ . '/shell/translations.php',
__DIR__ . '/shell/update-copyright.php',
__DIR__ . '/tests/unit/Mage/Reports/Model/Resource/Report/CollectionTest.php',
Expand Down
52 changes: 36 additions & 16 deletions app/code/core/Mage/Admin/Model/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* Class Mage_Admin_Model_Block
*
Expand All @@ -29,6 +32,8 @@
*/
class Mage_Admin_Model_Block extends Mage_Core_Model_Abstract
{
public const BLOCK_NAME_REGEX = '/^[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9\/]+$/';

/**
* Initialize variable model
*/
Expand All @@ -40,31 +45,46 @@ protected function _construct()
/**
* @return array|true
* @throws Exception
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = [];
$validator = Validation::createValidator();
$violations = [];
$errors = new ArrayObject();

if (!Zend_Validate::is($this->getBlockName(), 'NotEmpty')) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is required field.');
}
$disallowedBlockNames = Mage::helper('admin/block')->getDisallowedBlockNames();
if (in_array($this->getBlockName(), $disallowedBlockNames)) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is disallowed.');
}
if (!Zend_Validate::is($this->getBlockName(), 'Regex', ['/^[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9\/]+$/'])) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is incorrect.');
}
$violations[] = $validator->validate($this->getBlockName(), [
new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('Block Name is required field.'),
]),
new Assert\Regex([
'pattern' => self::BLOCK_NAME_REGEX,
'message' => Mage::helper('adminhtml')->__('Block Name is incorrect.'),
]),
new Assert\Choice([
'choices' => Mage::helper('admin/block')->getDisallowedBlockNames(),
'match' => false,
'message' => Mage::helper('adminhtml')->__('Block Name is disallowed.'),
]),
]);

$violations[] = $validator->validate($this->getIsAllowed(), [
new Assert\Choice([
'choices' => ['0', '1'],
'message' => Mage::helper('adminhtml')->__('Is Allowed is required field.'),
]),
]);

if (!in_array($this->getIsAllowed(), ['0', '1'])) {
$errors[] = Mage::helper('adminhtml')->__('Is Allowed is required field.');
foreach ($violations as $violation) {
foreach ($violation as $error) {
$errors->append($error->getMessage());
}
}

if (empty($errors)) {
if (count($errors) === 0) {
return true;
}
return $errors;

return (array) $errors;
}

/**
Expand Down
83 changes: 56 additions & 27 deletions app/code/core/Mage/Admin/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* Admin user model
*
Expand Down Expand Up @@ -579,26 +582,38 @@ public function getStartupPageUrl()
* Returns TRUE or array of errors.
*
* @return array|true
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = new ArrayObject();

if (!Zend_Validate::is($this->getUsername(), 'NotEmpty')) {
$errors->append(Mage::helper('adminhtml')->__('User Name is required field.'));
}

if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
$errors->append(Mage::helper('adminhtml')->__('First Name is required field.'));
}

if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
$errors->append(Mage::helper('adminhtml')->__('Last Name is required field.'));
}
$validator = Validation::createValidator();
$violations = [];
$errors = new ArrayObject();

$violations[] = $validator->validate($this->getUsername(), [new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('User Name is required field.'),
])]);

$violations[] = $validator->validate($this->getFirstname(), [new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('First Name is required field.'),
])]);

$violations[] = $validator->validate($this->getLastname(), [new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('Last Name is required field.'),
])]);

$violations[] = $validator->validate($this->getEmail(), [
new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('Please enter a valid email.'),
]),
new Assert\Email([
'message' => Mage::helper('adminhtml')->__('Please enter a valid email.'),
]),
]);

if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
$errors->append(Mage::helper('adminhtml')->__('Please enter a valid email.'));
foreach ($violations as $violation) {
foreach ($violation as $error) {
$errors->append($error->getMessage());
}
}

if ($this->hasNewPassword()) {
Expand All @@ -608,18 +623,31 @@ public function validate()
}
if (isset($password)) {
$minAdminPasswordLength = $this->getMinAdminPasswordLength();
if (Mage::helper('core/string')->strlen($password) < $minAdminPasswordLength) {
$errors->append(Mage::helper('adminhtml')
->__('Password must be at least of %d characters.', $minAdminPasswordLength));
}
$violations = [];
$violations[] = $validator->validate($password, [
new Assert\Length([
'min' => $minAdminPasswordLength,
'minMessage' => Mage::helper('adminhtml')->__('Password must be at least of %d characters.', $minAdminPasswordLength),
]),
new Assert\Regex([
'pattern' => '/^(?=.*[a-z])(?=.*[0-9]).+$/iu',
'message' => Mage::helper('adminhtml')->__('Password must include both numeric and alphabetic characters.'),
]),
]);

if (!preg_match('/[a-z]/iu', $password) || !preg_match('/[0-9]/u', $password)) {
$errors->append(Mage::helper('adminhtml')
->__('Password must include both numeric and alphabetic characters.'));
if ($this->hasPasswordConfirmation()) {
$violations[] = $validator->validate($this->getPasswordConfirmation(), [
new Assert\IdenticalTo([
'value' => $password,
'message' => Mage::helper('adminhtml')->__('Password confirmation must be same as password.'),
]),
]);
}

if ($this->hasPasswordConfirmation() && $password != $this->getPasswordConfirmation()) {
$errors->append(Mage::helper('adminhtml')->__('Password confirmation must be same as password.'));
foreach ($violations as $violation) {
foreach ($violation as $error) {
$errors->append($error->getMessage());
}
}

Mage::dispatchEvent('admin_user_validate', [
Expand All @@ -645,13 +673,14 @@ public function validate()
*
* @param string $password
* @return array|true
* @throws Zend_Validate_Exception
* @throws Exception
*/
public function validateCurrentPassword($password)
{
$validator = Validation::createValidator();
$result = [];

if (!Zend_Validate::is($password, 'NotEmpty')) {
if ($validator->validate($password, [new Assert\NotBlank()])->count() > 0) {
$result[] = $this->_getHelper('adminhtml')->__('Current password field cannot be empty.');
} elseif (is_null($this->getId()) || !Mage::helper('core')->validateHash($password, $this->getPassword())) {
$result[] = $this->_getHelper('adminhtml')->__('Invalid current password.');
Expand Down
41 changes: 28 additions & 13 deletions app/code/core/Mage/Admin/Model/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* Class Mage_Admin_Model_Variable
*
Expand All @@ -38,29 +41,41 @@ protected function _construct()
}

/**
* @return array|bool
* @return array|true
* @throws Exception
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = [];
$validator = Validation::createValidator();
$violations = [];
$errors = new ArrayObject();

if (!Zend_Validate::is($this->getVariableName(), 'NotEmpty')) {
$errors[] = Mage::helper('adminhtml')->__('Variable Name is required field.');
}
if (!Zend_Validate::is($this->getVariableName(), 'Regex', ['/^[-_a-zA-Z0-9\/]*$/'])) {
$errors[] = Mage::helper('adminhtml')->__('Variable Name is incorrect.');
}
$violations[] = $validator->validate($this->getVariableName(), [
new Assert\NotBlank([
'message' => Mage::helper('adminhtml')->__('Variable Name is required field.'),
]),
new Assert\Regex([
'message' => Mage::helper('adminhtml')->__('Variable Name is incorrect.'),
'pattern' => '/^[-_a-zA-Z0-9\/]*$/',
]),
]);

if (!in_array($this->getIsAllowed(), ['0', '1'])) {
$errors[] = Mage::helper('adminhtml')->__('Is Allowed is required field.');
$violations[] = $validator->validate($this->getIsAllowed(), [new Assert\Choice([
'choices' => ['0', '1'],
'message' => Mage::helper('adminhtml')->__('Is Allowed is required field.'),
])]);

foreach ($violations as $violation) {
foreach ($violation as $error) {
$errors->append($error->getMessage());
}
}

if (empty($errors)) {
if (count($errors) === 0) {
return true;
}
return $errors;

return (array) $errors;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* System config email field backend model
*
Expand All @@ -22,11 +25,15 @@
*/
class Mage_Adminhtml_Model_System_Config_Backend_Email_Address extends Mage_Core_Model_Config_Data
{
/**
* @throws Mage_Core_Exception
*/
protected function _beforeSave()
{
$value = $this->getValue();
if (!Zend_Validate::is($value, 'EmailAddress')) {
Mage::throwException(Mage::helper('adminhtml')->__('Invalid email address "%s".', $value));
$email = $this->getValue();
$validator = Validation::createValidator();
if ($validator->validate($email, [new Assert\NotBlank(), new Assert\Email()])->count() > 0) {
Mage::throwException(Mage::helper('adminhtml')->__('Invalid email address "%s".', $email));
}
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* Catalog product attribute controller
*
Expand Down Expand Up @@ -207,8 +210,10 @@ public function saveAction()

//validate attribute_code
if (isset($data['attribute_code'])) {
$validatorAttrCode = new Zend_Validate_Regex(['pattern' => '/^(?!event$)[a-z][a-z_0-9]{1,254}$/']);
if (!$validatorAttrCode->isValid($data['attribute_code'])) {
$validator = Validation::createValidator();
if ($validator->validate($data['attribute_code'], new Assert\Regex([
'pattern' => '/^(?!event$)[a-z][a-z_0-9]{1,254}$/',
]))->count() > 0) {
$session->addError(
Mage::helper('catalog')->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter. Do not use "event" for an attribute code.'),
);
Expand Down
6 changes: 5 additions & 1 deletion app/code/core/Mage/Adminhtml/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validation;

/**
* Index admin controller
*
Expand Down Expand Up @@ -220,7 +223,8 @@ public function forgotpasswordAction()
if ($this->_validateFormKey()) {
if (!empty($email)) {
// Validate received data to be an email address
if (Zend_Validate::is($email, 'EmailAddress')) {
$validator = Validation::createValidator();
if ($validator->validate($email, [new Assert\NotBlank(), new Assert\Email()])->count() === 0) {
$collection = Mage::getResourceModel('admin/user_collection');
/** @var Mage_Admin_Model_Resource_User_Collection $collection */
$collection->addFieldToFilter('email', $email);
Expand Down
Loading
Loading