diff options
Diffstat (limited to 't/open311/getservicerequestupdates.t')
-rw-r--r-- | t/open311/getservicerequestupdates.t | 313 |
1 files changed, 247 insertions, 66 deletions
diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index aba811e58..8876a3ccf 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -4,23 +4,27 @@ use strict; use warnings; use Test::More; use CGI::Simple; - -use FindBin; -use lib "$FindBin::Bin/../perllib"; -use lib "$FindBin::Bin/../commonlib/perllib"; +use LWP::Protocol::PSGI; +use t::Mock::Static; use_ok( 'Open311' ); use_ok( 'Open311::GetServiceRequestUpdates' ); use DateTime; -use FixMyStreet::App; +use DateTime::Format::W3CDTF; +use FixMyStreet::DB; -my $user = FixMyStreet::App->model('DB::User')->find_or_create( +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'system_user@example.com' } ); +my %bodies = ( + 2482 => FixMyStreet::DB->resultset("Body")->new({ id => 2482 }), + 2651 => FixMyStreet::DB->resultset("Body")->new({ id => 2651 }), +); + my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> <service_requests_updates> <request_update> @@ -34,7 +38,7 @@ UPDATED_DATETIME }; -my $dt = DateTime->now; +my $dt = DateTime->now(formatter => DateTime::Format::W3CDTF->new); # basic xml -> perl object tests for my $test ( @@ -48,13 +52,13 @@ for my $test ( desc => 'basic parsing - empty element', updated_datetime => '<updated_datetime />', res => { update_id => 638344, service_request_id => 1, - status => 'open', description => 'This is a note', updated_datetime => {} } , + status => 'open', description => 'This is a note', updated_datetime => undef } , }, { desc => 'basic parsing - element with no content', updated_datetime => '<updated_datetime></updated_datetime>', res => { update_id => 638344, service_request_id => 1, - status => 'open', description => 'This is a note', updated_datetime => {} } , + status => 'open', description => 'This is a note', updated_datetime => undef } , }, { desc => 'basic parsing - element with content', @@ -102,7 +106,7 @@ subtest 'check extended request parsed correctly' => sub { }; -my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem_rs = FixMyStreet::DB->resultset('Problem'); my $problem = $problem_rs->new( { postcode => 'EH99 1SP', @@ -123,7 +127,7 @@ my $problem = $problem_rs->new( lastupdate => DateTime->now()->subtract( days => 1 ), anonymous => 1, external_id => time(), - council => 2482, + bodies_str => 2482, } ); @@ -131,71 +135,212 @@ $problem->insert; for my $test ( { - desc => 'element with content', - updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + desc => 'OPEN status for confirmed problem does not change state', description => 'This is a note', external_id => 638344, start_state => 'confirmed', - close_comment => 0, + comment_status => 'OPEN', mark_fixed=> 0, mark_open => 0, problem_state => undef, end_state => 'confirmed', }, { - desc => 'comment closes report', - updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + desc => 'bad state does not update states but does create update', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'INVALID_STATE', + mark_fixed=> 0, + mark_open => 0, + problem_state => undef, + end_state => 'confirmed', + }, + + { + desc => 'investigating status changes problem status', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'INVESTIGATING', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'investigating', + end_state => 'investigating', + }, + { + desc => 'in progress status changes problem status', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'IN_PROGRESS', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'in progress', + end_state => 'in progress', + }, + { + desc => 'action scheduled status changes problem status', description => 'This is a note', external_id => 638344, start_state => 'confirmed', - close_comment => 1, + comment_status => 'ACTION_SCHEDULED', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'action scheduled', + end_state => 'action scheduled', + }, + { + desc => 'not responsible status changes problem status', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'NOT_COUNCILS_RESPONSIBILITY', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'not responsible', + end_state => 'not responsible', + }, + { + desc => 'internal referral status changes problem status', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'INTERNAL_REFERRAL', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'internal referral', + end_state => 'internal referral', + }, + { + desc => 'duplicate status changes problem status', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'DUPLICATE', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'duplicate', + end_state => 'duplicate', + }, + { + desc => 'fixed status marks report as fixed - council', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'FIXED', mark_fixed=> 0, mark_open => 0, problem_state => 'fixed - council', end_state => 'fixed - council', }, { - desc => 'comment re-opens fixed report', - updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + desc => 'status of CLOSED marks report as fixed - council', + description => 'This is a note', + external_id => 638344, + start_state => 'confirmed', + comment_status => 'CLOSED', + mark_fixed=> 0, + mark_open => 0, + problem_state => 'fixed - council', + end_state => 'fixed - council', + }, + { + desc => 'status of OPEN re-opens fixed report', description => 'This is a note', external_id => 638344, start_state => 'fixed - user', - close_comment => 0, + comment_status => 'OPEN', mark_fixed => 0, mark_open => 0, problem_state => 'confirmed', end_state => 'confirmed', }, { - desc => 'comment re-opens closed report', - updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + desc => 'action sheduled re-opens fixed report as action scheduled', + description => 'This is a note', + external_id => 638344, + start_state => 'fixed - user', + comment_status => 'ACTION_SCHEDULED', + mark_fixed => 0, + mark_open => 0, + problem_state => 'action scheduled', + end_state => 'action scheduled', + }, + { + desc => 'open status re-opens closed report', description => 'This is a note', external_id => 638344, - start_state => 'closed', - close_comment => 0, + start_state => 'not responsible', + comment_status => 'OPEN', mark_fixed => 0, mark_open => 0, problem_state => 'confirmed', end_state => 'confirmed', }, { - desc => 'comment leaves report closed', - updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + desc => 'fixed status leaves fixed - user report as fixed - user', + description => 'This is a note', + external_id => 638344, + start_state => 'fixed - user', + comment_status => 'FIXED', + mark_fixed => 0, + mark_open => 0, + problem_state => undef, + end_state => 'fixed - user', + }, + { + desc => 'closed status updates fixed report', + description => 'This is a note', + external_id => 638344, + start_state => 'fixed - user', + comment_status => 'NO_FURTHER_ACTION', + mark_fixed => 0, + mark_open => 0, + problem_state => 'unable to fix', + end_state => 'unable to fix', + }, + { + desc => 'no futher action status closes report', description => 'This is a note', external_id => 638344, - start_state => 'closed', - close_comment => 1, + start_state => 'confirmed', + comment_status => 'NO_FURTHER_ACTION', mark_fixed => 0, mark_open => 0, - end_state => 'closed', + problem_state => 'unable to fix', + end_state => 'unable to fix', + }, + { + desc => 'fixed status sets closed report as fixed', + description => 'This is a note', + external_id => 638344, + start_state => 'unable to fix', + comment_status => 'FIXED', + mark_fixed => 0, + mark_open => 0, + problem_state => 'fixed - council', + end_state => 'fixed - council', + }, + { + desc => 'open status does not re-open hidden report', + description => 'This is a note', + external_id => 638344, + start_state => 'hidden', + comment_status => 'OPEN', + mark_fixed => 0, + mark_open => 0, + problem_state => 'confirmed', + end_state => 'hidden', }, ) { subtest $test->{desc} => sub { my $local_requests_xml = $requests_xml; - $local_requests_xml =~ s/UPDATED_DATETIME/$test->{updated_datetime}/; + my $updated_datetime = sprintf( '<updated_datetime>%s</updated_datetime>', $dt ); + $local_requests_xml =~ s/UPDATED_DATETIME/$updated_datetime/; $local_requests_xml =~ s#<service_request_id>\d+</service_request_id>#<service_request_id>@{[$problem->external_id]}</service_request_id>#; $local_requests_xml =~ s#<service_request_id_ext>\d+</service_request_id_ext>#<service_request_id_ext>@{[$problem->id]}</service_request_id_ext>#; - $local_requests_xml =~ s#<status>\w+</status>#<status>closed</status># if $test->{close_comment}; + $local_requests_xml =~ s#<status>\w+</status>#<status>$test->{comment_status}</status># if $test->{comment_status}; my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); @@ -204,14 +349,13 @@ for my $test ( $problem->state( $test->{start_state} ); $problem->update; - my $council_details = { areaid => 2482 }; my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); is $problem->comments->count, 1, 'comment count'; $problem->discard_changes; - my $c = FixMyStreet::App->model('DB::Comment')->search( { external_id => $test->{external_id} } )->first; + my $c = FixMyStreet::DB->resultset('Comment')->search( { external_id => $test->{external_id} } )->first; ok $c, 'comment exists'; is $c->text, $test->{description}, 'text correct'; is $c->mark_fixed, $test->{mark_fixed}, 'mark_closed correct'; @@ -221,6 +365,31 @@ for my $test ( }; } +subtest 'Update with media_url includes image in update' => sub { + my $guard = LWP::Protocol::PSGI->register(t::Mock::Static->to_psgi_app, host => 'example.com'); + + my $local_requests_xml = $requests_xml; + my $updated_datetime = sprintf( '<updated_datetime>%s</updated_datetime>', $dt ); + $local_requests_xml =~ s/UPDATED_DATETIME/$updated_datetime/; + $local_requests_xml =~ s#<service_request_id>\d+</service_request_id># + <service_request_id>@{[$problem->external_id]}</service_request_id> + <media_url>http://example.com/image.jpeg</media_url>#; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); + + $problem->comments->delete; + $problem->lastupdate( DateTime->now()->subtract( days => 1 ) ); + $problem->state('confirmed'); + $problem->update; + + my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); + $update->update_comments( $o, $bodies{2482} ); + + is $problem->comments->count, 1, 'comment count'; + my $c = $problem->comments->first; + is $c->external_id, 638344; + is $c->photo, '74e3362283b6ef0c48686fb0e161da4043bbcc97.jpeg', 'photo exists'; +}; foreach my $test ( { @@ -234,7 +403,8 @@ foreach my $test ( $dt->subtract( minutes => 10 ); my $local_requests_xml = $requests_xml; - my $updated = sprintf( '<updated_datetime>%s</updated_datetime>', $dt ); + my $updated = sprintf( '<updated_datetime>%s</updated_datetime>', DateTime::Format::W3CDTF->format_datetime( $dt ) ); + $local_requests_xml =~ s/UPDATED_DATETIME/$updated/; $local_requests_xml =~ s#<service_request_id>\d+</service_request_id>#<service_request_id>@{[$problem->external_id]}</service_request_id>#; $local_requests_xml =~ s#<service_request_id_ext>\d+</service_request_id_ext>#<service_request_id_ext>@{[$problem->id]}</service_request_id_ext>#; @@ -243,9 +413,8 @@ foreach my $test ( $problem->comments->delete; - my $council_details = { areaid => 2482 }; my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); my $comment = $problem->comments->first; is $comment->created, $dt, 'created date set to date from XML'; @@ -273,7 +442,7 @@ my $problem2 = $problem_rs->new( lastupdate => DateTime->now(), anonymous => 1, external_id => $problem->external_id, - council => 2651, + bodies_str => 2651, } ); @@ -312,9 +481,8 @@ for my $test ( my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $local_requests_xml } ); - my $council_details = { areaid => $test->{area_id} }; my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{$test->{area_id}} ); is $problem->comments->count, $test->{p1_comments}, 'comment count for first problem'; is $problem2->comments->count, $test->{p2_comments}, 'comment count for second problem'; @@ -352,8 +520,7 @@ subtest 'using start and end date' => sub { end_date => $end_dt, ); - my $council_details = { areaid => 2482 }; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); my $start = $start_dt . ''; my $end = $end_dt . ''; @@ -387,7 +554,7 @@ subtest 'check that existing comments are not duplicated' => sub { $problem->comments->delete; - my $comment = FixMyStreet::App->model('DB::Comment')->new( + my $comment = FixMyStreet::DB->resultset('Comment')->new( { problem => $problem, external_id => 638344, @@ -404,7 +571,8 @@ subtest 'check that existing comments are not duplicated' => sub { is $problem->comments->count, 1, 'one comment before fetching updates'; $requests_xml =~ s/UPDATED_DATETIME2/$dt/; - $requests_xml =~ s/UPDATED_DATETIME/@{[ $comment->confirmed ]}/; + my $confirmed = DateTime::Format::W3CDTF->format_datetime($comment->confirmed); + $requests_xml =~ s/UPDATED_DATETIME/$confirmed/; my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } ); @@ -412,30 +580,29 @@ subtest 'check that existing comments are not duplicated' => sub { system_user => $user, ); - my $council_details = { areaid => 2482 }; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; is $problem->comments->count, 2, 're-fetching updates does not add comments'; $problem->comments->delete; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; is $problem->comments->count, 2, 'if comments are deleted then they are added'; }; foreach my $test ( { desc => 'check that closed and then open comment results in correct state', - dt1 => $dt->subtract( hours => 1 ), + dt1 => $dt->clone->subtract( hours => 1 ), dt2 => $dt, }, { desc => 'check that old comments do not change problem status', - dt1 => $dt->subtract( hours => 2 ), + dt1 => $dt->clone->subtract( minutes => 90 ), dt2 => $dt, } ) { @@ -461,7 +628,7 @@ foreach my $test ( { $problem->comments->delete; $problem->state( 'confirmed' ); - $problem->lastupdate( $dt->subtract( hours => 3 ) ); + $problem->lastupdate( $dt->clone->subtract( hours => 3 ) ); $problem->update; $requests_xml =~ s/UPDATED_DATETIME/$test->{dt1}/; @@ -473,8 +640,7 @@ foreach my $test ( { system_user => $user, ); - my $council_details = { areaid => 2482 }; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; is $problem->comments->count, 2, 'two comments after fetching updates'; @@ -484,10 +650,22 @@ foreach my $test ( { foreach my $test ( { desc => 'normally alerts are not suppressed', + num_alerts => 1, suppress_alerts => 0, }, { desc => 'alerts suppressed if suppress_alerts set', + num_alerts => 1, + suppress_alerts => 1, + }, + { + desc => 'alert suppression ok even if no alerts', + num_alerts => 0, + suppress_alerts => 1, + }, + { + desc => 'alert suppression ok even if 2x alerts', + num_alerts => 2, suppress_alerts => 1, } ) { @@ -506,15 +684,17 @@ foreach my $test ( { $problem->comments->delete; $problem->state( 'confirmed' ); - $problem->lastupdate( $dt->subtract( hours => 3 ) ); + $problem->lastupdate( $dt->clone->subtract( hours => 3 ) ); $problem->update; - my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( { - alert_type => 'new_updates', - parameter => $problem->id, - confirmed => 1, - user_id => $problem->user->id, - } ); + my @alerts = map { + my $alert = FixMyStreet::DB->resultset('Alert')->create( { + alert_type => 'new_updates', + parameter => $problem->id, + confirmed => 1, + user_id => $problem->user->id, + } ) + } (1..$test->{num_alerts}); $requests_xml =~ s/UPDATED_DATETIME/$dt/; @@ -525,25 +705,26 @@ foreach my $test ( { suppress_alerts => $test->{suppress_alerts}, ); - my $council_details = { areaid => 2482 }; - $update->update_comments( $o, $council_details ); + $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; - my $alerts_sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $alerts_sent = FixMyStreet::DB->resultset('AlertSent')->search( { - alert_id => $alert->id, + alert_id => [ map $_->id, @alerts ], parameter => $problem->comments->first->id, } ); if ( $test->{suppress_alerts} ) { - ok $alerts_sent->count(), 'alerts suppressed'; + is $alerts_sent->count(), $test->{num_alerts}, 'alerts suppressed'; } else { is $alerts_sent->count(), 0, 'alerts not suppressed'; } $alerts_sent->delete; - $alert->delete; + for my $alert (@alerts) { + $alert->delete; + } } } |