diff options
author | Matthew Somerville <matthew@mysociety.org> | 2015-12-02 17:33:48 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2015-12-15 17:02:11 +0000 |
commit | 92dfeac83378cd49fbb59591685e5bf849d317e6 (patch) | |
tree | f87175f6539728e319dc5bd027b1b94fd7eaa26b | |
parent | 64d24b8627879fc68f9f883d6e24a9c7cb72449f (diff) |
Fix cobrand restriction of My/Nearby.
5c79337 simplified a bit too far, as after then a particular cobrand
could in Nearby and My only filter reports to a particular body, not
any other criteria. To fix this, introduce more generic functions in
the default cobrand to allow more flexibility.
Make sure a few tests delete their bodies fully so that new tests
pass when run as part of the suite.
Fixes #1289.
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | notes/cobranding.txt | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 35 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/SeeSomething.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UKCouncils.pm | 16 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Comment.pm | 14 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 6 | ||||
-rw-r--r-- | t/cobrand/bromley.t | 2 | ||||
-rw-r--r-- | t/cobrand/fixamingata.t | 2 | ||||
-rw-r--r-- | t/cobrand/fixmybarangay.t | 5 | ||||
-rw-r--r-- | t/cobrand/restriction.t | 55 | ||||
-rw-r--r-- | t/cobrand/two_tier.t | 4 |
15 files changed, 122 insertions, 51 deletions
@@ -47,6 +47,7 @@ We've extracted all of the mobile apps from this repository into the - Default the Google map view to hybrid. #1293 - Prevent SVG chevron being stretched in Firefox. #1256 - Better display/internationalisation of numbers. #1297 + - Fix cobrand restriction of My/Nearby. #1289 - Development improvements: - Add generic static route handler. #1235 - Store reports summary data by cobrand. #1290 diff --git a/notes/cobranding.txt b/notes/cobranding.txt index a62747397..68068fd23 100644 --- a/notes/cobranding.txt +++ b/notes/cobranding.txt @@ -10,7 +10,7 @@ NB: this is moderately specific to producing cobrands for UK councils 1: copy an exiting perllib/FixMyStreet/Cobrand/ file to perllib/FixMyStreet/Cobrand/ExampleCom.pm * Change package name at top of file * Change following functions accordingly: - body_restriction + problems_restriction/updates_restriction enter_postcode_text area_check base_url diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 9a6c7bded..0fc87c2f6 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -84,7 +84,7 @@ sub index : Path : Args(0) { for ( FixMyStreet::DB::Result::Problem->visible_states() ); $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users; - my $comments = $c->model('DB::Comment')->summary_count( $c->cobrand->body_restriction ); + my $comments = $c->cobrand->updates->summary_count; my %comment_counts = map { $_->state => $_->get_column('state_count') } $comments->all; @@ -171,7 +171,7 @@ sub timeline : Path( 'timeline' ) : Args(0) { push @{$time{$_->whenanswered->epoch}}, { type => 'quesAnswered', date => $_->whenanswered, obj => $_ } if $_->whenanswered; } - my $updates = $c->model('DB::Comment')->timeline( $c->cobrand->body_restriction ); + my $updates = $c->cobrand->updates->timeline; foreach ($updates->all) { push @{$time{$_->created->epoch}}, { type => 'update', date => $_->created, obj => $_} ; @@ -622,9 +622,7 @@ sub reports : Path('reports') { } if (@$query) { - my $updates = $c->model('DB::Comment') - ->to_body($c->cobrand->body_restriction) - ->search( + my $updates = $c->cobrand->updates->search( { -or => $query, }, @@ -967,9 +965,7 @@ sub users: Path('users') : Args(0) { sub update_edit : Path('update_edit') : Args(1) { my ( $self, $c, $id ) = @_; - my $update = $c->model('DB::Comment') - ->to_body($c->cobrand->body_restriction) - ->search({ id => $id })->first; + my $update = $c->cobrand->updates->search({ id => $id })->first; $c->detach( '/page_error_404_not_found' ) unless $update; diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index 3c4ce2cf7..81a9d7a93 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -36,6 +36,7 @@ sub my : Path : Args(0) { my $states = $c->stash->{filter_problem_states}; my $params = { state => [ keys %$states ], + user => $c->user->id, }; my $category = $c->get_param('filter_category'); @@ -44,9 +45,7 @@ sub my : Path : Args(0) { $c->stash->{filter_category} = $category; } - my $rs = $c->user->problems - ->to_body($c->cobrand->body_restriction) - ->search( $params, { + my $rs = $c->cobrand->problems->search( $params, { order_by => { -desc => 'confirmed' }, rows => 50 } )->page( $p_page ); @@ -77,7 +76,7 @@ sub my : Path : Args(0) { $c->stash->{updates} = \@updates; $c->stash->{updates_pager} = $rs->pager; - my @categories = $c->user->problems->search( undef, { + my @categories = $c->cobrand->problems->search( { user => $c->user->id }, { columns => [ 'category' ], distinct => 1, order_by => [ 'category' ], diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 48c997047..e7a4dad72 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -47,24 +47,45 @@ sub country { =head1 problems -Returns a ResultSet of Problems, restricted to a subset if we're on a cobrand -that only wants some of the data. +Returns a ResultSet of Problems, potentially restricted to a subset if we're on +a cobrand that only wants some of the data. =cut sub problems { my $self = shift; - return $self->{c}->model('DB::Problem'); + return $self->problems_restriction($self->{c}->model('DB::Problem')); } -=head1 body_restriction +=head1 updates -Return an extra query parameter to restrict reports to those sent to a -particular body. +Returns a ResultSet of Comments, potentially restricted to a subset if we're on +a cobrand that only wants some of the data. =cut -sub body_restriction {} +sub updates { + my $self = shift; + return $self->updates_restriction($self->{c}->model('DB::Comment')); +} + +=head1 problems_restriction/updates_restriction + +Used to restricts reports and updates in a cobrand in a particular way. Do +nothing by default. + +=cut + +sub problems_restriction { + my ($self, $rs) = @_; + return $rs; +} + +sub updates_restriction { + my ($self, $rs) = @_; + return $rs; +} + sub site_key { return 0; } diff --git a/perllib/FixMyStreet/Cobrand/SeeSomething.pm b/perllib/FixMyStreet/Cobrand/SeeSomething.pm index 9ae15fd8d..22750aafa 100644 --- a/perllib/FixMyStreet/Cobrand/SeeSomething.pm +++ b/perllib/FixMyStreet/Cobrand/SeeSomething.pm @@ -10,11 +10,6 @@ sub council_name { return 'See Something Say Something'; } sub council_url { return 'seesomething'; } sub area_types { [ 'MTD' ] } -sub body_restriction { - my $self = shift; - return $self->council_id; -} - sub area_check { my ( $self, $params, $context ) = @_; diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index 0fb8c23d0..b4b91b7dd 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -22,11 +22,6 @@ sub path_to_web_templates { ]; } -sub body_restriction { - my $self = shift; - return $self->council_id; -} - sub site_key { my $self = shift; return $self->council_url; @@ -36,9 +31,14 @@ sub restriction { return { cobrand => shift->moniker }; } -sub problems { - my $self = shift; - return $self->{c}->model('DB::Problem')->to_body($self->council_id); +sub problems_restriction { + my ($self, $rs) = @_; + return $rs->to_body($self->council_id); +} + +sub updates_restriction { + my ($self, $rs) = @_; + return $rs->to_body($self->council_id); } sub base_url { diff --git a/perllib/FixMyStreet/DB/ResultSet/Comment.pm b/perllib/FixMyStreet/DB/ResultSet/Comment.pm index 3059baab1..9f254a018 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Comment.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Comment.pm @@ -5,20 +5,20 @@ use strict; use warnings; sub to_body { - my ($rs, $body_restriction) = @_; - return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $body_restriction); + my ($rs, $bodies) = @_; + return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $bodies, 1); } sub timeline { - my ( $rs, $body_restriction ) = @_; + my ( $rs ) = @_; my $prefetch = $rs->result_source->storage->sql_maker->quote_char ? [ qw/user/ ] : []; - return $rs->to_body($body_restriction)->search( + return $rs->search( { state => 'confirmed', created => { '>=', \"current_timestamp-'7 days'::interval" }, @@ -30,17 +30,13 @@ sub timeline { } sub summary_count { - my ( $rs, $body_restriction ) = @_; + my ( $rs ) = @_; my $params = { group_by => ['me.state'], select => [ 'me.state', { count => 'me.id' } ], as => [qw/state state_count/], }; - if ($body_restriction) { - $rs = $rs->to_body($body_restriction); - $params->{join} = 'problem'; - } return $rs->search(undef, $params); } diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm index a6b00ef7b..9db1c6525 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -4,6 +4,11 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; +sub to_body { + my ($rs, $bodies) = @_; + return FixMyStreet::DB::ResultSet::Problem::to_body($rs, $bodies, 1); +} + sub nearby { my ( $rs, $c, $dist, $ids, $limit, $mid_lat, $mid_lon, $interval, $category, $states ) = @_; @@ -21,7 +26,7 @@ sub nearby { if $ids; $params->{category} = $category if $category; - $rs = FixMyStreet::DB::ResultSet::Problem::to_body($rs, $c->cobrand->body_restriction); + $rs = $c->cobrand->problems_restriction($rs); my $attrs = { prefetch => 'problem', diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index f82f0135c..488030928 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -16,13 +16,15 @@ sub set_restriction { } sub to_body { - my ($rs, $bodies) = @_; + my ($rs, $bodies, $join) = @_; return $rs unless $bodies; unless (ref $bodies eq 'ARRAY') { $bodies = [ map { ref $_ ? $_->id : $_ } $bodies ]; } + $join = { join => 'problem' } if $join; $rs = $rs->search( - \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ] + \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ], + $join ); return $rs; } diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index 91b34a289..1f61cd3de 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -58,5 +58,5 @@ subtest 'testing special Open311 behaviour', sub { # Clean up $mech->delete_user($user); -$mech->delete_problems_for_body( $body->id ); +$mech->delete_body($body); done_testing(); diff --git a/t/cobrand/fixamingata.t b/t/cobrand/fixamingata.t index 65236e6e1..ea3c2da92 100644 --- a/t/cobrand/fixamingata.t +++ b/t/cobrand/fixamingata.t @@ -115,7 +115,7 @@ subtest "Test ajax decimal points" => sub { }; END { - $mech->delete_problems_for_body(1); + $mech->delete_body($body); ok $mech->host("www.fixmystreet.com"), "change host back"; done_testing(); } diff --git a/t/cobrand/fixmybarangay.t b/t/cobrand/fixmybarangay.t index 00e792341..2f99b8c1e 100644 --- a/t/cobrand/fixmybarangay.t +++ b/t/cobrand/fixmybarangay.t @@ -141,8 +141,9 @@ is $luz_report->state, 'hidden', 'should be hidden'; $mech->delete_user($fmb_test_email); -$mech->delete_problems_for_body( $luz->id ); -$mech->delete_problems_for_body( $dps->id ); +$mech->delete_body($luz); +$mech->delete_body($bsn); +$mech->delete_body($dps); ok $mech->host("www.fixmystreet.com"), "change host back"; diff --git a/t/cobrand/restriction.t b/t/cobrand/restriction.t new file mode 100644 index 000000000..873a396b7 --- /dev/null +++ b/t/cobrand/restriction.t @@ -0,0 +1,55 @@ +use strict; +use warnings; + +package FixMyStreet::Cobrand::Tester; + +use parent 'FixMyStreet::Cobrand::Default'; + +sub problems_restriction { + my ($self, $rs) = @_; + return $rs->search({ cobrand => 'tester' }); +} + +sub updates_restriction { + my ($self, $rs) = @_; + return $rs->search({ 'problem.cobrand' => 'tester' }, { join => 'problem' }); +} + +package main; + +use Test::More; +use FixMyStreet::TestMech; + +my $c = FixMyStreet::App->new; +my $cobrand = FixMyStreet::Cobrand::Tester->new({c => $c}); +$c->stash->{cobrand} = $cobrand; + +my $mech = FixMyStreet::TestMech->new; + +my ($prob1) = $mech->create_problems_for_body(1, 1234, 'Title'); +my ($prob2) = $mech->create_problems_for_body(1, 1234, 'Title', { cobrand => 'tester' }); +$mech->create_problems_for_body(1, 1234, 'Title', { latitude => 0, longitude => 0 }); +$mech->create_problems_for_body(1, 1234, 'Title', { cobrand => 'tester', latitude => 0, longitude => 0 }); + +for (1..2) { + $c->model('DB::Comment')->create({ + problem_id => $_ == 1 ? $prob1->id : $prob2->id, + user_id => $prob2->user_id, + name => 'User', + mark_fixed => 'false', + text => 'This is some update text', + state => 'confirmed', + cobrand => 'tester', + anonymous => 'f', + }); +} + +is($c->model('DB::Problem')->count, 4, 'Four reports in database'); +is($cobrand->problems->count, 2, 'Two reports in the right cobrand'); +is($cobrand->updates->count, 1, 'One update in the right cobrand'); + +my $nearby = $c->model('DB::Nearby')->nearby($c, 5, [], 10, 0.003, 0.004); +is(@$nearby, 1, 'One report close to the origin point'); + +$mech->delete_problems_for_body(1234); +done_testing(); diff --git a/t/cobrand/two_tier.t b/t/cobrand/two_tier.t index b3d6ca7db..f52e66abc 100644 --- a/t/cobrand/two_tier.t +++ b/t/cobrand/two_tier.t @@ -20,8 +20,8 @@ FixMyStreet::override_config { for my $c (@cobrands) { my ($m, $id) = @$c; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($m); - my $body_restriction = $cobrand->body_restriction; - is $body_restriction, $id, "body_restriction for $m"; + my $council_id = $cobrand->council_id; + is $council_id, $id, "council_id for $m"; } }; |