Skip to content

Commit 3c3d3f2

Browse files
Joan HeIgor Melnikov
authored andcommitted
MAGETWO-67164: Syntax Errors in Widget encode/decode
1 parent e0bd129 commit 3c3d3f2

File tree

8 files changed

+111
-26
lines changed

8 files changed

+111
-26
lines changed

app/code/Magento/Cms/Setup/ContentConverter.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function convert($value)
7070
return $convertedValue;
7171
}
7272

73+
/**
74+
* @inheritdoc
75+
*/
76+
protected function isValidJsonValue($value)
77+
{
78+
return parent::isValidJsonValue($this->normalizer->restoreReservedCharacters($value));
79+
}
80+
7381
/**
7482
* Convert widget parameters from serialized format to JSON format
7583
*
@@ -83,11 +91,11 @@ private function convertWidgetParams($paramsString)
8391
$tokenizer->setString($paramsString);
8492
$widgetParameters = $tokenizer->tokenize();
8593
if (isset($widgetParameters['conditions_encoded'])) {
86-
$conditions = $this->normalizer->restoreReservedCharaters($widgetParameters['conditions_encoded']);
87-
if ($this->isValidJsonValue($conditions)) {
94+
if ($this->isValidJsonValue($widgetParameters['conditions_encoded'])) {
8895
return $paramsString;
8996
}
90-
$widgetParameters['conditions_encoded'] = $this->normalizer->replaceReservedCharaters(
97+
$conditions = $this->restoreReservedCharactersInSerializedContent($widgetParameters['conditions_encoded']);
98+
$widgetParameters['conditions_encoded'] = $this->normalizer->replaceReservedCharacters(
9199
parent::convert($conditions)
92100
);
93101
$newParamsString = '';
@@ -99,4 +107,25 @@ private function convertWidgetParams($paramsString)
99107
return $paramsString;
100108
}
101109
}
110+
111+
/**
112+
* Restore the reserved characters in the existing serialized content
113+
*
114+
* @param string $serializedContent
115+
* @return string
116+
*/
117+
private function restoreReservedCharactersInSerializedContent($serializedContent)
118+
{
119+
$map = [
120+
'{' => '[',
121+
'}' => ']',
122+
'"' => '`',
123+
'\\' => '|',
124+
];
125+
return str_replace(
126+
array_values($map),
127+
array_keys($map),
128+
$serializedContent
129+
);
130+
}
102131
}

app/code/Magento/Widget/Helper/Conditions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
*/
4545
public function encode(array $value)
4646
{
47-
return $this->normalizer->replaceReservedCharaters($this->serializer->serialize($value));
47+
return $this->normalizer->replaceReservedCharacters($this->serializer->serialize($value));
4848
}
4949

