diff --git a/lib/adminlib.php b/lib/adminlib.php index 66ad92bbd31..be24d9e2afe 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -2441,13 +2441,22 @@ class admin_setting_configfile extends admin_setting_configtext { '
'.$executable.'
', $this->description, true, '', $default, $query); } + /** - * checks if execpatch has been disabled in config.php + * Checks if execpatch has been disabled in config.php */ public function write_setting($data) { global $CFG; if (!empty($CFG->preventexecpath)) { - return ''; + if ($this->get_setting() === null) { + // Use default during installation. + $data = $this->get_defaultsetting(); + if ($data === null) { + $data = ''; + } + } else { + return ''; + } } return parent::write_setting($data); } diff --git a/lib/tests/admintree_test.php b/lib/tests/admintree_test.php index 82b5d177f54..24677af09ff 100644 --- a/lib/tests/admintree_test.php +++ b/lib/tests/admintree_test.php @@ -245,4 +245,57 @@ class core_admintree_testcase extends advanced_testcase { return $count; } + + public function test_preventexecpath() { + $this->resetAfterTest(); + + set_config('preventexecpath', 0); + set_config('execpath', null, 'abc_cde'); + $this->assertFalse(get_config('abc_cde', 'execpath')); + $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', '/xx/yy'); + $setting->write_setting('/oo/pp'); + $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath')); + + // Prevent changes. + set_config('preventexecpath', 1); + $setting->write_setting('/mm/nn'); + $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath')); + + // Use default in install. + set_config('execpath', null, 'abc_cde'); + $setting->write_setting('/mm/nn'); + $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath')); + + // Use empty value if no default. + $setting = new admin_setting_configexecutable('abc_cde/execpath', 'some desc', '', null); + set_config('execpath', null, 'abc_cde'); + $setting->write_setting('/mm/nn'); + $this->assertSame('', get_config('abc_cde', 'execpath')); + + // This also (most probably incorrectly) affects admin_setting_configfile. + + set_config('preventexecpath', 0); + set_config('execpath', null, 'abc_cde'); + $this->assertFalse(get_config('abc_cde', 'execpath')); + $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', '/xx/yy'); + $setting->write_setting('/oo/pp'); + $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath')); + + // Prevent changes. + set_config('preventexecpath', 1); + $setting->write_setting('/mm/nn'); + $this->assertSame('/oo/pp', get_config('abc_cde', 'execpath')); + + // Use default in install. + set_config('execpath', null, 'abc_cde'); + $setting->write_setting('/mm/nn'); + $this->assertSame('/xx/yy', get_config('abc_cde', 'execpath')); + + // Use empty value if no default. + $setting = new admin_setting_configfile('abc_cde/execpath', 'some desc', '', null); + set_config('execpath', null, 'abc_cde'); + $setting->write_setting('/mm/nn'); + $this->assertSame('', get_config('abc_cde', 'execpath')); + + } }