Skip to content

Commit 99e2a6f

Browse files
pquernerPascal QuernersreichelCopilot
authored
Fix environment loader (#4617)
* fix: set cache when env loader overrides data * feat?: call env helper on config get * feat?: backend: show data from env * feat?: backend: disable field when ENV data is available * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php * Update app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php * feat?: backend: do not log on invalid stores - creates infinite loop * feat: move env variables "str_starts_with" * feat: add env flag to disable/enable feature * fix: fix tests * feat: set disabled, set scope label * feat: remove checkbox to override * chore: remove inline helper variable name * Update tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php Co-authored-by: Sven Reichel <[email protected]> * Update tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php Co-authored-by: Sven Reichel <[email protected]> * Update tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php Co-authored-by: Sven Reichel <[email protected]> * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php Co-authored-by: Sven Reichel <[email protected]> * feat: add "hasPath" test * feat: add "asArray" test * tests: add test cases * Update app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php Co-authored-by: Sven Reichel <[email protected]> * fix: onStore(re)init the env vars would be lost * envLoader: fix loading from scopes * chore: run phpcs * chore: phptan: add ignore line comment about internal method * envLoader: fix tests, fix scope default * chore: run rector:fix * Update tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php * Update tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php * sonar update * fix * phpstan L10 * update * fix: run php-cs-fixer * fix? phpstan? * chore: run rector fix * fix config * refactor * update * update * added cypress test * Refactor web environment configuration in cypress.yml * Simplify ddev config command in workflow * Update registration success message in tests * Update cypress/e2e/openmage/backend/system/config/general/general.cy.js Co-authored-by: Copilot <[email protected]> * Update app/code/core/Mage/Adminhtml/Block/System/Config/Form.php Co-authored-by: Copilot <[email protected]> * Update cypress/support/openmage/backend/system/config/general/general.js Co-authored-by: Copilot <[email protected]> * fix: websites do not allow "-" character, but A-Z0-9_, starting with letter * fix: superflous website tests and testing suite bootstrap Websites may not have a "-" character so this test is removed Removed website test for "base_ch", as its the same as "base", so superflous. Add testing stores if not existing and remove afterwards. * chore: linter fixes * chore: run rector:fix --------- Co-authored-by: Pascal Querner <[email protected]> Co-authored-by: Sven Reichel <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 1814050 commit 99e2a6f

File tree

15 files changed

+810
-78
lines changed

15 files changed

+810
-78
lines changed

.github/workflows/cypress.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
autostart: false
2222

2323
# install DDEV configuration
24-
- run: ddev config --project-type=magento --php-version=8.1 --webserver-type=${{ matrix.webserver }} --web-environment="MAGE_IS_DEVELOPER_MODE=1"
24+
- run: ddev config --project-type=magento --php-version=8.1 --webserver-type=${{ matrix.webserver }} --web-environment="MAGE_IS_DEVELOPER_MODE=1,OPENMAGE_CONFIG_OVERRIDE_ALLOWED=1,OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME=ENV name default,OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__STORE_INFORMATION__PHONE=ENV phone website,OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__ADDRESS=ENV address store"
2525

2626
# install composer dependencies
2727
- run: ddev composer install

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Mage_Adminhtml_Block_System_Config_Form extends Mage_Adminhtml_Block_Widge
2020

2121
public const SCOPE_STORES = 'stores';
2222

23+
public const SCOPE_ENV = 'env';
24+
2325
/**
2426
* Config data array
2527
*
@@ -73,6 +75,7 @@ public function __construct()
7375
self::SCOPE_DEFAULT => Mage::helper('adminhtml')->__('[GLOBAL]'),
7476
self::SCOPE_WEBSITES => Mage::helper('adminhtml')->__('[WEBSITE]'),
7577
self::SCOPE_STORES => Mage::helper('adminhtml')->__('[STORE VIEW]'),
78+
self::SCOPE_ENV => Mage::helper('adminhtml')->__('[ENV]'),
7679
];
7780
}
7881

@@ -380,7 +383,7 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
380383
}
381384
}
382385

383-
$field = $fieldset->addField($id, $fieldType, [
386+
$elementFieldData = [
384387
'name' => $name,
385388
'label' => $label,
386389
'comment' => $comment,
@@ -396,7 +399,15 @@ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labe
396399
'scope_label' => $this->getScopeLabel($element),
397400
'can_use_default_value' => $this->canUseDefaultValue((int) $element->show_in_default),
398401
'can_use_website_value' => $this->canUseWebsiteValue((int) $element->show_in_website),
399-
]);
402+
];
403+
if ($this->isOverwrittenByEnvVariable($path)) {
404+
$elementFieldData['scope_label'] = $this->_scopeLabels[self::SCOPE_ENV];
405+
$elementFieldData['disabled'] = 1;
406+
$elementFieldData['can_use_default_value'] = 0;
407+
$elementFieldData['can_use_website_value'] = 0;
408+
}
409+
410+
$field = $fieldset->addField($id, $fieldType, $elementFieldData);
400411
$this->_prepareFieldOriginalData($field, $element);
401412

402413
if (isset($element->validate)) {
@@ -645,6 +656,32 @@ public function getScope()
645656
return $scope;
646657
}
647658

659+
/**
660+
* Returns true if element was overwritten by ENV variable
661+
*/
662+
public function isOverwrittenByEnvVariable(string $path): bool
663+
{
664+
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
665+
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
666+
667+
$scope = $this->getScope();
668+
$store = Mage::app()->getRequest()->getParam('store');
669+
$website = Mage::app()->getRequest()->getParam('website');
670+
671+
if ($store && $website) {
672+
$path = "$scope/$store/$path";
673+
return $environmentConfigLoaderHelper->hasPath($path);
674+
}
675+
676+
if ($website) {
677+
$path = "$scope/$website/$path";
678+
return $environmentConfigLoaderHelper->hasPath($path);
679+
}
680+
681+
$path = "$scope/$path";
682+
return $environmentConfigLoaderHelper->hasPath($path);
683+
}
684+
648685
/**
649686
* Retrieve label for scope
650687
*

app/code/core/Mage/Adminhtml/Model/Config/Data.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ protected function _getPathConfig($path, $full = true)
355355
}
356356
}
357357

358+
if (!$full) {
359+
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
360+
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
361+
$store = $this->getStore();
362+
$envConfig = $environmentConfigLoaderHelper->getAsArray($store);
363+
$config = array_merge($config, $envConfig);
364+
}
365+
358366
return $config;
359367
}
360368

0 commit comments

Comments
 (0)