6
6
namespace Magento \Framework \View \Test \Unit \Element \Html ;
7
7
8
8
use \Magento \Framework \View \Element \Html \Select ;
9
+ use Magento \Framework \Escaper ;
9
10
10
11
class SelectTest extends \PHPUnit_Framework_TestCase
11
12
{
@@ -14,25 +15,27 @@ class SelectTest extends \PHPUnit_Framework_TestCase
14
15
*/
15
16
protected $ select ;
16
17
18
+ /**
19
+ * @var Escaper|\PHPUnit_Framework_MockObject_MockObject
20
+ */
21
+ protected $ escaper ;
22
+
17
23
protected function setUp ()
18
24
{
19
25
$ eventManager = $ this ->getMock (\Magento \Framework \Event \ManagerInterface::class);
20
26
21
27
$ scopeConfig = $ this ->getMock (\Magento \Framework \App \Config \ScopeConfigInterface::class);
22
28
23
- $ escaper = $ this ->getMockBuilder (\Magento \Framework \Escaper::class)
29
+ $ this -> escaper = $ this ->getMockBuilder (\Magento \Framework \Escaper::class)
24
30
->disableOriginalConstructor ()
25
31
->getMock ();
26
- $ escaper ->expects ($ this ->any ())
27
- ->method ('escapeHtml ' )
28
- ->will ($ this ->returnArgument (0 ));
29
32
30
33
$ context = $ this ->getMockBuilder (\Magento \Framework \View \Element \Context::class)
31
34
->disableOriginalConstructor ()
32
35
->getMock ();
33
36
$ context ->expects ($ this ->once ())
34
37
->method ('getEscaper ' )
35
- ->will ($ this ->returnValue ($ escaper ));
38
+ ->will ($ this ->returnValue ($ this -> escaper ));
36
39
$ context ->expects ($ this ->once ())
37
40
->method ('getEventManager ' )
38
41
->will ($ this ->returnValue ($ eventManager ));
@@ -92,8 +95,52 @@ public function testGetSetTitle()
92
95
$ this ->assertEquals ($ selectTitle , $ this ->select ->getTitle ());
93
96
}
94
97
98
+ public function testGetHtmlJs ()
99
+ {
100
+ $ this ->escaper ->expects ($ this ->any ())
101
+ ->method ('escapeHtml ' )
102
+ ->will ($ this ->returnArgument (0 ));
103
+ $ this ->escaper ->expects ($ this ->any ())
104
+ ->method ('escapeHtmlAttr ' )
105
+ ->will ($ this ->returnArgument (0 ));
106
+
107
+ $ selectId = 'testId ' ;
108
+ $ selectClass = 'testClass ' ;
109
+ $ selectTitle = 'testTitle ' ;
110
+ $ selectName = 'testName ' ;
111
+
112
+ $ options = [
113
+ 'testValue ' => 'testLabel ' ,
114
+ 'selectedValue ' => 'selectedLabel ' ,
115
+ ];
116
+ $ selectedValue = 'selectedValue ' ;
117
+
118
+ $ this ->select ->setId ($ selectId );
119
+ $ this ->select ->setClass ($ selectClass );
120
+ $ this ->select ->setTitle ($ selectTitle );
121
+ $ this ->select ->setName ($ selectName );
122
+ $ this ->select ->setOptions ($ options );
123
+ $ this ->select ->setValue ($ selectedValue );
124
+
125
+ $ result = '<select name="testName" id="testId" class="testClass" title="testTitle" > '
126
+ . '<option value="testValue" <%= option_extra_attrs.option_4016862802 %> >testLabel</option> '
127
+ . '<option value="selectedValue" selected="selected" <%= option_extra_attrs.option_662265145 %> > '
128
+ . 'selectedLabel</option> '
129
+ . '</select> ' ;
130
+
131
+ $ this ->select ->setIsRenderToJsTemplate (true );
132
+ $ this ->assertEquals ($ result , $ this ->select ->getHtml ());
133
+ }
134
+
95
135
public function testGetHtml ()
96
136
{
137
+ $ this ->escaper ->expects ($ this ->any ())
138
+ ->method ('escapeHtml ' )
139
+ ->will ($ this ->returnArgument (0 ));
140
+ $ this ->escaper ->expects ($ this ->any ())
141
+ ->method ('escapeHtmlAttr ' )
142
+ ->will ($ this ->returnArgument (0 ));
143
+
97
144
$ selectId = 'testId ' ;
98
145
$ selectClass = 'testClass ' ;
99
146
$ selectTitle = 'testTitle ' ;
@@ -137,33 +184,112 @@ public function testGetHtml()
137
184
$ this ->assertEquals ($ result , $ this ->select ->getHtml ());
138
185
}
139
186
140
- public function testGetHtmlJs ()
187
+ public function testGetHtmlEscapes ()
141
188
{
142
- $ selectId = 'testId ' ;
143
- $ selectClass = 'testClass ' ;
144
- $ selectTitle = 'testTitle ' ;
145
- $ selectName = 'testName ' ;
189
+ $ this ->escaper ->expects ($ this ->any ())
190
+ ->method ('escapeHtml ' )
191
+ ->will ($ this ->returnValue ('ESCAPED ' ));
192
+ $ this ->escaper ->expects ($ this ->any ())
193
+ ->method ('escapeHtmlAttr ' )
194
+ ->will ($ this ->returnValue ('ESCAPED_ATTR ' ));
146
195
147
- $ options = [
148
- ' testValue ' => ' testLabel ' ,
149
- ' selectedValue ' => ' selectedLabel ' ,
196
+ $ optionsSets = [
197
+ $ this -> getOptionsWithSingleQuotes () ,
198
+ $ this -> getOptionsWithDoubleQuotes ()
150
199
];
151
- $ selectedValue = 'selectedValue ' ;
152
-
153
- $ this ->select ->setId ($ selectId );
154
- $ this ->select ->setClass ($ selectClass );
155
- $ this ->select ->setTitle ($ selectTitle );
156
- $ this ->select ->setName ($ selectName );
157
- $ this ->select ->setOptions ($ options );
158
- $ this ->select ->setValue ($ selectedValue );
159
200
160
- $ result = '<select name="testName" id="testId" class="testClass" title="testTitle" > '
161
- . '<option value="testValue" <%= option_extra_attrs.option_4016862802 %> >testLabel</option> '
162
- . '<option value="selectedValue" selected="selected" <%= option_extra_attrs.option_662265145 %> > '
163
- . 'selectedLabel</option> '
201
+ $ expectedResult = '<select name="test[name]" id="testId" class="test class" title="ESCAPED" > '
202
+ . '<option value="ESCAPED" paramKey="ESCAPED" >ESCAPED</option> '
203
+ . '<option value="ESCAPED" selected="selected" >ESCAPED</option> '
204
+ . '<optgroup label="ESCAPED" data-optgroup-name="ESCAPED"> '
205
+ . '<option value="ESCAPED" >ESCAPED</option> '
206
+ . '<option value="ESCAPED" selected="selected" >ESCAPED</option> '
207
+ . '</optgroup> '
164
208
. '</select> ' ;
165
209
166
- $ this ->select ->setIsRenderToJsTemplate (true );
167
- $ this ->assertEquals ($ result , $ this ->select ->getHtml ());
210
+ foreach ($ optionsSets as $ inOptions ) {
211
+ $ this ->select ->setId ($ inOptions ['id ' ]);
212
+ $ this ->select ->setClass ($ inOptions ['class ' ]);
213
+ $ this ->select ->setTitle ($ inOptions ['title ' ]);
214
+ $ this ->select ->setName ($ inOptions ['name ' ]);
215
+
216
+ foreach ($ inOptions ['options ' ] as $ option ) {
217
+ $ this ->select ->addOption ($ option ['value ' ], $ option ['label ' ], $ option ['params ' ]);
218
+ }
219
+ $ this ->select ->setValue ($ inOptions ['values ' ]);
220
+
221
+ $ this ->assertEquals ($ expectedResult , $ this ->select ->getHtml ());
222
+
223
+ // reset
224
+ $ this ->select ->setOptions ([]);
225
+ }
226
+ }
227
+
228
+ /**
229
+ * @return array
230
+ */
231
+ private function getOptionsWithSingleQuotes ()
232
+ {
233
+ return [
234
+ 'id ' => "testId " ,
235
+ 'name ' => "test[name] " ,
236
+ 'class ' => "test class " ,
237
+ 'title ' => "test'Title " ,
238
+ 'options ' => [
239
+ 'regular ' => [
240
+ 'value ' => 'testValue ' ,
241
+ 'label ' => "test'Label " ,
242
+ 'params ' => ['paramKey ' => "param'Value " ]
243
+ ],
244
+ 'selected ' => [
245
+ 'value ' => 'selectedValue ' ,
246
+ 'label ' => "selected'Label " ,
247
+ 'params ' => []
248
+ ],
249
+ 'optgroup ' => [
250
+ 'value ' => [
251
+ 'groupElementValue ' => "GroupElement'Label " ,
252
+ 'selectedGroupElementValue ' => "SelectedGroupElement'Label "
253
+ ],
254
+ 'label ' => "group'Label " ,
255
+ 'params ' => []
256
+ ]
257
+ ],
258
+ 'values ' => ['selectedValue ' , 'selectedGroupElementValue ' ]
259
+ ];
260
+ }
261
+
262
+ /**
263
+ * @return array
264
+ */
265
+ private function getOptionsWithDoubleQuotes ()
266
+ {
267
+ return [
268
+ 'id ' => 'testId ' ,
269
+ 'name ' => 'test[name] ' ,
270
+ 'class ' => 'test class ' ,
271
+ 'title ' => 'test"Title ' ,
272
+ 'options ' => [
273
+ 'regular ' => [
274
+ 'value ' => 'testValue ' ,
275
+ 'label ' => 'test"Label ' ,
276
+ 'params ' => ['paramKey ' => 'param"Value ' ]
277
+ ],
278
+ 'selected ' => [
279
+ 'value ' => 'selectedValue ' ,
280
+ 'label ' => 'selected"Label ' ,
281
+ 'params ' => []
282
+ ],
283
+ 'optgroup ' => [
284
+ 'value ' => [
285
+ 'groupElementValue ' => 'GroupElement"Label ' ,
286
+ 'selectedGroupElementValue ' => 'SelectedGroupElement"Label '
287
+ ],
288
+ 'label ' => 'group"Label ' ,
289
+ 'params ' => []
290
+ ]
291
+ ],
292
+ 'values ' => ['selectedValue ' , 'selectedGroupElementValue ' ]
293
+ ];
168
294
}
169
295
}
0 commit comments