diff options
Diffstat (limited to 't/open311')
-rw-r--r-- | t/open311/getservicerequestupdates.t | 313 | ||||
-rw-r--r-- | t/open311/getupdates.t | 260 | ||||
-rw-r--r-- | t/open311/populate-service-list.t | 194 |
3 files changed, 586 insertions, 181 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; + } } } diff --git a/t/open311/getupdates.t b/t/open311/getupdates.t new file mode 100644 index 000000000..0e31db482 --- /dev/null +++ b/t/open311/getupdates.t @@ -0,0 +1,260 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use URI::Split qw(uri_split); + +use FixMyStreet; +use FixMyStreet::DB; + +use_ok( 'Open311::GetUpdates' ); +use_ok( 'Open311' ); + +my $user = FixMyStreet::DB->resultset('User')->find_or_create( + { + email => 'system_user@example.com' + } +); + +my $body = FixMyStreet::DB->resultset('Body')->new( { + name => 'Test Body', +} ); + +my $updates = Open311::GetUpdates->new( system_user => $user ); +ok $updates, 'created object'; + +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>006</service_code> +<description></description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +UPDATED_DATETIME +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>37.762221815</lat> +<long>-122.4651145</long> +</request> +</service_requests> +}; + +my $problem_rs = FixMyStreet::DB->resultset('Problem'); +my $problem = $problem_rs->new( + { + postcode => 'EH99 1SP', + latitude => 1, + longitude => 1, + areas => 1, + title => '', + detail => '', + used_map => 1, + name => '', + state => 'confirmed', + cobrand => 'default', + user => $user, + created => DateTime->now()->subtract( days => 1 ), + lastupdate => DateTime->now()->subtract( days => 1 ), + anonymous => 1, + external_id => 638344, + } +); + +$problem->insert; + +for my $test ( + { + desc => 'element missing', + updated_datetime => '', + comment_count => 0, + }, + { + desc => 'empty element', + updated_datetime => '<updated_datetime />', + comment_count => 0, + }, + { + desc => 'element with no content', + updated_datetime => '<updated_datetime></updated_datetime>', + comment_count => 0, + }, + { + desc => 'element with old content', + updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', DateTime->now->subtract( days => 3 ) ), + comment_count => 0, + }, + { + desc => 'element with new content', + updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', DateTime->now ), + comment_count => 1, + }, +) { + subtest $test->{desc} => sub { + $problem->comments->delete; + $problem->lastupdate(DateTime->now()->subtract( days => 1 ) ), + $problem->update; + + my $local_requests_xml = $requests_xml; + $local_requests_xml =~ s/UPDATED_DATETIME/$test->{updated_datetime}/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'requests.xml' => $local_requests_xml } ); + + ok $updates->update_reports( [ 638344 ], $o, $body ), 'Updated reports'; + my @parts = uri_split($o->test_uri_used); + is $parts[2], '/requests.xml', 'path matches'; + my @qs = sort split '&', $parts[3]; + is_deeply(\@qs, [ 'jurisdiction_id=mysociety', 'service_request_id=638344' ], 'query string matches'); + + is $problem->comments->count, $test->{comment_count}, 'added a comment'; + }; +} + +$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>006</service_code> +<description></description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +<updated_datetime>UPDATED_DATETIME</updated_datetime> +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>37.762221815</lat> +<long>-122.4651145</long> +</request> +<request> +<service_request_id>638345</service_request_id> +<status>open</status> +<status_notes>This is a for a different issue.</status_notes> +<service_name>Sidewalk and Curb Issues</service_name> +<service_code>006</service_code> +<description></description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +<updated_datetime>UPDATED_DATETIME2</updated_datetime> +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>37.762221815</lat> +<long>-122.4651145</long> +</request> +</service_requests> +}; + +my $problem2 = $problem_rs->create( + { + postcode => 'EH99 1SP', + latitude => 1, + longitude => 1, + areas => 1, + title => '', + detail => '', + used_map => 1, + name => '', + state => 'confirmed', + cobrand => 'default', + user => $user, + created => DateTime->now()->subtract( days => 1 ), + lastupdate => DateTime->now()->subtract( days => 1 ), + anonymous => 1, + external_id => 638345, + } +); + +$problem->comments->delete; +subtest 'update with two requests' => sub { + $problem->comments->delete; + $problem->lastupdate(DateTime->now()->subtract( days => 1 ) ), + + my $date1 = DateTime::Format::W3CDTF->new->format_datetime( DateTime->now() ); + my $date2 = DateTime::Format::W3CDTF->new->format_datetime( DateTime->now->subtract( hours => 1) ); + my $local_requests_xml = $requests_xml; + $local_requests_xml =~ s/UPDATED_DATETIME2/$date2/; + $local_requests_xml =~ s/UPDATED_DATETIME/$date1/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'requests.xml' => $local_requests_xml } ); + + ok $updates->update_reports( [ 638344,638345 ], $o, $body ), 'Updated reports'; + my @parts = uri_split($o->test_uri_used); + is $parts[2], '/requests.xml', 'path matches'; + my @qs = sort split '&', $parts[3]; + is_deeply(\@qs, [ 'jurisdiction_id=mysociety', 'service_request_id=638344%2C638345' ], 'query string matches'); + + is $problem->comments->count, 1, 'added a comment to first problem'; + is $problem2->comments->count, 1, 'added a comment to second problem'; +}; + +# No status_notes field now, so that static string in code is used. +$requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> +<service_requests> +<request> +<service_request_id>638346</service_request_id> +<status>closed</status> +<service_name>Sidewalk and Curb Issues</service_name> +<service_code>006</service_code> +<description></description> +<agency_responsible></agency_responsible> +<service_notice></service_notice> +<requested_datetime>2010-04-14T06:37:38-08:00</requested_datetime> +UPDATED_DATETIME +<expected_datetime>2010-04-15T06:37:38-08:00</expected_datetime> +<lat>37.762221815</lat> +<long>-122.4651145</long> +</request> +</service_requests> +}; + +my $problem3 = $problem_rs->create( { + postcode => 'EH99 1SP', + latitude => 1, + longitude => 1, + areas => 1, + title => 'Title', + detail => 'Details', + used_map => 1, + name => '', + state => 'confirmed', + cobrand => 'fixamingata', + user => $user, + created => DateTime->now()->subtract( days => 1 ), + lastupdate => DateTime->now()->subtract( days => 1 ), + anonymous => 1, + external_id => 638346, +} ); + +subtest 'test translation of auto-added comment from old-style Open311 update' => sub { + my $dt = sprintf( '<updated_datetime>%s</updated_datetime>', DateTime->now ); + $requests_xml =~ s/UPDATED_DATETIME/$dt/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'requests.xml' => $requests_xml } ); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixamingata' ], + }, sub { + ok $updates->update_reports( [ 638346 ], $o, $body ), 'Updated reports'; + }; + my @parts = uri_split($o->test_uri_used); + is $parts[2], '/requests.xml', 'path matches'; + my @qs = sort split '&', $parts[3]; + is_deeply(\@qs, [ 'jurisdiction_id=mysociety', 'service_request_id=638346' ], 'query string matches'); + + is $problem3->comments->count, 1, 'added a comment'; + is $problem3->comments->first->text, "St\xe4ngd av kommunen", 'correct comment text'; +}; + +END { + if ($user) { + $user->comments->delete; + $user->problems->delete; + $user->delete; + } + done_testing(); +} diff --git a/t/open311/populate-service-list.t b/t/open311/populate-service-list.t index ec6c175f9..606bcbc44 100644 --- a/t/open311/populate-service-list.t +++ b/t/open311/populate-service-list.t @@ -4,42 +4,50 @@ use strict; use warnings; use Test::More; -use FixMyStreet::App; - -use FindBin; -use lib "$FindBin::Bin/../perllib"; -use lib "$FindBin::Bin/../commonlib/perllib"; +use FixMyStreet::DB; use_ok( 'Open311::PopulateServiceList' ); use_ok( 'Open311' ); -my $processor = Open311::PopulateServiceList->new( council_list => [] ); +my $processor = Open311::PopulateServiceList->new(); ok $processor, 'created object'; - +my $body = FixMyStreet::DB->resultset('Body')->find_or_create( { + id => 1, + name => 'Body Numero Uno', +} ); +$body->body_areas->find_or_create({ + area_id => 1 +} ); + +my $BROMLEY = 'Bromley Council'; +my $bromley = FixMyStreet::DB->resultset('Body')->find_or_create( { + id => 2482, + name => $BROMLEY, +} ); +$bromley->update({ name => $BROMLEY }); +$bromley->body_areas->find_or_create({ + area_id => 2482 +} ); subtest 'check basic functionality' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); my $service_list = get_xml_simple_object( get_standard_xml() ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - - my $processor = Open311::PopulateServiceList->new( council_list => [] ); - $processor->_current_council( $council ); + my $processor = Open311::PopulateServiceList->new(); + $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check non open311 contacts marked as deleted' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => 'contact@example.com', @@ -47,32 +55,28 @@ subtest 'check non open311 contacts marked as deleted' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); my $service_list = get_xml_simple_object( get_standard_xml() ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - - my $processor = Open311::PopulateServiceList->new( council_list => [] ); - $processor->_current_council( $council ); + my $processor = Open311::PopulateServiceList->new(); + $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 4, 'correct number of contacts'; - $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1, deleted => 1 } )->count(); + $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1, deleted => 1 } )->count(); is $contact_count, 1, 'correct number of deleted contacts'; }; subtest 'check email changed if matching category' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '009', @@ -80,7 +84,7 @@ subtest 'check email changed if matching category' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -89,12 +93,8 @@ subtest 'check email changed if matching category' => sub { my $service_list = get_xml_simple_object( get_standard_xml() ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - - my $processor = Open311::PopulateServiceList->new( council_list => [] ); - $processor->_current_council( $council ); + my $processor = Open311::PopulateServiceList->new(); + $processor->_current_body( $body ); $processor->process_services( $service_list ); $contact->discard_changes; @@ -102,14 +102,14 @@ subtest 'check email changed if matching category' => sub { is $contact->confirmed, 1, 'contact still confirmed'; is $contact->deleted, 0, 'contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check category name changed if updated' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '001', @@ -117,7 +117,7 @@ subtest 'check category name changed if updated' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -126,12 +126,8 @@ subtest 'check category name changed if updated' => sub { my $service_list = get_xml_simple_object( get_standard_xml() ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - - my $processor = Open311::PopulateServiceList->new( council_list => [] ); - $processor->_current_council( $council ); + my $processor = Open311::PopulateServiceList->new(); + $processor->_current_body( $body ); $processor->process_services( $service_list ); $contact->discard_changes; @@ -140,14 +136,14 @@ subtest 'check category name changed if updated' => sub { is $contact->confirmed, 1, 'contact still confirmed'; is $contact->deleted, 0, 'contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check conflicting contacts not changed' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => 'existing@example.com', @@ -155,14 +151,14 @@ subtest 'check conflicting contacts not changed' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); ok $contact, 'contact created'; - my $contact2 = FixMyStreet::App->model('DB::Contact')->create( + my $contact2 = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '001', @@ -170,7 +166,7 @@ subtest 'check conflicting contacts not changed' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -179,12 +175,8 @@ subtest 'check conflicting contacts not changed' => sub { my $service_list = get_xml_simple_object( get_standard_xml() ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - - my $processor = Open311::PopulateServiceList->new( council_list => [] ); - $processor->_current_council( $council ); + my $processor = Open311::PopulateServiceList->new(); + $processor->_current_body( $body ); $processor->process_services( $service_list ); $contact->discard_changes; @@ -199,12 +191,12 @@ subtest 'check conflicting contacts not changed' => sub { is $contact2->confirmed, 1, 'second contact contact still confirmed'; is $contact2->deleted, 0, 'second contact contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 4, 'correct number of contacts'; }; subtest 'check meta data population' => sub { - my $processor = Open311::PopulateServiceList->new( council_list => [] ); + my $processor = Open311::PopulateServiceList->new(); my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> <service_definition> @@ -223,7 +215,7 @@ subtest 'check meta data population' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', @@ -231,7 +223,7 @@ subtest 'check meta data population' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -243,12 +235,8 @@ subtest 'check meta data population' => sub { test_get_returns => { 'services/100.xml' => $meta_xml } ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 2482 - } ); - $processor->_current_open311( $o ); - $processor->_current_council( $council ); + $processor->_current_body( $bromley ); $processor->_current_service( { service_code => 100 } ); $processor->_add_meta_to_contact( $contact ); @@ -266,14 +254,14 @@ subtest 'check meta data population' => sub { $contact->discard_changes; - is_deeply $contact->extra, $extra, 'meta data saved'; + is_deeply $contact->get_extra_fields, $extra, 'meta data saved'; }; for my $test ( { desc => 'check meta data added to existing contact', has_meta => 1, - orig_meta => undef, + orig_meta => [], end_meta => [ { variable => 'true', code => 'type', @@ -344,7 +332,7 @@ for my $test ( { desc => 'check meta data removed', has_meta => 0, - end_meta => undef, + end_meta => [], orig_meta => [ { variable => 'true', code => 'type', @@ -375,8 +363,8 @@ for my $test ( { desc => 'check empty meta data handled', has_meta => 1, - orig_meta => undef, - end_meta => undef, + orig_meta => [], + end_meta => [], meta_xml => '<?xml version="1.0" encoding="utf-8"?> <service_definition> <service_code>100</service_code> @@ -387,7 +375,7 @@ for my $test ( }, ) { subtest $test->{desc} => sub { - my $processor = Open311::PopulateServiceList->new( council_list => [] ); + my $processor = Open311::PopulateServiceList->new(); my $services_xml = '<?xml version="1.0" encoding="utf-8"?> <services> @@ -407,7 +395,7 @@ for my $test ( $services_xml =~ s/metadata>false/metadata>true/ms; } - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '100', @@ -415,12 +403,13 @@ for my $test ( confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); - $contact->update( { extra => $test->{orig_meta} } ); + $contact->set_extra_fields(@{$test->{orig_meta}}); + $contact->update; my $o = Open311->new( jurisdiction => 'mysociety', @@ -430,25 +419,20 @@ for my $test ( ); my $service_list = get_xml_simple_object( $services_xml ); - $service_list = { service => [ $service_list->{ service } ] }; - - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); $processor->_current_open311( $o ); - $processor->_current_council( $council ); + $processor->_current_body( $body ); $processor->process_services( $service_list ); $contact->discard_changes; - is_deeply $contact->extra, $test->{end_meta}, 'meta data saved'; + is_deeply $contact->get_extra_fields, $test->{end_meta}, 'meta data saved'; }; } subtest 'check attribute ordering' => sub { - my $processor = Open311::PopulateServiceList->new( council_list => [] ); + my $processor = Open311::PopulateServiceList->new(); my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> <service_definition> @@ -485,7 +469,7 @@ subtest 'check attribute ordering' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', @@ -493,7 +477,7 @@ subtest 'check attribute ordering' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -505,12 +489,8 @@ subtest 'check attribute ordering' => sub { test_get_returns => { 'services/100.xml' => $meta_xml } ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 1 - } ); - $processor->_current_open311( $o ); - $processor->_current_council( $council ); + $processor->_current_body( $body ); $processor->_current_service( { service_code => 100 } ); $processor->_add_meta_to_contact( $contact ); @@ -550,11 +530,11 @@ subtest 'check attribute ordering' => sub { $contact->discard_changes; - is_deeply $contact->extra, $extra, 'meta data re-ordered correctly'; + is_deeply $contact->get_extra_fields, $extra, 'meta data re-ordered correctly'; }; subtest 'check bromely skip code' => sub { - my $processor = Open311::PopulateServiceList->new( council_list => [] ); + my $processor = Open311::PopulateServiceList->new(); my $meta_xml = '<?xml version="1.0" encoding="utf-8"?> <service_definition> @@ -591,7 +571,7 @@ subtest 'check bromely skip code' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', @@ -599,7 +579,7 @@ subtest 'check bromely skip code' => sub { confirmed => 1, deleted => 0, editor => $0, - whenedited => \'ms_current_timestamp()', + whenedited => \'current_timestamp', note => 'test contact', } ); @@ -611,12 +591,8 @@ subtest 'check bromely skip code' => sub { test_get_returns => { 'services/100.xml' => $meta_xml } ); - my $council = FixMyStreet::App->model('DB::Body')->new( { - area_id => 2482 - } ); - $processor->_current_open311( $o ); - $processor->_current_council( $council ); + $processor->_current_body( $bromley ); $processor->_current_service( { service_code => 100 } ); $processor->_add_meta_to_contact( $contact ); @@ -634,11 +610,9 @@ subtest 'check bromely skip code' => sub { $contact->discard_changes; - is_deeply $contact->extra, $extra, 'only non std bromley meta data saved'; + is_deeply $contact->get_extra_fields, $extra, 'only non std bromley meta data saved'; - $council->area_id(1); - - $processor->_current_council( $council ); + $processor->_current_body( $body ); $processor->_add_meta_to_contact( $contact ); $extra = [ @@ -676,7 +650,7 @@ subtest 'check bromely skip code' => sub { $contact->discard_changes; - is_deeply $contact->extra, $extra, 'all meta data saved for non bromley'; + is_deeply $contact->get_extra_fields, $extra, 'all meta data saved for non bromley'; }; sub get_standard_xml { @@ -715,17 +689,7 @@ sub get_standard_xml { sub get_xml_simple_object { my $xml = shift; - - my $simple = XML::Simple->new(); - my $obj; - - eval { - $obj = $simple->XMLin( $xml ); - }; - - die $@ if $@; - - return $obj; + return Open311->_get_xml_object($xml); } done_testing(); |