From 78e70c7d3a4a14300b291be974cdde1e70674ca1 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 13 May 2013 14:19:13 -0400
Subject: [PATCH] Resolving VUFIND-752 (\VuFind\Config\Writer is confused by
 duplicate values). Expanded test suite to match.

---
 module/VuFind/src/VuFind/Config/Writer.php    | 44 +++++++++----------
 .../unit-tests/src/Config/WriterTest.php      | 28 ++++++++++++
 2 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/module/VuFind/src/VuFind/Config/Writer.php b/module/VuFind/src/VuFind/Config/Writer.php
index 360938b8801..6cbc5551ea7 100644
--- a/module/VuFind/src/VuFind/Config/Writer.php
+++ b/module/VuFind/src/VuFind/Config/Writer.php
@@ -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;
                 }
             }
 
diff --git a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
index 497394503ce..9cc770be200 100644
--- a/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
+++ b/module/VuFind/tests/unit-tests/src/Config/WriterTest.php
@@ -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
-- 
GitLab