aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-12-13 10:49:08 +0000
committerStruan Donald <struan@exo.org.uk>2011-12-13 10:49:08 +0000
commitdb621a8b669d402f15131e6da82352c2b380ae72 (patch)
tree30548a182cbf581bb3975d165b6fc27246936be2
parentfc36c3efb725c1bce74853c250ab61fca838d4de (diff)
deal with missing geocode data when generating nearest road text in email alerts
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/AlertType.pm10
-rw-r--r--t/app/model/alert_type.t142
2 files changed, 147 insertions, 5 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm
index c3d4a3672..32654e534 100644
--- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm
@@ -235,12 +235,14 @@ sub _get_address_from_gecode {
my $h = new IO::String($geocode);
my $data = RABX::wire_rd($h);
+ my $str = '';
+
my $address = $data->{resourceSets}[0]{resources}[0]{address};
my @address;
- push @address, $address->{addressLine} if $address->{addressLine} ne 'Street';
- push @address, $address->{locality};
- my $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s\n\n"),
- join( ', ', @address ) );
+ push @address, $address->{addressLine} if $address->{addressLine} && $address->{addressLine} ne 'Street';
+ push @address, $address->{locality} if $address->{locality};
+ $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s\n\n"),
+ join( ', ', @address ) ) if @address;
return $str;
}
diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t
index e88aeea5f..1adc584d7 100644
--- a/t/app/model/alert_type.t
+++ b/t/app/model/alert_type.t
@@ -195,7 +195,7 @@ for my $test (
$report->update;
FixMyStreet::App->model('DB::AlertType')->email_alerts();
-
+
$mech->email_count_is( 1 );
my $email = $mech->get_email;
my $pc = $test->{expected_postcode};
@@ -209,5 +209,145 @@ for my $test (
$report->postcode( 'SW1A 1AA' );
$report->update;
+$report->geocode(
+{
+ 'traceId' => 'ae7c4880b70b423ebc8ab4d80961b3e9|LTSM001158|02.00.71.1600|LTSMSNVM002010, LTSMSNVM001477',
+ 'statusDescription' => 'OK',
+ 'brandLogoUri' => 'http://dev.virtualearth.net/Branding/logo_powered_by.png',
+ 'resourceSets' => [
+ {
+ 'resources' => [
+ {
+ 'geocodePoints' => [
+ {
+ 'calculationMethod' => 'Interpolation',
+ 'coordinates' => [
+ '55.9532357007265',
+ '-3.18906001746655'
+ ],
+ 'usageTypes' => [
+ 'Display',
+ 'Route'
+ ],
+ 'type' => 'Point'
+ }
+ ],
+ 'entityType' => 'Address',
+ 'name' => '18 N Bridge, Edinburgh EH1 1',
+ 'point' => {
+ 'coordinates' => [
+ '55.9532357007265',
+ '-3.18906001746655'
+ ],
+ 'type' => 'Point'
+ },
+ 'bbox' => [
+ '55.9493729831558',
+ '-3.19825819222605',
+ '55.9570984182972',
+ '-3.17986184270704'
+ ],
+ 'matchCodes' => [
+ 'Good'
+ ],
+ 'address' => {
+ 'countryRegion' => 'United Kingdom',
+ 'adminDistrict2' => 'Edinburgh City',
+ 'adminDistrict' => 'Scotland',
+ 'addressLine' => '18 North Bridge',
+ 'formattedAddress' => '18 N Bridge, Edinburgh EH1 1',
+ 'postalCode' => 'EH1 1',
+ 'locality' => 'Edinburgh'
+ },
+ 'confidence' => 'Medium',
+ '__type' => 'Location:http://schemas.microsoft.com/search/local/ws/rest/v1'
+ }
+ ],
+ 'estimatedTotal' => 1
+ }
+ ],
+ 'copyright' => "Copyright \x{a9} 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
+ 'statusCode' => 200,
+ 'authenticationResultCode' => 'ValidCredentials'
+ }
+);
+$report->update();
+
+foreach my $test (
+ {
+ desc => 'all fields',
+ addressLine => '18 North Bridge',
+ locality => 'Edinburgh',
+ nearest => qr/18 North Bridge, Edinburgh/,
+ },
+ {
+ desc => 'address with Street only',
+ addressLine => 'Street',
+ locality => 'Edinburgh',
+ nearest => qr/: Edinburgh/,
+ },
+ {
+ desc => 'locality only',
+ addressLine => undef,
+ locality => 'Edinburgh',
+ nearest => qr/: Edinburgh/,
+ },
+ {
+ desc => 'address only',
+ addressLine => '18 North Bridge',
+ locality => undef,
+ nearest => qr/: 18 North Bridge\n/,
+ },
+ {
+ desc => 'no fields',
+ addressLine => undef,
+ locality => undef,
+ nearest => '',
+ },
+ {
+ desc => 'no address',
+ no_address => 1,
+ nearest => '',
+ },
+) {
+ subtest "correct Nearest Road text with $test->{desc}" => sub {
+ $mech->clear_emails_ok;
+
+ my $sent = FixMyStreet::App->model('DB::AlertSent')->search(
+ {
+ alert_id => $council_alert->id,
+ parameter => $report->id,
+ }
+ )->delete;
+
+ my $g = $report->geocode;
+ if ( $test->{no_address} ) {
+ $g->{resourceSets}[0]{resources}[0]{address} = undef;
+ } else {
+ $g->{resourceSets}[0]{resources}[0]{address}->{addressLine} = $test->{addressLine};
+ $g->{resourceSets}[0]{resources}[0]{address}->{locality} = $test->{locality};
+ }
+
+ # if we don't do this then it ignores the change
+ $report->geocode( undef );
+ $report->geocode( $g );
+ $report->update();
+
+ FixMyStreet::App->model('DB::AlertType')->email_alerts();
+
+ $mech->email_count_is( 1 );
+ my $email = $mech->get_email;
+ my $body = $email->body;
+
+ if ( $test->{nearest} ) {
+ like $body, $test->{nearest}, 'correct nearest line';
+ } else {
+ unlike $body, qr/Nearest Road/, 'no nearest line';
+ }
+ };
+}
+
+$report->comments->delete();
+$report->delete();
done_testing();