7
7
8
8
use Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface ;
9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \Exception \SerializationException ;
10
11
use Magento \Framework \Filesystem ;
11
12
use Magento \Framework \Filesystem \Directory \ReadInterface ;
12
13
use Magento \Framework \Filesystem \DriverPool ;
@@ -53,6 +54,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
53
54
*/
54
55
private $ escaper ;
55
56
57
+ /**
58
+ * @var \Magento\Quote\Model\Quote\Item\OptionFactory|\PHPUnit_Framework_MockObject_MockObject
59
+ */
60
+ private $ itemOptionFactoryMock ;
61
+
56
62
protected function setUp ()
57
63
{
58
64
$ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -71,6 +77,7 @@ protected function setUp()
71
77
72
78
$ this ->serializer = $ this ->getMockBuilder (\Magento \Framework \Serialize \Serializer \Json::class)
73
79
->disableOriginalConstructor ()
80
+ ->setMethods (['serialize ' , 'unserialize ' ])
74
81
->getMock ();
75
82
76
83
$ this ->urlBuilder = $ this ->getMockBuilder (\Magento \Catalog \Model \Product \Option \UrlBuilder::class)
@@ -81,13 +88,38 @@ protected function setUp()
81
88
->disableOriginalConstructor ()
82
89
->getMock ();
83
90
91
+ $ this ->itemOptionFactoryMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \OptionFactory::class)
92
+ ->setMethods (['create ' ])
93
+ ->disableOriginalConstructor ()
94
+ ->getMock ();
95
+
84
96
$ this ->coreFileStorageDatabase = $ this ->getMock (
85
97
\Magento \MediaStorage \Helper \File \Storage \Database::class,
86
98
['copyFile ' ],
87
99
[],
88
100
'' ,
89
101
false
90
102
);
103
+
104
+ $ this ->serializer ->expects ($ this ->any ())
105
+ ->method ('unserialize ' )
106
+ ->will (
107
+ $ this ->returnCallback (
108
+ function ($ value ) {
109
+ return json_decode ($ value , true );
110
+ }
111
+ )
112
+ );
113
+
114
+ $ this ->serializer ->expects ($ this ->any ())
115
+ ->method ('serialize ' )
116
+ ->will (
117
+ $ this ->returnCallback (
118
+ function ($ value ) {
119
+ return json_encode ($ value );
120
+ }
121
+ )
122
+ );
91
123
}
92
124
93
125
/**
@@ -102,7 +134,8 @@ protected function getFileObject()
102
134
'coreFileStorageDatabase ' => $ this ->coreFileStorageDatabase ,
103
135
'serializer ' => $ this ->serializer ,
104
136
'urlBuilder ' => $ this ->urlBuilder ,
105
- 'escaper ' => $ this ->escaper
137
+ 'escaper ' => $ this ->escaper ,
138
+ 'itemOptionFactory ' => $ this ->itemOptionFactoryMock ,
106
139
]
107
140
);
108
141
}
@@ -222,17 +255,134 @@ public function testGetFormattedOptionValue()
222
255
$ fileObject ->getFormattedOptionValue ($ optionValue );
223
256
}
224
257
225
- public function testPrepareOptionValueForRequest ()
258
+ public function testGetFormattedOptionValueInvalid ()
226
259
{
227
- $ optionValue = 'string ' ;
228
- $ resultValue = [ ' result ' ] ;
229
- $ fileObject = $ this -> getFileObject ();
260
+ $ optionValue = 'invalid json option value... ' ;
261
+ $ this -> assertEquals ( $ optionValue , $ this -> getFileObject ()-> getFormattedOptionValue ( $ optionValue )) ;
262
+ }
230
263
264
+ public function testGetEditableOptionValue ()
265
+ {
266
+ $ configurationItemOption = $ this ->getMockBuilder (
267
+ \Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface::class
268
+ )->disableOriginalConstructor ()
269
+ ->setMethods (['getId ' , 'getValue ' ])
270
+ ->getMock ();
271
+ $ configurationItemOption ->expects ($ this ->once ())
272
+ ->method ('getId ' )
273
+ ->will ($ this ->returnValue (2 ));
274
+ $ fileObject = $ this ->getFileObject ()->setData ('configuration_item_option ' , $ configurationItemOption );
275
+ $ optionTitle = 'Option Title ' ;
276
+ $ optionValue = json_encode (['title ' => $ optionTitle ]);
231
277
$ this ->serializer ->expects ($ this ->once ())
232
278
->method ('unserialize ' )
233
279
->with ($ optionValue )
234
- ->willReturn ($ resultValue );
280
+ ->willReturn (json_decode ($ optionValue , true ));
281
+ $ this ->escaper ->expects ($ this ->once ())
282
+ ->method ('escapeHtml ' )
283
+ ->with ($ optionTitle )
284
+ ->will ($ this ->returnValue ($ optionTitle ));
285
+
286
+ $ this ->assertEquals ('Option Title [2] ' , $ fileObject ->getEditableOptionValue ($ optionValue ));
287
+ }
288
+
289
+ public function testGetEditableOptionValueInvalid ()
290
+ {
291
+ $ fileObject = $ this ->getFileObject ();
292
+ $ optionValue = '#invalid jSoN*(&@#^$(*& ' ;
293
+ $ this ->escaper ->expects ($ this ->never ())
294
+ ->method ('escapeHtml ' );
295
+
296
+ $ this ->assertEquals ($ optionValue , $ fileObject ->getEditableOptionValue ($ optionValue ));
297
+ }
298
+
299
+ public function testParseOptionValue ()
300
+ {
301
+ $ optionTitle = 'Option Title ' ;
302
+ $ optionValue = json_encode (['title ' => $ optionTitle ]);
303
+
304
+ $ userInput = 'Option [2] ' ;
305
+ $ fileObject = $ this ->getFileObject ();
306
+
307
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
308
+ ->disableOriginalConstructor ()
309
+ ->setMethods (['load ' , 'getValue ' ])
310
+ ->getMock ();
311
+
312
+ $ itemMock ->expects ($ this ->any ())
313
+ ->method ('load ' )
314
+ ->will ($ this ->returnSelf ());
235
315
316
+ $ itemMock ->expects ($ this ->any ())
317
+ ->method ('getValue ' )
318
+ ->will ($ this ->returnValue ($ optionValue ));
319
+
320
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
321
+ ->method ('create ' )
322
+ ->will ($ this ->returnValue ($ itemMock ));
323
+
324
+ $ this ->assertEquals ($ optionValue , $ fileObject ->parseOptionValue ($ userInput , []));
325
+ }
326
+
327
+ public function testParseOptionValueNoId ()
328
+ {
329
+ $ optionValue = 'value ' ;
330
+
331
+ $ userInput = 'Option [xx] ' ;
332
+ $ fileObject = $ this ->getFileObject ();
333
+
334
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
335
+ ->disableOriginalConstructor ()
336
+ ->setMethods (['load ' , 'getValue ' ])
337
+ ->getMock ();
338
+
339
+ $ itemMock ->expects ($ this ->any ())
340
+ ->method ('load ' )
341
+ ->will ($ this ->returnSelf ());
342
+
343
+ $ itemMock ->expects ($ this ->any ())
344
+ ->method ('getValue ' )
345
+ ->will ($ this ->returnValue ($ optionValue ));
346
+
347
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
348
+ ->method ('create ' )
349
+ ->will ($ this ->returnValue ($ itemMock ));
350
+
351
+ $ this ->assertEquals (null , $ fileObject ->parseOptionValue ($ userInput , []));
352
+ }
353
+
354
+ public function testParseOptionValueInvalid ()
355
+ {
356
+ $ optionValue = 'Invalid json serialized value... ' ;
357
+
358
+ $ userInput = 'Option [2] ' ;
359
+ $ fileObject = $ this ->getFileObject ();
360
+
361
+ $ itemMock = $ this ->getMockBuilder (\Magento \Quote \Model \Quote \Item \Option::class)
362
+ ->disableOriginalConstructor ()
363
+ ->setMethods (['load ' , 'getValue ' ])
364
+ ->getMock ();
365
+
366
+ $ itemMock ->expects ($ this ->any ())
367
+ ->method ('load ' )
368
+ ->will ($ this ->returnSelf ());
369
+
370
+ $ itemMock ->expects ($ this ->any ())
371
+ ->method ('getValue ' )
372
+ ->will ($ this ->returnValue ($ optionValue ));
373
+
374
+ $ this ->itemOptionFactoryMock ->expects ($ this ->any ())
375
+ ->method ('create ' )
376
+ ->will ($ this ->returnValue ($ itemMock ));
377
+
378
+ $ this ->assertEquals (null , $ fileObject ->parseOptionValue ($ userInput , []));
379
+ }
380
+
381
+ public function testPrepareOptionValueForRequest ()
382
+ {
383
+ $ resultValue = ['result ' ];
384
+ $ optionValue = json_encode ($ resultValue );
385
+ $ fileObject = $ this ->getFileObject ();
236
386
$ this ->assertEquals ($ resultValue , $ fileObject ->prepareOptionValueForRequest ($ optionValue ));
237
387
}
238
388
}
0 commit comments