Skip to content

Commit

Permalink
Trim whitespace on external status codes for response templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nephila-nacrea committed Jun 27, 2022
1 parent dbb5c3b commit 0693a5a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
36 changes: 35 additions & 1 deletion perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,39 @@ __PACKAGE__->has_many(

__PACKAGE__->many_to_many( contacts => 'contact_response_templates', 'contact' );

# You can replace this text with custom code or comments, and it will be preserved on regeneration
use Utils;

# Trim whitespace for external_status_code

sub insert {
my ( $self, @args ) = @_;

$self->external_status_code(
Utils::trim_text( $self->external_status_code ) )
if $self->external_status_code;

$self->next::method(@args);

return $self;
}

sub update {
my ( $self, $columns ) = @_;

# If update has been passed an external_status_code argument, trim that.
# Otherwise, fall back to external_status_code on object.
if ( $columns && $columns->{external_status_code} ) {
$columns->{external_status_code}
= Utils::trim_text( $columns->{external_status_code} );
}
elsif ( $self->external_status_code ) {
$self->external_status_code(
Utils::trim_text( $self->external_status_code ) );
}

$self->next::method($columns);

return $self;
}

1;
54 changes: 53 additions & 1 deletion t/app/model/responsetemplate.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $t2->add_to_contacts($c2);

my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $body->id ] } )->all;

subtest 'by_categories returns allresponse templates grouped by category' => sub {
subtest 'by_categories returns all response templates grouped by category' => sub {
my $templates = FixMyStreet::DB->resultset('ResponseTemplate')->by_categories(\@contacts, body_id => $body->id);
my $potholes = JSON::MaybeXS->new->decode($templates->{Potholes});
my $graffiti = JSON::MaybeXS->new->decode($templates->{Graffiti});
Expand All @@ -25,6 +25,58 @@ subtest 'by_categories returns allresponse templates grouped by category' => sub
is scalar @$graffiti, 2, 'Graffiti has 2 templates';
is $graffiti->[0]->{state}, 'investigating', 'Graffiti first template has right state';
is $potholes->[0]->{id}, 'Text 1 ⛄', 'Pothole first template has right text';
is $graffiti->[1]->{id}, $potholes->[1]->{id},
'3rd template applies to both graffiti and potholes';
# is $graffiti->[1]->external_status_code, '060',
# 'Whitespace trimmed from external_status_code';
};

subtest 'Trim whitespace on external_status_code' => sub {
my $t_whitespace
= FixMyStreet::DB->resultset('ResponseTemplate')->create(
{ body_id => $body->id,
title => 'Title',
text => 'Text',
external_status_code => "      \t\n060\n\t     ",
}
);

note 'Create template:';
is $t_whitespace->external_status_code, '060',
'external_status_code correctly munged';

note 'Update with external_status_code arg:';
$t_whitespace->update( { external_status_code => ' 171 ' } );
$t_whitespace->discard_changes;
is $t_whitespace->external_status_code, '171',
'external_status_code correctly munged';

note 'Unset with external_status_code arg:';
$t_whitespace->update( { external_status_code => undef } );
$t_whitespace->discard_changes;
is $t_whitespace->external_status_code, undef,
'external_status_code unset';

note 'Set external_status_code followed by call to update:';
$t_whitespace->external_status_code(' 282 ');
$t_whitespace->update;
$t_whitespace->discard_changes;
is $t_whitespace->external_status_code, '282',
'external_status_code correctly munged';

note 'Unset external_status_code followed by call to update:';
$t_whitespace->external_status_code(undef);
$t_whitespace->update;
$t_whitespace->discard_changes;
is $t_whitespace->external_status_code, undef,
'external_status_code unset';

note 'Set external_status_code AND pass arg to update:';
$t_whitespace->external_status_code(' 393 ');
$t_whitespace->update( { external_status_code => ' 404 ' } );
$t_whitespace->discard_changes;
is $t_whitespace->external_status_code, '404',
'arg passed to update takes precedence';
};

done_testing;

0 comments on commit 0693a5a

Please sign in to comment.