diff options
author | Dave Arter <davea@mysociety.org> | 2018-03-22 14:35:57 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2018-03-29 16:04:31 +0100 |
commit | 4893ad4538aa035a90979b85d45579592a72bf1e (patch) | |
tree | 06e73af6d5fd8df3686a9f0fd0e5478c528f9e39 | |
parent | bd23bc137f37a87147e4f2f52578d10bf81473b0 (diff) |
Add external_status_code to ResponseTemplate admin editing forms
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 19 | ||||
-rw-r--r-- | t/app/controller/admin/templates.t | 51 | ||||
-rw-r--r-- | templates/web/base/admin/template_edit.html | 20 | ||||
-rw-r--r-- | templates/web/base/admin/templates.html | 5 |
4 files changed, 87 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index c12fdf9b9..cf5ab9b10 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -1122,8 +1122,15 @@ sub template_edit : Path('templates') : Args(2) { $template->title( $c->get_param('title') ); $template->text( $c->get_param('text') ); $template->state( $c->get_param('state') ); + $template->external_status_code( $c->get_param('external_status_code') ); - $template->auto_response( $c->get_param('auto_response') && $template->state ? 1 : 0 ); + if ( $template->state && $template->external_status_code ) { + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{state} = _("State and external status code cannot be used simultaneously."); + $c->stash->{errors}->{external_status_code} = _("State and external status code cannot be used simultaneously."); + } + + $template->auto_response( $c->get_param('auto_response') && ( $template->state || $template->external_status_code ) ? 1 : 0 ); if ($template->auto_response) { my @check_contact_ids = @new_contact_ids; # If the new template has not specific categories (i.e. it @@ -1135,7 +1142,10 @@ sub template_edit : Path('templates') : Args(2) { my $query = { 'auto_response' => 1, 'contact.id' => [ @check_contact_ids, undef ], - 'me.state' => $template->state, + -or => { + $template->state ? ('me.state' => $template->state) : (), + $template->external_status_code ? ('me.external_status_code' => $template->external_status_code) : (), + }, }; if ($template->in_storage) { $query->{'me.id'} = { '!=', $template->id }; @@ -1143,9 +1153,8 @@ sub template_edit : Path('templates') : Args(2) { if ($c->stash->{body}->response_templates->search($query, { join => { 'contact_response_templates' => 'contact' }, })->count) { - $c->stash->{errors} = { - auto_response => _("There is already an auto-response template for this category/state.") - }; + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{auto_response} = _("There is already an auto-response template for this category/state."); } } diff --git a/t/app/controller/admin/templates.t b/t/app/controller/admin/templates.t index 179a1afcb..0d4430cad 100644 --- a/t/app/controller/admin/templates.t +++ b/t/app/controller/admin/templates.t @@ -180,4 +180,55 @@ subtest "all-category auto-response templates that duplicate a single category c is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added"; }; +subtest "auto-response templates that duplicate external_status_code can't be added" => sub { + $mech->delete_response_template($_) for $oxfordshire->response_templates; + my $template = $oxfordshire->response_templates->create({ + title => "Report fixed - potholes", + text => "Thank you for your report. This problem has been fixed.", + auto_response => 1, + external_status_code => '100', + }); + $template->contact_response_templates->find_or_create({ + contact_id => $oxfordshirecontact->id, + }); + is $oxfordshire->response_templates->count, 1, "Initial response template was created"; + + + $mech->log_in_ok( $superuser->email ); + $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" ); + + my $fields = { + title => "Report marked fixed - all cats", + text => "Thank you for your report. This problem has been fixed.", + auto_response => 'on', + external_status_code => '100', + }; + $mech->submit_form_ok( { with_fields => $fields } ); + is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected'; + $mech->content_contains( 'Please correct the errors below' ); + $mech->content_contains( 'There is already an auto-response template for this category/state.' ); + + is $oxfordshire->response_templates->count, 1, "Duplicate response template wasn't added"; +}; + +subtest "templates that set state and external_status_code can't be added" => sub { + $mech->delete_response_template($_) for $oxfordshire->response_templates; + $mech->log_in_ok( $superuser->email ); + $mech->get_ok( "/admin/templates/" . $oxfordshire->id . "/new" ); + + my $fields = { + title => "Report marked fixed - all cats", + text => "Thank you for your report. This problem has been fixed.", + auto_response => 'on', + state => 'fixed - council', + external_status_code => '100', + }; + $mech->submit_form_ok( { with_fields => $fields } ); + is $mech->uri->path, '/admin/templates/' . $oxfordshire->id . '/new', 'not redirected'; + $mech->content_contains( 'Please correct the errors below' ); + $mech->content_contains( 'State and external status code cannot be used simultaneously.' ); + + is $oxfordshire->response_templates->count, 0, "Invalid response template wasn't added"; +}; + done_testing(); diff --git a/templates/web/base/admin/template_edit.html b/templates/web/base/admin/template_edit.html index 3e436dbf9..428aa46ac 100644 --- a/templates/web/base/admin/template_edit.html +++ b/templates/web/base/admin/template_edit.html @@ -21,7 +21,7 @@ </div> <p> <strong>[% loc('Title:') %] </strong> - <input type="text" name="title" class="required form-control" size="30" value="[% rt.title| html %]"> + <input type="text" name="title" class="required form-control" size="30" value="[% rt.title | html %]"> </p> <div class="admin-hint"> @@ -31,7 +31,7 @@ </div> <p> <strong>[% loc('Text:') %] </strong> - <textarea class="form-control" name="text" class="required">[% rt.text |html %]</textarea> + <textarea class="form-control" name="text" class="required">[% rt.text | html %]</textarea> </p> <div class="admin-hint"> @@ -41,6 +41,9 @@ </div> [% INCLUDE 'admin/category-checkboxes.html' %] + [% IF errors.state %] + <div class="form-error">[% errors.state %]</div> + [% END %] <div class="admin-hint"> <p> [% loc('If you want to use this template to prefill the update field when changing a report’s state, select the state here.') %] @@ -51,6 +54,19 @@ [% INCLUDE 'admin/state_groups_select.html' current_state=rt.state include_empty=1 %] </p> + [% IF errors.external_status_code %] + <div class="form-error">[% errors.external_status_code %]</div> + [% END %] + <div class="admin-hint"> + <p> + [% loc('If you want to use this template to prefill the update field when a report’s <strong>external</strong> (e.g. Confirm) status code changes, enter the status code here.') %] + </p> + </div> + <p> + <label for="external_status_code">[% loc('External status code') %]</label> + <input type="text" name="external_status_code" class="form-control" size="30" value="[% rt.external_status_code | html %]"> + </p> + [% IF errors.auto_response %] <div class="form-error">[% errors.auto_response %]</div> [% END %] diff --git a/templates/web/base/admin/templates.html b/templates/web/base/admin/templates.html index 444f2734d..21e4eea25 100644 --- a/templates/web/base/admin/templates.html +++ b/templates/web/base/admin/templates.html @@ -23,7 +23,10 @@ [% END %] [% END %] </td> - <td> [% t.state | html %] </td> + <td> + [% IF t.state %][% t.state | html %][% END %] + [% IF t.external_status_code %][% t.external_status_code | html %] (external)[% END %] + </td> <td> [% IF t.auto_response %]X[% END %] </td> <td> <a href="[% c.uri_for('templates', body.id, t.id) %]" class="btn">[% loc('Edit') %]</a> </td> </tr> |