5050
/**
@@ -56,7 +56,7 @@ public function encode(array $value)
5656
public function decode($value)
5757
{
5858
return $this->serializer->unserialize(
59-
$this->normalizer->restoreReservedCharaters($value)
59+
$this->normalizer->restoreReservedCharacters($value)
6060
);
6161
}
6262
}

app/code/Magento/Widget/Model/Widget/Wysiwyg/Normalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Normalizer
1212
{
1313

1414
const WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP = [
15-
'{' => '[',
16-
'}' => ']',
15+
'{' => '^[',
16+
'}' => '^]',
1717
'"' => '`',
1818
'\\' => '|',
1919
];
@@ -24,7 +24,7 @@ class Normalizer
2424
* @param string $content
2525
* @return string
2626
*/
27-
public function replaceReservedCharaters($content)
27+
public function replaceReservedCharacters($content)
2828
{
2929
return str_replace(
3030
array_keys(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
@@ -39,7 +39,7 @@ public function replaceReservedCharaters($content)
3939
* @param string $content
4040
* @return string
4141
*/
42-
public function restoreReservedCharaters($content)
42+
public function restoreReservedCharacters($content)
4343
{
4444
return str_replace(
4545
array_values(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),

app/code/Magento/Widget/Setup/LayoutUpdateConverter.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,57 @@ public function convert($value)
5454
);
5555
if (isset($matches[0])) {
5656
$matchSegments = $matches[0];
57-
$matchSegments[2] = $this->normalizer->replaceReservedCharaters(
58-
parent::convert($this->normalizer->restoreReservedCharaters($matchSegments[2]))
59-
);
57+
$matchSegments[2] = parent::convert($matchSegments[2]);
6058
return $matchSegments[1] . $matchSegments[2] . $matchSegments[3];
6159
} else {
6260
return $value;
6361
}
6462
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
protected function isValidJsonValue($value)
68+
{
69+
$value = $this->normalizer->restoreReservedCharacters($value);
70+
return parent::isValidJsonValue($value);
71+
}
72+
73+
/**
74+
* @inheritdoc
75+
*/
76+
protected function unserializeValue($value)
77+
{
78+
$value = $this->restoreReservedCharactersInSerializedContent($value);
79+
return parent::unserializeValue($value);
80+
}
81+
82+
/**
83+
* @inheritdoc
84+
*/
85+
protected function encodeJson($value)
86+
{
87+
return $this->normalizer->replaceReservedCharacters(parent::encodeJson($value));
88+
}
89+
90+
/**
91+
* Restore the reserved characters in the existing serialized content
92+
*
93+
* @param string $serializedContent
94+
* @return string
95+
*/
96+
private function restoreReservedCharactersInSerializedContent($serializedContent)
97+
{
98+
$map = [
99+
'{' => '[',
100+
'}' => ']',
101+
'"' => '`',
102+
'\\' => '|',
103+
];
104+
return str_replace(
105+
array_values($map),
106+
array_keys($map),
107+
$serializedContent
108+
);
109+
}
65110
}

app/code/Magento/Widget/Test/Unit/Helper/ConditionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public function testEncodeDecode()
5959
->with($serializedValue)
6060
->willReturn($value);
6161
$this->normalizer->expects($this->once())
62-
->method('replaceReservedCharaters')
62+
->method('replaceReservedCharacters')
6363
->with($serializedValue)
6464
->willReturn($normalizedValue);
6565
$this->normalizer->expects($this->once())
66-
->method('restoreReservedCharaters')
66+
->method('restoreReservedCharacters')
6767
->with($normalizedValue)
6868
->willReturn($serializedValue);
6969
$encoded = $this->conditions->encode($value);

dev/tests/integration/testsuite/Magento/Cms/Setup/ContentConverterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function convertDataProvider()
3434
<h2 class="title">Hot Sellers</h2>
3535
<p class="info">Here is what`s trending on Luma right now</p>
3636
</div>';
37-
$serializedWidgetContent = '{{widget type="Magento\\CatalogWidget\\Block\\Product\\ProductsList" products_per_page="8" products_count="8" template="product/widget/content/grid.phtml" conditions_encoded="a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:3:`sku`;s:8:`operator`;s:2:`()`;s:5:`value`;s:60:`WS12, WT09, MT07, MH07, 24-MB02, 24-WB04, 241-MB08, 240-LV05`;]]"}}';
38-
$jsonEncodedWidgetContent = '{{widget type="Magento\\CatalogWidget\\Block\\Product\\ProductsList" products_per_page="8" products_count="8" template="product/widget/content/grid.phtml" conditions_encoded="[`1`:[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``],`1--1`:[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`sku`,`operator`:`()`,`value`:`WS12, WT09, MT07, MH07, 24-MB02, 24-WB04, 241-MB08, 240-LV05`]]"}}';
37+
$serializedWidgetContent = '{{widget type="Magento\\CatalogWidget\\Block\\Product\\ProductsList" products_per_page="8" products_count="8" template="product/widget/content/grid.phtml" conditions_encoded="a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:3:`sku`;s:8:`operator`;s:2:`()`;s:5:`value`;a:8:[i:0;s:4:`WS12`;i:1;s:4:`WT09`;i:2;s:4:`MT07`;i:3;s:4:`MH07`;i:4;s:7:`24-MB02`;i:5;s:7:`24-WB04`;i:6;s:8:`241-MB08`;i:7;s:8:`240-LV05`;]]]"}}';
38+
$jsonEncodedWidgetContent = '{{widget type="Magento\\CatalogWidget\\Block\\Product\\ProductsList" products_per_page="8" products_count="8" template="product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`sku`,`operator`:`()`,`value`:[`WS12`,`WT09`,`MT07`,`MH07`,`24-MB02`,`24-WB04`,`241-MB08`,`240-LV05`]^]^]"}}';
3939
// @codingStandardsIgnoreEnd
4040
return [
4141
'no widget' => [

dev/tests/integration/testsuite/Magento/Widget/Model/Widget/Wysiwyg/NormalizerTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@ protected function setUp()
1919
);
2020
}
2121

22-
public function testReplaceReservedCharaters()
22+
public function testReplaceReservedCharacters()
2323
{
24-
$content = '{}\\""';
25-
$expected = '[]|``';
26-
$this->assertEquals($expected, $this->normalizer->replaceReservedCharaters($content));
24+
$content = '{}\\""[]';
25+
$expected = '^[^]|``[]';
26+
$this->assertEquals($expected, $this->normalizer->replaceReservedCharacters($content));
2727
}
2828

29-
public function testRestoreReservedCharaters()
29+
public function testRestoreReservedCharacters()
3030
{
31-
$content = '[]|``';
32-
$expected = '{}\\""';
33-
$this->assertEquals($expected, $this->normalizer->restoreReservedCharaters($content));
31+
$content = '^[^]|``[]';
32+
$expected = '{}\\""[]';
33+
$this->assertEquals($expected, $this->normalizer->restoreReservedCharacters($content));
34+
}
35+
36+
public function testReplaceAndRestoreReservedCharacters()
37+
{
38+
$value = '{"1":{"type":"Magento\\CatalogWidget\\Model\\Rule\\Condition\\Combine","aggregator":"all","value":"1","new_child":""},"1--1":{"type":"Magento\\CatalogWidget\\Model\\Rule\\Condition\\Product","attribute":"pattern","operator":"{}","value":["212,213"]}}';
39+
$this->assertEquals(
40+
$value,
41+
$this->normalizer->restoreReservedCharacters(
42+
$this->normalizer->replaceReservedCharacters($value)
43+
)
44+
);
3445
}
3546
}

dev/tests/integration/testsuite/Magento/Widget/Setup/LayoutUpdateConverterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function convertDataProvider()
3131
{
3232
// @codingStandardsIgnoreStart
3333
$beginning = '<body><referenceContainer name="content"><block class="Magento\CatalogWidget\Block\Product\ProductsList" name="23e38bbfa7cc6474454570e51aeffcc3" template="product/widget/content/grid.phtml"><action method="setData"><argument name="name" xsi:type="string">show_pager</argument><argument name="value" xsi:type="string">0</argument></action><action method="setData"><argument name="name" xsi:type="string">products_count</argument><argument name="value" xsi:type="string">10</argument></action><action method="setData">';
34-
$serializedWidgetXml = '<argument name="name" xsi:type="string">conditions_encoded</argument><argument name="value" xsi:type="string">a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:3:`sku`;s:8:`operator`;s:2:`==`;s:5:`value`;s:10:`sku321-341`;]]</argument>';
35-
$jsonEncodedWidgetXml = '<argument name="name" xsi:type="string">conditions_encoded</argument><argument name="value" xsi:type="string">[`1`:[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``],`1--1`:[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`sku`,`operator`:`==`,`value`:`sku321-341`]]</argument>';
34+
$serializedWidgetXml = '<argument name="name" xsi:type="string">conditions_encoded</argument><argument name="value" xsi:type="string">a:2:[i:1;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Combine`;s:10:`aggregator`;s:3:`all`;s:5:`value`;s:1:`1`;s:9:`new_child`;s:0:``;]s:4:`1--1`;a:4:[s:4:`type`;s:50:`Magento|CatalogWidget|Model|Rule|Condition|Product`;s:9:`attribute`;s:3:`sku`;s:8:`operator`;s:2:`==`;s:5:`value`;a:8:[i:0;s:4:`WS12`;i:1;s:4:`WT09`;i:2;s:4:`MT07`;i:3;s:4:`MH07`;i:4;s:7:`24-MB02`;i:5;s:7:`24-WB04`;i:6;s:8:`241-MB08`;i:7;s:8:`240-LV05`;]]]</argument>';
35+
$jsonEncodedWidgetXml = '<argument name="name" xsi:type="string">conditions_encoded</argument><argument name="value" xsi:type="string">^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`sku`,`operator`:`==`,`value`:[`WS12`,`WT09`,`MT07`,`MH07`,`24-MB02`,`24-WB04`,`241-MB08`,`240-LV05`]^]^]</argument>';
3636
$ending = '</action><action method="setData"><argument name="name" xsi:type="string">page_var_name</argument><argument name="value" xsi:type="string">pobqks</argument></action></block></referenceContainer></body>';
3737
// @codingStandardsIgnoreEnd
3838
return [

0 commit comments

Comments
 (0)