Skip to content

Commit a397d1c

Browse files
ENGCOM-6482: Normalize new line symbols in Product Text Option (type=area) #26033
2 parents 02ba37b + 015deb1 commit a397d1c

File tree

2 files changed

+93
-0
lines changed
  • app/code/Magento/Catalog/Model/Product/Option/Type
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Option/Type

2 files changed

+93
-0
lines changed

app/code/Magento/Catalog/Model/Product/Option/Type/Text.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Catalog product option text type
13+
*
14+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1315
*/
1416
class Text extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
1517
{
@@ -68,6 +70,7 @@ public function validateUserValue($values)
6870

6971
// Check maximal length limit
7072
$maxCharacters = $option->getMaxCharacters();
73+
$value = $this->normalizeNewLineSymbols($value);
7174
if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) {
7275
$this->setIsValid(false);
7376
throw new LocalizedException(__('The text is too long. Shorten the text and try again.'));
@@ -101,4 +104,15 @@ public function getFormattedOptionValue($value)
101104
{
102105
return $this->_escaper->escapeHtml($value);
103106
}
107+
108+
/**
109+
* Normalize newline symbols
110+
*
111+
* @param string $value
112+
* @return string
113+
*/
114+
private function normalizeNewLineSymbols(string $value)
115+
{
116+
return str_replace(["\r\n", "\n\r", "\r"], "\n", $value);
117+
}
104118
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\Product\Option\Type;
8+
9+
use Magento\Catalog\Model\Product\Option;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Test for customizable product option with "Text" type
16+
*/
17+
class TextTest extends TestCase
18+
{
19+
const STUB_OPTION_DATA = ['id' => 11, 'type' => 'area'];
20+
21+
/**
22+
* @var Text
23+
*/
24+
protected $optionText;
25+
26+
/**
27+
* @var ObjectManagerInterface
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
protected function setUp()
35+
{
36+
$this->objectManager = Bootstrap::getObjectManager();
37+
$this->optionText = $this->objectManager->create(Text::class);
38+
}
39+
40+
/**
41+
* Check if newline symbols are normalized in option value
42+
*
43+
* @dataProvider optionValueDataProvider
44+
* @param array $productOptionData
45+
* @param string $optionValue
46+
* @param string $expectedOptionValue
47+
*/
48+
public function testNormalizeNewlineSymbols(
49+
array $productOptionData,
50+
string $optionValue,
51+
string $expectedOptionValue
52+
) {
53+
$productOption = $this->objectManager->create(
54+
Option::class,
55+
['data' => $productOptionData]
56+
);
57+
58+
$this->optionText->setOption($productOption);
59+
$this->optionText->setUserValue($optionValue);
60+
$this->optionText->validateUserValue([]);
61+
62+
$this->assertSame($expectedOptionValue, $this->optionText->getUserValue());
63+
}
64+
65+
/**
66+
* Data provider for testNormalizeNewlineSymbols
67+
*
68+
* @return array
69+
*/
70+
public function optionValueDataProvider()
71+
{
72+
return [
73+
[self::STUB_OPTION_DATA, 'string string', 'string string'],
74+
[self::STUB_OPTION_DATA, "string \r\n string", "string \n string"],
75+
[self::STUB_OPTION_DATA, "string \n\r string", "string \n string"],
76+
[self::STUB_OPTION_DATA, "string \r string", "string \n string"]
77+
];
78+
}
79+
}

0 commit comments

Comments
 (0)