Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent creation of two templates with same title. #2471

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Releases

* Unreleased
- Bugfixes:
- Prevent creation of two templates with same title.

* v2.6 (3rd May 2019)
- New features:
Expand Down
9 changes: 9 additions & 0 deletions perllib/FixMyStreet/App/Controller/Admin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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') );
Expand Down
24 changes: 24 additions & 0 deletions t/app/controller/admin/templates.t
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
Expand Down
4 changes: 3 additions & 1 deletion templates/web/base/admin/template_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -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.') %]
Expand Down
3 changes: 3 additions & 0 deletions templates/web/zurich/admin/template_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ <h3> [% IF rt.id %]
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 %]">
Expand Down