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

Normalize new line symbols in Product Text Option (type=area) #26033

Merged
merged 4 commits into from
Dec 29, 2019

Conversation

Leone
Copy link
Contributor

@Leone Leone commented Dec 13, 2019

Description (*)

Normalize new line symbols in Product Text Option (type=area).

If you add a customizable option to the product (type = area, max characters X), the length of the option value is counts differently by javascript validator and php.
I added the method to normalize new line symbols in text option.

Fixed Issues (if relevant)

  1. Amount of characters on a 'Area' Customizable Option counted differently on backend/frontend #25974: Amount of characters on a 'Area' Customizable Option counted differently on backend/frontend

Manual testing scenarios (*)

Steps to reproduce from #25974

  1. Try to put this product in your basket with a message 'Is twenty characters' => OK on front and can add to cart.
  2. Try to put this product in your basket with a message with a newline:
    'Is
    twenty
    characters'
    => Ok on front, but error on adding to cart: 'The text is too long.'
  3. Error shown although the Javascript validator passed.

Questions or comments

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 are green)

…Option counted differently on backend/frontend"
@Leone Leone requested a review from akaplya as a code owner December 13, 2019 18:31
@m2-assistant
Copy link

m2-assistant bot commented Dec 13, 2019

Hi @Leone. 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 give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@magento-engcom-team magento-engcom-team added Partner: creativestyle partners-contribution Pull Request is created by Magento Partner labels Dec 13, 2019
Copy link
Contributor

@lbajsarowicz lbajsarowicz left a comment

Choose a reason for hiding this comment

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

How your change would behave if some extension code will provide null or other unexpected input?

*/
private function normalizeNewLineSymbols($value)
{
return str_replace(["\r\n", "\n\r", "\r"], ["\n", "\n", "\n"], $value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Second argument does not have to be an array: https://3v4l.org/lkhbV

echo str_replace(['a','b','c'], 'd', 'abcdef');

* @param string $value
* @return string
*/
private function normalizeNewLineSymbols($value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add type hints

/**
* Test for customizable product option with "Text" type
*/
class TextTest extends \PHPUnit\Framework\TestCase
Copy link
Contributor

Choose a reason for hiding this comment

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

Import class.

/**
* @var Text
*/
protected $model;
Copy link
Contributor

Choose a reason for hiding this comment

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

Good practice is to use class name / class abstract instead of generic model.

protected $model;

/**
* @var \Magento\Framework\ObjectManagerInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Import class.

protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to split that into multiple lines.

*/
protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
Copy link
Contributor

Choose a reason for hiding this comment

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

Import class.

{
return [
[
// $productOptionData
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding comments, just replace that with:

[self::STUB_OPTION_DATA, "string \r\n string", "string \n string"],

And extract ['id' => 11, 'type' => 'area'] to private const.

@ghost ghost assigned lbajsarowicz Dec 14, 2019
@Leone
Copy link
Contributor Author

Leone commented Dec 16, 2019

@lbajsarowicz thank you for feedback.

How your change would behave if some extension code will provide null or other unexpected input?

null or other unexpected input will be cast to string because there is $value = trim($this->getUserValue()); above my change in validateUserValue($values) method.

@Leone Leone requested a review from lbajsarowicz December 16, 2019 07:30

namespace Magento\Catalog\Model\Product\Option\Type;

use Magento\Catalog\Model\Product\Option;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can organize imports (CTRL + SHIFT + O in PHPStorm)

Copy link
Contributor

@lbajsarowicz lbajsarowicz left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution. You may organize imports.

@magento-engcom-team
Copy link
Contributor

Hi @lbajsarowicz, thank you for the review.
ENGCOM-6482 has been created to process this Pull Request
✳️ @lbajsarowicz, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@engcom-Charlie
Copy link
Contributor

✔️ QA Passed

@engcom-Foxtrot engcom-Foxtrot self-assigned this Dec 16, 2019
@engcom-Foxtrot engcom-Foxtrot added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Dec 16, 2019
@engcom-Foxtrot
Copy link
Contributor

@Leone, please fix the failed tests.

@sivaschenko
Copy link
Member

sivaschenko commented Dec 16, 2019

If eliminating Session dependency requires too much effort, the test can be skipped by adding
@SuppressWarnings(PHPMD.CookieAndSessionMisuse)
to the class phpdoc

@magento-engcom-team
Copy link
Contributor

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

@Leone
Copy link
Contributor Author

Leone commented Dec 18, 2019

@sivaschenko Indeed, eliminating Session dependency requires too much effort. I added
@SuppressWarnings(PHPMD.CookieAndSessionMisuse)
and tests passed. Thank you for this tip.

@engcom-Charlie
Copy link
Contributor

✔️ QA Passed

@m2-assistant
Copy link

m2-assistant bot commented Dec 29, 2019

Hi @Leone, 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.

@sdzhepa sdzhepa mentioned this pull request May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Component: Catalog Partner: creativestyle partners-contribution Pull Request is created by Magento Partner Progress: accept Release Line: 2.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants