Skip to content

Commit

Permalink
[issue-25974] Fix for "Amount of characters on a 'Area' Customizable …
Browse files Browse the repository at this point in the history
…Option counted differently on backend/frontend"
  • Loading branch information
Leone committed Dec 13, 2019
1 parent e8c2526 commit 284e3ed
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Option/Type/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function validateUserValue($values)

// Check maximal length limit
$maxCharacters = $option->getMaxCharacters();
$value = $this->normalizeNewLineSymbols($value);
if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) {
$this->setIsValid(false);
throw new LocalizedException(__('The text is too long. Shorten the text and try again.'));
Expand Down Expand Up @@ -101,4 +102,15 @@ public function getFormattedOptionValue($value)
{
return $this->_escaper->escapeHtml($value);
}

/**
* Normalize newline symbols
*
* @param string $value
* @return string
*/
private function normalizeNewLineSymbols($value)

This comment has been minimized.

Copy link
@drazulay

drazulay Jan 30, 2020

Shouldn't this method be placed inside of \Magento\Framework\Stdlib\StringUtils ? I'm sure this isn't the only place this method will come in handy..

{
return str_replace(["\r\n", "\n\r", "\r"], ["\n", "\n", "\n"], $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

use Magento\Catalog\Model\Product\Option;

/**
* Test for customizable product option with "Text" type
*/
class TextTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Text
*/
protected $model;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->model = $this->objectManager->create(
Text::class
);
}

/**
* Check if newline symbols are normalized in option value
*
* @dataProvider optionValueDataProvider
* @param array $productOptionData
* @param string $optionValue
* @param string $expectedOptionValue
*/
public function testNormalizeNewlineSymbols(
array $productOptionData,
string $optionValue,
string $expectedOptionValue
) {
$productOption = $this->objectManager->create(
Option::class,
['data' => $productOptionData]
);

$this->model->setOption($productOption);
$this->model->setUserValue($optionValue);
$this->model->validateUserValue([]);

$this->assertSame($expectedOptionValue, $this->model->getUserValue());
}

/**
* Data provider for testNormalizeNewlineSymbols
*
* @return array
*/
public function optionValueDataProvider()
{
return [
[
// $productOptionData
['id' => 11, 'type' => 'area'],
// $optionValue
'string string',
// $expectedOptionValue
'string string'
],
[
// $productOptionData
['id' => 11, 'type' => 'area'],
// $optionValue
"string \r\n string",
// $expectedOptionValue
"string \n string"
],
[
// $productOptionData
['id' => 11, 'type' => 'area'],
// $optionValue
"string \n\r string",
// $expectedOptionValue
"string \n string"
],
[
// $productOptionData
['id' => 11, 'type' => 'area'],
// $optionValue
"string \r string",
// $expectedOptionValue
"string \n string"
]
];
}
}

0 comments on commit 284e3ed

Please sign in to comment.