diff options
author | Struan Donald <struan@exo.org.uk> | 2012-03-19 18:10:47 +0000 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2012-03-19 18:10:47 +0000 |
commit | 3fce129f4aa4ae25ebaad559d8da9c30cad484c6 (patch) | |
tree | 9c2ab6c8521536d205823f961d421e50c6eae209 | |
parent | 246303b1e234fbde17775054859af4d6fe7e4a80 (diff) |
add first pass of update comments method and tests
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 77 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 79 |
2 files changed, 152 insertions, 4 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm new file mode 100644 index 000000000..d4e323cc3 --- /dev/null +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -0,0 +1,77 @@ +package Open311::GetServiceRequestUpdates; + +use Moose; +use Open311; +use FixMyStreet::App; + +has council_list => ( is => 'ro' ); +has system_user => ( is => 'ro' ); + +sub update_comments { + my ( $self, $open311, $council_details ) = @_; + + my $service_requests = $open311->get_service_request_updates( ); + + my $requests; + + # XML::Simple is a bit inconsistent in how it structures + # things depending on the number of children an element has :( + if ( ref $service_requests->{request_update } eq 'ARRAY' ) { + $requests = $service_requests->{requesti_update}; + } + else { + $requests = [ $service_requests->{request_update} ]; + } + + for my $request (@$requests) { + # if it's a ref that means it's an empty element + # however, if there's no updated date then we can't + # tell if it's newer that what we have so we should skip it + next if ref $request->{updated_datetime} || ! exists $request->{updated_datetime}; + + my $request_id = $request->{service_request_id}; + + my $problem = + FixMyStreet::App->model('DB::Problem') + ->search( { external_id => $request_id, } ); + + if (my $p = $problem->first) { + my $c = FixMyStreet::App->model('DB::Comment') + ->search( { external_id => $request->{update_id} } ); + + if ( !$c->first ) { + my $comment = FixMyStreet::App->model('DB::Comment')->new( + { + problem => $p, + user => $self->system_user, + external_id => $request->{update_id}, + text => $request->{description}, + mark_fixed => 0, + mark_open => 0, + anonymous => 0, + name => $self->system_user->name + } + ); + $comment->confirm; + + if ( $p->is_open and $request->{status} eq 'closed' ) { + $p->state( 'closed' ); + $p->update; + + $comment->mark_fixed( 1 ); + } elsif ( $p->is_closed and $request->{status} eq 'open' ) { + $p->state( 'open' ); + $p->update; + + $comment->mark_open( 1 ); + } + + $comment->insert(); + } + } + } + + return 1; +} + +1; diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 7bccc9943..1d0c19c8e 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -9,8 +9,16 @@ use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../commonlib/perllib"; use_ok( 'Open311' ); + +use_ok( 'Open311::GetServiceRequestUpdates' ); use DateTime; +use FixMyStreet::App; +my $user = FixMyStreet::App->model('DB::User')->find_or_create( + { + email => 'system_user@example.com' + } +); my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> <service_requests_updates> @@ -31,25 +39,25 @@ my $dt = DateTime->now; # basic xml -> perl object tests for my $test ( { - desc => 'element missing', + desc => 'basic parsing - element missing', updated_datetime => '', res => { update_id => 638344, service_request_id => 1, service_request_id_ext => 1, status => 'open', description => 'This is a note' }, }, { - desc => 'empty element', + desc => 'basic parsing - empty element', updated_datetime => '<updated_datetime />', res => { update_id => 638344, service_request_id => 1, service_request_id_ext => 1, status => 'open', description => 'This is a note', updated_datetime => {} } , }, { - desc => 'element with no content', + desc => 'basic parsing - element with no content', updated_datetime => '<updated_datetime></updated_datetime>', res => { update_id => 638344, service_request_id => 1, service_request_id_ext => 1, status => 'open', description => 'This is a note', updated_datetime => {} } , }, { - desc => 'element with content', + desc => 'basic parsing - element with content', updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), res => { update_id => 638344, service_request_id => 1, service_request_id_ext => 1, status => 'open', description => 'This is a note', updated_datetime => $dt } , @@ -66,3 +74,66 @@ for my $test ( }; } + +my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem = $problem_rs->new( + { + 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 => time(), + } +); + +$problem->insert; + +for my $test ( + { + desc => 'element with content', + updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + res => { update_id => 638344, service_request_id => $problem->external_id, service_request_id_ext => 1, + status => 'open', description => 'This is a note', updated_datetime => $dt } , + }, +) { + subtest $test->{desc} => sub { + my $local_requests_xml = $requests_xml; + $local_requests_xml =~ s/UPDATED_DATETIME/$test->{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>#; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'update.xml' => $local_requests_xml } ); + + $problem->comments->delete; + + my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); + $update->update_comments( $o ); + + is $problem->comments->count, 1, 'comment count'; + + my $c = FixMyStreet::App->model('DB::Comment')->search( { external_id => $test->{res}->{update_id} } )->first; + ok $c, 'comment exists'; + is $c->text, $test->{res}->{description}, 'text correct'; + }; +} + +$problem->comments->delete(); +$problem->delete; +$user->comments->delete; +$user->problems->delete; +$user->delete; + +done_testing(); |