diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-10-12 14:58:26 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-10-22 15:00:46 +0100 |
commit | c73e8b5485c4016921777b95582bbe92853f5e4c (patch) | |
tree | 415b778c29c8ca6fe723e10e9f9365c1cd666025 | |
parent | d4f330c29611ad469bc9bfa5a46ddc9bc25501cb (diff) |
Allow moderation to potentially change photos.
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Moderate.pm | 25 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 11 | ||||
-rw-r--r-- | t/app/controller/moderate.t | 40 | ||||
-rw-r--r-- | t/app/controller/photo.t | 26 |
6 files changed, 57 insertions, 53 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b7342f92..621c61192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,10 @@ - Allow cobrands to limit contact form to abuse reports only - Front end improvements: - Clearer relocation options while you’re reporting a problem #2238 - - Admin improvements + - Admin improvements: - List number of alerts on report page #669 - viewing and managing of user alerts in admin #676 + - Allow moderation to potentially change photos. #2291 - Bugfixes: - Add perl 5.26/5.28 support. - Fix subcategory issues when visiting /report/new directly #2276 diff --git a/perllib/FixMyStreet/App/Controller/Moderate.pm b/perllib/FixMyStreet/App/Controller/Moderate.pm index c47c34f37..d8fc38211 100644 --- a/perllib/FixMyStreet/App/Controller/Moderate.pm +++ b/perllib/FixMyStreet/App/Controller/Moderate.pm @@ -69,19 +69,40 @@ sub report : Chained('moderate') : PathPart('report') : CaptureArgs(1) { sub moderate_report : Chained('report') : PathPart('') : Args(0) { my ($self, $c) = @_; + my $problem = $c->stash->{problem}; + # Make sure user can moderate this report - $c->detach unless $c->user->can_moderate($c->stash->{problem}); + $c->detach unless $c->user->can_moderate($problem); $c->forward('report_moderate_hide'); my @types = grep $_, - ($c->user->can_moderate_title($c->stash->{problem}, 1) + ($c->user->can_moderate_title($problem, 1) ? $c->forward('moderate_text', [ 'title' ]) : ()), $c->forward('moderate_text', [ 'detail' ]), $c->forward('moderate_boolean', [ 'anonymous', 'show_name' ]), $c->forward('moderate_boolean', [ 'photo' ]); + # Deal with possible photo changes. If a moderate form uses a standard + # photo upload field (with upload_fileid, label and file upload handlers), + # this will allow photos to be changed, not just switched on/off. You will + # probably want a hidden field with problem_photo=1 to skip that check. + my $photo_edit_form = defined $c->get_param('photo1'); + if ($photo_edit_form) { + $c->forward('/photo/process_photo'); + if ( my $photo_error = delete $c->stash->{photo_error} ) { + $c->flash->{photo_error} = $photo_error; + } else { + my $fileid = $c->stash->{upload_fileid}; + if ($fileid ne $problem->photo) { + $problem->get_photoset->delete_cached; + $problem->update({ photo => $fileid || undef }); + push @types, 'photo'; + } + } + } + $c->detach( 'report_moderate_audit', \@types ) } diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index 198edb5f7..d4ff0a2ca 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -194,7 +194,7 @@ sub load_updates : Private { $c->stash->{updates} = \@combined; if ($c->sessionid) { - foreach (qw(alert_to_reporter anonymized)) { + foreach (qw(alert_to_reporter anonymized photo_error)) { $c->stash->{$_} = $c->flash->{$_} if $c->flash->{$_}; } } @@ -207,6 +207,9 @@ sub format_problem_for_display : Private { my $problem = $c->stash->{problem}; + # upload_fileid is used by the update form on this page + $c->stash->{problem_upload_fileid} = $problem->get_photoset->data; + ( $c->stash->{latitude}, $c->stash->{longitude} ) = map { Utils::truncate_coordinate($_) } ( $problem->latitude, $problem->longitude ); diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index e7fc573c0..495310063 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -706,7 +706,7 @@ sub create_problems_for_body { latitude => '51.5016605453401', longitude => '-0.142497580865087', user_id => $user->id, - photo => $mech->get_photo_data, + photo => '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg', }; my %report_params = ( %$default_params, %$params ); @@ -721,15 +721,6 @@ sub create_problems_for_body { return @problems; } -sub get_photo_data { - my $mech = shift; - return $mech->{sample_photo} ||= do { - my $sample_file = FixMyStreet->path_to( 't/app/controller/sample.jpg' ); - $mech->builder->ok( -f "$sample_file", "sample file $sample_file exists" ); - $sample_file->slurp(iomode => '<:raw'); - }; -} - sub create_comment_for_problem { my ( $mech, $problem, $user, $name, $text, $anonymous, $state, $problem_state, $params ) = @_; $params ||= {}; diff --git a/t/app/controller/moderate.t b/t/app/controller/moderate.t index c3aba0ebb..c2b185cde 100644 --- a/t/app/controller/moderate.t +++ b/t/app/controller/moderate.t @@ -49,11 +49,10 @@ sub create_report { latitude => '51.4129', longitude => '0.007831', user_id => $user2->id, - photo => $mech->get_photo_data, + photo => '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg', }); } my $report = create_report(); -my $report2 = create_report(); my $REPORT_URL = '/report/' . $report->id ; @@ -236,31 +235,22 @@ subtest 'Problem moderation' => sub { }; $mech->content_lacks('Posted anonymously', 'sanity check'); +my ($csrf) = $mech->content =~ /meta content="([^"]*)" name="csrf-token"/; -subtest 'Problem 2' => sub { - my $REPORT2_URL = '/report/' . $report2->id ; - $mech->get_ok($REPORT2_URL); - $mech->submit_form_ok({ with_fields => { +subtest 'Edit photos' => sub { + $mech->post_ok('http://www.example.org/moderate/report/' . $report->id, { %problem_prepopulated, - problem_title => 'Good good', - problem_detail => 'Good good improved', - }}); - $mech->base_like( qr{\Q$REPORT2_URL\E} ); - - $report2->discard_changes; - is $report2->title, 'Good good'; - is $report2->detail, 'Good good improved'; - - $mech->submit_form_ok({ with_fields => { + photo1 => 'something-wrong', + token => $csrf, + }); + $mech->post_ok('http://www.example.org/moderate/report/' . $report->id, { %problem_prepopulated, - problem_revert_title => 1, - problem_revert_detail => 1, - }}); - $mech->base_like( qr{\Q$REPORT2_URL\E} ); - - $report2->discard_changes; - is $report2->title, 'Good bad good'; - is $report2->detail, 'Good bad bad bad good bad'; + photo1 => '', + upload_fileid => '', + token => $csrf, + }); + $report->discard_changes; + is $report->photo, undef; }; sub create_update { @@ -268,7 +258,7 @@ sub create_update { user => $user2, name => 'Test User 2', anonymous => 'f', - photo => $mech->get_photo_data, + photo => '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg', text => 'update good good bad good', state => 'confirmed', mark_fixed => 0, diff --git a/t/app/controller/photo.t b/t/app/controller/photo.t index e28ee1946..19249611a 100644 --- a/t/app/controller/photo.t +++ b/t/app/controller/photo.t @@ -75,7 +75,7 @@ subtest "Check multiple upload worked" => sub { }; }; -subtest "Check photo uploading URL works" => sub { +subtest "Check photo uploading URL and endpoints work" => sub { my $UPLOAD_DIR = tempdir( CLEANUP => 1 ); # submit initial pc form @@ -95,20 +95,18 @@ subtest "Check photo uploading URL works" => sub { is $mech->content, '{"id":"74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg"}'; my $image_file = path($UPLOAD_DIR, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); ok $image_file->exists, 'File uploaded to temp'; - }; -}; -subtest "Check photo URL endpoints work" => sub { - my $p = FixMyStreet::DB->resultset("Problem")->first; - - $mech->get_ok('/photo/temp.74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); - my $image_file = FixMyStreet->path_to('web/photo/temp.74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); - ok -e $image_file, 'File uploaded to temp'; - $mech->get_ok('/photo/' . $p->id . '.jpeg'); - $image_file = FixMyStreet->path_to('web/photo/' . $p->id . '.jpeg'); - ok -e $image_file, 'File uploaded to temp'; - my $res = $mech->get('/photo/0.jpeg'); - is $res->code, 404, "got 404"; + my $p = FixMyStreet::DB->resultset("Problem")->first; + + $mech->get_ok('/photo/temp.74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); + $image_file = FixMyStreet->path_to('web/photo/temp.74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg'); + ok -e $image_file, 'File uploaded to temp'; + $mech->get_ok('/photo/' . $p->id . '.jpeg'); + $image_file = FixMyStreet->path_to('web/photo/' . $p->id . '.jpeg'); + ok -e $image_file, 'File uploaded to temp'; + my $res = $mech->get('/photo/0.jpeg'); + is $res->code, 404, "got 404"; + }; }; done_testing(); |