diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-05-03 13:31:57 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-05-07 15:01:07 +0100 |
commit | 0236d3a37da94e06e9e6208f50f6c0b9388feb7c (patch) | |
tree | 53a206661f0b46303faaa3cf1e77c748f59b1621 | |
parent | b8fd2a09332798214188cbedecb85294645c06dc (diff) |
Prevent creation of two templates with same title.
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 9 | ||||
-rw-r--r-- | t/app/controller/admin/templates.t | 24 | ||||
-rw-r--r-- | templates/web/base/admin/template_edit.html | 4 | ||||
-rw-r--r-- | templates/web/zurich/admin/template_edit.html | 3 |
5 files changed, 41 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c07db815f..ba0e8ae95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## Releases * Unreleased + - Bugfixes: + - Prevent creation of two templates with same title. * v2.6 (3rd May 2019) - New features: diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 2f4669456..05ac48c8c 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -782,6 +782,15 @@ sub template_edit : Path('templates') : Args(2) { } $template->title( $c->get_param('title') ); + my $query = { title => $template->title }; + if ($template->in_storage) { + $query->{id} = { '!=', $template->id }; + } + if ($c->stash->{body}->response_templates->search($query)->count) { + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{title} = _("There is already a template with that title."); + } + $template->text( $c->get_param('text') ); $template->state( $c->get_param('state') ); $template->external_status_code( $c->get_param('external_status_code') ); diff --git a/t/app/controller/admin/templates.t b/t/app/controller/admin/templates.t index 0d4430cad..6944f4b04 100644 --- a/t/app/controller/admin/templates.t +++ b/t/app/controller/admin/templates.t @@ -66,6 +66,30 @@ subtest "response templates can be added" => sub { is $oxfordshire->response_templates->count, 1, "Response template was added"; }; +subtest "but not another with the same title" => sub { + my $fields = { + title => "Report acknowledgement", + text => "Another report acknowledgement.", + auto_response => undef, + "contacts[".$oxfordshirecontact->id."]" => 1, + }; + my $list_url = "/admin/templates/" . $oxfordshire->id; + $mech->get_ok( "$list_url/new" ); + $mech->submit_form_ok( { with_fields => $fields } ); + is $mech->uri->path, "$list_url/new", 'not redirected'; + $mech->content_contains( 'Please correct the errors below' ); + $mech->content_contains( 'There is already a template with that title.' ); + + my @ts = $oxfordshire->response_templates->all; + is @ts, 1, "No new response template was added"; + + my $url = "$list_url/" . $ts[0]->id; + $mech->get_ok($url); + $mech->submit_form_ok( { with_fields => $fields } ); + is $mech->uri->path, $list_url, 'redirected'; + is $oxfordshire->response_templates->count, 1, "No new response template was added"; +}; + subtest "response templates are included on page" => sub { FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'oxfordshire' ], diff --git a/templates/web/base/admin/template_edit.html b/templates/web/base/admin/template_edit.html index 2945e36c5..87218c7dd 100644 --- a/templates/web/base/admin/template_edit.html +++ b/templates/web/base/admin/template_edit.html @@ -13,7 +13,9 @@ <p class="error">[% loc('Please correct the errors below') %]</p> [% END %] - + [% IF errors.title %] + <div class="form-error">[% errors.title %]</div> + [% END %] <div class="admin-hint"> <p> [% loc('This is a <strong>private</strong> name for this template so you can identify it when updating reports or editing in the admin.') %] diff --git a/templates/web/zurich/admin/template_edit.html b/templates/web/zurich/admin/template_edit.html index e954d566c..b6f68106c 100644 --- a/templates/web/zurich/admin/template_edit.html +++ b/templates/web/zurich/admin/template_edit.html @@ -16,6 +16,9 @@ accept-charset="utf-8" class="validate"> + [% IF errors.title %] + <div class="form-error">[% errors.title %]</div> + [% END %] <p> <strong>[% loc('Title:') %] </strong> <input type="text" name="title" class="form-control required" size="30" value="[% rt.title| html %]"> |