Skip to content

Commit

Permalink
Add site contact setting tests, fix results.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 26, 2023
1 parent 89f4523 commit 14cff4d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/settings/s_sitecontact.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class SiteContact_SettingParser extends SettingParser {
private $updated = false;

const EMAIL_PLACEHOLDER = "[email protected]";
const NAME_PLACEHOLDER = "Your Name";

static function print_site_contact(SettingValues $sv) {
$sv->print_entry_group("site_contact_email", null, [
"hint" => "The site contact is the contact point for users if something goes wrong. It defaults to the chair."
Expand All @@ -15,7 +18,7 @@ static function print_site_contact(SettingValues $sv) {
/** @param string $v
* @param Si $si */
static private function cleanstr($v, $si) {
$iv = $si->name === "site_contact_email" ? "[email protected]" : "Your Name";
$iv = $si->name === "site_contact_email" ? self::EMAIL_PLACEHOLDER : self::NAME_PLACEHOLDER;
return $v !== $iv ? $v : "";
}

Expand All @@ -40,11 +43,7 @@ function set_oldv(Si $si, SettingValues $sv) {

function apply_req(Si $si, SettingValues $sv) {
if (($creqv = $sv->base_parse_req($si)) !== null) {
$creqv = self::cleanstr($creqv, $si);
if ($creqv === "") {
$creqv = self::basev($sv, $si) ?? "";
}
$sv->save($si, $creqv);
$sv->save($si, self::cleanstr($creqv, $si));
$sv->request_store_value($si);
}
return true;
Expand All @@ -64,24 +63,24 @@ function store_value(Si $si, SettingValues $sv) {
$newemail = $sv->newv("site_contact_email") ?? "";
$oldemail = self::basev($sv, "site_contact_email");
if ($newemail !== ""
&& $newemail !== $oldemail
&& $newemail !== self::EMAIL_PLACEHOLDER
&& strcasecmp($newemail, $defuser->email) !== 0) {
return;
}
// and name is default contact name,
$newname = $sv->newv("site_contact_name") ?? "";
$oldname = self::basev($sv, "site_contact_name");
if ($newname !== ""
&& $newname !== $oldname
&& $newname !== self::NAME_PLACEHOLDER
&& $newname !== $defuser->name()) {
return;
}
// save contactName and contactEmail as empty strings
if ($oldemail === "[email protected]") {
if ($oldemail === self::EMAIL_PLACEHOLDER) {
$sv->save("site_contact_email", $oldemail);
} else {
$sv->save("site_contact_email", "");
}
$sv->save("site_contact_name", $oldname);
$sv->save("site_contact_name", $oldname ?? "");
}
}
117 changes: 117 additions & 0 deletions test/t_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1574,4 +1574,121 @@ function test_intrinsic_vs_wizard_settings() {
xassert_eqq($this->conf->setting("ioptions"), null);

}

function test_site_contact() {
$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Eddie Kohler");
xassert_eqq($sc->email, "[email protected]");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"Jane Chair","site_contact_email":"chair@_.com"}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Jane Chair");
xassert_eqq($sc->email, "chair@_.com");
xassert_eqq($this->conf->opt("contactEmail"), "");
xassert_eqq($this->conf->setting_data("opt.contactEmail"), "");

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"Eddie Kohler","site_contact_email":"[email protected]"}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Eddie Kohler");
xassert_eqq($sc->email, "[email protected]");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);
unset($this->conf->opt_override["contactName"]);
unset($this->conf->opt_override["contactEmail"]);
}

function test_site_contact_empty_defaults() {
$this->conf->set_opt("contactName", "Your Name");
$this->conf->set_opt("contactEmail", "[email protected]");
xassert(!array_key_exists("contactName", $this->conf->opt_override));
xassert(!array_key_exists("contactEmail", $this->conf->opt_override));
xassert_eqq($this->conf->setting("opt.contactName"), null);
xassert_eqq($this->conf->setting("opt.contactEmail"), null);

$this->conf->refresh_options();
$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Jane Chair");
xassert_eqq($sc->email, "chair@_.com");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"Jane Chair","site_contact_email":"chair@_.com"}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Jane Chair");
xassert_eqq($sc->email, "chair@_.com");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"Eddie Kohler","site_contact_email":"[email protected]"}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Eddie Kohler");
xassert_eqq($sc->email, "[email protected]");
xassert_eqq($this->conf->setting_data("opt.contactName"), "Eddie Kohler");
xassert_eqq($this->conf->setting_data("opt.contactEmail"), "[email protected]");

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"","site_contact_email":""}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Jane Chair");
xassert_eqq($sc->email, "chair@_.com");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);

$sv = new SettingValues($this->u_chair);
$sv->add_json_string('{"site_contact_name":"Your Name","site_contact_email":"[email protected]"}');
xassert($sv->execute());

$dsc = $this->conf->default_site_contact();
$sc = $this->conf->site_contact();
xassert_eqq($dsc->name(), "Jane Chair");
xassert_eqq($dsc->email, "chair@_.com");
xassert_eqq($sc->name(), "Jane Chair");
xassert_eqq($sc->email, "chair@_.com");
xassert_eqq($this->conf->setting_data("opt.contactName"), null);
xassert_eqq($this->conf->setting_data("opt.contactEmail"), null);

$this->conf->set_opt("contactName", "Eddie Kohler");
$this->conf->set_opt("contactEmail", "[email protected]");
$this->conf->save_setting("opt.contactName", null);
$this->conf->save_refresh_setting("opt.contactEmail", null);
unset($this->conf->opt_override["contactName"]);
unset($this->conf->opt_override["contactEmail"]);
}
}

0 comments on commit 14cff4d

Please sign in to comment.