diff options
Diffstat (limited to 't/open311/getservicerequestupdates.t')
-rw-r--r-- | t/open311/getservicerequestupdates.t | 397 |
1 files changed, 296 insertions, 101 deletions
diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index f680985a4..4651c38b2 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -12,6 +12,7 @@ use_ok( 'Open311' ); use_ok( 'Open311::GetServiceRequestUpdates' ); use DateTime; use DateTime::Format::W3CDTF; +use File::Temp 'tempdir'; use FixMyStreet::DB; my $user = FixMyStreet::DB->resultset('User')->find_or_create( @@ -22,6 +23,8 @@ my $user = FixMyStreet::DB->resultset('User')->find_or_create( my %bodies = ( 2237 => FixMyStreet::DB->resultset("Body")->create({ name => 'Oxfordshire' }), + 2494 => FixMyStreet::DB->resultset("Body")->create({ name => 'Bexley' }), + 2636 => FixMyStreet::DB->resultset("Body")->create({ name => 'Isle of Wight' }), 2482 => FixMyStreet::DB->resultset("Body")->create({ name => 'Bromley', send_method => 'Open311', @@ -33,6 +36,8 @@ my %bodies = ( 2651 => FixMyStreet::DB->resultset("Body")->create({ name => 'Edinburgh' }), ); $bodies{2237}->body_areas->create({ area_id => 2237 }); +$bodies{2494}->body_areas->create({ area_id => 2494 }); +$bodies{2636}->body_areas->create({ area_id => 2636 }); my $response_template = $bodies{2482}->response_templates->create({ title => "investigating template", @@ -129,8 +134,10 @@ subtest 'check extended request parsed correctly' => sub { }; my $problem_rs = FixMyStreet::DB->resultset('Problem'); -my $problem = $problem_rs->new( - { + +sub create_problem { + my ($body_id, $external_id) = @_; + my $problem = $problem_rs->create({ postcode => 'EH99 1SP', latitude => 1, longitude => 1, @@ -139,7 +146,7 @@ my $problem = $problem_rs->new( detail => '', used_map => 1, user_id => 1, - name => '', + name => 'Test User', state => 'confirmed', service => '', cobrand => 'default', @@ -148,12 +155,13 @@ my $problem = $problem_rs->new( created => DateTime->now()->subtract( days => 1 ), lastupdate => DateTime->now()->subtract( days => 1 ), anonymous => 1, - external_id => int(rand(time())), - bodies_str => $bodies{2482}->id, - } -); + external_id => $external_id || int(rand(time())), + bodies_str => $body_id, + }); + return $problem; +} -$problem->insert; +my $problem = create_problem($bodies{2482}->id); for my $test ( { @@ -447,27 +455,38 @@ for my $test ( }; } -my $problemOx = $problem_rs->create({ - postcode => 'EH99 1SP', - latitude => 1, - longitude => 1, - areas => 1, - title => '', - detail => '', - used_map => 1, - user_id => 1, - name => '', - state => 'confirmed', - service => '', - cobrand => 'default', - cobrand_data => '', - user => $user, - created => DateTime->now()->subtract( days => 1 ), - lastupdate => DateTime->now()->subtract( days => 1 ), - anonymous => 1, - external_id => int(rand(time())), - bodies_str => $bodies{2237}->id, +my $response_template_vars = $bodies{2482}->response_templates->create({ + title => "a placeholder action scheduled template", + text => "We are investigating this report: {{description}}", + auto_response => 1, + state => "action scheduled" }); +subtest 'Check template placeholders' => sub { + my $local_requests_xml = setup_xml($problem->external_id, $problem->id, 'ACTION_SCHEDULED', 'We will do this in the morning.'); + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', extended_statuses => undef, test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); + + $problem->lastupdate( DateTime->now()->subtract( days => 1 ) ); + $problem->state( 'fixed - council' ); + $problem->update; + + my $update = Open311::GetServiceRequestUpdates->new; + $update->fetch($o); + + is $problem->comments->count, 1, 'comment count'; + $problem->discard_changes; + + my $c = FixMyStreet::DB->resultset('Comment')->search( { external_id => 638344 } )->first; + ok $c, 'comment exists'; + is $c->text, "We are investigating this report: We will do this in the morning.", 'text correct'; + is $c->mark_fixed, 0, 'mark_closed correct'; + is $c->problem_state, 'action scheduled', 'problem_state correct'; + is $c->mark_open, 0, 'mark_open correct'; + is $c->state, 'confirmed', 'comment state correct'; + is $problem->state, 'action scheduled', 'correct problem state'; + $problem->comments->delete; +}; + +my $problemB = create_problem($bodies{2237}->id); for my $test ( { @@ -486,28 +505,68 @@ for my $test ( }, ) { subtest $test->{desc} => sub { - my $local_requests_xml = setup_xml($problemOx->external_id, $problemOx->id, $test->{comment_status}); + my $local_requests_xml = setup_xml($problemB->external_id, $problemB->id, $test->{comment_status}); my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); - $problemOx->lastupdate( DateTime->now()->subtract( days => 1 ) ); - $problemOx->state( $test->{start_state} ); - $problemOx->update; + $problemB->lastupdate( DateTime->now()->subtract( days => 1 ) ); + $problemB->state( $test->{start_state} ); + $problemB->update; + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2237}, + ); + $update->process_body; - my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $bodies{2237} ); - is $problemOx->comments->count, 1, 'comment count'; - $problemOx->discard_changes; + is $problemB->comments->count, 1, 'comment count'; + $problemB->discard_changes; my $c = FixMyStreet::DB->resultset('Comment')->search( { external_id => 638344 } )->first; ok $c, 'comment exists'; is $c->problem_state, $test->{problem_state}, 'problem_state correct'; - is $problemOx->state, $test->{end_state}, 'correct problem state'; - $problemOx->comments->delete; + is $problemB->state, $test->{end_state}, 'correct problem state'; + $problemB->comments->delete; + }; +} + +for ( + { id => 2494, cobrand => 'bexley' }, + { id => 2636, cobrand => 'isleofwight' } +) { + subtest "Marking report as fixed closes it for updates ($_->{cobrand})" => sub { + my $local_requests_xml = setup_xml($problemB->external_id, $problemB->id, 'CLOSED'); + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); + + $problemB->update( { bodies_str => $bodies{$_->{id}}->id } ); + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{$_->{id}}, + ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => $_->{cobrand}, + }, sub { + $update->process_body; + }; + + $problemB->discard_changes; + is $problemB->comments->count, 1, 'comment count'; + is $problemB->get_extra_metadata('closed_updates'), 1; + $problemB->comments->delete; }; } subtest 'Update with media_url includes image in update' => sub { + my $UPLOAD_DIR = tempdir( CLEANUP => 1 ); + FixMyStreet::override_config { + PHOTO_STORAGE_BACKEND => 'FileSystem', + PHOTO_STORAGE_OPTIONS => { + UPLOAD_DIR => $UPLOAD_DIR, + }, + }, sub { my $guard = LWP::Protocol::PSGI->register(t::Mock::Static->to_psgi_app, host => 'example.com'); my $local_requests_xml = setup_xml($problem->external_id, 1, ""); @@ -519,14 +578,19 @@ subtest 'Update with media_url includes image in update' => sub { $problem->state('confirmed'); $problem->update; - my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $bodies{2482} ); + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, + ); + $update->process_body; is $problem->comments->count, 1, 'comment count'; my $c = $problem->comments->first; is $c->external_id, 638344; is $c->photo, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg', 'photo exists'; $problem->comments->delete; + }; }; subtest 'Update with customer_reference adds reference to problem' => sub { @@ -541,8 +605,12 @@ subtest 'Update with customer_reference adds reference to problem' => sub { $problem->state('confirmed'); $problem->update; - my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $bodies{2482} ); + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, + ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 1, 'comment count'; @@ -556,11 +624,15 @@ subtest 'date for comment correct' => sub { my $local_requests_xml = setup_xml($problem->external_id, $problem->id, ""); my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); - my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, + ); FixMyStreet::override_config { TIME_ZONE => 'Australia/Sydney', }, sub { - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; }; my $comment = $problem->comments->first; @@ -569,29 +641,7 @@ subtest 'date for comment correct' => sub { $problem->comments->delete; }; -my $problem2 = $problem_rs->create( - { - postcode => 'EH99 1SP', - latitude => 1, - longitude => 1, - areas => 1, - title => '', - detail => '', - used_map => 1, - user_id => 1, - name => '', - state => 'confirmed', - service => '', - cobrand => 'default', - cobrand_data => '', - user => $user, - created => DateTime->now(), - lastupdate => DateTime->now(), - anonymous => 1, - external_id => $problem->external_id, - bodies_str => $bodies{2651}->id, - } -); +my $problem2 = create_problem($bodies{2651}->id, $problem->external_id); for my $test ( { @@ -617,8 +667,12 @@ for my $test ( my $local_requests_xml = setup_xml($test->{request_id}, $test->{request_id_ext}, ""); my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); - my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $bodies{$test->{area_id}} ); + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{$test->{area_id}}, + ); + $update->process_body; is $problem->comments->count, $test->{p1_comments}, 'comment count for first problem'; is $problem2->comments->count, $test->{p2_comments}, 'comment count for second problem'; @@ -629,33 +683,19 @@ subtest 'using start and end date' => sub { my $local_requests_xml = $requests_xml; my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); - my $start_dt = DateTime->now(); + my $start_dt = DateTime->now(formatter => DateTime::Format::W3CDTF->new); + my $end_dt = $start_dt->clone; $start_dt->subtract( days => 1 ); - my $end_dt = DateTime->now(); my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, start_date => $start_dt, - ); - - my $res = $update->update_comments( $o ); - is $res, 0, 'returns 0 if start but no end date'; - - $update = Open311::GetServiceRequestUpdates->new( - system_user => $user, - end_date => $end_dt, - ); - - $res = $update->update_comments( $o ); - is $res, 0, 'returns 0 if end but no start date'; - - $update = Open311::GetServiceRequestUpdates->new( - system_user => $user, - start_date => $start_dt, end_date => $end_dt, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; my $start = $start_dt . ''; my $end = $end_dt . ''; @@ -713,19 +753,21 @@ subtest 'check that existing comments are not duplicated' => sub { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 're-fetching updates does not add comments'; $problem->comments->delete; - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 'if comments are deleted then they are added'; }; @@ -804,9 +846,11 @@ subtest 'check that external_status_code is stored correctly' => sub { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; @@ -818,6 +862,36 @@ subtest 'check that external_status_code is stored correctly' => sub { is $problem->get_extra_metadata('external_status_code'), "101", "correct external status code"; + $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> + <service_requests_updates> + <request_update> + <update_id>638364</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <status>open</status> + <description>This is a note</description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + <external_status_code></external_status_code> + </request_update> + </service_requests_updates> + }; + + $problem->comments->delete; + + my $dt3 = $dt->clone->add( minutes => 1 ); + $requests_xml =~ s/UPDATED_DATETIME/$dt3/; + + $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } ); + + $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, + ); + + $update->process_body; + + $problem->discard_changes; + is $problem->get_extra_metadata('external_status_code'), '', "external status code unset"; }; subtest 'check that external_status_code triggers auto-responses' => sub { @@ -849,15 +923,15 @@ subtest 'check that external_status_code triggers auto-responses' => sub { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 1, 'one comment after fetching updates'; - my $comment = $problem->comments->first; - is $problem->comments->first->text, "Thank you for your report. We will provide an update within 24 hours.", "correct external status code on first comment"; }; @@ -903,9 +977,11 @@ foreach my $test ( { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; @@ -914,6 +990,117 @@ foreach my $test ( { }; } +my $response_template_in_progress = $bodies{2482}->response_templates->create({ + title => "Acknowledgement 1", + text => "Thank you for your report. We will provide an update within 48 hours.", + auto_response => 1, + state => "in progress" +}); + +for my $test ( + { + external_code => '090', + description => 'check numeric external status code in response template override state', + }, + { + external_code => 'futher', + description => 'check alpha external status code in response template override state', + }, +) { + subtest $test->{description} => 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>in_progress</status> + <description></description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + <external_status_code></external_status_code> + </request_update> + <request_update> + <update_id>638345</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <status>in_progress</status> + <description></description> + <updated_datetime>UPDATED_DATETIME2</updated_datetime> + <external_status_code>@{[ $test->{external_code} ]}</external_status_code> + </request_update> + </service_requests_updates> + }; + + my $response_template = $bodies{2482}->response_templates->create({ + # the default ordering uses the title of the report so + # make sure this comes second + title => "Acknowledgement 2", + text => "Thank you for your report. We will provide an update within 24 hours.", + auto_response => 1, + external_status_code => $test->{external_code} + }); + + $problem->comments->delete; + + my $dt2 = $dt->clone->add( minutes => 1 ); + $requests_xml =~ s/UPDATED_DATETIME/$dt/; + $requests_xml =~ s/UPDATED_DATETIME2/$dt2/; + + 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, + current_open311 => $o, + current_body => $bodies{2482}, + ); + + $update->process_body; + + $problem->discard_changes; + is $problem->comments->count, 2, 'two comment after fetching updates'; + + my @comments = $problem->comments->search(undef, { order_by => 'confirmed' }); + + is $comments[0]->text, "Thank you for your report. We will provide an update within 48 hours.", "correct external status code on first comment"; + is $comments[1]->text, "Thank you for your report. We will provide an update within 24 hours.", "correct external status code on second comment"; + $problem->comments->delete; + $response_template->delete; + }; +} + +subtest 'check that first comment always updates state' => 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>in_progress</status> + <description>This is a note</description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + </request_update> + </service_requests_updates> + }; + + $problem->state( 'confirmed' ); + $problem->lastupdate( $dt->clone->subtract( hours => 1 ) ); + $problem->update; + + $requests_xml =~ s/UPDATED_DATETIME/@{[$dt->clone->subtract( minutes => 62 )]}/; + + 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, + current_open311 => $o, + current_body => $bodies{2482}, + ); + + $update->process_body; + + $problem->discard_changes; + is $problem->comments->count, 1, 'one comment after fetching updates'; + is $problem->state, 'in progress', 'correct problem status'; + $problem->comments->delete; +}; + foreach my $test ( { desc => 'normally alerts are not suppressed', num_alerts => 1, @@ -968,9 +1155,11 @@ foreach my $test ( { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, suppress_alerts => $test->{suppress_alerts}, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; my $alerts_sent = FixMyStreet::DB->resultset('AlertSent')->search( @@ -1030,12 +1219,14 @@ foreach my $test ( { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, blank_updates_permitted => $test->{blank_updates_permitted}, + current_open311 => $o, + current_body => $bodies{2482}, ); if ( $test->{blank_updates_permitted} ) { - stderr_is { $update->update_comments( $o, $bodies{2482} ) } '', 'No error message' + stderr_is { $update->process_body } '', 'No error message' } else { - stderr_like { $update->update_comments( $o, $bodies{2482} ) } qr/Couldn't determine update text for/, 'Error message displayed' + stderr_like { $update->process_body } qr/Couldn't determine update text for/, 'Error message displayed' } $problem->discard_changes; $problem->comments->delete; @@ -1084,9 +1275,11 @@ subtest 'check matching on fixmystreet_id overrides service_request_id' => sub { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); - $update->update_comments( $o, $bodies{2482} ); + $update->process_body; $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; @@ -1119,10 +1312,12 @@ subtest 'check bad fixmystreet_id is handled' => sub { my $update = Open311::GetServiceRequestUpdates->new( system_user => $user, + current_open311 => $o, + current_body => $bodies{2482}, ); warning_like { - $update->update_comments( $o, $bodies{2482} ) + $update->process_body } qr/skipping bad fixmystreet id in updates for Bromley: \[123456 654321\], external id is 8888888888888/, "warning emitted for bad fixmystreet id"; |