summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--giglogadmin.php1
-rw-r--r--includes/view-helpers/select_field.php26
-rw-r--r--tests/SelectFieldTest.php53
3 files changed, 80 insertions, 0 deletions
diff --git a/giglogadmin.php b/giglogadmin.php
index c8ae9b0..c35c470 100644
--- a/giglogadmin.php
+++ b/giglogadmin.php
@@ -33,6 +33,7 @@ if ( !class_exists( 'GiglogAdmin_Plugin' ) ) {
require_once __DIR__ . '/includes/admin/helpfiles/instrunctions.php';
require_once __DIR__ . '/includes/admin/helpfiles/instr_reviewers.php';
require_once __DIR__ . '/includes/admin/helpfiles/instr_photog.php';
+ require_once __DIR__ . '/includes/view-helpers/select_field.php';
class GiglogAdmin_Plugin
{
diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php
new file mode 100644
index 0000000..7cff3ef
--- /dev/null
+++ b/includes/view-helpers/select_field.php
@@ -0,0 +1,26 @@
+<?php
+// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
+// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+namespace EternalTerror\ViewHelpers;
+
+/**
+ * Return HTML code for a selction box with the given options and preselected value.
+ *
+ * @param string $name The name attribute for the selection box
+ * @param array $opts The options as arrays of [value, label] pairs
+ * @param mixed|int $selected The value of the preselected option, or null if no
+ * option is preselected.
+ * @return string
+ */
+function select_field(string $name, ?array $opts = [], $selected = null) : string
+{
+ $body = '';
+ foreach ($opts as $opt) {
+ $sel = selected($selected, $opt[0], false);
+ $body .= "<option value=\"{$opt[0]}\"{$sel}>{$opt[1]}</option>";
+ }
+ return "<select name=\"{$name}\">{$body}</select>";
+}
diff --git a/tests/SelectFieldTest.php b/tests/SelectFieldTest.php
new file mode 100644
index 0000000..1c1a8c2
--- /dev/null
+++ b/tests/SelectFieldTest.php
@@ -0,0 +1,53 @@
+<?php
+// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
+// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+declare(strict_types=1);
+
+use \EternalTerror\ViewHelpers;
+
+final class SelectFieldTest extends WP_UnitTestCase
+{
+ public function testEmptySelectField()
+ {
+ $res = ViewHelpers\select_field("fooselect");
+ $this->assertStringStartsWith('<select name="fooselect">', $res);
+ $this->assertStringEndsWith('</select>', $res);
+ }
+
+ public function testSelectFieldWithOneOption()
+ {
+ $res = ViewHelpers\select_field("fooselect", [[42, 'An option']]);
+ $this->assertStringStartsWith('<select name="fooselect">', $res);
+ $this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="42">An option</option>', $res);
+ }
+
+ public function testSelectFieldWithMultipleOption()
+ {
+ $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']];
+
+ $res = ViewHelpers\select_field("fooselect", $opts);
+
+ $this->assertStringStartsWith('<select name="fooselect">', $res);
+ $this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="42">An option</option>', $res);
+ $this->assertStringContainsString('<option value="666">Another option</option>', $res);
+ $this->assertStringContainsString('<option value="foo">A foo option</option>', $res);
+ }
+
+ public function testSelectFieldWithSelectedOption()
+ {
+ $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']];
+
+ $res = ViewHelpers\select_field("fooselect", $opts, 666);
+
+ $this->assertStringStartsWith('<select name="fooselect">', $res);
+ $this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="42">An option</option>', $res);
+ $this->assertStringContainsString('<option value="666" selected=\'selected\'>Another option</option>', $res);
+ $this->assertStringContainsString('<option value="foo">A foo option</option>', $res);
+ }
+}