5
5
*/
6
6
namespace Magento \Config \Model ;
7
7
8
+ use Magento \Backend \App \Area \FrontNameResolver ;
9
+ use Magento \Config \Model \ResourceModel \Config \Data \Collection ;
10
+ use Magento \Config \Model \ResourceModel \Config \Data \CollectionFactory ;
11
+ use Magento \Framework \Config \ScopeInterface ;
12
+ use Magento \Framework \Encryption \EncryptorInterface ;
8
13
use Magento \TestFramework \Helper \Bootstrap ;
14
+ use PHPUnit \Framework \TestCase ;
9
15
10
16
/**
11
17
* @magentoAppArea adminhtml
12
18
*/
13
- class ConfigTest extends \ PHPUnit \ Framework \ TestCase
19
+ class ConfigTest extends TestCase
14
20
{
15
21
/**
16
22
* @covers \Magento\Config\Model\Config::save
@@ -22,25 +28,25 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
22
28
public function testSaveWithSingleStoreModeEnabled ($ groups )
23
29
{
24
30
Bootstrap::getObjectManager ()->get (
25
- \ Magento \ Framework \ Config \ ScopeInterface::class
31
+ ScopeInterface::class
26
32
)->setCurrentScope (
27
- \ Magento \ Backend \ App \ Area \ FrontNameResolver::AREA_CODE
33
+ FrontNameResolver::AREA_CODE
28
34
);
29
- /** @var $_configDataObject \Magento\Config\Model\ Config */
30
- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
35
+ /** @var $_configDataObject Config */
36
+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
31
37
$ _configData = $ _configDataObject ->setSection ('dev ' )->setWebsite ('base ' )->load ();
32
38
$ this ->assertEmpty ($ _configData );
33
39
34
- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
40
+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
35
41
$ _configDataObject ->setSection ('dev ' )->setGroups ($ groups )->save ();
36
42
37
- /** @var $_configDataObject \Magento\Config\Model\ Config */
38
- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
43
+ /** @var $_configDataObject Config */
44
+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
39
45
$ _configData = $ _configDataObject ->setSection ('dev ' )->load ();
40
46
$ this ->assertArrayHasKey ('dev/debug/template_hints_admin ' , $ _configData );
41
47
$ this ->assertArrayHasKey ('dev/debug/template_hints_blocks ' , $ _configData );
42
48
43
- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
49
+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
44
50
$ _configData = $ _configDataObject ->setSection ('dev ' )->setWebsite ('base ' )->load ();
45
51
$ this ->assertArrayNotHasKey ('dev/debug/template_hints_admin ' , $ _configData );
46
52
$ this ->assertArrayNotHasKey ('dev/debug/template_hints_blocks ' , $ _configData );
@@ -63,16 +69,16 @@ public function testSave($section, $groups, $expected)
63
69
{
64
70
$ objectManager = Bootstrap::getObjectManager ();
65
71
66
- /** @var $_configDataObject \Magento\Config\Model\ Config */
67
- $ _configDataObject = $ objectManager ->create (\ Magento \ Config \ Model \ Config::class);
72
+ /** @var $_configDataObject Config */
73
+ $ _configDataObject = $ objectManager ->create (Config::class);
68
74
$ _configDataObject ->setSection ($ section )->setWebsite ('base ' )->setGroups ($ groups )->save ();
69
75
70
76
foreach ($ expected as $ group => $ expectedData ) {
71
- $ _configDataObject = $ objectManager ->create (\ Magento \ Config \ Model \ Config::class);
77
+ $ _configDataObject = $ objectManager ->create (Config::class);
72
78
$ _configData = $ _configDataObject ->setSection ($ group )->setWebsite ('base ' )->load ();
73
79
if (array_key_exists ('payment/payflow_link/pwd ' , $ _configData )) {
74
80
$ _configData ['payment/payflow_link/pwd ' ] = $ objectManager ->get (
75
- \ Magento \ Framework \ Encryption \ EncryptorInterface::class
81
+ EncryptorInterface::class
76
82
)->decrypt (
77
83
$ _configData ['payment/payflow_link/pwd ' ]
78
84
);
@@ -85,4 +91,102 @@ public function saveDataProvider()
85
91
{
86
92
return require __DIR__ . '/_files/config_section.php ' ;
87
93
}
94
+
95
+ /**
96
+ * @param string $website
97
+ * @param string $section
98
+ * @param array $override
99
+ * @param array $inherit
100
+ * @param array $expected
101
+ * @dataProvider saveWebsiteScopeDataProvider
102
+ */
103
+ public function testSaveUseDefault (
104
+ string $ website ,
105
+ string $ section ,
106
+ array $ override ,
107
+ array $ inherit ,
108
+ array $ expected
109
+ ): void {
110
+ $ objectManager = Bootstrap::getObjectManager ();
111
+ /** @var Config $config*/
112
+ $ configFactory = $ objectManager ->create (ConfigFactory::class);
113
+ $ config = $ configFactory ->create ()
114
+ ->setSection ($ section )
115
+ ->setWebsite ($ website )
116
+ ->setGroups ($ override ['groups ' ])
117
+ ->save ();
118
+
119
+ $ paths = array_keys ($ expected );
120
+
121
+ $ this ->assertEquals (
122
+ $ expected ,
123
+ $ this ->getConfigValues ($ config ->getScope (), $ config ->getScopeId (), $ paths )
124
+ );
125
+
126
+ $ config = $ configFactory ->create ()
127
+ ->setSection ($ section )
128
+ ->setWebsite ($ website )
129
+ ->setGroups ($ inherit ['groups ' ])
130
+ ->save ();
131
+
132
+ $ this ->assertEmpty (
133
+ $ this ->getConfigValues ($ config ->getScope (), $ config ->getScopeId (), $ paths )
134
+ );
135
+ }
136
+
137
+ /**
138
+ * @return array
139
+ */
140
+ public function saveWebsiteScopeDataProvider (): array
141
+ {
142
+ return [
143
+ [
144
+ 'website ' => 'base ' ,
145
+ 'section ' => 'payment ' ,
146
+ [
147
+ 'groups ' => [
148
+ 'account ' => [
149
+ 'fields ' => [
150
+ 'merchant_country ' => ['value ' => 'GB ' ],
151
+ ],
152
+ ],
153
+ ]
154
+ ],
155
+ [
156
+ 'groups ' => [
157
+ 'account ' => [
158
+ 'fields ' => [
159
+ 'merchant_country ' => ['inherit ' => 1 ],
160
+ ],
161
+ ],
162
+ ],
163
+ ],
164
+ 'expected ' => [
165
+ 'paypal/general/merchant_country ' => 'GB ' ,
166
+ ],
167
+ ]
168
+ ];
169
+ }
170
+
171
+ /**
172
+ * @param string $scope
173
+ * @param int $scopeId
174
+ * @param array $paths
175
+ * @return array
176
+ */
177
+ private function getConfigValues (string $ scope , int $ scopeId , array $ paths ): array
178
+ {
179
+ $ objectManager = Bootstrap::getObjectManager ();
180
+ /** @var Collection $configCollection */
181
+ $ configCollectionFactory = $ objectManager ->create (CollectionFactory::class);
182
+ $ configCollection = $ configCollectionFactory ->create ();
183
+ $ configCollection ->addFieldToFilter ('scope ' , $ scope );
184
+ $ configCollection ->addFieldToFilter ('scope_id ' , $ scopeId );
185
+ $ configCollection ->addFieldToFilter ('path ' , ['in ' => $ paths ]);
186
+ $ result = [];
187
+ foreach ($ configCollection as $ data ) {
188
+ $ result [$ data ->getPath ()] = $ data ->getValue ();
189
+ }
190
+ return $ result ;
191
+ }
88
192
}
0 commit comments