aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2018-10-18 14:24:31 +0100
committerStruan Donald <struan@exo.org.uk>2018-11-26 09:43:10 +0000
commitcd34f8336f9fe5b3f334d350027948dbddc04345 (patch)
tree78d0bda1681fd670d9e63971ee917b06ab069152
parentd191b211cc25c60d2a1464711ff4212f4d4a5e6e (diff)
[UK] server side cobrand specific validation
`report_check_for_errors` now fetches the cobrand for the current report and, if present, runs `report_validation` method from cobrand over the report.
-rw-r--r--perllib/FixMyStreet/Cobrand/BathNES.pm3
-rw-r--r--perllib/FixMyStreet/Cobrand/Bromley.pm10
-rw-r--r--perllib/FixMyStreet/Cobrand/Buckinghamshire.pm4
-rw-r--r--perllib/FixMyStreet/Cobrand/Lincolnshire.pm3
-rw-r--r--perllib/FixMyStreet/Cobrand/Oxfordshire.pm10
-rw-r--r--perllib/FixMyStreet/Cobrand/Rutland.pm10
-rw-r--r--perllib/FixMyStreet/Cobrand/UK.pm10
-rw-r--r--perllib/FixMyStreet/Roles/ConfirmValidation.pm39
-rw-r--r--t/Mock/MapIt.pm3
-rw-r--r--t/app/controller/report_new.t137
-rw-r--r--templates/web/base/report/new/form_user_loggedin.html3
-rw-r--r--templates/web/base/report/new/form_user_loggedout_by_email.html3
-rw-r--r--templates/web/bromley/report/new/form_user_loggedout_by_email.html3
13 files changed, 230 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/Cobrand/BathNES.pm b/perllib/FixMyStreet/Cobrand/BathNES.pm
index 187c2afbb..025648591 100644
--- a/perllib/FixMyStreet/Cobrand/BathNES.pm
+++ b/perllib/FixMyStreet/Cobrand/BathNES.pm
@@ -4,6 +4,9 @@ use parent 'FixMyStreet::Cobrand::Whitelabel';
use strict;
use warnings;
+use Moo;
+with 'FixMyStreet::Roles::ConfirmValidation';
+
use LWP::Simple;
use URI;
use Try::Tiny;
diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm
index 53fb9010c..11c685c91 100644
--- a/perllib/FixMyStreet/Cobrand/Bromley.pm
+++ b/perllib/FixMyStreet/Cobrand/Bromley.pm
@@ -10,6 +10,16 @@ sub council_area { return 'Bromley'; }
sub council_name { return 'Bromley Council'; }
sub council_url { return 'bromley'; }
+sub report_validation {
+ my ($self, $report, $errors) = @_;
+
+ if ( length( $report->detail ) > 1750 ) {
+ $errors->{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1750 );
+ }
+
+ return $errors;
+}
+
sub base_url {
my $self = shift;
return $self->next::method() if FixMyStreet->config('STAGING_SITE');
diff --git a/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm b/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm
index fb074da85..1b437b2c5 100644
--- a/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm
+++ b/perllib/FixMyStreet/Cobrand/Buckinghamshire.pm
@@ -4,11 +4,15 @@ use parent 'FixMyStreet::Cobrand::UKCouncils';
use strict;
use warnings;
+use Moo;
+with 'FixMyStreet::Roles::ConfirmValidation';
+
sub council_area_id { return 2217; }
sub council_area { return 'Buckinghamshire'; }
sub council_name { return 'Buckinghamshire County Council'; }
sub council_url { return 'buckinghamshire'; }
+
sub example_places {
return ( 'HP19 7QF', "Walton Road" );
}
diff --git a/perllib/FixMyStreet/Cobrand/Lincolnshire.pm b/perllib/FixMyStreet/Cobrand/Lincolnshire.pm
index 431314333..a4cee8a03 100644
--- a/perllib/FixMyStreet/Cobrand/Lincolnshire.pm
+++ b/perllib/FixMyStreet/Cobrand/Lincolnshire.pm
@@ -9,6 +9,9 @@ use URI;
use Try::Tiny;
use JSON::MaybeXS;
+use Moo;
+with 'FixMyStreet::Roles::ConfirmValidation';
+
sub council_area_id { return 2232; }
sub council_area { return 'Lincolnshire'; }
sub council_name { return 'Lincolnshire County Council'; }
diff --git a/perllib/FixMyStreet/Cobrand/Oxfordshire.pm b/perllib/FixMyStreet/Cobrand/Oxfordshire.pm
index 4d627c756..9c1e541d4 100644
--- a/perllib/FixMyStreet/Cobrand/Oxfordshire.pm
+++ b/perllib/FixMyStreet/Cobrand/Oxfordshire.pm
@@ -10,6 +10,16 @@ sub council_name { return 'Oxfordshire County Council'; }
sub council_url { return 'oxfordshire'; }
sub is_two_tier { return 1; }
+sub report_validation {
+ my ($self, $report, $errors) = @_;
+
+ if ( length( $report->detail ) > 1700 ) {
+ $errors->{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1700 );
+ }
+
+ return $errors;
+}
+
sub is_council_with_case_management {
# XXX Change this to return 1 when OCC FMSfC goes live.
return FixMyStreet->config('STAGING_SITE');
diff --git a/perllib/FixMyStreet/Cobrand/Rutland.pm b/perllib/FixMyStreet/Cobrand/Rutland.pm
index 6993b0964..af635ac59 100644
--- a/perllib/FixMyStreet/Cobrand/Rutland.pm
+++ b/perllib/FixMyStreet/Cobrand/Rutland.pm
@@ -9,6 +9,16 @@ sub council_area { return 'Rutland'; }
sub council_name { return 'Rutland County Council'; }
sub council_url { return 'rutland'; }
+sub report_validation {
+ my ($self, $report, $errors) = @_;
+
+ if ( length( $report->name ) > 40 ) {
+ $errors->{name} = sprintf( _('Names are limited to %d characters in length.'), 40 );
+ }
+
+ return $errors;
+}
+
sub open311_config {
my ($self, $row, $h, $params) = @_;
diff --git a/perllib/FixMyStreet/Cobrand/UK.pm b/perllib/FixMyStreet/Cobrand/UK.pm
index ebedf8711..1202d48a4 100644
--- a/perllib/FixMyStreet/Cobrand/UK.pm
+++ b/perllib/FixMyStreet/Cobrand/UK.pm
@@ -328,13 +328,9 @@ sub report_check_for_errors {
);
}
- if ( $report->bodies_str && $report->detail ) {
- # Custom character limit:
- if ( $report->to_body_named('Bromley') && length($report->detail) > 1750 ) {
- $errors{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1750 );
- } elsif ( $report->to_body_named('Oxfordshire') && length($report->detail) > 1700 ) {
- $errors{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), 1700 );
- }
+ my $cobrand = $self->get_body_handler_for_problem($report);
+ if ( $cobrand->can('report_validation') ) {
+ $cobrand->report_validation( $report, \%errors );
}
return %errors;
diff --git a/perllib/FixMyStreet/Roles/ConfirmValidation.pm b/perllib/FixMyStreet/Roles/ConfirmValidation.pm
new file mode 100644
index 000000000..5d407af6d
--- /dev/null
+++ b/perllib/FixMyStreet/Roles/ConfirmValidation.pm
@@ -0,0 +1,39 @@
+package FixMyStreet::Roles::ConfirmValidation;
+use Moo::Role;
+
+=head1 NAME
+
+FixMyStreet::Roles::ConfirmValidation - role for adding standard confirm validation
+
+=head1 SYNOPSIS
+
+This is applied to a Cobrand class to add validation of reports using standard
+Confirm field lengths.
+
+ use Moo;
+ with 'FixMyStreet::Roles::ConfirmValidation';
+
+=cut
+
+has max_report_length => ( is => 'ro', default => 2000 );
+
+sub report_validation {
+ my ($self, $report, $errors) = @_;
+
+ if ( length( $report->name ) > 50 ) {
+ $errors->{name} = sprintf( _('Names are limited to %d characters in length.'), 50 );
+ }
+
+ if ( length( $report->user->phone ) > 20 ) {
+ $errors->{phone} = sprintf( _('Phone numbers are limited to %s characters in length.'), 20 );
+ }
+
+ if ( length( $report->detail ) > $self->max_report_length ) {
+ $errors->{detail} = sprintf( _('Reports are limited to %s characters in
+ length. Please shorten your report'), $self->max_report_length );
+ }
+
+ return $errors;
+}
+
+1;
diff --git a/t/Mock/MapIt.pm b/t/Mock/MapIt.pm
index 0b9d398e8..f3f9f89b2 100644
--- a/t/Mock/MapIt.pm
+++ b/t/Mock/MapIt.pm
@@ -33,6 +33,9 @@ my @PLACES = (
[ '?', 51.754926, -1.256179, 2237, 'Oxfordshire County Council', 'CTY', 2421, 'Oxford City Council', 'DIS' ],
[ 'OX20 1SZ', 51.754926, -1.256179, 2237, 'Oxfordshire County Council', 'CTY', 2421, 'Oxford City Council', 'DIS' ],
[ 'OX16 9UP', 52.038712, -1.346397, 2237, 'Oxfordshire County Council', 'CTY', 2419, 'Cherwell District Council', 'DIS', 151767, "Banbury, Calthorpe & Easington", "DIW" ],
+ [ 'RG9 6TL', 51.561705, -0.868388, 2217, 'Buckinghamshire County Council', 'CTY'],
+ [ 'PE9 2GX', 52.656144, -0.502566, 2232, 'Lincolnshire County Council', 'CTY'],
+ [ 'LE15 0GJ', 52.670447, -0.727877, 2600, 'Rutland County Council', 'CTY'],
[ 'BR1 3UH', 51.4021, 0.01578, 2482, 'Bromley Council', 'LBO' ],
[ 'BR1 3UH', 51.402096, 0.015784, 2482, 'Bromley Council', 'LBO' ],
[ '?', 50.78301, -0.646929 ],
diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t
index 5f807f0e9..133dc8047 100644
--- a/t/app/controller/report_new.t
+++ b/t/app/controller/report_new.t
@@ -42,6 +42,10 @@ for my $body (
{ area_id => 2333, name => 'Hart Council' },
{ area_id => 2535, name => 'Sandwell Borough Council' },
{ area_id => 1000, name => 'Highways England' },
+ { area_id => 2217, name => 'Buckinghamshire County Council' },
+ { area_id => 2232, name => 'Lincolnshire County Council' },
+ { area_id => 2237, name => 'Oxfordshire County Council' },
+ { area_id => 2600, name => 'Rutland County Council' },
) {
my $body_obj = $mech->create_body_ok($body->{area_id}, $body->{name});
push @bodies, $body_obj;
@@ -104,6 +108,26 @@ my $contact11 = $mech->create_contact_ok(
category => 'Pothole',
email => 'pothole-1000@example.com',
);
+my $contact12 = $mech->create_contact_ok(
+ body_id => $body_ids{2217}, # Buckinghamshire
+ category => 'Street lighting',
+ email => 'streetlights-2217@example.com',
+);
+my $contact13 = $mech->create_contact_ok(
+ body_id => $body_ids{2232}, # Lincolnshire
+ category => 'Trees',
+ email => 'trees-2232@example.com',
+);
+my $contact14 = $mech->create_contact_ok(
+ body_id => $body_ids{2237}, # Oxfordshire
+ category => 'Trees',
+ email => 'trees-2247@example.com',
+);
+my $contact15 = $mech->create_contact_ok(
+ body_id => $body_ids{2600}, # Rutland
+ category => 'Trees',
+ email => 'trees-2600@example.com',
+);
# test that the various bit of form get filled in and errors correctly
# generated.
@@ -495,6 +519,117 @@ foreach my $test (
},
errors => [ "Please enter a subject" ],
},
+ {
+ msg => 'Bromley long detail',
+ pc => 'BR1 3UH',
+ fields => {
+ fms_extra_title => 'MR',
+ title => '',
+ detail => 'X' . 'x' x 1751,
+ photo1 => '',
+ photo2 => '',
+ photo3 => '',
+ name => 'Bob Example',
+ may_show_name => '1',
+ username => 'bob@example.com',
+ email => 'bob@example.com',
+ phone => '',
+ category => 'Trees',
+ password_sign_in => '',
+ password_register => '',
+ remember_me => undef,
+ },
+ changes => { },
+ errors => [ 'Please enter a subject', 'Reports are limited to 1750 characters in length. Please shorten your report' ],
+ },
+ {
+ msg => 'Oxfordshire long detail',
+ pc => 'OX20 1SZ',
+ fields => {
+ title => '',
+ detail => 'X' . 'x' x 1701,
+ photo1 => '',
+ photo2 => '',
+ photo3 => '',
+ name => 'Bob Example',
+ may_show_name => '1',
+ username => 'bob@example.com',
+ email => 'bob@example.com',
+ phone => '',
+ category => 'Trees',
+ password_sign_in => '',
+ password_register => '',
+ remember_me => undef,
+ },
+ changes => { },
+ errors => [ 'Please enter a subject', 'Reports are limited to 1700 characters in length. Please shorten your report' ],
+ },
+ {
+ msg => 'Lincolnshire long phone',
+ pc => 'PE9 2GX',
+ fields => {
+ title => '',
+ detail => 'Detail',
+ photo1 => '',
+ photo2 => '',
+ photo3 => '',
+ name => 'Bob Example',
+ may_show_name => '1',
+ username => 'bob@example.com',
+ email => 'bob@example.com',
+ phone => '123456789 12345678910',
+ category => 'Trees',
+ password_sign_in => '',
+ password_register => '',
+ remember_me => undef,
+ },
+ changes => { },
+ errors => [ 'Please enter a subject', 'Phone numbers are limited to 20 characters in length.' ],
+ },
+ {
+ msg => 'Buckinghamshire long name',
+ pc => 'RG9 6TL',
+ fields => {
+ title => '',
+ detail => '',
+ photo1 => '',
+ photo2 => '',
+ photo3 => '',
+ name => 'This is a very long name that should fail validation',
+ may_show_name => '1',
+ username => 'bob@example.com',
+ email => 'bob@example.com',
+ phone => '',
+ category => 'Street lighting',
+ password_sign_in => '',
+ password_register => '',
+ remember_me => undef,
+ },
+ changes => { },
+ errors => [ 'Please enter a subject', 'Please enter some details', 'Names are limited to 50 characters in length.' ],
+ },
+ {
+ msg => 'Rutland long name',
+ pc => 'LE15 0GJ',
+ fields => {
+ title => '',
+ detail => '',
+ photo1 => '',
+ photo2 => '',
+ photo3 => '',
+ name => 'This is a very long name that should fail validation',
+ may_show_name => '1',
+ username => 'bob@example.com',
+ email => 'bob@example.com',
+ phone => '',
+ category => 'Trees',
+ password_sign_in => '',
+ password_register => '',
+ remember_me => undef,
+ },
+ changes => { },
+ errors => [ 'Please enter a subject', 'Please enter some details', 'Names are limited to 40 characters in length.' ],
+ },
)
{
subtest "check form errors where $test->{msg}" => sub {
@@ -502,7 +637,7 @@ foreach my $test (
# submit initial pc form
FixMyStreet::override_config {
- ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
+ ALLOWED_COBRANDS => [ { fixmystreet => '.' }, 'bromley', 'oxfordshire', 'rutland', 'lincolnshire', 'buckinghamshire' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->submit_form_ok( { with_fields => { pc => $test->{pc} } },
diff --git a/templates/web/base/report/new/form_user_loggedin.html b/templates/web/base/report/new/form_user_loggedin.html
index 8c53b4365..f8e34a313 100644
--- a/templates/web/base/report/new/form_user_loggedin.html
+++ b/templates/web/base/report/new/form_user_loggedin.html
@@ -50,6 +50,9 @@
[% IF NOT c.user.phone_verified AND NOT c.cobrand.call_hook('disable_phone_number_entry') %]
<label for="form_phone">[% loc('Phone number (optional)') %]</label>
+ [% IF field_errors.phone %]
+ <p class='form-error'>[% field_errors.phone %]</p>
+ [% END %]
<input class="form-control" type="text" value="[% report.user.phone_display | html %]" name="phone" id="form_phone">
[% END %]
[% IF NOT c.user.email_verified %]
diff --git a/templates/web/base/report/new/form_user_loggedout_by_email.html b/templates/web/base/report/new/form_user_loggedout_by_email.html
index 73db38795..5171abd59 100644
--- a/templates/web/base/report/new/form_user_loggedout_by_email.html
+++ b/templates/web/base/report/new/form_user_loggedout_by_email.html
@@ -13,6 +13,9 @@
[% UNLESS c.cobrand.call_hook('disable_phone_number_entry') %]
<div id="js-hide-if-username-phone">
<label class="form-focus-hidden" for="form_phone">[% loc('Phone number (optional)') %]</label>
+ [% IF field_errors.phone %]
+ <p class='form-error'>[% field_errors.phone %]</p>
+ [% END %]
<input class="form-control form-focus-hidden" type="text" value="[% phone | html %]" name="phone" id="form_phone">
</div>
[% END %]
diff --git a/templates/web/bromley/report/new/form_user_loggedout_by_email.html b/templates/web/bromley/report/new/form_user_loggedout_by_email.html
index 7613ca339..8c772ac53 100644
--- a/templates/web/bromley/report/new/form_user_loggedout_by_email.html
+++ b/templates/web/bromley/report/new/form_user_loggedout_by_email.html
@@ -9,6 +9,9 @@
[% UNLESS c.cobrand.call_hook('disable_phone_number_entry') %]
<div id="js-hide-if-username-phone">
<label class="form-focus-hidden" for="form_phone">[% loc('Phone number (optional)') %]</label>
+ [% IF field_errors.phone %]
+ <p class='form-error'>[% field_errors.phone %]</p>
+ [% END %]
<input class="form-control form-focus-hidden" type="text" value="[% report.user.phone_display | html %]" name="phone" id="form_phone">
</div>
[% END %]