Skip to content

Commit 14a7222

Browse files
committed
MAGETWO-64465: Investigate and fix post-refactoring issues
1 parent ab23fb9 commit 14a7222

File tree

3 files changed

+171
-7
lines changed

3 files changed

+171
-7
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Model\Product\Option\Type;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\SerializationException;
910
use Magento\Framework\Filesystem;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Catalog\Model\Product\Exception as ProductException;
@@ -316,7 +317,7 @@ public function getFormattedOptionValue($optionValue)
316317
{
317318
if ($this->_formattedOptionValue === null) {
318319
try {
319-
$value = $this->serializer->unserialize($optionValue);
320+
$value = $this->unserializeJsonValue($optionValue);
320321

321322
$customOptionUrlParams = $this->getCustomOptionUrlParams() ? $this->getCustomOptionUrlParams() : [
322323
'id' => $this->getConfigurationItemOption()->getId(),
@@ -402,7 +403,7 @@ public function getPrintableOptionValue($optionValue)
402403
public function getEditableOptionValue($optionValue)
403404
{
404405
try {
405-
$value = $this->serializer->unserialize($optionValue);
406+
$value = $this->unserializeJsonValue($optionValue);
406407
return sprintf(
407408
'%s [%d]',
408409
$this->_escaper->escapeHtml($value['title']),
@@ -431,7 +432,7 @@ public function parseOptionValue($optionValue, $productOptionValues)
431432
$confItemOptionId = $matches[1];
432433
$option = $this->_itemOptionFactory->create()->load($confItemOptionId);
433434
try {
434-
$this->serializer->unserialize($option->getValue());
435+
$this->unserializeJsonValue($option->getValue());
435436
return $option->getValue();
436437
} catch (\Exception $e) {
437438
return null;
@@ -441,6 +442,23 @@ public function parseOptionValue($optionValue, $productOptionValues)
441442
}
442443
}
443444

445+
/**
446+
* Validate Json serialized value
447+
*
448+
* @param string $jsonValue - JSON serialized value
449+
* @return mixed
450+
* @throws SerializationException
451+
*/
452+
private function unserializeJsonValue($jsonValue)
453+
{
454+
$value = $this->serializer->unserialize($jsonValue);
455+
if (json_last_error() === JSON_ERROR_NONE) {
456+
return $value;
457+
} else {
458+
throw new SerializationException(__('Invalid JSON value.'));
459+
}
460+
}
461+
444462
/**
445463
* Prepare option value for info buy request
446464
*
@@ -450,8 +468,7 @@ public function parseOptionValue($optionValue, $productOptionValues)
450468
public function prepareOptionValueForRequest($optionValue)
451469
{
452470
try {
453-
$result = $this->serializer->unserialize($optionValue);
454-
return $result;
471+
return $this->unserializeJsonValue($optionValue);
455472
} catch (\Exception $e) {
456473
return null;
457474
}

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Type/FileTest.php

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface;
99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Exception\SerializationException;
1011
use Magento\Framework\Filesystem;
1112
use Magento\Framework\Filesystem\Directory\ReadInterface;
1213
use Magento\Framework\Filesystem\DriverPool;
@@ -53,6 +54,11 @@ class FileTest extends \PHPUnit_Framework_TestCase
5354
*/
5455
private $escaper;
5556

57+
/**
58+
* @var \Magento\Quote\Model\Quote\Item\OptionFactory|\PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
private $itemOptionFactoryMock;
61+
5662
protected function setUp()
5763
{
5864
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -81,6 +87,11 @@ protected function setUp()
8187
->disableOriginalConstructor()
8288
->getMock();
8389

90+
$this->itemOptionFactoryMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\OptionFactory::class)
91+
->setMethods(['create'])
92+
->disableOriginalConstructor()
93+
->getMock();
94+
8495
$this->coreFileStorageDatabase = $this->getMock(
8596
\Magento\MediaStorage\Helper\File\Storage\Database::class,
8697
['copyFile'],
@@ -102,7 +113,8 @@ protected function getFileObject()
102113
'coreFileStorageDatabase' => $this->coreFileStorageDatabase,
103114
'serializer' => $this->serializer,
104115
'urlBuilder' => $this->urlBuilder,
105-
'escaper' => $this->escaper
116+
'escaper' => $this->escaper,
117+
'itemOptionFactory' => $this->itemOptionFactoryMock,
106118
]
107119
);
108120
}
@@ -222,6 +234,141 @@ public function testGetFormattedOptionValue()
222234
$fileObject->getFormattedOptionValue($optionValue);
223235
}
224236

237+
public function testGetFormattedOptionValueInvalid()
238+
{
239+
$optionValue = 'invalid json option value...';
240+
$this->serializer->expects($this->once())
241+
->method('unserialize')
242+
->willThrowException(new SerializationException(__('Invalid JSON value.')));
243+
$this->assertEquals($optionValue, $this->getFileObject()->getFormattedOptionValue($optionValue));
244+
}
245+
246+
public function testGetEditableOptionValue()
247+
{
248+
$configurationItemOption = $this->getMockBuilder(\Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface::class)
249+
->disableOriginalConstructor()
250+
->setMethods(['getId', 'getValue'])
251+
->getMock();
252+
$configurationItemOption->expects($this->once())
253+
->method('getId')
254+
->will($this->returnValue(2));
255+
$fileObject = $this->getFileObject()->setData('configuration_item_option', $configurationItemOption);
256+
$optionTitle = 'Option Title';
257+
$optionValue = json_encode(['title' => $optionTitle]);
258+
$this->serializer->expects($this->once())
259+
->method('unserialize')
260+
->with($optionValue)
261+
->willReturn(json_decode($optionValue, true));
262+
$this->escaper->expects($this->once())
263+
->method('escapeHtml')
264+
->with($optionTitle)
265+
->will($this->returnValue($optionTitle));
266+
267+
$this->assertEquals('Option Title [2]', $fileObject->getEditableOptionValue($optionValue));
268+
}
269+
270+
public function testGetEditableOptionValueInvalid()
271+
{
272+
$fileObject = $this->getFileObject();
273+
$optionTitle = 'Option Title';
274+
$optionValue = json_encode(['title' => $optionTitle]);
275+
$this->serializer->expects($this->once())
276+
->method('unserialize')
277+
->with($optionValue)
278+
->willThrowException(new SerializationException(__('Invalid JSON value.')));
279+
$this->escaper->expects($this->never())
280+
->method('escapeHtml');
281+
282+
$this->assertEquals($optionValue, $fileObject->getEditableOptionValue($optionValue));
283+
}
284+
285+
public function testParseOptionValue()
286+
{
287+
$optionTitle = 'Option Title';
288+
$optionValue = json_encode(['title' => $optionTitle]);
289+
290+
$userInput = 'Option [2]';
291+
$fileObject = $this->getFileObject();
292+
293+
$itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
294+
->disableOriginalConstructor()
295+
->setMethods(['load', 'getValue'])
296+
->getMock();
297+
298+
$itemMock->expects($this->any())
299+
->method('load')
300+
->will($this->returnSelf());
301+
302+
$itemMock->expects($this->any())
303+
->method('getValue')
304+
->will($this->returnValue($optionValue));
305+
306+
$this->itemOptionFactoryMock->expects($this->any())
307+
->method('create')
308+
->will($this->returnValue($itemMock));
309+
310+
$this->assertEquals($optionValue, $fileObject->parseOptionValue($userInput, []));
311+
}
312+
313+
public function testParseOptionValueNoId()
314+
{
315+
$optionValue = 'value';
316+
317+
$userInput = 'Option [xx]';
318+
$fileObject = $this->getFileObject();
319+
320+
$itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
321+
->disableOriginalConstructor()
322+
->setMethods(['load', 'getValue'])
323+
->getMock();
324+
325+
$itemMock->expects($this->any())
326+
->method('load')
327+
->will($this->returnSelf());
328+
329+
$itemMock->expects($this->any())
330+
->method('getValue')
331+
->will($this->returnValue($optionValue));
332+
333+
$this->itemOptionFactoryMock->expects($this->any())
334+
->method('create')
335+
->will($this->returnValue($itemMock));
336+
337+
$this->assertEquals(null, $fileObject->parseOptionValue($userInput, []));
338+
}
339+
340+
public function testParseOptionValueInvalid()
341+
{
342+
$optionValue = 'Invalid json serialized value...';
343+
344+
$userInput = 'Option [2]';
345+
$fileObject = $this->getFileObject();
346+
347+
$itemMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
348+
->disableOriginalConstructor()
349+
->setMethods(['load', 'getValue'])
350+
->getMock();
351+
352+
$itemMock->expects($this->any())
353+
->method('load')
354+
->will($this->returnSelf());
355+
356+
$itemMock->expects($this->any())
357+
->method('getValue')
358+
->will($this->returnValue($optionValue));
359+
360+
$this->itemOptionFactoryMock->expects($this->any())
361+
->method('create')
362+
->will($this->returnValue($itemMock));
363+
364+
$this->serializer->expects($this->once())
365+
->method('unserialize')
366+
->with($optionValue)
367+
->willThrowException(new SerializationException(__('Invalid JSON value.')));
368+
369+
$this->assertEquals(null, $fileObject->parseOptionValue($userInput, []));
370+
}
371+
225372
public function testPrepareOptionValueForRequest()
226373
{
227374
$optionValue = 'string';

app/code/Magento/Quote/Model/Quote/Item/Updater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function update(Item $item, array $info)
105105
*
106106
* @param array $info
107107
* @param Item $item
108-
* @return array
108+
* @return void
109109
*/
110110
protected function setCustomPrice(array $info, Item $item)
111111
{

0 commit comments

Comments
 (0)