From 389e185635f97865a7dbfefdec30ebe64cd1f45a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= <grosse@cosmocode.de>
Date: Thu, 26 Oct 2017 11:50:12 +0200
Subject: [PATCH] =?UTF-8?q?fix:=20prevent=20two=20selected=20options,=20b/?=
 =?UTF-8?q?c=20apparently=20'String'=20=3D=3D=200=20=20=F0=9F=A4=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

There was a bug, where the options array ['Auto', 0, 1] would result in
HTML option tags where both the 'Auto' and the 0 option were selected.
---
 _test/tests/inc/form/dropdownelement.test.php | 15 +++++++++++++++
 inc/Form/OptGroup.php                         |  2 +-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/_test/tests/inc/form/dropdownelement.test.php b/_test/tests/inc/form/dropdownelement.test.php
index 3aa0f16e4..5f6b35e75 100644
--- a/_test/tests/inc/form/dropdownelement.test.php
+++ b/_test/tests/inc/form/dropdownelement.test.php
@@ -129,6 +129,21 @@ class form_dropdownelement_test extends DokuWikiTest {
         $this->assertEquals('label of third option', $selected->text());
     }
 
+    /**
+     * Prevent double select that might occur because `'Auto' == 0` is true
+     */
+    public function test_doubleselect() {
+        $form = new Form\Form();
+        $form->addDropdown('foo', ['Auto', 0, 1]);
+
+        $html = $form->toHTML();
+
+        $pq = phpQuery::newDocumentXHTML($html);
+        $selected = $pq->find('option[selected=selected]');
+        $this->assertEquals(1, $selected->length);
+        $this->assertEquals('Auto', $selected->text());
+    }
+
     /**
      * Ensure that there is always only a single one selected option
      */
diff --git a/inc/Form/OptGroup.php b/inc/Form/OptGroup.php
index dbc0a115c..791f0b3f6 100644
--- a/inc/Form/OptGroup.php
+++ b/inc/Form/OptGroup.php
@@ -88,7 +88,7 @@ class OptGroup extends Element {
     protected function renderOptions() {
         $html = '';
         foreach($this->options as $key => $val) {
-            $selected = ($key == $this->value) ? ' selected="selected"' : '';
+            $selected = ((string)$key === (string)$this->value) ? ' selected="selected"' : '';
             $attrs = '';
             if (!empty($val['attrs']) && is_array($val['attrs'])) {
                 $attrs = buildAttributes($val['attrs']);
-- 
GitLab