Merge pull request #3349 from cziehr/master

Fix loading order of extra defaults settings in the Configuration Manager
This commit is contained in:
Andreas Gohr 2022-06-24 14:18:08 +02:00 committed by GitHub
commit 71ff00b097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 233 additions and 6 deletions

View File

@ -9,3 +9,5 @@ $conf['superuser'] = 'testuser'; //password: testpass
$conf['dnslookups'] = 0; //speedup tests $conf['dnslookups'] = 0; //speedup tests
$conf['updatecheck'] = 0; //speedup tests $conf['updatecheck'] = 0; //speedup tests
$conf['plugin']['testing']['second'] = 'Local setting';

View File

@ -0,0 +1,56 @@
<?php
class CascadeExtraDefaultsTest extends DokuWikiTest
{
protected $oldSetting = [];
public function setUp(): void
{
global $config_cascade;
$this->pluginsEnabled = [
'testing'
];
$out = "<?php\n/*\n * protected settings, cannot modified in the Config manager\n" .
" * Some test data */\n";
$out .= "\$conf['title'] = 'New default Title';\n";
$out .= "\$conf['tagline'] = 'New default Tagline';\n";
$out .= "\$conf['plugin']['testing']['schnibble'] = 1;\n";
$out .= "\$conf['plugin']['testing']['second'] = 'New default setting';\n";
$file = DOKU_CONF . 'otherdefaults.php';
file_put_contents($file, $out);
//store original settings
$this->oldSetting = $config_cascade['main']['default'];
//add second file with defaults, which override the defaults of DokuWiki
$config_cascade['main']['default'][] = $file;
parent::setUp();
}
public function testNewDefaults()
{
global $conf;
$this->assertEquals('New default Tagline', $conf['tagline'], 'new default value');
$testing = plugin_load('action', 'testing');
$this->assertEquals(1, $testing->getConf('schnibble'), 'new default value');
$this->assertEquals('My Test Wiki', $conf['title'], 'local value still overrides default');
$this->assertEquals('Local setting', $testing->getConf('second'), 'local value still overrides default');
}
public function tearDown(): void
{
global $config_cascade;
$config_cascade['main']['default'] = $this->oldSetting;
unlink(DOKU_CONF . 'otherdefaults.php');
parent::tearDown();
}
}

View File

@ -0,0 +1,37 @@
<?php
class CascadeNormalTest extends DokuWikiTest
{
public function setUp(): void
{
$this->pluginsEnabled = [
'testing'
];
parent::setUp();
}
public function testDefaults()
{
global $conf;
$this->assertEquals('start', $conf['start'], 'default value');
$this->assertEquals('', $conf['tagline'], 'default value');
$this->assertFalse(isset($conf['plugin']['testing']['schnibble']), 'not set before plugin call');
$testing = plugin_load('action', 'testing');
$this->assertEquals(0, $testing->getConf('schnibble'), 'default value');
}
public function testLocal()
{
global $conf;
$this->assertEquals('My Test Wiki', $conf['title'], 'overriden in local.php (values from Config manager)');
$testing = plugin_load('action', 'testing');
$this->assertEquals('Local setting', $testing->getConf('second'), 'overriden in local.php');
}
}

View File

