diff options
author | Struan Donald <struan@exo.org.uk> | 2012-03-28 15:02:08 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2012-03-28 15:02:08 +0100 |
commit | a0f32a1cb710f50bcb312678130697fe0473ba23 (patch) | |
tree | 834c8b4df9bfc8ab5205c12bcb66ca403bbcda6e | |
parent | 5b1b93d60f6e6e4b9f6abca461beb91f206806c7 (diff) |
tests for time and comment replacement plus fixes for comment time
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 10 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 97 |
2 files changed, 104 insertions, 3 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index cb6cf7661..ed483cfd8 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -3,6 +3,7 @@ package Open311::GetServiceRequestUpdates; use Moose; use Open311; use FixMyStreet::App; +use DateTime::Format::W3CDTF; has council_list => ( is => 'ro' ); has system_user => ( is => 'ro' ); @@ -43,6 +44,8 @@ sub update_comments { my $c = $p->comments->search( { external_id => $request->{update_id} } ); if ( !$c->first ) { + my $comment_time = DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} ); + my $comment = FixMyStreet::App->model('DB::Comment')->new( { problem => $p, @@ -52,11 +55,12 @@ sub update_comments { mark_fixed => 0, mark_open => 0, anonymous => 0, - name => $self->system_user->name + name => $self->system_user->name, + confirmed => $comment_time, + created => $comment_time, + state => 'confirmed', } ); - $comment->confirm; - if ( $p->is_open and $request->{status} eq 'closed' ) { $p->state( 'fixed - council' ); diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 5f32a7537..063d7e378 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -189,6 +189,38 @@ for my $test ( }; } + +foreach my $test ( + { + desc => 'date for comment correct', + updated_datetime => sprintf( '<updated_datetime>%s</updated_datetime>', $dt ), + external_id => 638344, + }, +) { + subtest $test->{desc} => sub { + my $dt = DateTime->now(); + $dt->subtract( minutes => 10 ); + my $local_requests_xml = $requests_xml; + + my $updated = sprintf( '<updated_datetime>%s</updated_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>#; + + 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 $council_details = { areaid => 2482 }; + my $update = Open311::GetServiceRequestUpdates->new( system_user => $user ); + $update->update_comments( $o, $council_details ); + + my $comment = $problem->comments->first; + is $comment->created, $dt, 'created date set to date from XML'; + is $comment->confirmed, $dt, 'confirmed date set to date from XML'; + }; +} + my $problem2 = $problem_rs->new( { postcode => 'EH99 1SP', @@ -301,6 +333,71 @@ subtest 'using start and end date' => sub { is $c->param('end_date'), $end, 'end date used'; }; +subtest 'check that existing comments are not duplicated' => 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> + <service_request_id_ext>@{[ $problem->id ]}</service_request_id_ext> + <status>open</status> + <description>This is a note</description> + <updated_datetime>UPDATED_DATETIME</updated_datetime> + </request_update> + <request_update> + <update_id>638354</update_id> + <service_request_id>@{[ $problem->external_id ]}</service_request_id> + <service_request_id_ext>@{[ $problem->id ]}</service_request_id_ext> + <status>open</status> + <description>This is a different note</description> + <updated_datetime>UPDATED_DATETIME2</updated_datetime> + </request_update> + </service_requests_updates> + }; + + $problem->comments->delete; + + my $comment = FixMyStreet::App->model('DB::Comment')->new( + { + problem => $problem, + external_id => 638344, + text => 'This is a note', + user => $user, + state => 'confirmed', + mark_fixed => 0, + anonymous => 0, + confirmed => $dt, + } + ); + $comment->insert; + + is $problem->comments->count, 1, 'one comment before fetching updates'; + + $requests_xml =~ s/UPDATED_DATETIME2/$dt/; + $requests_xml =~ s/UPDATED_DATETIME/@{[ $comment->confirmed ]}/; + + my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'update.xml' => $requests_xml } ); + + my $update = Open311::GetServiceRequestUpdates->new( + system_user => $user, + ); + + my $council_details = { areaid => 2482 }; + $update->update_comments( $o, $council_details ); + + $problem->discard_changes; + is $problem->comments->count, 2, 'two comments after fetching updates'; + + $update->update_comments( $o, $council_details ); + $problem->discard_changes; + is $problem->comments->count, 2, 're-fetching updates does not add comments'; + + $problem->comments->delete; + $update->update_comments( $o, $council_details ); + $problem->discard_changes; + is $problem->comments->count, 2, 'if comments are deleted then they are added'; +}; + $problem2->comments->delete(); $problem->comments->delete(); $problem2->delete; |