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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
use CGI::Simple;
use DateTime;
use FixMyStreet::TestMech;
use Open311;
use Open311::GetServiceRequestUpdates;
use FixMyStreet::Script::Alerts;
use FixMyStreet::Script::Reports;
ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' );
my $params = {
send_method => 'Open311',
send_comments => 1,
api_key => 'KEY',
endpoint => 'endpoint',
jurisdiction => 'home',
can_be_devolved => 1,
};
my $isleofwight = $mech->create_body_ok(2636, 'Isle of Wight Council', $params);
my $contact = $mech->create_contact_ok(
body_id => $isleofwight->id,
category => 'Potholes',
email => 'pothole@example.org',
);
$contact->set_extra_fields( ( {
code => 'urgent',
datatype => 'string',
description => 'question',
variable => 'true',
required => 'false',
order => 1,
datatype_description => 'datatype',
} ) );
$contact->update;
my $user = $mech->create_user_ok('user@example.org', name => 'Test User');
my $iow_user = $mech->create_user_ok('iow_user@example.org', from_body => $isleofwight);
$iow_user->user_body_permissions->create({
body => $isleofwight,
permission_type => 'moderate',
});
my $contact2 = $mech->create_contact_ok(
body_id => $isleofwight->id,
category => 'Roads',
email => 'roads@example.org',
send_method => 'Triage',
);
my $admin_user = $mech->create_user_ok('admin-user@example.org', name => 'Admin User', from_body => $isleofwight);
$admin_user->user_body_permissions->create({
body => $isleofwight,
permission_type => 'triage'
});
my @reports = $mech->create_problems_for_body(1, $isleofwight->id, 'An Isle of wight report', {
confirmed => '2019-05-25 09:00',
lastupdate => '2019-05-25 09:00',
latitude => 50.7108,
longitude => -1.29573,
user => $user,
external_id => 101202303
});
subtest "check clicking all reports link" => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
$mech->get_ok('/');
$mech->follow_link_ok({ text => 'All reports' });
};
$mech->content_contains("An Isle of wight report", "Isle of Wight report there");
$mech->content_contains("Island Roads", "is still on cobrand");
};
subtest "use external id for reference number" => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
$mech->get_ok('/report/' . $reports[0]->id);
};
$mech->content_contains("101202303", "Display external id as reference number");
};
subtest "only original reporter can comment" => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
$mech->get_ok('/report/' . $reports[0]->id);
$mech->content_contains('Only the original reporter may leave updates');
$mech->log_in_ok('user@example.org');
$mech->get_ok('/report/' . $reports[0]->id);
$mech->content_lacks('Only the original reporter may leave updates');
};
};
subtest "check moderation label uses correct name" => sub {
my $REPORT_URL = '/report/' . $reports[0]->id;
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => ['isleofwight'],
}, sub {
$mech->log_out_ok;
$mech->log_in_ok( $iow_user->email );
$mech->get_ok($REPORT_URL);
$mech->content_lacks('show-moderation');
$mech->follow_link_ok({ text_regex => qr/^Moderate$/ });
$mech->content_contains('show-moderation');
$mech->submit_form_ok({ with_fields => {
problem_title => 'Good good',
problem_detail => 'Good good improved',
}});
$mech->base_like( qr{\Q$REPORT_URL\E} );
$mech->content_like(qr/Moderated by Island Roads/);
};
};
$_->delete for @reports;
my $system_user = $mech->create_user_ok('system_user@example.org');
for my $status ( qw/ CLOSED FIXED DUPLICATE NOT_COUNCILS_RESPONSIBILITY NO_FURTHER_ACTION / ) {
subtest "updates which mark report as $status close it to comments" => sub {
my $dt = DateTime->now(formatter => DateTime::Format::W3CDTF->new)->add( minutes => -5 );
my ($p) = $mech->create_problems_for_body(1, $isleofwight->id, '', { lastupdate => $dt });
$p->update({ external_id => $p->id });
my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?>
<service_requests_updates>
<request_update>
<update_id>UPDATE_ID</update_id>
<service_request_id>SERVICE_ID</service_request_id>
<service_request_id_ext>ID_EXTERNAL</service_request_id_ext>
<status>STATUS</status>
<description>This is a note</description>
<updated_datetime>UPDATED_DATETIME</updated_datetime>
</request_update>
</service_requests_updates>
};
my $update_dt = DateTime->now(formatter => DateTime::Format::W3CDTF->new);
$requests_xml =~ s/STATUS/$status/;
$requests_xml =~ s/UPDATE_ID/@{[$p->id]}/;
$requests_xml =~ s/SERVICE_ID/@{[$p->id]}/;
$requests_xml =~ s/ID_EXTERNAL/@{[$p->id]}/;
$requests_xml =~ s/UPDATED_DATETIME/$update_dt/;
my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1, test_get_returns => { 'servicerequestupdates.xml' => $requests_xml } );
my $update = Open311::GetServiceRequestUpdates->new(
system_user => $system_user,
current_open311 => $o,
current_body => $isleofwight,
);
FixMyStreet::override_config {
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
$update->process_body;
};
$mech->log_in_ok('user@example.org');
$mech->get_ok('/report/' . $p->id);
$mech->content_lacks('Provide an update', "No update form on report");
$p->discard_changes;
is $p->get_extra_metadata('closed_updates'), 1, "report closed to updates";
$p->comments->delete;
$p->delete;
};
}
subtest "fixing passes along the correct message" => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
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 => $isleofwight,
test_mode => 1,
test_get_returns => { 'servicerequestupdates.xml' => $test_res },
);
my ($p) = $mech->create_problems_for_body(1, $isleofwight->id, 'Title', { external_id => 1 });
my $c = FixMyStreet::App->model('DB::Comment')->create({
problem => $p, user => $p->user, anonymous => 't', text => 'Update text',
problem_state => 'fixed - council', state => 'confirmed', mark_fixed => 0,
confirmed => DateTime->now(),
});
my $id = $o->post_service_request_update($c);
is $id, 248, 'correct update ID returned';
my $cgi = CGI::Simple->new($o->test_req_used->content);
like $cgi->param('description'), qr/^FMS-Update:/, 'FMS update prefix included';
unlike $cgi->param('description'), qr/The customer indicated that this issue had been fixed/, 'No fixed message included';
$c = $mech->create_comment_for_problem($p, $p->user, 'Name', 'Update text', 'f', 'confirmed', 'fixed - user', { confirmed => \'current_timestamp' });
$c->discard_changes; # Otherwise cannot set_nanosecond
$id = $o->post_service_request_update($c);
is $id, 248, 'correct update ID returned';
$cgi = CGI::Simple->new($o->test_req_used->content);
like $cgi->param('description'), qr/^FMS-Update: \[The customer indicated that this issue had been fixed/, 'Fixed message included';
$p->comments->delete;
$p->delete;
};
};
subtest 'Check special Open311 request handling', sub {
$mech->clear_emails_ok;
my ($p) = $mech->create_problems_for_body(1, $isleofwight->id, 'Title', { category => 'Potholes', latitude => 50.7108, longitude => -1.29573, cobrand => 'isleofwight' });
$p->set_extra_fields({ name => 'urgent', value => 'no'});
$p->update;
my $test_data;
FixMyStreet::override_config {
STAGING_FLAGS => { send_reports => 1 },
ALLOWED_COBRANDS => ['isleofwight' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$test_data = FixMyStreet::Script::Reports::send();
};
$p->discard_changes;
ok $p->whensent, 'Report marked as sent';
is $p->send_method_used, 'Open311', 'Report sent via Open311';
is $p->external_id, 248, 'Report has right external ID';
my $req = $test_data->{test_req_used};
my $c = CGI::Simple->new($req->content);
is $c->param('attribute[urgent]'), undef, 'no urgent param sent';
$mech->email_count_is(1);
my $email = $mech->get_email;
ok $email, "got an email";
like $mech->get_text_body_from_email($email),
qr/your enquiry has been received by Island Roads/, "correct report send email text";
};
subtest "triaging a report includes user in message" => sub {
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => 'isleofwight',
}, sub {
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 => $isleofwight,
test_mode => 1,
test_get_returns => { 'servicerequestupdates.xml' => $test_res },
);
my ($p) = $mech->create_problems_for_body(1, $isleofwight->id, 'Title', { external_id => 1 });
my $c = FixMyStreet::App->model('DB::Comment')->create({
problem => $p, user => $p->user, anonymous => 't', text => 'Update text',
problem_state => 'confirmed', state => 'confirmed', mark_fixed => 0,
confirmed => DateTime->now(),
});
$c->set_extra_metadata('triage_report', 1);
$c->update;
my $id = $o->post_service_request_update($c);
is $id, 248, 'correct update ID returned';
my $cgi = CGI::Simple->new($o->test_req_used->content);
my $name = $p->user->name . ' \(' . $p->user->email . '\)';
like $cgi->param('description'), qr/^FMS-Update:/, 'FMS update prefix included';
like $cgi->param('description'), qr/Triaged by $name/, 'Triage user details included';
$p->comments->delete;
$p->delete;
};
};
my ($p) = $mech->create_problems_for_body(1, $isleofwight->id, '', { cobrand => 'isleofwight' });
my $alert = FixMyStreet::App->model('DB::Alert')->create( {
parameter => $p->id,
alert_type => 'new_updates',
user => $user,
cobrand => 'isleofwight',
} )->confirm;
subtest "sends branded alert emails" => sub {
$mech->create_comment_for_problem($p, $system_user, 'Other User', 'This is some update text', 'f', 'confirmed', undef, { confirmed => DateTime->now->add( minutes => 5 ) });
$mech->clear_emails_ok;
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => ['isleofwight','fixmystreet'],
}, sub {
FixMyStreet::Script::Alerts::send();
};
$mech->email_count_is(1);
my $email = $mech->get_email;
ok $email, "got an email";
like $mech->get_text_body_from_email($email), qr/Island Roads/, "emails are branded";
};
$p->comments->delete;
$p->delete;
subtest "sends branded confirmation emails" => sub {
$mech->log_out_ok;
$mech->clear_emails_ok;
$mech->get_ok('/around');
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'isleofwight' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->submit_form_ok( { with_fields => { pc => 'PO30 5XJ', } },
"submit location" );
# click through to the report page
$mech->follow_link_ok( { text_regex => qr/skip this step/i, },
"follow 'skip this step' link" );
$mech->submit_form_ok(
{
button => 'submit_register',
with_fields => {
title => 'Test Report',
detail => 'Test report details.',
photo1 => '',
name => 'Joe Bloggs',
username => 'test-1@example.com',
category => 'Roads',
}
},
"submit good details"
);
$mech->email_count_is(1);
my $email = $mech->get_email;
ok $email, "got an email";
like $mech->get_text_body_from_email($email), qr/Island Roads/, "emails are branded";
my $url = $mech->get_link_from_email($email);
$mech->get_ok($url);
$mech->clear_emails_ok;
};
};
subtest "sends branded report sent emails" => sub {
$mech->clear_emails_ok;
FixMyStreet::override_config {
STAGING_FLAGS => { send_reports => 1 },
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => ['isleofwight','fixmystreet'],
}, sub {
FixMyStreet::Script::Reports::send();
};
$mech->email_count_is(1);
my $email = $mech->get_email;
ok $email, "got an email";
like $mech->get_text_body_from_email($email), qr/Island Roads/, "emails are branded";
};
subtest "check category extra uses correct name" => sub {
my @extras = ( {
code => 'test',
datatype => 'string',
description => 'question',
variable => 'true',
required => 'false',
order => 1,
datatype_description => 'datatype',
} );
$contact2->set_extra_fields( @extras );
$contact2->update;
my $extra_details;
FixMyStreet::override_config {
MAPIT_URL => 'http://mapit.uk/',
ALLOWED_COBRANDS => ['isleofwight','fixmystreet'],
}, sub {
$extra_details = $mech->get_ok_json('/report/new/category_extras?category=Roads&latitude=50.71086&longitude=-1.29573');
};
like $extra_details->{category_extra}, qr/Island Roads/, 'correct name in category extras';
};
subtest "reports are marked for triage upon submission" => sub {
$mech->log_out_ok;
$mech->clear_emails_ok;
$mech->log_in_ok($user->email);
$mech->get_ok('/around');
FixMyStreet::override_config {
STAGING_FLAGS => { send_reports => 1, skip_checks => 0 },
ALLOWED_COBRANDS => [ 'isleofwight' ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->submit_form_ok( { with_fields => { pc => 'PO30 5XJ', } },
"submit location" );
# click through to the report page
$mech->follow_link_ok( { text_regex => qr/skip this step/i, },
"follow 'skip this step' link" );
$mech->submit_form_ok(
{
button => 'submit_register',
with_fields => {
title => 'Test Report',
detail => 'Test report details.',
photo1 => '',
category => 'Roads',
}
},
"submit good details"
);
my $report = $user->problems->first;
ok $report, "Found the report";
is $report->state, 'confirmed', 'report confirmed';
$mech->clear_emails_ok;
FixMyStreet::Script::Reports::send();
$report->discard_changes;
is $report->state, 'for triage', 'report marked as for triage';
ok $report->whensent, 'report marked as sent';
$mech->email_count_is(1);
my $email = $mech->get_email;
like $mech->get_text_body_from_email($email),
qr/submitted to Island Roads for review/, 'correct text for email sent for Triage';
};
};
for my $cobrand ( 'fixmystreet', 'isleofwight' ) {
subtest "only categories for Triage are displayed on " . $cobrand => sub {
$mech->log_out_ok;
$mech->get_ok('/around');
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ $cobrand ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->submit_form_ok( { with_fields => { pc => 'PO30 5XJ', } },
"submit location" );
# click through to the report page
$mech->follow_link_ok( { text_regex => qr/skip this step/i, },
"follow 'skip this step' link" );
my $f = $mech->form_name('mapSkippedForm');
ok $f, 'found form';
my $cats = $f->find_input('category');
ok $cats, 'found category element';
my @values = $cats->possible_values;
is_deeply \@values, [ '-- Pick a category --', 'Roads' ], 'correct category list';
};
};
subtest "staff user can see non Triage categories on " . $cobrand => sub {
$mech->log_out_ok;
$mech->log_in_ok($admin_user->email);
$mech->get_ok('/around');
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ $cobrand ],
MAPIT_URL => 'http://mapit.uk/',
}, sub {
$mech->submit_form_ok( { with_fields => { pc => 'PO30 5XJ', } },
"submit location" );
# click through to the report page
$mech->follow_link_ok( { text_regex => qr/skip this step/i, },
"follow 'skip this step' link" );
my $f = $mech->form_name('mapSkippedForm');
ok $f, 'found form';
my $cats = $f->find_input('category');
ok $cats, 'found category element';
my @values = $cats->possible_values;
is_deeply \@values, [ '-- Pick a category --', 'Potholes' ], 'correct category list';
};
};
}
done_testing();
|