Skip to content

Commit ff4fe81

Browse files
author
Pascal Querner
committed
envLoader: fix tests, fix scope default
1 parent 565c3bc commit ff4fe81

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

app/code/core/Mage/Adminhtml/Block/System/Config/Form.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,12 @@ public function isOverwrittenByEnvVariable(string $path): bool
639639
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
640640
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
641641
$store = Mage::app()->getRequest()->getParam('store');
642-
$scope = $this->getScope();
643-
$path = "$scope/$store/$path";
642+
if ($store) {
643+
$scope = $this->getScope();
644+
$path = "$scope/$store/$path";
645+
return $environmentConfigLoaderHelper->hasPath($path);
646+
}
647+
$path = "default/$path";
644648
return $environmentConfigLoaderHelper->hasPath($path);
645649
}
646650

app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public function hasPath(string $wantedPath): bool
146146

147147
public function getAsArray(string $wantedStore): array
148148
{
149+
if (empty($wantedStore)) {
150+
$wantedStore = 'default';
151+
}
149152
$data = Mage::registry("config_env_array_$wantedStore");
150153
if ($data !== null) {
151154
return $data;
@@ -161,6 +164,11 @@ public function getAsArray(string $wantedStore): array
161164
list($configKeyParts, $scope) = $this->getConfigKey($configKey);
162165

163166
switch ($scope) {
167+
case static::CONFIG_KEY_DEFAULT:
168+
list($unused1, $unused2, $section, $group, $field) = $configKeyParts;
169+
$path = $this->buildPath($section, $group, $field);
170+
$config[$path] = $value;
171+
break;
164172
case static::CONFIG_KEY_WEBSITES:
165173
case static::CONFIG_KEY_STORES:
166174
[$unused1, $unused2, $storeCode, $section, $group, $field] = $configKeyParts;

tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,24 @@ public function testEnvOverridesForValidConfigKeys(array $config): void
114114
$xml = new Varien_Simplexml_Config();
115115
$xml->loadString($xmlStruct);
116116

117+
117118
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
118119
/** @phpstan-ignore method.internal */
119120
$loader->setEnvStore([
120121
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
121122
$config['env_path'] => $config['value'],
122123
]);
124+
Mage::unregister('current_env_config');
123125
$loader->overrideEnvironment($xml);
124126

125127
$configPath = $config['xml_path'];
126128
$defaultValue = $xmlDefault->getNode($configPath);
127129
$valueAfterOverride = $xml->getNode($configPath);
128130

129131
// assert
130-
static::assertNotSame((string) $defaultValue, (string) $valueAfterOverride, 'Default value was not overridden.');
132+
$expected = (string) $defaultValue;
133+
$actual = (string) $valueAfterOverride;
134+
static::assertNotSame($expected, $actual, 'Default value was not overridden.');
131135
}
132136

133137
public function envOverridesCorrectConfigKeysDataProvider(): Generator
@@ -216,35 +220,36 @@ public function testAsArray(array $config): void
216220
'OPENMAGE_CONFIG_OVERRIDE_ALLOWED' => 1,
217221
$config['env_path'] => 1,
218222
]);
219-
$actual = $loader->getAsArray($config['scope']);
223+
$store = $config['store'];
224+
$actual = $loader->getAsArray($store);
220225
$expected = $config['expected'];
221226
static::assertSame($expected, $actual);
222227
}
223228

224229
public function envAsArrayDataProvider(): Generator
225230
{
226-
yield 'defaultScope' => [
231+
yield 'default' => [
227232
[
228233
'env_path' => 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME',
229-
'scope' => 'default',
234+
'store' => '', // or 'default', which will be used internally, but this is how \Mage_Adminhtml_Model_Config_Data::_validate defines it
230235
'expected' => [
231236
'general/store_information/name' => 1,
232237
],
233238
],
234239
];
235-
yield 'storeScope' => [
240+
yield 'store' => [
236241
[
237242
'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
238-
'scope' => 'stores',
243+
'store' => 'german',
239244
'expected' => [
240245
'general/store_information/name' => 1,
241246
],
242247
],
243248
];
244-
yield 'invalidScope' => [
249+
yield 'invalidStore' => [
245250
[
246251
'env_path' => '',
247-
'scope' => 'foo',
252+
'store' => 'foo',
248253
'expected' => [],
249254
],
250255
];
@@ -283,7 +288,7 @@ public function envHasPathDataProvider(): Generator
283288
yield 'hasPath store' => [
284289
[
285290
'env_path' => 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME',
286-
'xml_path' => 'stores/general/store_information/name',
291+
'xml_path' => 'stores/german/general/store_information/name',
287292
'expected' => true,
288293
],
289294
];
@@ -313,11 +318,14 @@ public function testEnvDoesNotOverrideForInvalidConfigKeys(array $config): void
313318
$xml->loadString($xmlStruct);
314319

315320
$defaultValue = 'test_default';
316-
static::assertSame($defaultValue, (string) $xml->getNode(self::XML_PATH_DEFAULT));
321+
$actual = (string) $xml->getNode(self::XML_PATH_DEFAULT);
322+
static::assertSame($defaultValue, $actual);
317323
$defaultWebsiteValue = 'test_website';
318-
static::assertSame($defaultWebsiteValue, (string) $xml->getNode(self::XML_PATH_WEBSITE));
324+
$actual = (string) $xml->getNode(self::XML_PATH_WEBSITE);
325+
static::assertSame($defaultWebsiteValue, $actual);
319326
$defaultStoreValue = 'test_store';
320-
static::assertSame($defaultStoreValue, (string) $xml->getNode(self::XML_PATH_STORE));
327+
$actual = (string) $xml->getNode(self::XML_PATH_STORE);
328+
static::assertSame($defaultStoreValue, $actual);
321329

322330
$loader = new Mage_Core_Helper_EnvironmentConfigLoader();
323331
/** @phpstan-ignore method.internal */

0 commit comments

Comments
 (0)