aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/report_display.t
blob: aa7e507d95f6668fae644418633c42bf98cffd15 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use strict;
use warnings;
use Test::More;

use FixMyStreet::TestMech;
use Web::Scraper;
use Path::Class;
use DateTime;

my $mech = FixMyStreet::TestMech->new;

# create a test user and report
$mech->delete_user('test@example.com');
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              => 'confirmed',
        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";

subtest "check that no id redirects to homepage" => sub {
    $mech->get_ok('/report');
    is $mech->uri->path, '/', "at home page";
};

subtest "test id=NNN redirects to /NNN" => sub {
    $mech->get_ok("/report?id=$report_id");
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
};

subtest "test bad council email clients web links" => sub {
    $mech->get_ok("/report/3D$report_id");
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
};

subtest "test tailing non-ints get stripped" => sub {
    $mech->get_ok("/report/${report_id}xx ");
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
};

subtest "test bad ids get dealt with (404)" => sub {
    foreach my $id ( 'XXX', 99999999 ) {
        ok $mech->get("/report/$id"), "get '/report/$id'";
        is $mech->res->code, 404,           "page not found";
        is $mech->uri->path, "/report/$id", "at /report/$id";
        $mech->content_contains('Unknown problem ID');
    }
};

subtest "change report to unconfirmed and check for 404 status" => sub {
    ok $report->update( { state => 'unconfirmed' } ), 'unconfirm report';
    ok $mech->get("/report/$report_id"), "get '/report/$report_id'";
    is $mech->res->code, 404, "page not found";
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
    $mech->content_contains('Unknown problem ID');
    ok $report->update( { state => 'confirmed' } ), 'confirm report again';
};

subtest "change report to hidden and check for 410 status" => sub {
    ok $report->update( { state => 'hidden' } ), 'hide report';
    ok $mech->get("/report/$report_id"), "get '/report/$report_id'";
    is $mech->res->code, 410, "page gone";
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
    $mech->content_contains('That report has been removed from FixMyStreet.');
    ok $report->update( { state => 'confirmed' } ), 'confirm report again';
};

subtest "test a good report" => sub {
    $mech->get_ok("/report/$report_id");
    is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
    is $mech->extract_problem_title, 'Test 2', 'problem title';
    is $mech->extract_problem_meta,
      'Reported by Test User at 15:47, Saturday 16 April 2011',
      'correct problem meta information';
    $mech->content_contains('Test 2 Detail');

    my $update_form = $mech->form_name('updateForm');

    my %fields = (
        name      => '',
        rznvy     => '',
        update    => '',
        add_alert => 1, # defaults to true
        fixed     => undef
    );
    is $update_form->value($_), $fields{$_}, "$_ value" for keys %fields;
};

foreach my $meta (
    {
        anonymous => 'f',
        category  => 'Other',
        service   => '',
        meta      => 'Reported by Test User at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 'f',
        category  => 'Roads',
        service   => '',
        meta =>
'Reported in the Roads category by Test User at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 'f',
        category  => '',
        service   => 'Transport service',
        meta =>
'Reported by Transport service by Test User at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 'f',
        category  => 'Roads',
        service   => 'Transport service',
        meta =>
'Reported by Transport service in the Roads category by Test User at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 't',
        category  => 'Other',
        service   => '',
        meta      => 'Reported anonymously at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 't',
        category  => 'Roads',
        service   => '',
        meta =>
'Reported in the Roads category anonymously at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 't',
        category  => '',
        service   => 'Transport service',
        meta =>
'Reported by Transport service anonymously at 15:47, Saturday 16 April 2011'
    },
    {
        anonymous => 't',
        category  => 'Roads',
        service   => 'Transport service',
        meta =>
'Reported by Transport service in the Roads category anonymously at 15:47, Saturday 16 April 2011'
    },
  )
{
    $report->service( $meta->{service} );
    $report->category( $meta->{category} );
    $report->anonymous( $meta->{anonymous} );
    $report->update;
    subtest "test correct problem meta information" => sub {
        $mech->get_ok("/report/$report_id");
    
    is $mech->extract_problem_meta, $meta->{meta};

    };
}

for my $test ( 
    {
        description => 'old report',
        date => DateTime->new(
            year => 2009,
            month => 6,
            day => 12,
            hour => 9,
            minute => 43,
            second => 12
        ),
        state => 'confirmed',
        banner_id => 'unknown',
        banner_text => 'This problem is old and of unknown status.',
        fixed => 0
    },
    {
        description => 'old fixed report',
        date => DateTime->new(
            year => 2009,
            month => 6,
            day => 12,
            hour => 9,
            minute => 43,
            second => 12
        ),
        state => 'fixed',
        banner_id => 'fixed',
        banner_text => 'This problem has been fixed.',
        fixed => 1
    },
    {
        description => 'fixed report',
        date => DateTime->now,
        state => 'fixed',
        banner_id => 'fixed',
        banner_text => 'This problem has been fixed.',
        fixed => 1
    },
) {
    subtest "banner for $test->{description}" => sub {
        $report->confirmed( $test->{date}->ymd . ' ' . $test->{date}->hms );
        $report->state( $test->{state} );
        $report->update;

        $mech->get_ok("/report/$report_id");
        is $mech->uri->path, "/report/$report_id", "at /report/$report_id";
        my $banner = $mech->extract_problem_banner;
        $banner->{text} =~ s/^ //g;
        $banner->{text} =~ s/ $//g;

        is $banner->{id}, $test->{banner_id}, 'banner id';
        is $banner->{text}, $test->{banner_text}, 'banner text';

        my $update_form = $mech->form_name( 'updateForm' );
        if ( $test->{fixed} ) {
            is $update_form->find_input( 'fixed' ), undef, 'problem is fixed';
        } else {
            ok $update_form->find_input( 'fixed' ), 'problem is not fixed';
        }
    };
}

# tidy up
$mech->delete_user('test@example.com');
done_testing();