77
88namespace Magento \TestFramework \Annotation ;
99
10- use Magento \Config \Model \Config \ Factory as ConfigFactory ;
10+ use Magento \Config \Model \ResourceModel \ Config as ConfigResource ;
1111use Magento \Framework \App \Config \MutableScopeConfigInterface ;
1212use Magento \Framework \App \Config \ScopeConfigInterface ;
13- use Magento \Store \Api \StoreRepositoryInterface ;
14- use Magento \Store \Api \WebsiteRepositoryInterface ;
1513use Magento \Store \Model \ScopeInterface ;
14+ use Magento \Store \Model \StoreManagerInterface ;
1615use Magento \TestFramework \App \ApiMutableScopeConfig ;
1716use Magento \TestFramework \Config \Model \ConfigStorage ;
1817use Magento \TestFramework \Helper \Bootstrap ;
2322class ApiConfigFixture extends ConfigFixture
2423{
2524 /**
26- * Values are inherited
25+ * Values need to be deleted form the database
2726 *
2827 * @var array
2928 */
30- private $ valuesNotFromDatabase = [];
29+ private $ valuesToDeleteFromDatabase = [];
3130
3231 /**
3332 * @inheritdoc
@@ -41,7 +40,7 @@ protected function setStoreConfigValue(array $matches, $configPathAndValue): voi
4140 /** @var ConfigStorage $configStorage */
4241 $ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
4342 if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_STORES , $ storeCode )) {
44- $ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
43+ $ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
4544 }
4645
4746 parent ::setStoreConfigValue ($ matches , $ configPathAndValue );
@@ -57,7 +56,7 @@ protected function setGlobalConfigValue($configPathAndValue): void
5756 /** @var ConfigStorage $configStorage */
5857 $ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
5958 if (!$ configStorage ->checkIsRecordExist ($ configPath )) {
60- $ this ->valuesNotFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
59+ $ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ] = $ requiredValue ;
6160 }
6261
6362 $ originalValue = $ this ->getScopeConfigValue ($ configPath , ScopeConfigInterface::SCOPE_TYPE_DEFAULT );
@@ -77,7 +76,7 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
7776 /** @var ConfigStorage $configStorage */
7877 $ configStorage = Bootstrap::getObjectManager ()->get (ConfigStorage::class);
7978 if (!$ configStorage ->checkIsRecordExist ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ websiteCode )) {
80- $ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
79+ $ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ?? '' ] = $ requiredValue ?? '' ;
8180 }
8281
8382 parent ::setWebsiteConfigValue ($ matches , $ configPathAndValue );
@@ -89,15 +88,12 @@ protected function setWebsiteConfigValue(array $matches, $configPathAndValue): v
8988 */
9089 protected function _restoreConfigData ()
9190 {
91+ /** @var ConfigResource $configResource */
92+ $ configResource = Bootstrap::getObjectManager ()->get (ConfigResource::class);
9293 /* Restore global values */
9394 foreach ($ this ->globalConfigValues as $ configPath => $ originalValue ) {
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- );
95+ if (isset ($ this ->valuesToDeleteFromDatabase ['global ' ][$ configPath ])) {
96+ $ configResource ->deleteConfig ($ configPath );
10197 } else {
10298 $ this ->_setConfigValue ($ configPath , $ originalValue );
10399 }
@@ -107,13 +103,9 @@ protected function _restoreConfigData()
107103 foreach ($ this ->storeConfigValues as $ storeCode => $ originalData ) {
108104 foreach ($ originalData as $ configPath => $ originalValue ) {
109105 $ storeCode = $ storeCode ?: null ;
110- if (isset ($ this ->valuesNotFromDatabase [$ storeCode ][$ configPath ])) {
111- $ this ->inheritConfig (
112- $ configPath ,
113- (string )$ originalValue ,
114- ScopeInterface::SCOPE_STORES ,
115- $ storeCode
116- );
106+ if (isset ($ this ->valuesToDeleteFromDatabase [$ storeCode ][$ configPath ])) {
107+ $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_STORES , $ storeCode );
108+ $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_STORES , $ scopeId );
117109 } else {
118110 $ this ->setScopeConfigValue (
119111 $ configPath ,
@@ -129,13 +121,9 @@ protected function _restoreConfigData()
129121 foreach ($ this ->websiteConfigValues as $ websiteCode => $ originalData ) {
130122 foreach ($ originalData as $ configPath => $ originalValue ) {
131123 $ websiteCode = $ websiteCode ?: null ;
132- if (isset ($ this ->valuesNotFromDatabase [$ websiteCode ][$ configPath ])) {
133- $ this ->inheritConfig (
134- $ configPath ,
135- $ originalValue ,
136- ScopeInterface::SCOPE_WEBSITES ,
137- $ websiteCode
138- );
124+ if (isset ($ this ->valuesToDeleteFromDatabase [$ websiteCode ][$ configPath ])) {
125+ $ scopeId = $ this ->getIdByScopeType (ScopeInterface::SCOPE_WEBSITES , $ websiteCode );
126+ $ configResource ->deleteConfig ($ configPath , ScopeInterface::SCOPE_WEBSITES , $ scopeId );
139127 } else {
140128 $ this ->setScopeConfigValue (
141129 $ configPath ,
@@ -171,47 +159,28 @@ protected function getScopeConfigValue(string $configPath, string $scopeType, st
171159 }
172160
173161 /**
174- * Inherit the config and remove the config from database
162+ * Get id by code
175163 *
176- * @param string $path
177- * @param string $value
178164 * @param string $scopeType
179- * @param string|null $scopeCode
180- * @return void
165+ * @param string|null $scopeId
166+ * @return int
181167 */
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_STORES && $ 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 ;
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 ;
213182 }
214183
215- $ objectManager -> get (ConfigFactory::class)-> create ([ ' data ' => $ configData ])-> save () ;
184+ return $ id ;
216185 }
217186}
0 commit comments