Skip to content

Commit

Permalink
Merge pull request magento#4085 from magento-arcticfoxes/MC-15873-2
Browse files Browse the repository at this point in the history
 MC-15873: Catch unserializer exception
  • Loading branch information
joanhe authored Apr 22, 2019
2 parents f106d73 + cbf2eb3 commit 0ae802b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/code/Magento/Config/Model/Config/Backend/Serialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Magento\Framework\Serialize\Serializer\Json;

/**
* Serialized backend model
*
* @api
* @since 100.0.2
*/
Expand Down Expand Up @@ -46,17 +48,32 @@ public function __construct(
}

/**
* Processing object after load data
*
* @return void
*/
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);
}
}
}

/**
* Processing object before save data
*
* @return $this
*/
public function beforeSave()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
use Magento\Framework\Model\Context;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Psr\Log\LoggerInterface;

/**
* Class SerializedTest
*/
class SerializedTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Config\Model\Config\Backend\Serialized */
Expand All @@ -18,14 +22,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 +82,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 0ae802b

Please sign in to comment.