diff options
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Oxfordshire.pm | 29 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/UKCouncils.pm | 6 | ||||
-rw-r--r-- | t/cobrand/oxfordshire.t | 43 |
3 files changed, 75 insertions, 3 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Oxfordshire.pm b/perllib/FixMyStreet/Cobrand/Oxfordshire.pm index 0f34ff09f..34e21ddfc 100644 --- a/perllib/FixMyStreet/Cobrand/Oxfordshire.pm +++ b/perllib/FixMyStreet/Cobrand/Oxfordshire.pm @@ -155,6 +155,35 @@ sub open311_post_send { $row->detail($self->{ox_original_detail}); } +sub open311_munge_update_params { + my ($self, $params, $comment, $body) = @_; + + if ($comment->get_extra_metadata('defect_raised')) { + my $p = $comment->problem; + my ($e, $n) = $p->local_coords; + my $usrn = $p->get_extra_field_value('usrn'); + if (!$usrn) { + my $cfg = { + url => 'https://tilma.mysociety.org/mapserver/oxfordshire', + typename => "OCCRoads", + srsname => 'urn:ogc:def:crs:EPSG::27700', + accept_feature => sub { 1 }, + filter => "<Filter xmlns:gml=\"http://www.opengis.net/gml\"><DWithin><PropertyName>SHAPE_GEOMETRY</PropertyName><gml:Point><gml:coordinates>$e,$n</gml:coordinates></gml:Point><Distance units='m'>20</Distance></DWithin></Filter>", + }; + my $features = $self->_fetch_features($cfg); + my $feature = $self->_nearest_feature($cfg, $e, $n, $features); + if ($feature) { + my $props = $feature->{properties}; + $usrn = Utils::trim_text($props->{TYPE1_2_USRN}); + } + } + $params->{'attribute[usrn]'} = $usrn; + $params->{'attribute[raise_defect]'} = 1; + $params->{'attribute[easting]'} = $e; + $params->{'attribute[northing]'} = $n; + } +} + sub should_skip_sending_update { my ($self, $update ) = @_; diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index f472876ef..0e8341d57 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -425,7 +425,7 @@ sub _nearest_feature { # We have a list of features, and we want to find the one closest to the # report location. - my $site_code = ''; + my $chosen = ''; my $nearest; # We shouldn't receive anything aside from these geometry types, but belt and braces. @@ -452,14 +452,14 @@ sub _nearest_feature { for (my $i=0; $i<@$coordinates-1; $i++) { my $distance = $self->_distanceToLine($x, $y, $coordinates->[$i], $coordinates->[$i+1]); if ( !defined $nearest || $distance < $nearest ) { - $site_code = $feature->{properties}->{$cfg->{property}}; + $chosen = $feature; $nearest = $distance; } } } } - return $site_code; + return $cfg->{property} && $chosen ? $chosen->{properties}->{$cfg->{property}} : $chosen; } sub contact_name { diff --git a/t/cobrand/oxfordshire.t b/t/cobrand/oxfordshire.t index cfa98254f..b5e51e808 100644 --- a/t/cobrand/oxfordshire.t +++ b/t/cobrand/oxfordshire.t @@ -4,6 +4,7 @@ use CGI::Simple; use FixMyStreet::TestMech; use FixMyStreet::Script::Alerts; use FixMyStreet::Script::Reports; +use Open311; my $mech = FixMyStreet::TestMech->new; my $oxon = $mech->create_body_ok(2237, 'Oxfordshire County Council'); @@ -178,6 +179,48 @@ FixMyStreet::override_config { }; } + subtest 'extra data sent with defect update' => sub { + my $comment = FixMyStreet::DB->resultset('Comment')->first; + $comment->set_extra_metadata(defect_raised => 1); + $comment->update; + $comment->problem->external_id('hey'); + $comment->problem->update; + + my $cbr = Test::MockModule->new('FixMyStreet::Cobrand::Oxfordshire'); + $cbr->mock('_fetch_features', sub { + my ($self, $cfg, $x, $y) = @_; + [ { + type => 'Feature', + geometry => { type => 'LineString', coordinates => [ [ 1, 2 ], [ 3, 4 ] ] }, + properties => { TYPE1_2_USRN => 13579 }, + } ]; + }); + my $test_res = HTTP::Response->new(); + $test_res->code(200); + $test_res->message('OK'); + $test_res->content('<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>'); + + my $o = Open311->new( + fixmystreet_body => $oxon, + test_mode => 1, + test_get_returns => { 'servicerequestupdates.xml' => $test_res }, + ); + + $o->post_service_request_update($comment); + my $cgi = CGI::Simple->new($o->test_req_used->content); + is $cgi->param('attribute[usrn]'), 13579, 'USRN sent with update'; + is $cgi->param('attribute[raise_defect]'), 1, 'Defect flag sent with update'; + + # Now set a USRN on the problem (found at submission) + $comment->problem->push_extra_fields({ name => 'usrn', value => '12345' }); + $comment->problem->update; + + $o->post_service_request_update($comment); + $cgi = CGI::Simple->new($o->test_req_used->content); + is $cgi->param('attribute[usrn]'), 12345, 'USRN sent with update'; + is $cgi->param('attribute[raise_defect]'), 1, 'Defect flag sent with update'; + }; + }; done_testing(); |