Skip to content
Snippets Groups Projects
Commit 78e70c7d authored by Demian Katz's avatar Demian Katz
Browse files

Resolving VUFIND-752 (\VuFind\Config\Writer is confused by duplicate values).

Expanded test suite to match.
parent 7887c0cd
Branches
Tags
No related merge requests found
......@@ -100,32 +100,28 @@ class Writer
// Process one line at a time...
foreach ($lines as $line) {
// Once the setting is set, we can stop doing fancy processing -- it's
// just a matter of writing lines through unchanged:
if (!$settingSet) {
// Separate comments from content:
$parts = explode(';', trim($line), 2);
$content = trim($parts[0]);
$comment = isset($parts[1]) ? $parts[1] : '';
// Separate comments from content:
$parts = explode(';', trim($line), 2);
$content = trim($parts[0]);
$comment = isset($parts[1]) ? $parts[1] : '';
// Is this a section heading?
if (preg_match('/^\[(.+)\]$/', trim($content), $matches)) {
// If we just left the target section and didn't find the
// desired setting, we should write it to the end.
if ($currentSection == $section && !$settingSet) {
$line = $setting . ' = "' . $value . '"' . "\n\n" . $line;
$settingSet = true;
}
$currentSection = $matches[1];
} else if (strstr($content, '=')) {
list($key, $oldValue) = explode('=', $content, 2);
if ($currentSection == $section && trim($key) == $setting) {
$line = $setting . ' = "' . $value . '"';
if (!empty($comment)) {
$line .= ' ;' . $comment;
}
$settingSet = true;
// Is this a section heading?
if (preg_match('/^\[(.+)\]$/', trim($content), $matches)) {
// If we just left the target section and didn't find the
// desired setting, we should write it to the end.
if ($currentSection == $section && !$settingSet) {
$line = $setting . ' = "' . $value . '"' . "\n\n" . $line;
$settingSet = true;
}
$currentSection = $matches[1];
} else if (strstr($content, '=')) {
list($key, $oldValue) = explode('=', $content, 2);
if ($currentSection == $section && trim($key) == $setting) {
$line = $setting . ' = "' . $value . '"';
if (!empty($comment)) {
$line .= ' ;' . $comment;
}
$settingSet = true;
}
}
......
......@@ -110,4 +110,32 @@ class WriterTest extends \VuFindTest\Unit\TestCase
$this->assertEquals('val2', $ini['test']['key2']);
$this->assertEquals('val3', $ini['test']['key3']);
}
/**
* Test setting a duplicate value.
*
* @return void
*/
public function testSetDuplicateValue()
{
$cfg = "[test]\nkey1=val1\nkey1=val2\n";
$test = new Writer('fake.ini', $cfg);
$test->set('test', 'key1', 'val1b');
$ini = parse_ini_string($test->getContent(), true);
$this->assertEquals('val1b', $ini['test']['key1']);
}
/**
* Test that we add a missing section at the end if necessary.
*
* @return void
*/
public function testAddMissingSection()
{
$cfg = "[test]\nkey1=val1\n";
$test = new Writer('fake.ini', $cfg);
$test->set('test2', 'key1', 'val1b');
$ini = parse_ini_string($test->getContent(), true);
$this->assertEquals('val1b', $ini['test2']['key1']);
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment