Skip to content

Commit 264df42

Browse files
merge magento/2.3-develop into magento-trigger/MAGETWO-99210
2 parents 615f38e + 0ae802b commit 264df42

File tree

3 files changed

+919
-281
lines changed

3 files changed

+919
-281
lines changed

app/code/Magento/Config/Model/Config/Backend/Serialized.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\Serialize\Serializer\Json;
1010

1111
/**
12+
* Serialized backend model
13+
*
1214
* @api
1315
* @since 100.0.2
1416
*/
@@ -46,17 +48,32 @@ public function __construct(
4648
}
4749

4850
/**
51+
* Processing object after load data
52+
*
4953
* @return void
5054
*/
5155
protected function _afterLoad()
5256
{
5357
$value = $this->getValue();
5458
if (!is_array($value)) {
55-
$this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
59+
try {
60+
$this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
61+
} catch (\Exception $e) {
62+
$this->_logger->critical(
63+
sprintf(
64+
'Failed to unserialize %s config value. The error is: %s',
65+
$this->getPath(),
66+
$e->getMessage()
67+
)
68+
);
69+
$this->setValue(false);
70+
}
5671
}
5772
}
5873

5974
/**
75+
* Processing object before save data
76+
*
6077
* @return $this
6178
*/
6279
public function beforeSave()

app/code/Magento/Config/Test/Unit/Model/Config/Backend/SerializedTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
use Magento\Framework\Model\Context;
1010
use Magento\Framework\Serialize\Serializer\Json;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Psr\Log\LoggerInterface;
1213

14+
/**
15+
* Class SerializedTest
16+
*/
1317
class SerializedTest extends \PHPUnit\Framework\TestCase
1418
{
1519
/** @var \Magento\Config\Model\Config\Backend\Serialized */
@@ -18,14 +22,20 @@ class SerializedTest extends \PHPUnit\Framework\TestCase
1822
/** @var Json|\PHPUnit_Framework_MockObject_MockObject */
1923
private $serializerMock;
2024

25+
/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
26+
private $loggerMock;
27+
2128
protected function setUp()
2229
{
2330
$objectManager = new ObjectManager($this);
2431
$this->serializerMock = $this->createMock(Json::class);
32+
$this->loggerMock = $this->createMock(LoggerInterface::class);
2533
$contextMock = $this->createMock(Context::class);
2634
$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
2735
$contextMock->method('getEventDispatcher')
2836
->willReturn($eventManagerMock);
37+
$contextMock->method('getLogger')
38+
->willReturn($this->loggerMock);
2939
$this->serializedConfig = $objectManager->getObject(
3040
Serialized::class,
3141
[
@@ -72,6 +82,20 @@ public function afterLoadDataProvider()
7282
];
7383
}
7484

85+
public function testAfterLoadWithException()
86+
{
87+
$value = '{"key":';
88+
$expected = false;
89+
$this->serializedConfig->setValue($value);
90+
$this->serializerMock->expects($this->once())
91+
->method('unserialize')
92+
->willThrowException(new \Exception());
93+
$this->loggerMock->expects($this->once())
94+
->method('critical');
95+
$this->serializedConfig->afterLoad();
96+
$this->assertEquals($expected, $this->serializedConfig->getValue());
97+
}
98+
7599
/**
76100
* @param string $expected
77101
* @param int|double|string|array|boolean|null $value

0 commit comments

Comments
 (0)