Skip to content

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

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

Merged
merged 4 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 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 @@ -10,6 +10,8 @@

/**
* Catalog product option text type
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Text extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
{
Expand Down Expand Up @@ -68,6 +70,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 +104,15 @@ public function getFormattedOptionValue($value)
{
return $this->_escaper->escapeHtml($value);
}

/**
* Normalize newline symbols
*
* @param string $value
* @return string
*/
private function normalizeNewLineSymbols(string $value)
{
return str_replace(["\r\n", "\n\r", "\r"], "\n", $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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;
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)

use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for customizable product option with "Text" type
*/
class TextTest extends TestCase
{
const STUB_OPTION_DATA = ['id' => 11, 'type' => 'area'];

/**
* @var Text
*/
protected $optionText;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->optionText = $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->optionText->setOption($productOption);
$this->optionText->setUserValue($optionValue);
$this->optionText->validateUserValue([]);

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

/**
* Data provider for testNormalizeNewlineSymbols
*
* @return array
*/
public function optionValueDataProvider()
{
return [
[self::STUB_OPTION_DATA, 'string string', 'string string'],
[self::STUB_OPTION_DATA, "string \r\n string", "string \n string"],
[self::STUB_OPTION_DATA, "string \n\r string", "string \n string"],
[self::STUB_OPTION_DATA, "string \r string", "string \n string"]
];
}
}