diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 4 | ||||
-rw-r--r-- | t/app/controller/around.t | 32 |
3 files changed, 26 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d02130d56..ad53ca547 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Provide access to staff-only categories in admin. - Maintain group on pin move with same category in multiple groups. #2962 - Remove unnecessary margin-right on #postcodeForm. #3010 + - Fix sorting by most commented on /around map view. #3013 - Development improvements: - Refactor Script::Report into an object. - Move summary failures to a separate script. diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 359d5224a..e23cf78e1 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -266,7 +266,9 @@ sub categories_summary { sub include_comment_counts { my $rs = shift; my $order_by = $rs->{attrs}{order_by}; - return $rs unless ref $order_by eq 'ARRAY' && ref $order_by->[0] eq 'HASH' && $order_by->[0]->{-desc} eq 'comment_count'; + return $rs unless + (ref $order_by eq 'ARRAY' && ref $order_by->[0] eq 'HASH' && $order_by->[0]->{-desc} eq 'comment_count') + || (ref $order_by eq 'HASH' && $order_by->{-desc} eq 'comment_count'); $rs->search({}, { '+select' => [ { "" => \'(select count(*) from comment where problem_id=me.id and state=\'confirmed\')', diff --git a/t/app/controller/around.t b/t/app/controller/around.t index db7244863..186b833fd 100644 --- a/t/app/controller/around.t +++ b/t/app/controller/around.t @@ -313,7 +313,7 @@ subtest 'check old problems not shown by default on around page' => sub { $problems->update( { confirmed => \"current_timestamp" } ); }; -subtest 'check sorting by update uses lastupdate to determine age' => sub { +subtest 'check sorting' => sub { my $params = { postcode => 'OX20 1SZ', latitude => 51.754926, @@ -322,19 +322,31 @@ subtest 'check sorting by update uses lastupdate to determine age' => sub { my $bbox = ($params->{longitude} - 0.01) . ',' . ($params->{latitude} - 0.01) . ',' . ($params->{longitude} + 0.01) . ',' . ($params->{latitude} + 0.01); - my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id ); - $problems->first->update( { confirmed => \"current_timestamp-'7 months'::interval" } ); + subtest 'by update uses lastupdate to determine age' => sub { + my $problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id ); + $problems->first->update( { confirmed => \"current_timestamp-'7 months'::interval" } ); - my $json = $mech->get_ok_json( '/around?ajax=1&bbox=' . $bbox ); - my $pins = $json->{pins}; - is scalar @$pins, 8, 'correct number of reports with default sorting'; + my $json = $mech->get_ok_json( '/around?ajax=1&bbox=' . $bbox ); + my $pins = $json->{pins}; + is scalar @$pins, 8, 'correct number of reports with default sorting'; + $json = $mech->get_ok_json( '/around?ajax=1&sort=updated-desc&bbox=' . $bbox ); + $pins = $json->{pins}; + is scalar @$pins, 9, 'correct number of reports with updated sort'; - $json = $mech->get_ok_json( '/around?ajax=1&sort=updated-desc&bbox=' . $bbox ); - $pins = $json->{pins}; - is scalar @$pins, 9, 'correct number of reports with updated sort'; + $problems->update( { confirmed => \"current_timestamp" } ); + }; - $problems->update( { confirmed => \"current_timestamp" } ); + subtest 'by comment count' => sub { + my @problems = FixMyStreet::DB->resultset('Problem')->to_body( $body->id )->all; + $mech->create_comment_for_problem($problems[3], $problems[0]->user, 'Name', 'Text', 'f', 'confirmed', 'confirmed'); + $mech->create_comment_for_problem($problems[3], $problems[0]->user, 'Name', 'Text', 'f', 'confirmed', 'confirmed'); + $mech->create_comment_for_problem($problems[6], $problems[0]->user, 'Name', 'Text', 'f', 'confirmed', 'confirmed'); + my $json = $mech->get_ok_json( '/around?ajax=1&sort=comments-desc&bbox=' . $bbox ); + my $pins = $json->{pins}; + is $pins->[0][3], $problems[3]->id, 'Report with two updates first'; + is $pins->[1][3], $problems[6]->id, 'Report with one update second'; + }; }; subtest 'check show old reports checkbox shown on around page' => sub { |