use FixMyStreet::TestMech; use Test::MockModule; my $mech = FixMyStreet::TestMech->new; my $brum = $mech->create_body_ok(2514, 'Birmingham City Council'); my $oxon = $mech->create_body_ok(2237, 'Oxfordshire County Council', { can_be_devolved => 1 } ); my $contact = $mech->create_contact_ok( body_id => $oxon->id, category => 'Cows', email => 'cows@example.net' ); my $contact2 = $mech->create_contact_ok( body_id => $oxon->id, category => 'Sheep', email => 'SHEEP', send_method => 'Open311' ); my $contact3 = $mech->create_contact_ok( body_id => $oxon->id, category => 'Badgers', email => 'badgers@example.net' ); my $dt = FixMyStreet::DB->resultset("DefectType")->create({ body => $oxon, name => 'Small Defect', description => "Teeny", }); FixMyStreet::DB->resultset("ContactDefectType")->create({ contact => $contact, defect_type => $dt, }); my $rp = FixMyStreet::DB->resultset("ResponsePriority")->create({ body => $oxon, name => 'High Priority', }); my $rp2 = FixMyStreet::DB->resultset("ResponsePriority")->create({ body => $oxon, name => 'Low Priority', }); FixMyStreet::DB->resultset("ContactResponsePriority")->create({ contact => $contact, response_priority => $rp, }); FixMyStreet::DB->resultset("ContactResponsePriority")->create({ contact => $contact3, response_priority => $rp2, }); my $oxfordcity = $mech->create_body_ok(2421, 'Oxford City Council'); $mech->create_contact_ok( body_id => $oxfordcity->id, category => 'Horses', email => 'horses@example.net' ); my ($report, $report2, $report3) = $mech->create_problems_for_body(3, $oxon->id, 'Test', { category => 'Cows', cobrand => 'fixmystreet', areas => ',2237,2421,', whensent => \'current_timestamp', latitude => 51.754926, longitude => -1.256179, }); my $report_id = $report->id; my $report2_id = $report2->id; my $report3_id = $report3->id; $mech->create_user_ok('body@example.com', name => 'Body User'); my $user = $mech->log_in_ok('body@example.com'); $user->set_extra_metadata('categories', [ $contact->id ]); $user->update( { from_body => $oxon } ); FixMyStreet::override_config { MAPIT_URL => 'http://mapit.uk/', ALLOWED_COBRANDS => 'fixmystreet', }, sub { subtest "test inspect page" => sub { $mech->get_ok("/report/$report_id"); $mech->content_lacks('Save changes'); $mech->content_lacks('Private'); $mech->content_lacks('Priority'); $mech->content_lacks('Traffic management'); $mech->content_lacks('/admin/report_edit/'.$report_id.'">admin)'); $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_mark_private' }); $mech->get_ok("/report/$report_id"); $mech->content_contains('Private'); $mech->content_contains('Save changes'); $mech->content_lacks('Priority'); $mech->content_lacks('Traffic management'); $mech->content_lacks('/admin/report_edit/'.$report_id.'">admin)'); $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_edit_priority' }); $mech->get_ok("/report/$report_id"); $mech->content_contains('Private'); $mech->content_contains('Save changes'); $mech->content_contains('Priority'); $mech->content_lacks('Traffic management'); $mech->content_lacks('/admin/report_edit/'.$report_id.'">admin)'); $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_inspect' }); $mech->get_ok("/report/$report_id"); $mech->content_contains('Save changes'); $mech->content_contains('Private'); $mech->content_contains('Priority'); $mech->content_contains('Traffic management'); $mech->content_lacks('/admin/report_edit/'.$report_id.'">admin)'); }; subtest "council staff can't see admin report edit link on FMS.com" => sub { my $report_edit_permission = $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_edit' }); $mech->get_ok("/report/$report_id"); $mech->content_lacks('/admin/report_edit/'.$report_id.'">admin)'); $report_edit_permission->delete; }; subtest "superusers can see admin report edit link on FMS.com" => sub { $user->update({is_superuser => 1}); $mech->get_ok("/report/$report_id"); $mech->content_contains('/admin/report_edit/'.$report_id.'">admin)'); $user->update({is_superuser => 0}); }; subtest "test mark private submission" => sub { $user->user_body_permissions->delete; $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_mark_private' }); $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { non_public => 1 } }); $report->discard_changes; my $alert = FixMyStreet::App->model('DB::Alert')->find( { user => $user, alert_type => 'new_updates', confirmed => 1, } ); is $report->state, 'confirmed', 'report state not changed'; ok $report->non_public, 'report not public'; ok !defined( $alert ) , 'not signed up for alerts'; $report->update( { non_public => 0 } ); }; subtest "test basic inspect submission" => sub { $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_edit_priority' }); $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_inspect' }); $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { traffic_information => 'Yes', state => 'Action scheduled', include_update => undef } }); $report->discard_changes; my $alert = FixMyStreet::App->model('DB::Alert')->find( { user => $user, alert_type => 'new_updates', confirmed => 1, } ); is $report->state, 'action scheduled', 'report state changed'; is $report->get_extra_metadata('traffic_information'), 'Yes', 'report data changed'; ok defined( $alert ) , 'sign up for alerts'; }; subtest "test inspect & instruct submission" => sub { $user->user_body_permissions->create({ body => $oxon, permission_type => 'report_instruct' }); $user->user_body_permissions->create({ body => $oxon, permission_type => 'planned_reports' }); $report->state('confirmed'); $report->update; my $reputation = $report->user->get_extra_metadata("reputation"); $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { public_update => "This is a public update.", include_update => "1", state => 'action scheduled', raise_defect => 1, } }); $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ with_fields => { update => "This is a second public update, of normal update form, no actual change.", } }); $report->discard_changes; my $comment = ($report->comments( undef, { order_by => { -desc => 'id' } } )->all)[1]->text; is $comment, "This is a public update.", 'Update was created'; is $report->get_extra_metadata('inspected'), 1, 'report marked as inspected'; is $report->user->get_extra_metadata('reputation'), $reputation, "User reputation wasn't changed"; $mech->get_ok("/report/$report_id"); my $meta = $mech->extract_update_metas; like $meta->[0], qr/State changed to: Action scheduled/, 'First update mentions action scheduled'; like $meta->[2], qr/Posted by .*defect raised/, 'Update mentions defect raised'; $user->unset_extra_metadata('categories'); $user->update; }; subtest "test update is required when instructing" => sub { $report->unset_extra_metadata('inspected'); $report->update; $report->inspection_log_entry->delete; $report->comments->delete_all; $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { public_update => undef, include_update => "1" } }); is_deeply $mech->page_errors, [ "Please provide a public update for this report." ], 'errors match'; $report->discard_changes; is $report->comments->count, 0, "Update wasn't created"; is $report->get_extra_metadata('inspected'), undef, 'report not marked as inspected'; }; subtest "test location changes" => sub { $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { latitude => 55, longitude => -2 } }); $mech->content_contains('Invalid location'); $mech->submit_form_ok({ button => 'save', with_fields => { latitude => 51.754926, longitude => -1.256179, include_update => undef } }); $mech->content_lacks('Invalid location'); }; subtest "test areas update when location changes" => sub { $report->discard_changes; my ($lat, $lon, $areas) = ($report->latitude, $report->longitude, $report->areas); $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { latitude => 52.038712, longitude => -1.346397, include_update => undef } }); $mech->content_lacks('Invalid location'); $report->discard_changes; is $report->areas, ",151767,2237,2419,", 'Areas set correctly'; $mech->submit_form_ok({ button => 'save', with_fields => { latitude => $lat, longitude => $lon, include_update => undef } }); $report->discard_changes; is $report->areas, $areas, 'Areas reset correctly'; }; subtest "test duplicate reports are shown" => sub { my $old_state = $report->state; $report->set_extra_metadata('duplicate_of' => $report2->id); $report->state('duplicate'); $report->update; $report2->set_extra_metadata('duplicates' => [ $report->id ]); $report2->update; $mech->get_ok("/report/$report_id"); $mech->content_contains($report2->title); $mech->get_ok("/report/$report2_id"); $mech->content_contains($report->title); $report->unset_extra_metadata('duplicate_of'); $report->state($old_state); $report->update; $report2->unset_extra_metadata('duplicates'); $report2->update; }; subtest "can mark a report as duplicate without supplying a duplicate and a public update" => sub { my $old_state = $report->state; $report->comments->delete_all; $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', include_update => "0" } }); $mech->content_contains('provide a duplicate ID', "error message about missing duplicate id"); $report->discard_changes; $report2->discard_changes; is $report->state, $old_state, 'report not marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 0, 'no update marking report as duplicate was left'; is $report->get_extra_metadata('duplicate_of'), undef; $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', public_update => 'This is a duplicate', include_update => "1" } }); $mech->content_lacks('provide a duplicate ID', "no error message about missing duplicate id"); $report->discard_changes; $report2->discard_changes; is $report->state, 'duplicate', 'report marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 1, 'update marking report as duplicate was left'; is $report->get_extra_metadata('duplicate_of'), undef; is_deeply $report2->get_extra_metadata('duplicates'), undef; $report->update({ state => $old_state }); }; subtest "can mark a report as duplicate without supplying a public update and a duplicate id" => sub { my $old_state = $report->state; $report->comments->delete_all; $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', include_update => "0" } }); $mech->content_contains('provide a duplicate ID', "error message about missing duplicate id"); $report->discard_changes; $report2->discard_changes; is $report->state, $old_state, 'report not marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 0, 'no update marking report as duplicate was left'; is $report->get_extra_metadata('duplicate_of'), undef; $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', duplicate_of => $report2->id, include_update => "0" } }); $mech->content_lacks('provide a duplicate ID', "no error message about missing duplicate id"); $report->discard_changes; $report2->discard_changes; is $report->state, 'duplicate', 'report marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 1, 'update marking report as duplicate was left'; is $report->get_extra_metadata('duplicate_of'), $report2->id; is_deeply $report2->get_extra_metadata('duplicates'), [ $report->id ]; # Check that duplicate does not include shortlist add button (no form in form) $mech->get_ok("/report/$report_id"); $mech->content_lacks('item-list__item__shortlist-add'); $report->set_extra_metadata('duplicate_of', undef); $report->update({ state => $old_state }); $report2->set_extra_metadata('duplicates', undef); $report2->update; }; subtest "marking a report as a duplicate with update correctly sets update status" => sub { my $old_state = $report->state; $report->comments->delete_all; $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', duplicate_of => $report2->id, public_update => "This is a duplicate.", include_update => "1" } }); $report->discard_changes; $report2->discard_changes; is $report->state, 'duplicate', 'report marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 1, 'update marking report as duplicate was left'; is $report->get_extra_metadata('duplicate_of'), $report2->id; is_deeply $report2->get_extra_metadata('duplicates'), [ $report->id ]; $report->update({ state => $old_state }); }; subtest "changing state does not add another alert" =>sub { $mech->get_ok("/report/$report_id"); $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Investigating', public_update => "We're investigating.", include_update => "1" } }); my $alert_count = FixMyStreet::App->model('DB::Alert')->search( { user_id => $user->id, alert_type => 'new_updates', confirmed => 1, parameter => $report_id } )->count(); is $alert_count, 1 , 'User has only one alert'; }; subtest "marking a report as a duplicate doesn't clobber user-provided update" => sub { my $old_state = $report->state; $report->comments->delete_all; $mech->get_ok("/report/$report2_id/nearby.json"); $mech->content_lacks('Add to shortlist'); $mech->get_ok("/report/$report_id"); my $update_text = "This text was entered as an update by the user."; $mech->submit_form_ok({ button => 'save', with_fields => { state => 'Duplicate', duplicate_of => $report2->id, public_update => $update_text, include_update => "1", }}); $report->discard_changes; is $report->state, 'duplicate', 'report marked as duplicate'; is $report->comments->search({ problem_state => 'duplicate' })->count, 1, 'update marked report as duplicate'; $mech->get_ok("/report/$report_id"); # Get again as planned_reports permission means redirect to referer... $mech->content_contains($update_text); $mech->content_lacks("Thank you for your report. This problem has already been reported."); $report->update({ state => $old_state }); }; subtest "post-inspect redirect is to the right place if URL set" => sub { $mech->get_ok("/report/$report_id"); my $update_text = "This text was entered as an update by the user."; $mech->submit_form_ok({ button => 'save', with_fields => { public_update => $update_text, include_update => "1", post_inspect_url => "/" }}); is $mech->res->code, 200, "got 200"; is $mech->res->previous->code, 302, "got 302 for redirect"; is $mech->uri->path, '/', 'redirected to front page'; }; subtest "post-inspect redirect is to the right place if URL not set" => sub { $user->set_extra_metadata(categories => [ $contact->id ]); $user->update; $mech->get_ok("/report/$report_id"); my $update_text = "This text was entered as an update by the user."; $mech->submit_form_ok({ button => 'save', with_fields => { public_update => $update_text, include_update => "1", post_inspect_url => "" }}); is $mech->res->code, 200, "got 200"; is $mech->res->previous->code, 302, "got 302 for redirect"; is $mech->uri->path, '/around', 'redirected to /around'; my %params = $mech->uri->query_form; is $params{lat}, $report->latitude, "latitude param is correct"; is $params{lon}, $report->longitude, "longitude param is correct"; is $params{filter_category}, $contact->category, "categories param is correct"; }; subtest "default response priorities display correctly" => sub { $mech->get_ok("/report/$report_id"); $mech->content_contains('Prioritycontent, qr/[^<]*