Skip to content

Commit

Permalink
Fixes issue with not being able to order dropdowns in extra questions
Browse files Browse the repository at this point in the history
Raised by Peterborough but an FMS issue as, when creating dropdowns for extra questions on categories,
the dropdown options are sorted on index number, but this has not been a numeric sort so looks
random to the user.

The order of dropdown items now remains in the order they are in the creation/editing screen.

Adds a regression test.

mysociety/societyworks#2511
  • Loading branch information
MorayMySoc committed Aug 17, 2021
1 parent 32b4845 commit 6ccfa71
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- Fix checked order of updates in dashboard export.
- Fix unable to edit user with verified landline #3295
- Fix 'sites' page to reflect active fixmystreet sites #2481
- Fix ordering of dropdown lists in extra questions #3566
- Admin improvements:
- Enable per-category hint customisation.
- Move ban/unban buttons to user edit admin page.
Expand Down
4 changes: 1 addition & 3 deletions perllib/FixMyStreet/App/Controller/Admin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,8 @@ sub fetch_body_areas : Private {

sub update_extra_fields : Private {
my ($self, $c, $object) = @_;

my @indices = grep { /^metadata\[\d+\]\.code/ } keys %{ $c->req->params };
@indices = sort map { /(\d+)/ } @indices;

my @extra_fields;
foreach my $i (@indices) {
my $meta = {};
Expand All @@ -532,7 +530,7 @@ sub update_extra_fields : Private {
$meta->{values} = [];
my $re = qr{^metadata\[$i\]\.values\[\d+\]\.key};
my @vindices = grep { /$re/ } keys %{ $c->req->params };
@vindices = sort map { /values\[(\d+)\]/ } @vindices;
@vindices = sort { $a <=> $b } map { /values\[(\d+)\]/ } @vindices;
foreach my $j (@vindices) {
my $name = $c->get_param("metadata[$i].values[$j].name");
my $key = $c->get_param("metadata[$i].values[$j].key");
Expand Down
36 changes: 36 additions & 0 deletions t/app/controller/admin/reportextrafields.t
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,42 @@ FixMyStreet::override_config {
$contact->discard_changes;
is_deeply $contact->get_extra_fields, $contact_extra_fields, 'new field was added';

$mech->get_ok("/admin/body/" . $body->id . "/" . $contact->category);
$mech->submit_form_ok( { with_fields => {
"metadata[9999].order" => "4",
"metadata[9999].code" => "list_test_order",
"metadata[9999].behaviour" => "question",
"metadata[9999].disable_form" => "1",
"metadata[9999].description" => "this field is a list with multiple options",
"metadata[9999].datatype" => "singlevaluelist",
"note" => "Added list field",
}});
$mech->content_contains('Values updated');

push @$contact_extra_fields, {
order => "4",
code => "list_test_order",
required => "false",
variable => "true",
protected => "false",
description => "this field is a list with multiple options",
datatype => "singlevaluelist",
values => [],
};

for my $num (1..11) {
$mech->get_ok("/admin/body/" . $body->id . "/" . $contact->category);
$mech->submit_form_ok( { with_fields => {
"metadata[3].values[8888].key" => "key" . $num,
"metadata[3].values[8888].name" => "name" . $num,
} } );
$mech->content_contains('Values updated');
push @{ $contact_extra_fields->[3]{values} }, { name => 'name' . $num, key => 'key' . $num };
};

$contact->discard_changes;
is_deeply $contact->get_extra_fields, $contact_extra_fields, 'multiple fields were added and retain order';

$contact->set_extra_fields();
$contact->update;
};
Expand Down

0 comments on commit 6ccfa71

Please sign in to comment.