55 */
66namespace Magento \Config \Model ;
77
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 ;
813use Magento \TestFramework \Helper \Bootstrap ;
14+ use PHPUnit \Framework \TestCase ;
915
1016/**
1117 * @magentoAppArea adminhtml
1218 */
13- class ConfigTest extends \ PHPUnit \ Framework \ TestCase
19+ class ConfigTest extends TestCase
1420{
1521 /**
1622 * @covers \Magento\Config\Model\Config::save
@@ -22,25 +28,25 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
2228 public function testSaveWithSingleStoreModeEnabled ($ groups )
2329 {
2430 Bootstrap::getObjectManager ()->get (
25- \ Magento \ Framework \ Config \ ScopeInterface::class
31+ ScopeInterface::class
2632 )->setCurrentScope (
27- \ Magento \ Backend \ App \ Area \ FrontNameResolver::AREA_CODE
33+ FrontNameResolver::AREA_CODE
2834 );
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);
3137 $ _configData = $ _configDataObject ->setSection ('dev ' )->setWebsite ('base ' )->load ();
3238 $ this ->assertEmpty ($ _configData );
3339
34- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
40+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
3541 $ _configDataObject ->setSection ('dev ' )->setGroups ($ groups )->save ();
3642
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);
3945 $ _configData = $ _configDataObject ->setSection ('dev ' )->load ();
4046 $ this ->assertArrayHasKey ('dev/debug/template_hints_admin ' , $ _configData );
4147 $ this ->assertArrayHasKey ('dev/debug/template_hints_blocks ' , $ _configData );
4248
43- $ _configDataObject = Bootstrap::getObjectManager ()->create (\ Magento \ Config \ Model \ Config::class);
49+ $ _configDataObject = Bootstrap::getObjectManager ()->create (Config::class);
4450 $ _configData = $ _configDataObject ->setSection ('dev ' )->setWebsite ('base ' )->load ();
4551 $ this ->assertArrayNotHasKey ('dev/debug/template_hints_admin ' , $ _configData );
4652 $ this ->assertArrayNotHasKey ('dev/debug/template_hints_blocks ' , $ _configData );
@@ -63,16 +69,16 @@ public function testSave($section, $groups, $expected)
6369 {
6470 $ objectManager = Bootstrap::getObjectManager ();
6571
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);
6874 $ _configDataObject ->setSection ($ section )->setWebsite ('base ' )->setGroups ($ groups )->save ();
6975
7076 foreach ($ expected as $ group => $ expectedData ) {
71- $ _configDataObject = $ objectManager ->create (\ Magento \ Config \ Model \ Config::class);
77+ $ _configDataObject = $ objectManager ->create (Config::class);
7278 $ _configData = $ _configDataObject ->setSection ($ group )->setWebsite ('base ' )->load ();
7379 if (array_key_exists ('payment/payflow_link/pwd ' , $ _configData )) {
7480 $ _configData ['payment/payflow_link/pwd ' ] = $ objectManager ->get (
75- \ Magento \ Framework \ Encryption \ EncryptorInterface::class
81+ EncryptorInterface::class
7682 )->decrypt (
7783 $ _configData ['payment/payflow_link/pwd ' ]
7884 );
@@ -85,4 +91,102 @@ public function saveDataProvider()
8591 {
8692 return require __DIR__ . '/_files/config_section.php ' ;
8793 }
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+ }
88192}
0 commit comments