Skip to content
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

[EAV] Improving the EAV attribute code validation, by not allowing to use n… #20526

Conversation

eduard13
Copy link
Contributor

@eduard13 eduard13 commented Jan 23, 2019

Description (*)

This PR improves the attribute code validation, namely while creating the attribute programmatically. Before you were able to create (programmatically) an attribute with not allowed chars.

Fixed Issues (if relevant)

  1. No complete validation while creation of attributes. #20943: No complete validation while creation of attributes.
  2. AttributeCode column name length validation throws wrong error message #20766: AttributeCode column name length validation throws wrong error message

Manual testing scenarios (*)

  1. Try to create a new attribute using dots, or with a first char being not letter
  2. Try to create an attribute with code lenth less than 1 or more than 30 characters

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @eduard13. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@magento-engcom-team magento-engcom-team added Component: Eav Release Line: 2.3 Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels Jan 23, 2019
@@ -0,0 +1,63 @@
<?php
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to use strict types within a scope of new implementations

Copy link
Contributor Author

@eduard13 eduard13 Jan 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, the strict typing was declared.
Also do you think it's a good idea to rename this variable name into a less meaningful name (in order to fix the failing pipeline)?

Code $attributeCodeValidator = null

Copy link
Contributor

@rogyar rogyar Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have implemented the naming in the same manner as other validators. So, it's ok.
You could use an alias in the use section for this class. Something like:

use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rogyar the alias was added.

@irajneeshgupta
Copy link
Member

@eduard13
can you please add caps pattern also in Pregmatch regex.
['pattern' => '/^[a-zA-Z\x{600}-\x{6FF}][a-zA-Z\x{600}-\x{6FF}_0-9]{0,30}$/u']
Thank you.

@eduard13
Copy link
Contributor Author

eduard13 commented Feb 4, 2019

@irajneeshgupta, I don't think that we should add a such validation, as you're able to create attributes with capitalized letters from the admin panel, moreover I don't see what issue can occur by having such attributes.

@irajneeshgupta
Copy link
Member

if (trim($attributeCode)
            && !preg_match('/^[a-z][a-z0-9_]*$/', trim($attributeCode))
        ) {

@eduard13

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php#L200

I think validation pattern should be same for both admin and creation of attributes programmatically...if admin user can create such attributes , then why such limitations for developers hence i requested you to add it.

Thank you.

@irajneeshgupta
Copy link
Member

@eduard13 👍

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eduard13 thanks for the pull request! Please see my code review notes

$validatorAttrCode = new \Zend_Validate_Regex(
['pattern' => '/^[a-zA-Z\x{600}-\x{6FF}][a-zA-Z\x{600}-\x{6FF}_0-9]{0,30}$/u']
['pattern' => '/^[a-zA-Z]+[a-zA-Z0-9_]*$/u']
Copy link
Member

@sivaschenko sivaschenko Feb 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to move this pattern to a constant, as it is used more than once (I think added validation class is a good place for such constant to be declared).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.
Thanks.

* @return bool
* @throws LocalizedException
*/
public function isValid($attributeCode): bool
Copy link
Member

@sivaschenko sivaschenko Feb 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method named isValid should return bool and should not throw an exception. Validation messages should be collected using _addMessages parent method. Appropriate exceptions should be thrown if needed by client classes (that can access messages via getMessages validator method)

*
* Validation EAV attribute code
*/
class Code extends AbstractValidator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Code extends AbstractValidator
class Code extends AbstractValidator

*/
declare(strict_types=1);

namespace Magento\Eav\Model\Validator\Attribute;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cover the validator class with a unit test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class was covered by Unit Test, and also all the related tests (unit, integration) were updated.

@eduard13
Copy link
Contributor Author

@sivaschenko thank you for the review, I've updated the implementation.
Please note that I've completely removed the validation from Save controller, and moved it into Validate one, as you shouldn't reach the Save if the validation is failed.

Could you please review the updates?
Thank you.

@sivaschenko
Copy link
Member

I believe this pull request also resolves #20766

@magento-engcom-team
Copy link
Contributor

Hi @rogyar, thank you for the review.
ENGCOM-4246 has been created to process this Pull Request

@VasylShvorak
Copy link
Contributor

✔️ QA passed

@m2-assistant
Copy link

m2-assistant bot commented Mar 30, 2019

Hi @eduard13, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@eduard13
Copy link
Contributor Author

eduard13 commented Apr 1, 2019

@magento-engcom-team is there any reason of marking the PR just as an Improvement? Not even a Bug fix or Test Coverage?
cc: @sidolov, @sivaschenko
Thank you.

@sidolov
Copy link
Contributor

sidolov commented Apr 1, 2019

Hi @eduard13 , right now only members of the Community Engineering team are able to set up achievements for pull requests, that why our automated tool removed them. I reverted the changes because your pull request really complex and should be rewarded.
Thank you!

@eduard13
Copy link
Contributor Author

eduard13 commented Apr 1, 2019

Hi @sidolov, glad to hear that, but whom should I ping for recalculating the reward points for March?
Thank you.

@sidolov
Copy link
Contributor

sidolov commented Apr 1, 2019

Hi @eduard13 , points already recalculated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants