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

Trim whitespace on external status codes for response templates #3997

Merged
merged 1 commit into from
Jul 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@
- Inspector dropdown list doesn't show anonymised users, removing blank options #3873
- Fix report unassignment so it works for users who did not create the report #3903
- [Open311] External code removal is not a change.
- Trim whitespace on extra status codes for response templates
- Accessibility improvements:
- The "skip map" link on /around now has new wording. #3794
- Improve visual contrast of pagination links. #3794
Expand Down
25 changes: 24 additions & 1 deletion perllib/FixMyStreet/DB/Result/ResponseTemplate.pm
Expand Up @@ -63,5 +63,28 @@ __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 new {
my ( $self, $attrs ) = @_;

$attrs->{external_status_code}
= Utils::trim_text( $attrs->{external_status_code} )
if $attrs->{external_status_code};

return $self->next::method($attrs);
}

sub set_column {
my ( $self, $column, $new_value ) = @_;

if ( $column eq 'external_status_code' ) {
$new_value = Utils::trim_text($new_value);
}

$self->next::method( $column, $new_value );
};

1;
54 changes: 53 additions & 1 deletion t/app/model/responsetemplate.t
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;