7
7
8
8
namespace Magento \TestFramework \Annotation ;
9
9
10
- use Magento \Config \Model \ResourceModel \ Config as ConfigResource ;
10
+ use Magento \Config \Model \Config \ Factory as ConfigFactory ;
11
11
use Magento \Framework \App \Config \MutableScopeConfigInterface ;
12
12
use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Store \Api \StoreRepositoryInterface ;
14
+ use Magento \Store \Api \WebsiteRepositoryInterface ;
13
15
use Magento \Store \Model \ScopeInterface ;
14
- use Magento \Store \Model \StoreManagerInterface ;
15
16
use Magento \TestFramework \App \ApiMutableScopeConfig ;
16
17
use Magento \TestFramework \Config \Model \ConfigStorage ;
17
18
use Magento \TestFramework \Helper \Bootstrap ;
22
23
class ApiConfigFixture extends ConfigFixture
23
24
{
24
25
/**
25
- * Values need to be deleted form the database
26
+ * Values are inherited
26
27
*
27
28
* @var array
28
29
*/
29
- private $ valuesToDeleteFromDatabase = [];
30
+ private $ valuesNotFromDatabase = [];
30
31
31
32
/**
32
33
* @inheritdoc
@@ -40,7 +41,7 @@ protected function setStoreConfigValue(array $matches, $configPathAndValue): voi
40
41
/** @var ConfigStorage $configStorage */
41
42
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
42
43
if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_STORES , $ storeCode )) {
43
- $ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
44
+ $ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
44
45
}
45
46
46
47
parent ::setStoreConfigValue ($ matches , $ configPathAndValue );
@@ -56,7 +57,7 @@ protected function setGlobalConfigValue($configPathAndValue): void
56
57
/** @var ConfigStorage $configStorage */
57
58
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
58
59
if (!$ configStorage ->checkIsRecordExist ($ configPath )) {
59
- $ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
60
+ $ this ->valuesNotFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
60
61
}
61
62
62
63
$ originalValue = $ this ->getScopeConfigValue ($ configPath , ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
@@ -76,7 +77,7 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
76
77
/** @var ConfigStorage $configStorage */
77
78
$ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
78
79
if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ websiteCode )) {
79
- $ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
80
+ $ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
80
81
}
81
82
82
83
parent ::setWebsiteConfigValue ($ matches , $ configPathAndValue );
@@ -88,12 +89,15 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
88
89
*/
89
90
protected function _restoreConfigData ()
90
91
{
91
- /** @var ConfigResource $configResource */
92
- $ configResource = Bootstrap::getObjectManager ()->get (ConfigResource::class);
93
92
/* Restore global values */
94
93
foreach ($ this ->globalConfigValues as $ configPath => $ originalValue ) {
95
- if (isset ($ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ])) {
96
- $ configResource ->deleteConfig ($ configPath );
94
+ if (isset ($ this ->valuesNotFromDatabase ['global ' ][$ configPath ])) {
95
+ $ this ->inheritConfig (
96
+ $ configPath ,
97
+ $ originalValue ,
98
+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
99
+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT
100
+ );
97
101
} else {
98
102
$ this ->_setConfigValue ($ configPath , $ originalValue );
99
103
}
@@ -103,9 +107,13 @@ protected function _restoreConfigData()
103
107
foreach ($ this ->storeConfigValues as $ storeCode => $ originalData ) {
104
108
foreach ($ originalData as $ configPath => $ originalValue ) {
105
109
$ storeCode = $ storeCode ?: null ;
106
- if (isset ($ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ])) {
107
- $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_STORES , $ storeCode );
108
- $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_STORES , $ scopeId );
110
+ if (isset ($ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ])) {
111
+ $ this ->inheritConfig (
112
+ $ configPath ,
113
+ (string )$ originalValue ,
114
+ ScopeInterface::SCOPE_STORES ,
115
+ $ storeCode
116
+ );
109
117
} else {
110
118
$ this ->setScopeConfigValue (
111
119
$ configPath ,
@@ -121,9 +129,13 @@ protected function _restoreConfigData()
121
129
foreach ($ this ->websiteConfigValues as $ websiteCode => $ originalData ) {
122
130
foreach ($ originalData as $ configPath => $ originalValue ) {
123
131
$ websiteCode = $ websiteCode ?: null ;
124
- if (isset ($ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ])) {
125
- $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_WEBSITES , $ websiteCode );
126
- $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ scopeId );
132
+ if (isset ($ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ])) {
133
+ $ this ->inheritConfig (
134
+ $ configPath ,
135
+ $ originalValue ,
136
+ ScopeInterface::SCOPE_WEBSITES ,
137
+ $ websiteCode
138
+ );
127
139
} else {
128
140
$ this ->setScopeConfigValue (
129
141
$ configPath ,
@@ -159,28 +171,47 @@ protected function getScopeConfigValue(string $configPath, string $scopeType, st
159
171
}
160
172
161
173
/**
162
- * Get id by code
174
+ * Inherit the config and remove the config from database
163
175
*
176
+ * @param string $path
177
+ * @param string $value
164
178
* @param string $scopeType
165
- * @param string|null $scopeId
166
- * @return int
179
+ * @param string|null $scopeCode
180
+ * @return void
167
181
*/
168
- private function getIdByScopeType (string $ scopeType , ?string $ scopeId ): int
169
- {
170
- $ id = 0 ;
171
- /** @var StoreManagerInterface $storeManager */
172
- $ storeManager = Bootstrap::getObjectManager ()->get (StoreManagerInterface::class);
173
- switch ($ scopeType ) {
174
- case ScopeInterface::SCOPE_WEBSITES :
175
- $ id = (int )$ storeManager ->getWebsite ($ scopeId )->getId ();
176
- break ;
177
- case ScopeInterface::SCOPE_STORES :
178
- $ id = (int )$ storeManager ->getStore ($ scopeId )->getId ();
179
- break ;
180
- default :
181
- break ;
182
+ private function inheritConfig (
183
+ string $ path ,
184
+ ?string $ value ,
185
+ string $ scopeType ,
186
+ ?string $ scopeCode
187
+ ) {
188
+ $ pathParts = explode ('/ ' , $ path );
189
+ $ store = 0 ;
190
+ $ configData = [
191
+ 'section ' => $ pathParts [0 ],
192
+ 'website ' => '' ,
193
+ 'store ' => $ store ,
194
+ 'groups ' => [
195
+ $ pathParts [1 ] => [
196
+ 'fields ' => [
197
+ $ pathParts [2 ] => [
198
+ 'value ' => $ value ,
199
+ 'inherit ' => 1
200
+ ]
201
+ ]
202
+ ]
203
+ ]
204
+ ];
205
+ $ objectManager = Bootstrap::getObjectManager ();
206
+ if ($ scopeType === ScopeInterface::SCOPE_STORE && $ scopeCode !== null ) {
207
+ $ store = $ objectManager ->get (StoreRepositoryInterface::class)->get ($ scopeCode )->getId ();
208
+ $ configData ['store ' ] = $ store ;
209
+ } elseif ($ scopeType === ScopeInterface::SCOPE_WEBSITES && $ scopeCode !== null ) {
210
+ $ website = $ objectManager ->get (WebsiteRepositoryInterface::class)->get ($ scopeCode )->getId ();
211
+ $ configData ['store ' ] = '' ;
212
+ $ configData ['website ' ] = $ website ;
182
213
}
183
214
184
- return $ id ;
215
+ $ objectManager -> get (ConfigFactory::class)-> create ([ ' data ' => $ configData ])-> save () ;
185
216
}
186
217
}
0 commit comments