From d5f5f320cc930b379478b1410b00359bea1ddbd4 Mon Sep 17 00:00:00 2001 From: Sonny Kieu Date: Tue, 30 Apr 2024 04:52:57 +1000 Subject: [PATCH] Issue #5960: Environment variables ignored if the value provided in the user configuration file. (#5961) * Issue #5960: Environment variables ignored if the value provided in the user configuration file. On-behalf-of: @salsadigitalauorg * Issue #5960: Updated unit test. On-behalf-of: @salsadigitalauorg --------- Co-authored-by: Moshe Weitzman --- src/Config/ConfigLocator.php | 2 +- tests/unit/ConfigLocatorTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Config/ConfigLocator.php b/src/Config/ConfigLocator.php index e8543b1c20..01e7f4630c 100644 --- a/src/Config/ConfigLocator.php +++ b/src/Config/ConfigLocator.php @@ -105,11 +105,11 @@ public function __construct($envPrefix = '', $configFileVariant = '') // Add placeholders to establish priority. We add // contexts from lowest to highest priority. $this->config->addPlaceholder(self::DRUSH_CONTEXT); + $this->config->addPlaceholder(self::USER_CONTEXT); if (!empty($envPrefix)) { $envConfig = new EnvConfig($envPrefix); $this->config->addContext(self::ENV_CONTEXT, $envConfig); } - $this->config->addPlaceholder(self::USER_CONTEXT); $this->config->addPlaceholder(self::DRUPAL_CONTEXT); $this->config->addPlaceholder(self::SITE_CONTEXT); $this->config->addPlaceholder(self::ALIAS_CONTEXT); diff --git a/tests/unit/ConfigLocatorTest.php b/tests/unit/ConfigLocatorTest.php index fcc4d146f8..c9606eee8a 100644 --- a/tests/unit/ConfigLocatorTest.php +++ b/tests/unit/ConfigLocatorTest.php @@ -91,6 +91,25 @@ function ($item) { $this->assertEquals($expected, $aliasPaths); } + /** + * Test an env var supersedes user's configuration file. + */ + public function testEnvVar() + { + $configLocator = $this->createConfigLocator(); + + $sources = $configLocator->sources(); + $this->assertEquals($this->fixturesDir() . '/home/.drush/drush.yml', Path::canonicalize($sources['test']['home'])); + + $config = $configLocator->config(); + $this->assertEquals('A user-specific setting', $config->get('test.home')); + + putenv('TEST_HOME=An env overridden setting'); + $this->assertNotEquals('A user-specific setting', $config->get('test.home')); + $this->assertEquals('An env overridden setting', $config->get('test.home')); + putenv('TEST_HOME'); + } + /** * Create a config locator from All The Sources, for use in multiple tests. */