diff options
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 14 | ||||
-rw-r--r-- | t/Mock/Static.pm | 18 | ||||
-rw-r--r-- | t/open311/getservicerequestupdates.t | 27 |
3 files changed, 59 insertions, 0 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 6d846de42..4b6d56146 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -3,6 +3,7 @@ package Open311::GetServiceRequestUpdates; use Moo; use Open311; use FixMyStreet::DB; +use FixMyStreet::App::Model::PhotoSet; use DateTime::Format::W3CDTF; has system_user => ( is => 'rw' ); @@ -116,6 +117,19 @@ sub update_comments { } ); + # ref test as XML::Simple will have returned an empty hashref for empty element + if ($request->{media_url} && !ref $request->{media_url}) { + my $ua = LWP::UserAgent->new; + my $res = $ua->get($request->{media_url}); + if ( $res->is_success && $res->content_type eq 'image/jpeg' ) { + my $photoset = FixMyStreet::App::Model::PhotoSet->new({ + data_items => [ $res->decoded_content ], + }); + my $data = $photoset->get_raw_image_data(0); + $comment->photo($data->[0]); + } + } + # if the comment is older than the last update # do not change the status of the problem as it's # tricky to determine the right thing to do. diff --git a/t/Mock/Static.pm b/t/Mock/Static.pm new file mode 100644 index 000000000..260c181cd --- /dev/null +++ b/t/Mock/Static.pm @@ -0,0 +1,18 @@ +package t::Mock::Static; + +use Path::Tiny; +use Web::Simple; + +my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg"); +my $sample_photo = $sample_file->slurp_raw; + +sub dispatch_request { + my $self = shift; + + sub (GET + /image.jpeg) { + my ($self) = @_; + return [ 200, [ 'Content-Type' => 'image/jpeg' ], [ $sample_photo ] ]; + }, +} + +__PACKAGE__->run_if_script; diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 46c3feda4..18a5802bb 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -4,6 +4,8 @@ use strict; use warnings; use Test::More; use CGI::Simple; +use LWP::Protocol::PSGI; +use t::Mock::Static; use_ok( 'Open311' ); @@ -363,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, '1cdd4329ceee2234bd4e89cb33b42061a0724687', 'photo exists'; +}; foreach my $test ( { |