From 424547bb704a1a245ec6a527f9756359d7abec68 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Fri, 1 Jul 2022 11:08:35 +0200 Subject: [PATCH] Correctly merge boolean flag of allow-plugin config (#10909) --- src/Composer/Config.php | 9 ++------- tests/Composer/Test/ConfigTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index e81c11738..eb4999fda 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -34,7 +34,7 @@ class Config public static $defaultConfig = array( 'process-timeout' => 300, 'use-include-path' => false, - 'allow-plugins' => null, // null for BC for now, will become array() after July 2022 + 'allow-plugins' => array(), 'use-parent-dir' => 'prompt', 'preferred-install' => 'dist', 'notify-on-install' => true, @@ -119,11 +119,6 @@ class Config // load defaults $this->config = static::$defaultConfig; - // TODO after July 2022 remove this and update the default value above in self::$defaultConfig + remove note from 06-config.md - if (strtotime('2022-07-01') < time()) { - $this->config['allow-plugins'] = array(); - } - $this->repositories = static::$defaultRepositories; $this->useEnvironment = (bool) $useEnvironment; $this->baseDir = $baseDir; @@ -185,7 +180,7 @@ class Config if (in_array($key, array('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic', 'bearer'), true) && isset($this->config[$key])) { $this->config[$key] = array_merge($this->config[$key], $val); $this->setSourceOfConfigValue($val, $key, $source); - } elseif (in_array($key, array('allow-plugins'), true) && isset($this->config[$key]) && is_array($this->config[$key])) { + } elseif (in_array($key, array('allow-plugins'), true) && isset($this->config[$key]) && is_array($this->config[$key]) && is_array($val)) { // merging $val first to get the local config on top of the global one, then appending the global config, // then merging local one again to make sure the values from local win over global ones for keys present in both $this->config[$key] = array_merge($val, $this->config[$key], $val); diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index d9d5c8cb2..51d71ef29 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -371,4 +371,34 @@ class ConfigTest extends TestCase $this->assertEquals('COMPOSER_HTACCESS_PROTECT', $result); } + + public function testMergesPluginConfig() + { + $config = new Config(false); + $config->merge(array('config' => array('allow-plugins' => array('some/plugin' => true)))); + $this->assertEquals(array('some/plugin' => true), $config->get('allow-plugins')); + + $config->merge(array('config' => array('allow-plugins' => array('another/plugin' => true)))); + $this->assertEquals(array('some/plugin' => true, 'another/plugin' => true), $config->get('allow-plugins')); + } + + public function testOverridesGlobalBooleanPluginsConfig() + { + $config = new Config(false); + $config->merge(array('config' => array('allow-plugins' => true))); + $this->assertEquals(true, $config->get('allow-plugins')); + + $config->merge(array('config' => array('allow-plugins' => array('another/plugin' => true)))); + $this->assertEquals(array('another/plugin' => true), $config->get('allow-plugins')); + } + + public function testAllowsAllPluginsFromLocalBoolean() + { + $config = new Config(false); + $config->merge(array('config' => array('allow-plugins' => array('some/plugin' => true)))); + $this->assertEquals(array('some/plugin' => true), $config->get('allow-plugins')); + + $config->merge(array('config' => array('allow-plugins' => true))); + $this->assertEquals(true, $config->get('allow-plugins')); + } }