Skip to content

Commit 8a306f1

Browse files
committed
Merge branch 'mainline_develop' into stock_indexer_batching
2 parents 591783c + 8b656e2 commit 8a306f1

File tree

3 files changed

+177
-34
lines changed

3 files changed

+177
-34
lines changed

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,21 @@ public function prepareForCart()
315315
public function getFormattedOptionValue($optionValue)
316316
{
317317
if ($this->_formattedOptionValue === null) {
318-
try {
319-
$value = $this->serializer->unserialize($optionValue);
320-
321-
$customOptionUrlParams = $this->getCustomOptionUrlParams() ? $this->getCustomOptionUrlParams() : [
318+
$value = $this->serializer->unserialize($optionValue);
319+
if ($value === null) {
320+
return $optionValue;
321+
}
322+
$customOptionUrlParams = $this->getCustomOptionUrlParams()
323+
? $this->getCustomOptionUrlParams()
324+
: [
322325
'id' => $this->getConfigurationItemOption()->getId(),
323326
'key' => $value['secret_key']
324327
];
325328

326-
$value['url'] = ['route' => $this->_customOptionDownloadUrl, 'params' => $customOptionUrlParams];
329+
$value['url'] = ['route' => $this->_customOptionDownloadUrl, 'params' => $customOptionUrlParams];
327330

328-
$this->_formattedOptionValue = $this->_getOptionHtml($value);
329-
$this->getConfigurationItemOption()->setValue($this->serializer->serialize($value));
330-
return $this->_formattedOptionValue;
331-
} catch (\Exception $e) {
332-
return $optionValue;
333-
}
331+
$this->_formattedOptionValue = $this->_getOptionHtml($value);
332+
$this->getConfigurationItemOption()->setValue($this->serializer->serialize($value));
334333
}
335334
return $this->_formattedOptionValue;
336335
}
@@ -401,16 +400,15 @@ public function getPrintableOptionValue($optionValue)
401400
*/
402401
public function getEditableOptionValue($optionValue)
403402
{
404-
try {
405-
$value = $this->serializer->unserialize($optionValue);
403+
$unserializedValue = $this->serializer->unserialize($optionValue);
404+
if ($unserializedValue !== null) {
406405
return sprintf(
407406
'%s [%d]',
408-
$this->_escaper->escapeHtml($value['title']),
407+
$this->_escaper->escapeHtml($unserializedValue['title']),
409408
$this->getConfigurationItemOption()->getId()
410409
);
411-
} catch (\Exception $e) {
412-
return $optionValue;
413410
}
411+
return $optionValue;
414412
}
415413

416414
/**
@@ -430,15 +428,11 @@ public function parseOptionValue($optionValue, $productOptionValues)
430428
if (preg_match('/\[([0-9]+)\]/', $optionValue, $matches)) {
431429
$confItemOptionId = $matches[1];
432430
$option = $this->_itemOptionFactory->create()->load($confItemOptionId);
433-
try {
434-
$this->serializer->unserialize($option->getValue());
431+
if ($this->serializer->unserialize($option->getValue()) !== null) {
435432
return $option->getValue();
436-
} catch (\Exception $e) {
437-
return null;
438433
}
439-
} else {
440-
return null;
441434
}
435+
return null;
442436
}
443437

444438
/**
@@ -449,12 +443,11 @@ public function parseOptionValue($optionValue, $productOptionValues)
449443
*/
450444
public function prepareOptionValueForRequest($optionValue)
451445
{
452-
try {
453-
$result = $this->serializer->unserialize($optionValue);
454-
return $result;
455-
} catch (\Exception $e) {
456-
return null;
446+
$unserializedValue = $this->serializer->unserialize($optionValue);
447+
if ($unserializedValue !== null) {
448+
return $unserializedValue;
457449
}
450+
return null;
458451
}
459452

460453
/**

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

Lines changed: 156 additions & 6 deletions
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);
@@ -71,6 +77,7 @@ protected function setUp()
7177

7278
$this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
7379
->disableOriginalConstructor()
80+
->setMethods(['serialize', 'unserialize'])
7481
->getMock();
7582

7683
$this->urlBuilder = $this->getMockBuilder(\Magento\Catalog\Model\Product\Option\UrlBuilder::class)
@@ -81,13 +88,38 @@ protected function setUp()
8188
->disableOriginalConstructor()
8289
->getMock();
8390

91+
$this->itemOptionFactoryMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\OptionFactory::class)
92+
->setMethods(['create'])
93+
->disableOriginalConstructor()
94+
->getMock();
95+
8496
$this->coreFileStorageDatabase = $this->getMock(
8597
\Magento\MediaStorage\Helper\File\Storage\Database::class,
8698
['copyFile'],
8799
[],
88100
'',
89101
false
90102
);
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+
);
91123
}
92124

93125
/**
@@ -102,7 +134,8 @@ protected function getFileObject()
102134
'coreFileStorageDatabase' => $this->coreFileStorageDatabase,
103135
'serializer' => $this->serializer,
104136
'urlBuilder' => $this->urlBuilder,
105-
'escaper' => $this->escaper
137+
'escaper' => $this->escaper,
138+
'itemOptionFactory' => $this->itemOptionFactoryMock,
106139
]
107140
);
108141
}
@@ -222,17 +255,134 @@ public function testGetFormattedOptionValue()
222255
$fileObject->getFormattedOptionValue($optionValue);
223256
}
224257

225-
public function testPrepareOptionValueForRequest()
258+
public function testGetFormattedOptionValueInvalid()
226259
{
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+
}
230263

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]);
231277
$this->serializer->expects($this->once())
232278
->method('unserialize')
233279
->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());
235315

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();
236386
$this->assertEquals($resultValue, $fileObject->prepareOptionValueForRequest($optionValue));
237387
}
238388
}

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)