Skip to content

Commit bb948ef

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-72037' into 2.3-develop-pr6
2 parents 20c9d17 + 3158d03 commit bb948ef

File tree

3 files changed

+63
-39
lines changed

3 files changed

+63
-39
lines changed

app/code/Magento/Variable/Model/Variable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getVariablesOptionArray($withGroup = false)
153153
foreach ($collection->toOptionArray() as $variable) {
154154
$variables[] = [
155155
'value' => '{{customVar code=' . $variable['value'] . '}}',
156-
'label' => __('%1', $variable['label']),
156+
'label' => __('%1', $this->_escaper->escapeHtml($variable['label'])),
157157
];
158158
}
159159
if ($withGroup && $variables) {

app/code/Magento/Variable/Test/Unit/Model/VariableTest.php

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,38 @@
66
namespace Magento\Variable\Test\Unit\Model;
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Variable\Model\ResourceModel\Variable\Collection;
910

1011
class VariableTest extends \PHPUnit\Framework\TestCase
1112
{
12-
/** @var \Magento\Variable\Model\Variable */
13+
/**
14+
* @var \Magento\Variable\Model\Variable
15+
*/
1316
private $model;
1417

15-
/** @var \PHPUnit_Framework_MockObject_MockObject */
18+
/**
19+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
20+
*/
1621
private $escaperMock;
1722

18-
/** @var \PHPUnit_Framework_MockObject_MockObject */
23+
/**
24+
* @var \Magento\Variable\Model\ResourceModel\Variable|\PHPUnit_Framework_MockObject_MockObject
25+
*/
1926
private $resourceMock;
2027

21-
/** @var \Magento\Framework\Phrase */
28+
/**
29+
* @var \Magento\Variable\Model\ResourceModel\Variable\Collection|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $resourceCollectionMock;
32+
33+
/**
34+
* @var \Magento\Framework\Phrase
35+
*/
2236
private $validationFailedPhrase;
2337

24-
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
38+
/**
39+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
40+
*/
2541
private $objectManager;
2642

2743
protected function setUp()
@@ -33,11 +49,15 @@ protected function setUp()
3349
$this->resourceMock = $this->getMockBuilder(\Magento\Variable\Model\ResourceModel\Variable::class)
3450
->disableOriginalConstructor()
3551
->getMock();
52+
$this->resourceCollectionMock = $this->getMockBuilder(Collection::class)
53+
->disableOriginalConstructor()
54+
->getMock();
3655
$this->model = $this->objectManager->getObject(
3756
\Magento\Variable\Model\Variable::class,
3857
[
3958
'escaper' => $this->escaperMock,
40-
'resource' => $this->resourceMock
59+
'resource' => $this->resourceMock,
60+
'resourceCollection' => $this->resourceCollectionMock,
4161
]
4262
);
4363
$this->validationFailedPhrase = __('Validation has failed.');
@@ -101,58 +121,46 @@ public function testValidate($variableArray, $objectId, $expectedResult)
101121
public function testGetVariablesOptionArrayNoGroup()
102122
{
103123
$origOptions = [
104-
['value' => 'VAL', 'label' => 'LBL']
124+
['value' => 'VAL', 'label' => 'LBL'],
105125
];
106126

107127
$transformedOptions = [
108-
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')]
128+
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
109129
];
110130

111-
$collectionMock = $this->getMockBuilder(\Magento\Variable\Model\ResourceModel\Variable\Collection::class)
112-
->disableOriginalConstructor()
113-
->getMock();
114-
$collectionMock->expects($this->any())
131+
$this->resourceCollectionMock->expects($this->any())
115132
->method('toOptionArray')
116133
->willReturn($origOptions);
117-
$mockVariable = $this->getMockBuilder(\Magento\Variable\Model\Variable::class)
118-
->setMethods(['getCollection'])
119-
->setConstructorArgs($this->objectManager->getConstructArguments(\Magento\Variable\Model\Variable::class))
120-
->getMock();
121-
$mockVariable->expects($this->any())
122-
->method('getCollection')
123-
->willReturn($collectionMock);
124-
$this->assertEquals($transformedOptions, $mockVariable->getVariablesOptionArray());
134+
$this->escaperMock->expects($this->once())
135+
->method('escapeHtml')
136+
->with($origOptions[0]['label'])
137+
->willReturn($origOptions[0]['label']);
138+
$this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray());
125139
}
126140

127141
public function testGetVariablesOptionArrayWithGroup()
128142
{
129143
$origOptions = [
130-
['value' => 'VAL', 'label' => 'LBL']
144+
['value' => 'VAL', 'label' => 'LBL'],
131145
];
132146

133147
$transformedOptions = [
134148
[
135149
'label' => __('Custom Variables'),
136150
'value' => [
137-
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')]
138-
]
139-
]
151+
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
152+
],
153+
],
140154
];
141155

142-
$collectionMock = $this->getMockBuilder(\Magento\Variable\Model\ResourceModel\Variable\Collection::class)
143-
->disableOriginalConstructor()
144-
->getMock();
145-
$collectionMock->expects($this->any())
156+
$this->resourceCollectionMock->expects($this->any())
146157
->method('toOptionArray')
147158
->willReturn($origOptions);
148-
$mockVariable = $this->getMockBuilder(\Magento\Variable\Model\Variable::class)
149-
->setMethods(['getCollection'])
150-
->setConstructorArgs($this->objectManager->getConstructArguments(\Magento\Variable\Model\Variable::class))
151-
->getMock();
152-
$mockVariable->expects($this->any())
153-
->method('getCollection')
154-
->willReturn($collectionMock);
155-
$this->assertEquals($transformedOptions, $mockVariable->getVariablesOptionArray(true));
159+
$this->escaperMock->expects($this->atLeastOnce())
160+
->method('escapeHtml')
161+
->with($origOptions[0]['label'])
162+
->willReturn($origOptions[0]['label']);
163+
$this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray(true));
156164
}
157165

158166
public function validateDataProvider()
@@ -163,15 +171,15 @@ public function validateDataProvider()
163171
return [
164172
'Empty Variable' => [[], null, true],
165173
'IDs match' => [$variable, 'matching_id', true],
166-
'IDs do not match' => [$variable, 'non_matching_id', __('Variable Code must be unique.')]
174+
'IDs do not match' => [$variable, 'non_matching_id', __('Variable Code must be unique.')],
167175
];
168176
}
169177

170178
public function validateMissingInfoDataProvider()
171179
{
172180
return [
173181
'Missing code' => ['', 'some-name'],
174-
'Missing name' => ['some-code', '']
182+
'Missing name' => ['some-code', ''],
175183
];
176184
}
177185
}

dev/tests/integration/testsuite/Magento/Variable/Model/VariableTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ public function testCollection()
7777
$collection->addValuesToResult();
7878
$this->assertContains('variable_value', (string)$collection->getSelect());
7979
}
80+
81+
/**
82+
* Test to verify that returned by getVariablesOptionArray()
83+
* custom variable label is HTML escaped.
84+
*/
85+
public function testGetVariablesOptionArrayWithHtmlLabel()
86+
{
87+
$expectedLabel = '<b>HTML Name value</b>';
88+
$data = [
89+
'code' => 'html_name',
90+
'name' => '<b>HTML Name value</b>'
91+
];
92+
$this->_model->setData($data)->save();
93+
$actualLabel = current(current($this->_model->getVariablesOptionArray())['label']->getArguments());
94+
$this->assertEquals($expectedLabel, $actualLabel);
95+
}
8096
}

0 commit comments

Comments
 (0)