@ -0,0 +1,46 @@
<?php
class CascadeProtectedTest extends DokuWikiTest
{
public function setUp(): void
{
global $config_cascade;
$this->pluginsEnabled = [
'testing'
];
$out = "<?php\n/*\n * protected settings, cannot modified in the Config manager\n" .
" * Some test data */\n";
$out .= "\$conf['title'] = 'Protected Title';\n";
$out .= "\$conf['tagline'] = 'Protected Tagline';\n";
$out .= "\$conf['plugin']['testing']['schnibble'] = 1;\n";
$out .= "\$conf['plugin']['testing']['second'] = 'Protected setting';\n";
file_put_contents(end($config_cascade['main']['protected']), $out);
parent::setUp();
}
public function testDefaults()
{
global $conf;
$this->assertEquals('Protected Title', $conf['title'], 'protected local value, overrides local');
$this->assertEquals('Protected Tagline', $conf['tagline'], 'protected local value, override default');
$testing = plugin_load('action', 'testing');
$this->assertEquals(1, $testing->getConf('schnibble'), 'protected local value, ');
$this->assertEquals('Protected setting', $testing->getConf('second'), 'protected local value');
}
public function tearDown(): void
{
global $config_cascade;
unlink(end($config_cascade['main']['protected']));
parent::tearDown();
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace dokuwiki\plugin\config\test;
use dokuwiki\plugin\config\core\ConfigParser;
use dokuwiki\plugin\config\core\Loader;
/**
* @group plugin_config
* @group admin_plugins
* @group plugins
* @group bundled_plugins
*/
class LoaderExtraDefaultsTest extends \DokuWikiTest
{
protected $pluginsEnabled = ['testing'];
protected $oldSetting = [];
public function setUp(): void
{
global $config_cascade;
$out = "<?php\n/*\n * protected settings, cannot modified in the Config manager\n" .
" * Some test data */\n";
$out .= "\$conf['title'] = 'New default Title';\n";
$out .= "\$conf['tagline'] = 'New default Tagline';\n";
$out .= "\$conf['plugin']['testing']['schnibble'] = 1;\n";
$out .= "\$conf['plugin']['testing']['second'] = 'New default setting';\n";
$file = DOKU_CONF . 'otherdefaults.php';
file_put_contents($file, $out);
//store original settings
$this->oldSetting = $config_cascade['main']['default'];
//add second file with defaults, which override the defaults of DokuWiki
$config_cascade['main']['default'][] = $file;
parent::setUp();
}
/**
* Ensure loading the defaults work, and that the extra default for plugins provided via an extra main default file
* override the plugin defaults as well
*/
public function testDefaultsOverwriting()
{
$loader = new Loader(new ConfigParser());
$conf = $loader->loadDefaults();
$this->assertTrue(is_array($conf));
// basic defaults
$this->assertArrayHasKey('title', $conf);
$this->assertEquals('New default Title', $conf['title']);
$this->assertEquals('New default Tagline', $conf['tagline']);
// plugin defaults
$this->assertArrayHasKey('plugin____testing____schnibble', $conf);
$this->assertEquals(1, $conf['plugin____testing____schnibble']);
$this->assertEquals('New default setting', $conf['plugin____testing____second']);
}
public function tearDown(): void
{
global $config_cascade;
$config_cascade['main']['default'] = $this->oldSetting;
unlink(DOKU_CONF . 'otherdefaults.php');
parent::tearDown();
}
}

View File

@ -53,6 +53,7 @@ class LoaderTest extends \DokuWikiTest {
// plugin defaults // plugin defaults
$this->assertArrayHasKey('plugin____testing____schnibble', $conf); $this->assertArrayHasKey('plugin____testing____schnibble', $conf);
$this->assertEquals(0, $conf['plugin____testing____schnibble']); $this->assertEquals(0, $conf['plugin____testing____schnibble']);
$this->assertEquals('Default value', $conf['plugin____testing____second']);
} }
/** /**

View File

@ -77,13 +77,14 @@ class Loader {
* *
* @return array * @return array
*/ */
public function loadDefaults() { public function loadDefaults()
// load main files {
global $config_cascade;
$conf = $this->loadConfigs($config_cascade['main']['default']); // initialize array
$conf = array();
// plugins // plugins
foreach($this->plugins as $plugin) { foreach ($this->plugins as $plugin) {
$conf = array_merge( $conf = array_merge(
$conf, $conf,
$this->loadExtensionConf( $this->loadExtensionConf(
@ -104,7 +105,12 @@ class Loader {
) )
); );
return $conf; // load main files
global $config_cascade;
return array_merge(
$conf,
$this->loadConfigs($config_cascade['main']['default'])
);
} }
/** /**

View File

@ -5,3 +5,5 @@
* They don't do anything and are just there for testing config reading * They don't do anything and are just there for testing config reading
*/ */
$conf['schnibble'] = 0; $conf['schnibble'] = 0;
$conf['second'] = 'Default value';

View File

@ -5,3 +5,4 @@
* They don't do anything and are just there for testing config reading * They don't do anything and are just there for testing config reading
*/ */
$meta['schnibble'] = array('onoff'); $meta['schnibble'] = array('onoff');
$meta['second'] = array('string');