aboutsummaryrefslogtreecommitdiffstats
path: root/t/cobrand/closest.t
blob: 02c9797562c9fcd350e0feebc9b1e93b37a0965c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use strict;
use warnings;

use Test::More;

use mySociety::Locale;
use FixMyStreet::App;

use_ok 'FixMyStreet::Cobrand';

mySociety::Locale::gettext_domain( 'FixMyStreet' );

my $c = FixMyStreet::Cobrand::UK->new();

my $user =
  FixMyStreet::App->model('DB::User')
  ->find_or_create( { email => 'test@example.com', name => 'Test User' } );
ok $user, "created test user";

my $dt = DateTime->new(
    year   => 2011,
    month  => 04,
    day    => 16,
    hour   => 15,
    minute => 47,
    second => 23
);

my $report = FixMyStreet::App->model('DB::Problem')->find_or_create(
    {
        postcode           => 'SW1A 1AA',
        council            => '2504',
        areas              => ',105255,11806,11828,2247,2504,',
        category           => 'Other',
        title              => 'Test 2',
        detail             => 'Test 2 Detail',
        used_map           => 't',
        name               => 'Test User',
        anonymous          => 'f',
        state              => 'closed',
        confirmed          => $dt->ymd . ' ' . $dt->hms,
        lang               => 'en-gb',
        service            => '',
        cobrand            => 'default',
        cobrand_data       => '',
        send_questionnaire => 't',
        latitude           => '51.5016605453401',
        longitude          => '-0.142497580865087',
        user_id            => $user->id,
    }
);
my $report_id = $report->id;
ok $report, "created test report - $report_id";

$report->geocode( undef );

ok !$report->geocode, 'no gecode entry for report';

my $near = $c->find_closest( $report->latitude, $report->longitude, $report );

SKIP: {
    if (!FixMyStreet->config('BING_MAPS_API_KEY')) {
        skip 'No Bing Maps key', 0;
    }

    ok $report->geocode, 'geocode entry added to report';
    ok $report->geocode->{resourceSets}, 'geocode entry looks like right sort of thing';

    like $near, qr/Constitution Hill/i, 'nearest street looks right';
    like $near, qr/Nearest postcode .*: SW1A 1AA/i, 'nearest postcode looks right';

    $near = $c->find_closest_address_for_rss( $report->latitude, $report->longitude, $report );

    like $near, qr/Constitution Hill/i, 'nearest street for RSS looks right';
    unlike $near, qr/Nearest postcode/i, 'no nearest postcode in RSS text';

    $report->geocode( undef );
    $near = $c->find_closest_address_for_rss( $report->latitude, $report->longitude, $report );

    ok !$near, 'no closest address for RSS if not cached';
}

# all done
done_testing();