Skip to content

Commit

Permalink
Merge pull request #4090 from magento-arcticfoxes/MC-15874-2
Browse files Browse the repository at this point in the history
MC-15874: Catch unserializer exception
  • Loading branch information
joanhe authored Apr 22, 2019
2 parents c52ee4a + 05417ce commit 14e5d75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/code/Magento/Config/Model/Config/Backend/Serialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ protected function _afterLoad()
{
$value = $this->getValue();
if (!is_array($value)) {
$this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
try {
$this->setValue(empty($value) ? false : $this->serializer->unserialize($value));
} catch (\Exception $e) {
$this->_logger->critical(
sprintf(
'Failed to unserialize %s config value. The error is: %s',
$this->getPath(),
$e->getMessage()
)
);
$this->setValue(false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\Model\Context;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Psr\Log\LoggerInterface;

class SerializedTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -18,14 +19,20 @@ class SerializedTest extends \PHPUnit\Framework\TestCase
/** @var Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

/** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
private $loggerMock;

protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->serializerMock = $this->createMock(Json::class);
$this->loggerMock = $this->createMock(LoggerInterface::class);
$contextMock = $this->createMock(Context::class);
$eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
$contextMock->method('getEventDispatcher')
->willReturn($eventManagerMock);
$contextMock->method('getLogger')
->willReturn($this->loggerMock);
$this->serializedConfig = $objectManager->getObject(
Serialized::class,
[
Expand Down Expand Up @@ -72,6 +79,20 @@ public function afterLoadDataProvider()
];
}

public function testAfterLoadWithException()
{
$value = '{"key":';
$expected = false;
$this->serializedConfig->setValue($value);
$this->serializerMock->expects($this->once())
->method('unserialize')
->willThrowException(new \Exception());
$this->loggerMock->expects($this->once())
->method('critical');
$this->serializedConfig->afterLoad();
$this->assertEquals($expected, $this->serializedConfig->getValue());
}

/**
* @param string $expected
* @param int|double|string|array|boolean|null $value
Expand Down

0 comments on commit 14e5d75

Please sign in to comment.