88namespace Magento \Config \Test \Unit \Model \Config \Backend ;
99
1010use Magento \Config \Model \Config \Backend \Serialized ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1112use Magento \Framework \Event \ManagerInterface ;
1213use Magento \Framework \Model \Context ;
1314use Magento \Framework \Serialize \Serializer \Json ;
@@ -27,11 +28,14 @@ class SerializedTest extends TestCase
2728 /** @var LoggerInterface|MockObject */
2829 private $ loggerMock ;
2930
31+ private $ scopeConfigMock ;
32+
3033 protected function setUp (): void
3134 {
3235 $ objectManager = new ObjectManager ($ this );
3336 $ this ->serializerMock = $ this ->createMock (Json::class);
3437 $ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
38+ $ this ->scopeConfigMock = $ this ->createMock (ScopeConfigInterface::class);
3539 $ contextMock = $ this ->createMock (Context::class);
3640 $ eventManagerMock = $ this ->getMockForAbstractClass (ManagerInterface::class);
3741 $ contextMock ->method ('getEventDispatcher ' )
@@ -43,6 +47,7 @@ protected function setUp(): void
4347 [
4448 'serializer ' => $ this ->serializerMock ,
4549 'context ' => $ contextMock ,
50+ 'config ' => $ this ->scopeConfigMock ,
4651 ]
4752 );
4853 }
@@ -135,4 +140,29 @@ public function beforeSaveDataProvider()
135140 ]
136141 ];
137142 }
143+
144+ /**
145+ * If a config value is not available in core_confid_data the defaults are
146+ * loaded from the config.xml file. Those defaults may be arrays.
147+ * The Serialized backend model has to override its parent
148+ * getOldValue function, to prevent an array to string conversion error
149+ * and serialize those values.
150+ */
151+ public function testGetOldValueWithNonScalarDefaultValue (): void
152+ {
153+ $ value = [
154+ ['foo ' => '1 ' , 'bar ' => '2 ' ],
155+ ];
156+ $ serializedValue = \json_encode ($ value );
157+
158+ $ this ->scopeConfigMock ->method ('getValue ' )->willReturn ($ value );
159+ $ this ->serializerMock ->method ('serialize ' )->willReturn ($ serializedValue );
160+
161+ $ this ->serializedConfig ->setData ('value ' , $ serializedValue );
162+
163+ $ oldValue = $ this ->serializedConfig ->getOldValue ();
164+
165+ $ this ->assertIsString ($ oldValue , 'Default value from the config is not serialized. ' );
166+ $ this ->assertSame ($ serializedValue , $ oldValue );
167+ }
138168}
0 commit comments