diff options
Diffstat (limited to 't')
-rw-r--r-- | t/app/controller/report_display.t | 106 | ||||
-rw-r--r-- | t/app/controller/report_new_open311.t | 5 | ||||
-rw-r--r-- | t/app/script/archive_old_enquiries.t | 16 | ||||
-rw-r--r-- | t/cobrand/rutland.t | 60 | ||||
-rw-r--r-- | t/cobrand/zurich.t | 147 | ||||
-rw-r--r-- | t/open311/getservicerequests.t | 301 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 48 | ||||
-rw-r--r-- | t/open311/populate-service-list.t | 90 | ||||
-rw-r--r-- | t/sendreport/open311.t | 81 |
9 files changed, 720 insertions, 134 deletions
diff --git a/t/app/controller/report_display.t b/t/app/controller/report_display.t index f0913fbd2..17b9180c1 100644 --- a/t/app/controller/report_display.t +++ b/t/app/controller/report_display.t @@ -115,6 +115,12 @@ subtest "duplicate reports are signposted correctly" => sub { $report2->update; }; +subtest "test /report/ajax" => sub { + my $json = $mech->get_ok_json( "/report/ajax/$report_id" ); + is $json->{report}->{title}, "Test 2", "correct title"; + is $json->{report}->{state}, "confirmed", "correct state"; +}; + subtest "test a good report" => sub { $mech->get_ok("/report/$report_id"); is $mech->uri->path, "/report/$report_id", "at /report/$report_id"; @@ -419,106 +425,6 @@ for my $test ( }; } -subtest "Zurich unconfirmeds are 200" => sub { - FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'zurich' ], - MAP_TYPE => 'Zurich,OSM', - }, sub { - $mech->host( 'zurich.example.com' ); - ok $report->update( { state => 'unconfirmed' } ), 'unconfirm report'; - $mech->get_ok("/report/$report_id"); - $mech->content_contains( 'Überprüfung ausstehend' ); - ok $report->update( { state => 'confirmed' } ), 'confirm report again'; - $mech->host( 'www.fixmystreet.com' ); - }; -}; - -subtest "Zurich banners are displayed correctly" => sub { - FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'zurich' ], - MAP_TYPE => 'Zurich,OSM', - }, sub { - $mech->host( 'zurich.example.com' ); - - for my $test ( - { - description => 'new report', - state => 'unconfirmed', - banner_id => 'closed', - banner_text => 'Erfasst' - }, - { - description => 'confirmed report', - state => 'confirmed', - banner_id => 'closed', - banner_text => 'Aufgenommen', - }, - { - description => 'fixed report', - state => 'fixed - council', - banner_id => 'fixed', - banner_text => 'Beantwortet', - }, - { - description => 'closed report', - state => 'closed', - banner_id => 'closed', - banner_text => _('Extern'), - }, - { - description => 'in progress report', - state => 'in progress', - banner_id => 'progress', - banner_text => 'In Bearbeitung', - }, - { - description => 'planned report', - state => 'planned', - banner_id => 'progress', - banner_text => 'In Bearbeitung', - }, - { - description => 'planned report', - state => 'planned', - banner_id => 'progress', - banner_text => 'In Bearbeitung', - }, - { - description => 'jurisdiction unknown', - state => 'unable to fix', - banner_id => 'fixed', - # We can't use _('Jurisdiction Unknown') here because - # TestMech::extract_problem_banner decodes the HTML entities before - # the string is passed back. - banner_text => 'Zust\x{e4}ndigkeit unbekannt', - }, - ) { - subtest "banner for $test->{description}" => sub { - $report->state( $test->{state} ); - $report->update; - - $mech->get_ok("/report/$report_id"); - is $mech->uri->path, "/report/$report_id", "at /report/$report_id"; - my $banner = $mech->extract_problem_banner; - if ( $banner->{text} ) { - $banner->{text} =~ s/^ //g; - $banner->{text} =~ s/ $//g; - } - - is $banner->{id}, $test->{banner_id}, 'banner id'; - if ($test->{banner_text}) { - like_string( $banner->{text}, qr/$test->{banner_text}/i, 'banner text is ' . $test->{banner_text} ); - } else { - is $banner->{text}, $test->{banner_text}, 'banner text'; - } - - }; - } - - $mech->host( 'www.fixmystreet.com' ); - }; -}; - my $oxfordshire = $mech->create_body_ok(2237, 'Oxfordshire County Council'); my $oxfordshireuser = $mech->create_user_ok('counciluser@example.com', name => 'Council User', from_body => $oxfordshire); diff --git a/t/app/controller/report_new_open311.t b/t/app/controller/report_new_open311.t index 0224e7e47..d79f7883c 100644 --- a/t/app/controller/report_new_open311.t +++ b/t/app/controller/report_new_open311.t @@ -64,7 +64,7 @@ foreach my $test ( }, changes => { number => '', - type => 'old', + type => '', }, errors => [ 'This information is required', @@ -80,6 +80,7 @@ foreach my $test ( username => 'testopen311@example.com', category => 'Street lighting', number => 27, + type => 'old', }, extra => [ { @@ -146,7 +147,7 @@ foreach my $test ( } ->scrape( $mech->response ); - is_deeply $result->{option}, [ qw/old modern/], 'displayed streetlight type select'; + is_deeply $result->{option}, [ "", qw/old modern/], 'displayed streetlight type select'; } $new_values = { diff --git a/t/app/script/archive_old_enquiries.t b/t/app/script/archive_old_enquiries.t index e1adeec85..0475cb9ea 100644 --- a/t/app/script/archive_old_enquiries.t +++ b/t/app/script/archive_old_enquiries.t @@ -5,15 +5,18 @@ my $mech = FixMyStreet::TestMech->new(); $mech->clear_emails_ok; -my $opts = { - commit => 1, -}; - my $user = $mech->create_user_ok('test@example.com', name => 'Test User'); my $oxfordshire = $mech->create_body_ok(2237, 'Oxfordshire County Council'); my $west_oxon = $mech->create_body_ok(2420, 'West Oxfordshire District Council'); -$opts->{body} = $oxfordshire->id; +my $opts = { + commit => 1, + body => $oxfordshire->id, + cobrand => 'oxfordshire', + closure_cutoff => "2015-01-01 00:00:00", + email_cutoff => "2016-01-01 00:00:00", + user => $user->id, +}; subtest 'sets reports to the correct status' => sub { FixMyStreet::override_config { @@ -65,6 +68,9 @@ subtest 'sets reports to the correct status' => sub { is $report4->state, 'closed', 'Report 4 has been set to closed'; is $report5->state, 'closed', 'Report 5 has been set to closed'; + my $comment = $report1->comments->first; + is $comment->problem_state, 'closed'; + is $report->state, 'confirmed', 'Recent report has been left alone'; }; }; diff --git a/t/cobrand/rutland.t b/t/cobrand/rutland.t new file mode 100644 index 000000000..8943e64fc --- /dev/null +++ b/t/cobrand/rutland.t @@ -0,0 +1,60 @@ +use CGI::Simple; +use FixMyStreet::TestMech; +use FixMyStreet::Script::Reports; +my $mech = FixMyStreet::TestMech->new; + +# Create test data +my $user = $mech->create_user_ok( 'rutland@example.com' ); +my $body = $mech->create_body_ok( 2482, 'Rutland County Council'); +my $contact = $mech->create_contact_ok( + body_id => $body->id, + category => 'Other', + email => 'LIGHT', +); +$contact->update; + +my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', { + cobrand => 'rutland', + user => $user, +}); +my $report = $reports[0]; + +for my $update ('in progress', 'unable to fix') { + FixMyStreet::DB->resultset('Comment')->find_or_create( { + problem_state => $update, + problem_id => $report->id, + user_id => $user->id, + name => 'User', + mark_fixed => 'f', + text => "This update marks it as $update", + state => 'confirmed', + confirmed => 'now()', + anonymous => 'f', + } ); +} + +subtest 'testing special Open311 behaviour', sub { + $report->set_extra_fields(); + $report->update; + $body->update( { send_method => 'Open311', endpoint => 'http://rutland.endpoint.example.com', jurisdiction => 'FMS', api_key => 'test', send_comments => 1 } ); + my $test_data; + FixMyStreet::override_config { + STAGING_FLAGS => { send_reports => 1 }, + ALLOWED_COBRANDS => [ 'fixmystreet', 'rutland' ], + }, sub { + $test_data = FixMyStreet::Script::Reports::send(); + }; + $report->discard_changes; + ok $report->whensent, 'Report marked as sent'; + is $report->send_method_used, 'Open311', 'Report sent via Open311'; + is $report->external_id, 248, 'Report has right external ID'; + + my $req = $test_data->{test_req_used}; + my $c = CGI::Simple->new($req->content); + is $c->param('attribute[title]'), $report->title, 'Request had title'; + is $c->param('attribute[description]'), $report->detail, 'Request had description'; + is $c->param('attribute[external_id]'), $report->id, 'Request had correct ID'; + is $c->param('jurisdiction_id'), 'FMS', 'Request had correct jurisdiction'; +}; + +done_testing(); diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 4ae9a0cde..eccb0c8eb 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -23,6 +23,7 @@ my $mech = FixMyStreet::TestMech->new; use FixMyStreet; my $cobrand = FixMyStreet::Cobrand::Zurich->new(); +$cobrand->db_state_migration; my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg"); ok $sample_file->exists, "sample file $sample_file exists"; @@ -48,7 +49,7 @@ sub reset_report_state { $report->unset_extra_metadata('closed_overdue'); $report->unset_extra_metadata('closure_status'); $report->whensent(undef); - $report->state('unconfirmed'); + $report->state('submitted'); $report->created($created) if $created; $report->update; } @@ -109,7 +110,7 @@ subtest "set up superuser" => sub { }; my @reports = $mech->create_problems_for_body( 1, $division->id, 'Test', { - state => 'unconfirmed', + state => 'submitted', confirmed => undef, cobrand => 'zurich', areas => ',423017,', @@ -125,6 +126,89 @@ FixMyStreet::override_config { $mech->content_contains('Überprüfung ausstehend') or die $mech->content; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + MAP_TYPE => 'Zurich,OSM', +}, sub { + my $json = $mech->get_ok_json( '/report/ajax/' . $report->id ); + is $json->{report}->{title}, "Überprüfung ausstehend", "correct title"; + is $json->{report}->{state}, "submitted", "correct state"; +}; + +subtest "Banners are displayed correctly" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + MAP_TYPE => 'Zurich,OSM', + }, sub { + for my $test ( + { + description => 'new report', + state => 'submitted', + banner_id => 'closed', + banner_text => 'Erfasst' + }, + { + description => 'confirmed report', + state => 'confirmed', + banner_id => 'closed', + banner_text => 'Aufgenommen', + }, + { + description => 'fixed report', + state => 'fixed - council', + banner_id => 'fixed', + banner_text => 'Beantwortet', + }, + { + description => 'closed report', + state => 'external', + banner_id => 'closed', + banner_text => 'Extern', + }, + { + description => 'in progress report', + state => 'in progress', + banner_id => 'progress', + banner_text => 'In Bearbeitung', + }, + { + description => 'planned report', + state => 'feedback pending', + banner_id => 'progress', + banner_text => 'In Bearbeitung', + }, + { + description => 'jurisdiction unknown', + state => 'jurisdiction unknown', + banner_id => 'fixed', + banner_text => 'Zust\x{e4}ndigkeit unbekannt', + }, + ) { + subtest "banner for $test->{description}" => sub { + $report->state( $test->{state} ); + $report->update; + + $mech->get_ok("/report/" . $report->id); + is $mech->uri->path, "/report/" . $report->id, "at /report/" . $report->id; + my $banner = $mech->extract_problem_banner; + if ( $banner->{text} ) { + $banner->{text} =~ s/^ //g; + $banner->{text} =~ s/ $//g; + } + + is $banner->{id}, $test->{banner_id}, 'banner id'; + if ($test->{banner_text}) { + like_string( $banner->{text}, qr/$test->{banner_text}/i, 'banner text is ' . $test->{banner_text} ); + } else { + is $banner->{text}, $test->{banner_text}, 'banner text'; + } + + }; + } + $report->update({ state => 'submitted' }); + }; +}; + # Check logging in to deal with this report FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'zurich' ], @@ -158,7 +242,7 @@ subtest "changing of categories" => sub { ); } - # full Categories dropdown is hidden for unconfirmed reports + # full Categories dropdown is hidden for submitted reports $report->update({ state => 'confirmed' }); # put report into known category @@ -274,7 +358,7 @@ subtest "report_edit" => sub { $report->discard_changes; is ( $report->get_extra_metadata('moderated_overdue'), 0, 'Still marked moderated_overdue' ); is ( $report->get_extra_metadata('closed_overdue'), undef, "Marking hidden doesn't set closed_overdue..." ); - is ( $report->state, 'planned', 'Marking hidden actually sets state to planned'); + is ( $report->state, 'feedback pending', 'Marking hidden actually sets state to feedback pending'); is ( $report->get_extra_metadata('closure_status'), 'hidden', 'Marking hidden sets closure_status to hidden'); is get_moderated_count(), 1, 'Check still counted moderated' or diag $report->get_column('extra'); @@ -411,6 +495,12 @@ subtest 'SDM' => sub { $mech->submit_form_ok( { button => 'no_more_updates' } ); is $mech->uri->path, '/admin/summary', "redirected now finished with report."; + # Can still view the edit page but can't change anything + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_contains('<input disabled'); + $mech->submit_form_ok( { with_fields => { status_update => 'This is a disallowed update.' } } ); + $mech->content_lacks('This is a disallowed update'); + $mech->get_ok( '/report/' . $report->id ); $mech->content_contains('In Bearbeitung'); $mech->content_contains('Test Test'); @@ -423,7 +513,7 @@ subtest 'SDM' => sub { $mech->clear_emails_ok; $report->discard_changes; - is $report->state, 'planned', 'Report now in planned state'; + is $report->state, 'feedback pending', 'Report now in feedback pending state'; subtest 'send_back' => sub { FixMyStreet::override_config { @@ -448,8 +538,8 @@ subtest 'SDM' => sub { $mech->get_ok( '/admin/report_edit/' . $report->id ); $mech->submit_form_ok( { button => 'not_contactable', form_number => 2 } ); $report->discard_changes; - is $report->state, 'planned', 'Report sent back to Rueckmeldung ausstehend state'; - is $report->get_extra_metadata('closure_status'), 'partial', 'Report sent back to partial (not_contactable) state'; + is $report->state, 'feedback pending', 'Report sent back to Rueckmeldung ausstehend state'; + is $report->get_extra_metadata('closure_status'), 'not contactable', 'Report sent back to not_contactable state'; is $report->bodies_str, $division->id, 'Report sent back to division'; }; }; @@ -465,7 +555,7 @@ FixMyStreet::override_config { }; reset_report_state($report); -$report->update({ state => 'planned' }); +$report->update({ state => 'feedback pending' }); $mech->content_contains( 'report_edit/' . $report->id ); $mech->content_contains( DateTime->now->strftime("%d.%m.%Y") ); @@ -500,9 +590,9 @@ like $email->header('From'), qr/do-not-reply\@example.org/, 'from line looks cor like $email->body, qr/FINAL UPDATE/, 'body looks correct'; $mech->clear_emails_ok; -# Assign planned (via confirmed), don't confirm email +# Assign feedback pending (via confirmed), don't confirm email @reports = $mech->create_problems_for_body( 1, $division->id, 'Second', { - state => 'unconfirmed', + state => 'submitted', confirmed => undef, cobrand => 'zurich', areas => ',423017,', @@ -516,7 +606,7 @@ FixMyStreet::override_config { $mech->get_ok( '/admin/report_edit/' . $report->id ); $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); $mech->get_ok( '/admin/report_edit/' . $report->id ); - $mech->submit_form_ok( { with_fields => { state => 'planned' } } ); + $mech->submit_form_ok( { with_fields => { state => 'feedback pending' } } ); $mech->get_ok( '/report/' . $report->id ); }; $mech->content_contains('In Bearbeitung'); @@ -543,7 +633,7 @@ $mech->email_count_is(0); # Report assigned to third party @reports = $mech->create_problems_for_body( 1, $division->id, 'Third', { - state => 'unconfirmed', + state => 'submitted', confirmed => undef, cobrand => 'zurich', areas => ',423017,', @@ -558,8 +648,8 @@ subtest "external report triggers email" => sub { }, sub { # required to see body_external field - $report->state('planned'); - $report->set_extra_metadata('closure_status' => 'closed'); + $report->state('feedback pending'); + $report->set_extra_metadata('closure_status' => 'external'); # Set the public_response manually here because the default one will have line breaks that get escaped as HTML, causing the comparison to fail. $report->set_extra_metadata('public_response' => 'Freundliche Gruesse Ihre Stadt Zuerich'); $report->update; @@ -575,7 +665,7 @@ subtest "external report triggers email" => sub { $report->discard_changes; $mech->get_ok( '/report/' . $report->id ); }; - is ($report->state, 'closed', 'Report was closed correctly'); + is ($report->state, 'external', 'Report was closed correctly'); $mech->content_contains('Extern') or die $mech->content; $mech->content_contains('Third Test'); @@ -596,8 +686,8 @@ subtest "external report triggers email" => sub { }, sub { $mech->get_ok( '/admin' ); # required to see body_external field - $report->state('planned'); - $report->set_extra_metadata('closure_status' => 'closed'); + $report->state('feedback pending'); + $report->set_extra_metadata('closure_status' => 'external'); $report->set_extra_metadata('public_response' => 'Freundliche Gruesse Ihre Stadt Zuerich'); $report->update; @@ -632,10 +722,10 @@ subtest "external report triggers email" => sub { }, sub { # set as wish $report->discard_changes; - $report->state('planned'); - $report->set_extra_metadata('closure_status' => 'investigating'); + $report->state('feedback pending'); + $report->set_extra_metadata('closure_status' => 'wish'); $report->update; - is ($report->state, 'planned', 'Sanity check') or die; + is ($report->state, 'feedback pending', 'Sanity check') or die; $mech->get_ok( '/admin/report_edit/' . $report->id ); @@ -646,6 +736,9 @@ subtest "external report triggers email" => sub { body_external => $external_body->id, external_message => $EXTERNAL_MESSAGE, } }); + # Wishes publicly viewable + $mech->get_ok( '/report/' . $report->id ); + $mech->content_contains('Freundliche Gruesse Ihre Stadt Zuerich'); }; send_reports_for_zurich(); $email = $mech->get_email; @@ -665,12 +758,12 @@ subtest "external report triggers email" => sub { }, sub { # set as extern reset_report_state($report); - $report->state('planned'); - $report->set_extra_metadata('closure_status' => 'closed'); + $report->state('feedback pending'); + $report->set_extra_metadata('closure_status' => 'external'); $report->set_extra_metadata('email_confirmed' => 1); $report->unset_extra_metadata('public_response'); $report->update; - is ($report->state, 'planned', 'Sanity check') or die; + is ($report->state, 'feedback pending', 'Sanity check') or die; $mech->get_ok( '/admin/report_edit/' . $report->id ); @@ -872,7 +965,7 @@ subtest "test admin_log" => sub { # XXX: following is dependent on all of test up till now, rewrite to explicitly # test which things need to be logged! is scalar @entries, 4, 'State changes logged'; - is $entries[-1]->action, 'state change to closed', 'State change logged as expected'; + is $entries[-1]->action, 'state change to external', 'State change logged as expected'; }; subtest 'email images to external partners' => sub { @@ -892,7 +985,7 @@ subtest 'email images to external partners' => sub { # The below email comparison must not have an external message. $report->unset_extra_metadata('external_message'); $report->update({ - state => 'closed', + state => 'external', photo => $fileid, external_body => $external_body->id, }); @@ -944,9 +1037,9 @@ subtest 'Status update shown as appropriate' => sub { }, sub { # ALL closed states must hide the public_response edit, and public ones # must show the answer in blue. - for (['planned', 1, 0, 0], + for (['feedback pending', 1, 0, 0], ['fixed - council', 0, 1, 0], - ['closed', 0, 1, 0], + ['external', 0, 1, 0], ['hidden', 0, 0, 1]) { my ($state, $update, $public, $user_response) = @$_; diff --git a/t/open311/getservicerequests.t b/t/open311/getservicerequests.t new file mode 100644 index 000000000..878c178ef --- /dev/null +++ b/t/open311/getservicerequests.t @@ -0,0 +1,301 @@ +#!/usr/bin/env perl + +use FixMyStreet::TestMech; + +use_ok( 'Open311' ); +use_ok( 'Open311::GetServiceRequests' ); +use DateTime; +use DateTime::Format::W3CDTF; + +my $mech = FixMyStreet::TestMech->new; + +my $user = $mech->create_user_ok('system_user@example.com', name => 'test users'); +my $body = $mech->create_body_ok(2482, 'Bromley'); +my $contact = $mech->create_contact_ok( body_id => $body->id, category => 'sidewalks', email => 'sidewalks@example.com' ); + +my $dtf = DateTime::Format::W3CDTF->new; + +my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> +<service_requests> +<request> +<service_request_id>638344</service_request_id> +<status>open</status> +<status_notes>This is a note.</status_notes> +<service_name>Sidewalk and Curb Issues</service_name> +<service_code>sidewalks</service_code> +<description>This is a sidewalk problem</description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +<updated_datetime>2010-04-14T06:37:38-08:00</updated_datetime> +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>51.4021</lat> +<long>0.01578</long> +</request> +<request> +<service_request_id>638345</service_request_id> +<status>investigating</status> +<status_notes>This is a for a different issue.</status_notes> +<service_name>Not Sidewalk and Curb Issues</service_name> +<service_code>not_sidewalks</service_code> +<description>This is a problem</description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-15T06:37:38-08:00</requested_datetime> +<updated_datetime>2010-04-15T06:37:38-08:00</updated_datetime> +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>51.4021</lat> +<long>0.01578</long> +</request> +</service_requests> +}; + +my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'requests.xml' => $requests_xml } +); + +subtest 'basic parsing checks' => sub { + my $update = Open311::GetServiceRequests->new( system_user => $user ); + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $update->create_problems( $o, $body ); + }; + + my $p1_date = $dtf->parse_datetime('2010-04-14T06:37:38-08:00') + ->set_time_zone( + FixMyStreet->time_zone || FixMyStreet->local_time_zone + ); + + my $p = FixMyStreet::DB->resultset('Problem')->search( + { external_id => 638344 } + )->first; + + ok $p, 'Found problem'; + is $p->detail, 'This is a sidewalk problem', 'correct problem description'; + is $p->created, $p1_date, 'Problem has correct creation date'; + is $p->confirmed, $p1_date, 'Problem has correct confirmed date'; + is $p->whensent, $p1_date, 'Problem has whensent set'; + is $p->state, 'confirmed', 'correct problem state'; + is $p->user->id, $user->id, 'user set to system user'; + is $p->category, 'sidewalks', 'correct problem category'; + + my $p2 = FixMyStreet::DB->resultset('Problem')->search( { external_id => 638345 } )->first; + ok $p2, 'second problem found'; + ok $p2->whensent, 'second problem marked sent'; + is $p2->state, 'investigating', 'second problem correct state'; + is $p2->category, 'Other', 'category falls back to Other'; +}; + +subtest 'check problems not re-created' => sub { + my $update = Open311::GetServiceRequests->new( system_user => $user ); + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $update->create_problems( $o, $body ); + }; + + my $count = FixMyStreet::DB->resultset('Problem')->count; + + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $update->create_problems( $o, $body ); + }; + + my $after_count = FixMyStreet::DB->resultset('Problem')->count; + + is $count, $after_count, "problems not re-created"; +}; + +for my $test ( + { + desc => 'problem with no id is not created', + detail => 'This is a problem with no service_code', + subs => { id => '', desc => 'This is a problem with service code' }, + }, + { + desc => 'problem with no lat is not created', + detail => 'This is a problem with no lat', + subs => { lat => '', desc => 'This is a problem with no lat' }, + }, + { + desc => 'problem with no long is not created', + detail => 'This is a problem with no long', + subs => { long => '', desc => 'This is a problem with no long' }, + }, + { + desc => 'problem with bad lat/long is not created', + detail => 'This is a problem with bad lat/long', + subs => { lat => '51', long => 0.1, desc => 'This is a problem with bad lat/long' }, + }, +) { + subtest $test->{desc} => sub { + my $xml = prepare_xml( $test->{subs} ); + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'requests.xml' => $xml} + ); + + my $count = FixMyStreet::DB->resultset('Problem')->count; + my $update = Open311::GetServiceRequests->new( system_user => $user ); + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $update->create_problems( $o, $body ); + }; + my $after_count = FixMyStreet::DB->resultset('Problem')->count; + + warn $count; + is $count, $after_count, "problems not created"; + + my $with_text = FixMyStreet::DB->resultset('Problem')->search( { + detail => $test->{detail} + } )->count; + + is $with_text, 0, 'no matching problem created'; + }; +} + +my $date = DateTime->new( + year => 2010, + month => 4, + day => 14, + hour => 6, + minute => 37 +); + +for my $test ( + { + start_date => '1', + end_date => '', + desc => 'do not process if only a start_date', + subs => {}, + }, + { + start_date => '', + end_date => '1', + desc => 'do not process if only an end_date', + subs => {}, + }, +) { + subtest $test->{desc} => sub { + my $xml = prepare_xml( $test->{subs} ); + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'requests.xml' => $xml} + ); + + my $update = Open311::GetServiceRequests->new( + start_date => $test->{start_date}, + end_date => $test->{end_date}, + system_user => $user, + ); + my $ret = $update->create_problems( $o, $body ); + + is $ret, 0, 'failed correctly' + }; +} + +$date = DateTime->new( + year => 2010, + month => 4, + day => 14, + hour => 6, + minute => 37 +); + +for my $test ( + { + start_date => $date->clone->add(hours => -2), + end_date => $date->clone->add(hours => -1), + desc => 'do not process if update time after end_date', + subs => {}, + }, + { + start_date => $date->clone->add(hours => 2), + end_date => $date->clone->add(hours => 4), + desc => 'do not process if update time before start_date', + subs => {}, + }, + { + start_date => $date->clone->add(hours => -2), + end_date => $date->clone->add(hours => 4), + desc => 'do not process if update time is bad', + subs => { update_time => '2010/12/12' }, + }, +) { + subtest $test->{desc} => sub { + my $xml = prepare_xml( $test->{subs} ); + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'requests.xml' => $xml} + ); + + my $update = Open311::GetServiceRequests->new( + start_date => $test->{start_date}, + end_date => $test->{end_date}, + system_user => $user, + ); + my $count = FixMyStreet::DB->resultset('Problem')->count; + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $update->create_problems( $o, $body ); + }; + my $after = FixMyStreet::DB->resultset('Problem')->count; + + is $count, $after, 'problem not added'; + }; +} + +sub prepare_xml { + my $replacements = shift; + + my %defaults = ( + desc => 'this is a problem', + lat => 51.4021, + long => 0.01578, + id => 123456, + update_time => '2010-04-14T06:37:38-08:00', + %$replacements + ); + + my $xml = qq[<?xml version="1.0" encoding="utf-8"?> +<service_requests> +<request> +<service_request_id>XXX_ID</service_request_id> +<status>open</status> +<status_notes></status_notes> +<service_name>Sidewalk and Curb Issues</service_name> +<service_code>sidewalks</service_code> +<description>XXX_DESC</description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +<updated_datetime>XXX_UPDATE_TIME</updated_datetime> +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>XXX_LAT</lat> +<long>XXX_LONG</long> +</request> +</service_requests> +]; + + for my $key (keys %defaults) { + my $string = 'XXX_' . uc $key; + $xml =~ s/$string/$defaults{$key}/; + } + + return $xml; +} + +done_testing(); diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index da427e505..a53354685 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use FixMyStreet::Test; +use Test::Output; use CGI::Simple; use LWP::Protocol::PSGI; use t::Mock::Static; @@ -782,6 +783,53 @@ foreach my $test ( { } } +foreach my $test ( { + desc => 'normally blank text produces a warning', + num_alerts => 1, + blank_updates_permitted => 0, + }, + { + desc => 'no warning if blank updates permitted', + num_alerts => 1, + blank_updates_permitted => 1, + }, +) { + subtest $test->{desc} => sub { + my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> + <service_requests_updates> + <request_update> + <update_id>638344</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <status>closed</status> + <description></description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + </request_update> + </service_requests_updates> + }; + + $problem->state( 'confirmed' ); + $problem->lastupdate( $dt->clone->subtract( hours => 3 ) ); + $problem->update; + + $requests_xml =~ s/UPDATED_DATETIME/$dt/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } ); + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + blank_updates_permitted => $test->{blank_updates_permitted}, + ); + + if ( $test->{blank_updates_permitted} ) { + stderr_is { $update->update_comments( $o, $bodies{2482} ) } '', 'No error message' + } else { + stderr_like { $update->update_comments( $o, $bodies{2482} ) } qr/Couldn't determine update text for/, 'Error message displayed' + } + $problem->discard_changes; + $problem->comments->delete; + } +} + done_testing(); sub setup_xml { diff --git a/t/open311/populate-service-list.t b/t/open311/populate-service-list.t index 7d4f491c6..149fb4b2a 100644 --- a/t/open311/populate-service-list.t +++ b/t/open311/populate-service-list.t @@ -646,6 +646,96 @@ subtest 'check bromely skip code' => sub { is_deeply $contact->get_extra_fields, $extra, 'all meta data saved for non bromley'; }; +subtest 'check automated meta skip code' => sub { + my $processor = Open311::PopulateServiceList->new(); + + my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> +<service_definition> + <service_code>100</service_code> + <attributes> + <attribute> + <variable>true</variable> + <code>type</code> + <datatype>string</datatype> + <required>true</required> + <datatype_description>Type of bin</datatype_description> + <order>1</order> + <description>Type of bin</description> + </attribute> + <attribute> + <automated>server_set</automated> + <variable>true</variable> + <code>title</code> + <datatype>string</datatype> + <required>true</required> + <datatype_description>Type of bin</datatype_description> + <order>1</order> + <description>Type of bin</description> + </attribute> + <attribute> + <automated>hidden_field</automated> + <variable>true</variable> + <code>asset_id</code> + <datatype>string</datatype> + <required>true</required> + <datatype_description>Id of bin</datatype_description> + <order>1</order> + <description>Id of bin</description> + </attribute> + </attributes> +</service_definition> + '; + + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( + { + body_id => 1, + email => '001', + category => 'Bins left out 24x7', + state => 'confirmed', + editor => $0, + whenedited => \'current_timestamp', + note => 'test contact', + } + ); + + my $o = Open311->new( + jurisdiction => 'mysociety', + endpoint => 'http://example.com', + test_mode => 1, + test_get_returns => { 'services/100.xml' => $meta_xml } + ); + + $processor->_current_open311( $o ); + $processor->_current_body( $body ); + $processor->_current_service( { service_code => 100 } ); + + $processor->_add_meta_to_contact( $contact ); + + my $extra = [ { + variable => 'true', + code => 'type', + datatype => 'string', + required => 'true', + datatype_description => 'Type of bin', + order => 1, + description => 'Type of bin' + }, + { + automated => 'hidden_field', + variable => 'true', + code => 'asset_id', + datatype => 'string', + required => 'true', + datatype_description => 'Id of bin', + order => 1, + description => 'Id of bin' + } ]; + + $contact->discard_changes; + + is_deeply $contact->get_extra_fields, $extra, 'only hidden automated meta data saved'; +}; + sub get_standard_xml { return qq{<?xml version="1.0" encoding="utf-8"?> <services> diff --git a/t/sendreport/open311.t b/t/sendreport/open311.t index 1eb5535aa..23096aaac 100644 --- a/t/sendreport/open311.t +++ b/t/sendreport/open311.t @@ -1,4 +1,16 @@ +package FixMyStreet::Cobrand::Tester; + +use parent 'FixMyStreet::Cobrand::FixMyStreet'; + +sub open311_config { + my ($self, $row, $h, $params) = @_; + $params->{multi_photos} = 1; +} + +package main; + use CGI::Simple; +use Path::Tiny; use FixMyStreet::Script::Reports; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -41,4 +53,73 @@ subtest 'testing Open311 behaviour', sub { is $c->param('jurisdiction_id'), 'FMS', 'Request had correct jurisdiction'; }; +my ($photo_report) = $mech->create_problems_for_body( 1, $body->id, 'Test', { + cobrand => 'fixmystreet', + category => 'Potholes', + user => $user, +}); +my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg"); +my $UPLOAD_DIR = File::Temp->newdir(); +my @files = map { $_ x 40 . ".jpeg" } (1..3); +$sample_file->copy(path($UPLOAD_DIR, $_)) for @files; +$photo_report->photo(join(',', @files)); +$photo_report->update; + +subtest 'test report with multiple photos only sends one', sub { + $body->update( { send_method => 'Open311', endpoint => 'http://endpoint.example.com', jurisdiction => 'FMS', api_key => 'test' } ); + my $test_data; + + FixMyStreet::override_config { + STAGING_FLAGS => { send_reports => 1 }, + ALLOWED_COBRANDS => [ 'fixmystreet' ], + }, sub { + $test_data = FixMyStreet::Script::Reports::send(); + }; + $photo_report->discard_changes; + ok $photo_report->whensent, 'Report marked as sent'; + is $photo_report->send_method_used, 'Open311', 'Report sent via Open311'; + is $photo_report->external_id, 248, 'Report has right external ID'; + + my $req = $test_data->{test_req_used}; + my $c = CGI::Simple->new($req->content); + is $c->param('attribute[easting]'), 529025, 'Request had easting'; + is $c->param('attribute[northing]'), 179716, 'Request had northing'; + is $c->param('attribute[fixmystreet_id]'), $photo_report->id, 'Request had correct ID'; + is $c->param('jurisdiction_id'), 'FMS', 'Request had correct jurisdiction'; + my @media = $c->param('media_url'); + is_deeply \@media, [ + 'http://www.example.org/photo/' . $photo_report->id .'.0.full.jpeg?11111111' + ], 'One photo in media_url'; +}; + +$photo_report->whensent(undef); +$photo_report->cobrand('tester'); +$photo_report->send_method_used(''); +$photo_report->update(); + +subtest 'test sending multiple photos', sub { + $body->update( { send_method => 'Open311', endpoint => 'http://endpoint.example.com', jurisdiction => 'FMS', api_key => 'test' } ); + my $test_data; + + FixMyStreet::override_config { + STAGING_FLAGS => { send_reports => 1 }, + ALLOWED_COBRANDS => [ 'tester' ], + }, sub { + $test_data = FixMyStreet::Script::Reports::send(); + }; + $photo_report->discard_changes; + ok $photo_report->whensent, 'Report marked as sent'; + is $photo_report->send_method_used, 'Open311', 'Report sent via Open311'; + is $photo_report->external_id, 248, 'Report has right external ID'; + + my $req = $test_data->{test_req_used}; + my $c = CGI::Simple->new($req->content); + my @media = $c->param('media_url'); + is_deeply \@media, [ + 'http://www.example.org/photo/' . $photo_report->id .'.0.full.jpeg?11111111', + 'http://www.example.org/photo/' . $photo_report->id .'.1.full.jpeg?22222222', + 'http://www.example.org/photo/' . $photo_report->id .'.2.full.jpeg?33333333' + ], 'Multiple photos in media_url'; +}; + done_testing(); |