8
8
namespace Magento \Config \Test \Unit \Model \Config \Backend ;
9
9
10
10
use Magento \Config \Model \Config \Backend \Serialized ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
11
12
use Magento \Framework \Event \ManagerInterface ;
12
13
use Magento \Framework \Model \Context ;
13
14
use Magento \Framework \Serialize \Serializer \Json ;
@@ -27,11 +28,14 @@ class SerializedTest extends TestCase
27
28
/** @var LoggerInterface|MockObject */
28
29
private $ loggerMock ;
29
30
31
+ private $ scopeConfigMock ;
32
+
30
33
protected function setUp (): void
31
34
{
32
35
$ objectManager = new ObjectManager ($ this );
33
36
$ this ->serializerMock = $ this ->createMock (Json::class);
34
37
$ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
38
+ $ this ->scopeConfigMock = $ this ->createMock (ScopeConfigInterface::class);
35
39
$ contextMock = $ this ->createMock (Context::class);
36
40
$ eventManagerMock = $ this ->getMockForAbstractClass (ManagerInterface::class);
37
41
$ contextMock ->method ('getEventDispatcher ' )
@@ -43,6 +47,7 @@ protected function setUp(): void
43
47
[
44
48
'serializer ' => $ this ->serializerMock ,
45
49
'context ' => $ contextMock ,
50
+ 'config ' => $ this ->scopeConfigMock ,
46
51
]
47
52
);
48
53
}
@@ -135,4 +140,29 @@ public function beforeSaveDataProvider()
135
140
]
136
141
];
137
142
}
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
+ }
138
168
}
0 commit comments