Skip to content

Fix array to string conversion error when saving row system config with defaults #30322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/code/Magento/Config/Model/Config/Backend/Serialized.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Config\Model\Config\Backend;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

Expand Down Expand Up @@ -84,4 +85,26 @@ public function beforeSave()
parent::beforeSave();
return $this;
}

/**
* Get old value from existing config
*
* @return string
*/
public function getOldValue()
{
// If the value is retrieved from defaults defined in config.xml
// it may be returned as an array.
$value = $this->_config->getValue(
$this->getPath(),
$this->getScope() ?: ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$this->getScopeCode()
);

if (is_array($value)) {
return $this->serializer->serialize($value);
}

return (string)$value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Config\Test\Unit\Model\Config\Backend;

use Magento\Config\Model\Config\Backend\Serialized;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\Context;
use Magento\Framework\Serialize\Serializer\Json;
Expand All @@ -27,11 +28,14 @@ class SerializedTest extends TestCase
/** @var LoggerInterface|MockObject */
private $loggerMock;

private $scopeConfigMock;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->serializerMock = $this->createMock(Json::class);
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
$contextMock = $this->createMock(Context::class);
$eventManagerMock = $this->getMockForAbstractClass(ManagerInterface::class);
$contextMock->method('getEventDispatcher')
Expand All @@ -43,6 +47,7 @@ protected function setUp(): void
[
'serializer' => $this->serializerMock,
'context' => $contextMock,
'config' => $this->scopeConfigMock,
]
);
}
Expand Down Expand Up @@ -135,4 +140,29 @@ public function beforeSaveDataProvider()
]
];
}

/**
* If a config value is not available in core_confid_data the defaults are
* loaded from the config.xml file. Those defaults may be arrays.
* The Serialized backend model has to override its parent
* getOldValue function, to prevent an array to string conversion error
* and serialize those values.
*/
public function testGetOldValueWithNonScalarDefaultValue(): void
{
$value = [
['foo' => '1', 'bar' => '2'],
];
$serializedValue = \json_encode($value);

$this->scopeConfigMock->method('getValue')->willReturn($value);
$this->serializerMock->method('serialize')->willReturn($serializedValue);

$this->serializedConfig->setData('value', $serializedValue);

$oldValue = $this->serializedConfig->getOldValue();

$this->assertIsString($oldValue, 'Default value from the config is not serialized.');
$this->assertSame($serializedValue, $oldValue);
}
}