Skip to content

Commit 284e3ed

Browse files
committed
[issue-25974] Fix for "Amount of characters on a 'Area' Customizable Option counted differently on backend/frontend"
1 parent e8c2526 commit 284e3ed

File tree

2 files changed

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

2 files changed

+116
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function validateUserValue($values)
6868

6969
// Check maximal length limit
7070
$maxCharacters = $option->getMaxCharacters();
71+
$value = $this->normalizeNewLineSymbols($value);
7172
if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) {
7273
$this->setIsValid(false);
7374
throw new LocalizedException(__('The text is too long. Shorten the text and try again.'));
@@ -101,4 +102,15 @@ public function getFormattedOptionValue($value)
101102
{
102103
return $this->_escaper->escapeHtml($value);
103104
}
105+
106+
/**
107+
* Normalize newline symbols
108+
*
109+
* @param string $value
110+
* @return string
111+
*/
112+
private function normalizeNewLineSymbols($value)
113+
{
114+
return str_replace(["\r\n", "\n\r", "\r"], ["\n", "\n", "\n"], $value);
115+
}
104116
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
11+
/**
12+
* Test for customizable product option with "Text" type
13+
*/
14+
class TextTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @var Text
18+
*/
19+
protected $model;
20+
21+
/**
22+
* @var \Magento\Framework\ObjectManagerInterface
23+
*/
24+
private $objectManager;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
protected function setUp()
30+
{
31+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
32+
$this->model = $this->objectManager->create(
33+
Text::class
34+
);
35+
}
36+
37+
/**
38+
* Check if newline symbols are normalized in option value
39+
*
40+
* @dataProvider optionValueDataProvider
41+
* @param array $productOptionData
42+
* @param string $optionValue
43+
* @param string $expectedOptionValue
44+
*/
45+
public function testNormalizeNewlineSymbols(
46+
array $productOptionData,
47+
string $optionValue,
48+
string $expectedOptionValue
49+
) {
50+
$productOption = $this->objectManager->create(
51+
Option::class,
52+
['data' => $productOptionData]
53+
);
54+
55+
$this->model->setOption($productOption);
56+
$this->model->setUserValue($optionValue);
57+
$this->model->validateUserValue([]);
58+
59+
$this->assertSame($expectedOptionValue, $this->model->getUserValue());
60+
}
61+
62+
/**
63+
* Data provider for testNormalizeNewlineSymbols
64+
*
65+
* @return array
66+
*/
67+
public function optionValueDataProvider()
68+
{
69+
return [
70+
[
71+
// $productOptionData
72+
['id' => 11, 'type' => 'area'],
73+
// $optionValue
74+
'string string',
75+
// $expectedOptionValue
76+
'string string'
77+
],
78+
[
79+
// $productOptionData
80+
['id' => 11, 'type' => 'area'],
81+
// $optionValue
82+
"string \r\n string",
83+
// $expectedOptionValue
84+
"string \n string"
85+
],
86+
[
87+
// $productOptionData
88+
['id' => 11, 'type' => 'area'],
89+
// $optionValue
90+
"string \n\r string",
91+
// $expectedOptionValue
92+
"string \n string"
93+
],
94+
[
95+
// $productOptionData
96+
['id' => 11, 'type' => 'area'],
97+
// $optionValue
98+
"string \r string",
99+
// $expectedOptionValue
100+
"string \n string"
101+
]
102+
];
103+
}
104+
}

0 commit comments

Comments
 (0)