diff options
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 53 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 18 | ||||
-rw-r--r-- | perllib/Utils.pm | 70 | ||||
-rw-r--r-- | t/app/controller/report_updates.t | 15 |
4 files changed, 94 insertions, 62 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 2c1f65d96..4c2264c9f 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -584,8 +584,8 @@ sub process_user : Private { || $c->model('DB::User')->find_or_new( { email => $email } ); # set the user's name and phone (if given) - $report_user->name( _trim_text( $params{name} ) ); - $report_user->phone( _trim_text( $params{phone} ) ) if $params{phone}; + $report_user->name( Utils::trim_text( $params{name} ) ); + $report_user->phone( Utils::trim_text( $params{phone} ) ) if $params{phone}; $c->stash->{report_user} = $report_user; @@ -600,49 +600,6 @@ provided) returns undef. =cut -# args: allow_multiline => bool - strips out "\n\n" linebreaks -sub _cleanup_text { - my $input = shift || ''; - my $args = shift || {}; - - # lowercase everything if looks like it might be SHOUTING - $input = lc $input if $input !~ /[a-z]/; - - # clean up language and tradmarks - for ($input) { - - # shit -> poo - s{\bdog\s*shit\b}{dog poo}ig; - - # 'portakabin' to '[portable cabin]' (and variations) - s{\b(porta)\s*([ck]abin|loo)\b}{[$1ble $2]}ig; - s{kabin\]}{cabin\]}ig; - } - - # Remove unneeded whitespace - my @lines = grep { m/\S/ } split m/\n\n/, $input; - for (@lines) { - $_ = _trim_text($_); - $_ = ucfirst $_; # start with capital - } - - my $join_char = $args->{allow_multiline} ? "\n\n" : " "; - $input = join $join_char, @lines; - - return $input; -} - -sub _trim_text { - my $input = shift; - for ($input) { - last unless $_; - s{\s+}{ }g; # all whitespace to single space - s{^ }{}; # trim leading - s{ $}{}; # trim trailing - } - return $input; -} - sub process_report : Private { my ( $self, $c ) = @_; @@ -674,12 +631,12 @@ sub process_report : Private { $report->anonymous( $params{may_show_name} ? 0 : 1 ); # clean up text before setting - $report->title( _cleanup_text( $params{title} ) ); + $report->title( Utils::cleanup_text( $params{title} ) ); $report->detail( - _cleanup_text( $params{detail}, { allow_multiline => 1 } ) ); + Utils::cleanup_text( $params{detail}, { allow_multiline => 1 } ) ); # set these straight from the params - $report->name( _trim_text( $params{name} ) ); + $report->name( Utils::trim_text( $params{name} ) ); $report->category( _ $params{category} ); my $mapit_query = diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 105bf9993..31478df29 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -5,6 +5,7 @@ use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } use Path::Class; +use Utils; =head1 NAME @@ -116,7 +117,7 @@ sub process_user : Private { my $update_user = $c->model('DB::User')->find_or_new( { email => $email } ); # set the user's name if they don't have one - $update_user->name( _trim_text( $params{name} ) ) + $update_user->name( Utils::trim_text( $params{name} ) ) unless $update_user->name; $c->stash->{update_user} = $update_user; @@ -140,10 +141,13 @@ sub process_update : Private { my %params = # map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed' ); + $params{update} = + Utils::cleanup_text( $params{update}, { allow_multiline => 1 } ); + my $update = $c->model('DB::Comment')->new( { text => $params{update}, - name => _trim_text( $params{name} ), + name => Utils::trim_text( $params{name} ), problem => $c->stash->{problem}, user => $c->stash->{update_user}, state => 'unconfirmed', @@ -159,16 +163,6 @@ sub process_update : Private { return 1; } -sub _trim_text { - my $input = shift; - for ($input) { - last unless $_; - s{\s+}{ }g; # all whitespace to single space - s{^ }{}; # trim leading - s{ $}{}; # trim trailing - } - return $input; -} =head2 check_for_errors diff --git a/perllib/Utils.pm b/perllib/Utils.pm index c16a02cd4..c267bbea0 100644 --- a/perllib/Utils.pm +++ b/perllib/Utils.pm @@ -136,4 +136,74 @@ sub london_categories { }; } +=head2 trim_text + + my $text = trim_text( $text_to_trim ); + +Strip leading and trailing white space from a string. Also reduces all +white space to a single space. + +Trim + +=cut + +sub trim_text { + my $input = shift; + for ($input) { + last unless $_; + s{\s+}{ }g; # all whitespace to single space + s{^ }{}; # trim leading + s{ $}{}; # trim trailing + } + return $input; +} + + +=head2 cleanup_text + +Tidy up text including removing contentious phrases, +SHOUTING and new lines and adding sentence casing. Takes an optional HASHREF +of args as follows. + +=over + +=item allow_multiline + +Do not flatten down to a single line if true. + +=back + +=cut + +sub cleanup_text { + my $input = shift || ''; + my $args = shift || {}; + + # lowercase everything if looks like it might be SHOUTING + $input = lc $input if $input !~ /[a-z]/; + + # clean up language and tradmarks + for ($input) { + + # shit -> poo + s{\bdog\s*shit\b}{dog poo}ig; + + # 'portakabin' to '[portable cabin]' (and variations) + s{\b(porta)\s*([ck]abin|loo)\b}{[$1ble $2]}ig; + s{kabin\]}{cabin\]}ig; + } + + # Remove unneeded whitespace + my @lines = grep { m/\S/ } split m/\n\n/, $input; + for (@lines) { + $_ = trim_text($_); + $_ = ucfirst $_; # start with capital + } + + my $join_char = $args->{allow_multiline} ? "\n\n" : " "; + $input = join $join_char, @lines; + + return $input; +} + 1; diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t index 182f3a244..f1717a64e 100644 --- a/t/app/controller/report_updates.t +++ b/t/app/controller/report_updates.t @@ -258,7 +258,7 @@ subtest "submit an update for a non registered user" => sub { ok $update, 'found update in database'; is $update->state, 'unconfirmed', 'update unconfirmed'; is $update->user->email, 'unregistered@example.com', 'update email'; - is $update->text, 'update from an unregistered user', 'update text'; + is $update->text, 'Update from an unregistered user', 'update text'; is $add_alerts, 0, 'do not sign up for alerts'; }; @@ -269,6 +269,9 @@ for my $test ( rznvy => 'test@example.com', update => 'update from a registered user' }, + changed => { + update => 'Update from a registered user' + }, alert => 0, }, { @@ -278,6 +281,9 @@ for my $test ( update => 'update from a registered user', add_alert => 1, }, + changed => { + update => 'Update from a registered user' + }, alert => 1, }, ) { @@ -303,9 +309,14 @@ for my $test ( $mech->email_count_is(0); + my $results = { + %{ $test->{fields} }, + %{ $test->{changed} }, + }; + my $update = $report->comments->first; ok $update, 'found update'; - is $update->text, $test->{fields}->{update}, 'update text'; + is $update->text, $results->{update}, 'update text'; is $update->user->email, 'test@example.com', 'update user'; is $update->state, 'confirmed', 'update confirmed'; |