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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
# TODO
# Overdue alerts
use strict;
use warnings;
use DateTime;
use Test::More;
use Test::LongString;
use Path::Tiny;
# Check that you have the required locale installed - the following
# should return a line with de_CH.utf8 in. If not install that locale.
#
# locale -a | grep de_CH
#
# To generate the translations use:
#
# commonlib/bin/gettext-makemo FixMyStreet
use FixMyStreet;
my $cobrand = FixMyStreet::Cobrand::Zurich->new();
my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg");
ok $sample_file->exists, "sample file $sample_file exists";
my $sample_photo = $sample_file->slurp_raw;
# This is a helper method that will send the reports but with the config
# correctly set - notably SEND_REPORTS_ON_STAGING needs to be true, and
# zurich must be allowed cobrand if we want to be able to call cobrand
# methods on it.
sub send_reports_for_zurich {
FixMyStreet::override_config {
SEND_REPORTS_ON_STAGING => 1,
ALLOWED_COBRANDS => ['zurich']
}, sub {
# Actually send the report
FixMyStreet::DB->resultset('Problem')->send_reports('zurich');
};
}
sub reset_report_state {
my ($report, $created) = @_;
$report->discard_changes;
$report->unset_extra_metadata('moderated_overdue');
$report->unset_extra_metadata('subdiv_overdue');
$report->unset_extra_metadata('closed_overdue');
$report->unset_extra_metadata('closure_status');
$report->whensent(undef);
$report->state('unconfirmed');
$report->created($created) if $created;
$report->update;
}
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
# Front page test
ok $mech->host("zurich.example.com"), "change host to Zurich";
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok('/');
};
$mech->content_like( qr/zurich/i );
# Set up bodies
my $zurich = $mech->create_body_ok( 1, 'Zurich' );
$zurich->parent( undef );
$zurich->update;
my $division = $mech->create_body_ok( 2, 'Division 1' );
$division->parent( $zurich->id );
$division->send_method( 'Zurich' );
$division->endpoint( 'division@example.org' );
$division->update;
$division->body_areas->find_or_create({ area_id => 274456 });
my $subdivision = $mech->create_body_ok( 3, 'Subdivision A' );
$subdivision->parent( $division->id );
$subdivision->send_method( 'Zurich' );
$subdivision->endpoint( 'subdivision@example.org' );
$subdivision->update;
my $external_body = $mech->create_body_ok( 4, 'External Body' );
$external_body->send_method( 'Zurich' );
$external_body->endpoint( 'external_body@example.org' );
$external_body->update;
sub get_export_rows_count {
my $mech = shift;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok( '/admin/stats?export=1' );
};
is $mech->res->code, 200, 'csv retrieved ok';
is $mech->content_type, 'text/csv', 'content_type correct' and do {
my @lines = split /\n/, $mech->content;
return @lines - 1;
};
return;
}
my $EXISTING_REPORT_COUNT = 0;
subtest "set up superuser" => sub {
my $superuser = $mech->log_in_ok( 'super@example.org' );
# a user from body $zurich is a superuser, as $zurich has no parent id!
$superuser->update({ from_body => $zurich->id });
$EXISTING_REPORT_COUNT = get_export_rows_count($mech);
$mech->log_out_ok;
};
my @reports = $mech->create_problems_for_body( 1, $division->id, 'Test', {
state => 'unconfirmed',
confirmed => undef,
cobrand => 'zurich',
photo => $sample_photo,
});
my $report = $reports[0];
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/report/' . $report->id );
};
$mech->content_contains('Überprüfung ausstehend')
or die $mech->content;
# Check logging in to deal with this report
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok( '/admin' );
is $mech->uri->path, '/auth', "got sent to the sign in page";
my $user = $mech->log_in_ok( 'dm1@example.org') ;
$user->from_body( undef );
$user->update;
$mech->get_ok( '/admin' );
is $mech->uri->path, '/my', "got sent to /my";
$user->from_body( $division->id );
$user->update;
$mech->get_ok( '/admin' );
};
is $mech->uri->path, '/admin', "am logged in";
$mech->content_contains( 'report_edit/' . $report->id );
$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") );
$mech->content_contains( 'Erfasst' );
subtest "changing of categories" => sub {
# create a few categories (which are actually contacts)
foreach my $name ( qw/Cat1 Cat2/ ) {
$mech->create_contact_ok(
body => $division,
category => $name,
email => "$name\@example.org",
);
}
# full Categories dropdown is hidden for unconfirmed reports
$report->update({ state => 'confirmed' });
# put report into known category
my $original_category = $report->category;
$report->update({ category => 'Cat1' });
is( $report->category, "Cat1", "Category set to Cat1" );
# get the latest comment
my $comments_rs = $report->comments->search({},{ order_by => { -desc => "created" } });
ok ( !$comments_rs->first, "There are no comments yet" );
# change the category via the web interface
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { category => 'Cat2' } } );
};
# check changes correctly saved
$report->discard_changes();
is( $report->category, "Cat2", "Category changed to Cat2 as expected" );
# Check that a new comment has been created.
my $new_comment = $comments_rs->first();
is( $new_comment->text, "Weitergeleitet von Cat1 an Cat2", "category change comment created" );
# restore report to original category.
$report->update({category => $original_category });
};
sub get_moderated_count {
# my %date_params = ( );
# my $moderated = FixMyStreet::DB->resultset('Problem')->search({
# extra => { like => '%moderated_overdue,I1:0%' }, %date_params } )->count;
# return $moderated;
# use a separate mech to avoid stomping on test state
my $mech = FixMyStreet::TestMech->new;
my $user = $mech->log_in_ok( 'super@example.org' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get( '/admin/stats' );
};
if ($mech->content =~/Innerhalb eines Arbeitstages moderiert: (\d+)/) {
return $1;
}
else {
fail sprintf "Could not get moderation results (%d)", $mech->status;
return undef;
}
}
subtest "report_edit" => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
reset_report_state($report);
ok ( ! $report->get_extra_metadata('moderated_overdue'), 'Report currently unmoderated' );
is get_moderated_count(), 0;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->content_contains( 'Unbestätigt' ); # Unconfirmed email
$mech->submit_form_ok( { with_fields => { state => 'confirmed' } } );
$mech->get_ok( '/report/' . $report->id );
$report->discard_changes();
$mech->content_contains('Aufgenommen');
$mech->content_contains('Test Test');
$mech->content_lacks('photo/' . $report->id . '.0.jpeg');
$mech->email_count_is(0);
$report->discard_changes;
is ( $report->get_extra_metadata('moderated_overdue'), 0, 'Report now marked moderated' );
is get_moderated_count(), 1;
# Set state back to 10 days ago so that report is overdue
my $created = $report->created;
reset_report_state($report, $created->clone->subtract(days => 10));
is get_moderated_count(), 0;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'confirmed' } } );
$mech->get_ok( '/report/' . $report->id );
$report->discard_changes;
is ( $report->get_extra_metadata('moderated_overdue'), 1, 'moderated_overdue set correctly when overdue' );
is get_moderated_count(), 0, 'Moderated count not increased when overdue';
reset_report_state($report, $created);
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'confirmed' } } );
$mech->get_ok( '/report/' . $report->id );
$report->discard_changes;
is ( $report->get_extra_metadata('moderated_overdue'), 0, 'Marking confirmed sets moderated_overdue' );
is ( $report->get_extra_metadata('closed_overdue'), undef, 'Marking confirmed does NOT set closed_overdue' );
is get_moderated_count(), 1;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'hidden' } } );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$report->discard_changes;
is ( $report->get_extra_metadata('moderated_overdue'), 0, 'Still marked moderated_overdue' );
is ( $report->get_extra_metadata('closed_overdue'), undef, "Marking hidden doesn't set closed_overdue..." );
is ( $report->state, 'planned', 'Marking hidden actually sets state to planned');
is ( $report->get_extra_metadata('closure_status'), 'hidden', 'Marking hidden sets closure_status to hidden');
is get_moderated_count(), 1, 'Check still counted moderated'
or diag $report->get_column('extra');
# publishing actually sets hidden
$mech->form_with_fields( 'status_update' );
$mech->submit_form_ok( { button => 'publish_response' } );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$report->discard_changes;
is ( $report->get_extra_metadata('closed_overdue'), 0, "Closing as hidden sets closed_overdue..." );
is ( $report->state, 'hidden', 'Closing as hidden sets state to hidden');
is ( $report->get_extra_metadata('closure_status'), undef, 'Closing as hidden unsets closure_status');
reset_report_state($report);
is ( $report->get_extra_metadata('moderated_overdue'), undef, 'Sanity check' );
is get_moderated_count(), 0;
# Check that setting to 'hidden' also triggers moderation
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'hidden' } } );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'status_update' );
$mech->submit_form_ok( { button => 'publish_response' } );
$report->discard_changes;
is ( $report->get_extra_metadata('moderated_overdue'), 0, 'Marking hidden from scratch sets moderated_overdue' );
is ( $report->get_extra_metadata('closed_overdue'), 0, 'Marking hidden from scratch also set closed_overdue' );
is get_moderated_count(), 1;
is ($cobrand->get_or_check_overdue($report), 0, 'sanity check');
$report->update({ created => $created->clone->subtract(days => 10) });
is ($cobrand->get_or_check_overdue($report), 0, 'overdue call not increased');
reset_report_state($report, $created);
}
};
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
# Photo publishing
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'confirmed', publish_photo => 1 } } );
$mech->get_ok( '/report/' . $report->id );
$mech->content_contains('photo/' . $report->id . '.0.jpeg');
# Internal notes
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { new_internal_note => 'Initial internal note.' } } );
$mech->submit_form_ok( { with_fields => { new_internal_note => 'Another internal note.' } } );
$mech->content_contains( 'Initial internal note.' );
$mech->content_contains( 'Another internal note.' );
# Original description
$mech->submit_form_ok( { with_fields => { detail => 'Edited details text.' } } );
$mech->content_contains( 'Edited details text.' );
$mech->content_contains( 'Originaltext: “Test Test 1 for ' . $division->id . ' Detail”' );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { body_subdivision => $subdivision->id } } );
$mech->get_ok( '/report/' . $report->id );
$mech->content_contains('In Bearbeitung');
$mech->content_contains('Test Test');
};
send_reports_for_zurich();
my $email = $mech->get_email;
like $email->header('Subject'), qr/Neue Meldung/, 'subject looks okay';
like $email->header('To'), qr/subdivision\@example.org/, 'to line looks correct';
$mech->clear_emails_ok;
$mech->log_out_ok;
subtest 'SDM' => sub {
my $user = $mech->log_in_ok( 'sdm1@example.org') ;
$user->update({ from_body => undef });
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok( '/admin' );
};
is $mech->uri->path, '/my', "got sent to /my";
$user->from_body( $subdivision->id );
$user->update;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok( '/admin' );
};
is $mech->uri->path, '/admin', "am logged in";
$mech->content_contains( 'report_edit/' . $report->id );
$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") );
$mech->content_contains( 'In Bearbeitung' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->content_contains( 'Initial internal note' );
$mech->submit_form_ok( { with_fields => { status_update => 'This is an update.' } } );
is $mech->uri->path, '/admin/report_edit/' . $report->id, "still on edit page";
$mech->content_contains('This is an update');
ok $mech->form_with_fields( 'status_update' );
$mech->submit_form_ok( { button => 'no_more_updates' } );
is $mech->uri->path, '/admin/summary', "redirected now finished with report.";
$mech->get_ok( '/report/' . $report->id );
$mech->content_contains('In Bearbeitung');
$mech->content_contains('Test Test');
};
send_reports_for_zurich();
$email = $mech->get_email;
like $email->header('Subject'), qr/Feedback/, 'subject looks okay';
like $email->header('To'), qr/division\@example.org/, 'to line looks correct';
$mech->clear_emails_ok;
$report->discard_changes;
is $report->state, 'planned', 'Report now in planned state';
subtest 'send_back' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$report->update({ bodies_str => $subdivision->id, state => 'in progress' });
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { form_number => 2, button => 'send_back' } );
$report->discard_changes;
is $report->state, 'confirmed', 'Report sent back to confirmed state';
is $report->bodies_str, $division->id, 'Report sent back to division';
};
};
subtest 'not contactable' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$report->update({ bodies_str => $subdivision->id, state => 'in progress' });
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { button => 'not_contactable', form_number => 2 } );
$report->discard_changes;
is $report->state, 'planned', 'Report sent back to Rueckmeldung ausstehend state';
is $report->get_extra_metadata('closure_status'), 'partial', 'Report sent back to partial (not_contactable) state';
is $report->bodies_str, $division->id, 'Report sent back to division';
};
};
$mech->log_out_ok;
};
my $user = $mech->log_in_ok( 'dm1@example.org') ;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get_ok( '/admin' );
};
reset_report_state($report);
$report->update({ state => 'planned' });
$mech->content_contains( 'report_edit/' . $report->id );
$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") );
# User confirms their email address
$report->set_extra_metadata(email_confirmed => 1);
$report->update;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->content_lacks( 'Unbestätigt' ); # Confirmed email
$mech->submit_form_ok( { with_fields => { status_update => 'FINAL UPDATE' } } );
$mech->form_with_fields( 'status_update' );
$mech->submit_form_ok( { button => 'publish_response' } );
$mech->get_ok( '/report/' . $report->id );
};
$mech->content_contains('Beantwortet');
$mech->content_contains('Test Test');
$mech->content_contains('FINAL UPDATE');
$email = $mech->get_email;
like $email->header('To'), qr/test\@example.com/, 'to line looks correct';
like $email->header('From'), qr/division\@example.org/, 'from line looks correct';
like $email->body, qr/FINAL UPDATE/, 'body looks correct';
$mech->clear_emails_ok;
# Assign planned (via confirmed), don't confirm email
@reports = $mech->create_problems_for_body( 1, $division->id, 'Second', {
state => 'unconfirmed',
confirmed => undef,
cobrand => 'zurich',
photo => $sample_photo,
});
$report = $reports[0];
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'confirmed' } } );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->submit_form_ok( { with_fields => { state => 'planned' } } );
$mech->get_ok( '/report/' . $report->id );
};
$mech->content_contains('In Bearbeitung');
$mech->content_contains('Second Test');
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->content_contains( 'Unbestätigt' );
$report->discard_changes;
$mech->form_with_fields( 'status_update' );
$mech->submit_form_ok( { button => 'publish_response', with_fields => { status_update => 'FINAL UPDATE' } } );
$mech->get_ok( '/report/' . $report->id );
};
$mech->content_contains('Beantwortet');
$mech->content_contains('Second Test');
$mech->content_contains('FINAL UPDATE');
$mech->email_count_is(0);
# Report assigned to third party
@reports = $mech->create_problems_for_body( 1, $division->id, 'Third', {
state => 'unconfirmed',
confirmed => undef,
cobrand => 'zurich',
photo => $sample_photo,
});
$report = $reports[0];
subtest "external report triggers email" => sub {
my $EXTERNAL_MESSAGE = 'Look Ma, no hands!';
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
# required to see body_external field
$report->state('planned');
$report->set_extra_metadata('closure_status' => 'closed');
# Set the public_response manually here because the default one will have line breaks that get escaped as HTML, causing the comparison to fail.
$report->set_extra_metadata('public_response' => 'Freundliche Gruesse Ihre Stadt Zuerich');
$report->update;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'publish_response' );
$mech->submit_form_ok( {
button => 'publish_response',
with_fields => {
body_external => $external_body->id,
external_message => $EXTERNAL_MESSAGE,
} });
$report->discard_changes;
$mech->get_ok( '/report/' . $report->id );
};
is ($report->state, 'closed', 'Report was closed correctly');
$mech->content_contains('Extern')
or die $mech->content;
$mech->content_contains('Third Test');
$mech->content_contains($report->get_extra_metadata('public_response')) or die $mech->content;
send_reports_for_zurich();
$email = $mech->get_email;
like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay';
like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct';
like $email->body, qr/External Body/, 'body has right name';
like $email->body, qr/$EXTERNAL_MESSAGE/, 'external_message was passed on';
unlike $email->body, qr/test\@example.com/, 'body does not contain email address';
$mech->clear_emails_ok;
subtest "Test third_personal boolean setting" => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin' );
# required to see body_external field
$report->state('planned');
$report->set_extra_metadata('closure_status' => 'closed');
$report->set_extra_metadata('public_response' => 'Freundliche Gruesse Ihre Stadt Zuerich');
$report->update;
is $mech->uri->path, '/admin', "am logged in";
$mech->content_contains( 'report_edit/' . $report->id );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'publish_response' );
$mech->submit_form_ok( {
button => 'publish_response',
with_fields => {
body_external => $external_body->id,
third_personal => 1,
} });
$mech->get_ok( '/report/' . $report->id );
};
$mech->content_contains('Extern');
$mech->content_contains('Third Test');
$mech->content_contains($report->get_extra_metadata('public_response'));
send_reports_for_zurich();
$email = $mech->get_email;
like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay';
like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct';
like $email->body, qr/External Body/, 'body has right name';
like $email->body, qr/test\@example.com/, 'body does contain email address';
$mech->clear_emails_ok;
};
subtest "Test external wish sending" => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
# set as wish
$report->discard_changes;
$report->state('planned');
$report->set_extra_metadata('closure_status' => 'investigating');
$report->update;
is ($report->state, 'planned', 'Sanity check') or die;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'publish_response' );
$mech->submit_form_ok( {
button => 'publish_response',
with_fields => {
body_external => $external_body->id,
external_message => $EXTERNAL_MESSAGE,
} });
};
send_reports_for_zurich();
$email = $mech->get_email;
like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay';
like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct';
like $email->body, qr/External Body/, 'body has right name';
like $email->body, qr/$EXTERNAL_MESSAGE/, 'external_message was passed on';
like $email->body, qr/test\@example.com/, 'body contains email address';
$mech->clear_emails_ok;
};
subtest "Closure email includes public response" => sub {
my $PUBLIC_RESPONSE = "This is the public response to your report. Freundliche Gruesse.";
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
# set as extern
reset_report_state($report);
$report->state('planned');
$report->set_extra_metadata('closure_status' => 'closed');
$report->set_extra_metadata('email_confirmed' => 1);
$report->unset_extra_metadata('public_response');
$report->update;
is ($report->state, 'planned', 'Sanity check') or die;
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'publish_response' );
$mech->submit_form_ok( {
button => 'publish_response',
with_fields => {
body_external => $external_body->id,
external_message => $EXTERNAL_MESSAGE,
status_update => $PUBLIC_RESPONSE,
} });
};
$email = $mech->get_email;
my $report_id = $report->id;
like $email->header('Subject'), qr/Meldung #$report_id/, 'subject looks okay';
like $email->header('To'), qr/test\@example.com/, 'to line looks correct';
like $email->body, qr/$PUBLIC_RESPONSE/, 'public_response was passed on' or die $email->body;
$mech->clear_emails_ok;
};
$report->comments->delete; # delete the comments, as they confuse later tests
};
subtest "superuser and dm can see stats" => sub {
$mech->log_out_ok;
$user = $mech->log_in_ok( 'super@example.org' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get( '/admin/stats' );
};
is $mech->res->code, 200, "superuser should be able to see stats page";
$mech->log_out_ok;
$user = $mech->log_in_ok( 'dm1@example.org' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get( '/admin/stats' );
};
is $mech->res->code, 200, "dm can now also see stats page";
$mech->log_out_ok;
};
subtest "only superuser can edit bodies" => sub {
$user = $mech->log_in_ok( 'dm1@example.org' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$mech->get( '/admin/body/' . $zurich->id );
};
is $mech->res->code, 404, "only superuser should be able to edit bodies";
$mech->log_out_ok;
};
subtest "only superuser can see 'Add body' form" => sub {
$user = $mech->log_in_ok( 'dm1@example.org' );
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAPIT_URL => 'http://global.mapit.mysociety.org/',
MAPIT_TYPES => [ 'O08' ],
MAPIT_ID_WHITELIST => [ 423017 ],
}, sub {
$mech->get_ok( '/admin/bodies' );
};
$mech->content_lacks( '<form method="post" action="bodies"' );
$mech->log_out_ok;
};
subtest "phone number is mandatory" => sub {
FixMyStreet::override_config {
MAPIT_TYPES => [ 'O08' ],
MAPIT_URL => 'http://global.mapit.mysociety.org/',
ALLOWED_COBRANDS => [ 'zurich' ],
MAPIT_ID_WHITELIST => [ 274456 ],
MAPIT_GENERATION => 2,
MAP_TYPE => 'Zurich,OSM',
}, sub {
$user = $mech->log_in_ok( 'dm1@example.org' );
$mech->get_ok( '/report/new?lat=47.381817&lon=8.529156' );
$mech->submit_form( with_fields => { phone => "" } );
$mech->content_contains( 'Diese Information wird benötigt' );
$mech->log_out_ok;
};
};
subtest "phone number is not mandatory for reports from mobile apps" => sub {
FixMyStreet::override_config {
MAPIT_TYPES => [ 'O08' ],
MAPIT_URL => 'http://global.mapit.mysociety.org/',
ALLOWED_COBRANDS => [ 'zurich' ],
MAPIT_ID_WHITELIST => [ 423017 ],
MAPIT_GENERATION => 4,
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->post_ok( '/report/new/mobile?lat=47.381817&lon=8.529156' , {
service => 'iPhone',
detail => 'Problem-Bericht',
lat => 47.381817,
lon => 8.529156,
email => 'user@example.org',
pc => '',
name => '',
category => 'bad category',
});
my $res = $mech->response;
ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json';
unlike $res->content, qr/Diese Information wird benötigt/, 'response should not contain phone error';
# Clear out the mailq
$mech->clear_emails_ok;
};
};
subtest "problems can't be assigned to deleted bodies" => sub {
$user = $mech->log_in_ok( 'dm1@example.org' );
$user->from_body( $zurich->id );
$user->update;
$report->state( 'confirmed' );
$report->update;
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAPIT_URL => 'http://global.mapit.mysociety.org/',
MAPIT_TYPES => [ 'O08' ],
MAPIT_ID_WHITELIST => [ 423017 ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->get_ok( '/admin/body/' . $external_body->id );
$mech->submit_form_ok( { with_fields => { deleted => 1 } } );
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->content_lacks( $external_body->name )
or do {
diag $mech->content;
diag $external_body->name;
die;
};
};
$user->from_body( $division->id );
$user->update;
$mech->log_out_ok;
};
subtest "photo must be supplied for categories that require it" => sub {
FixMyStreet::App->model('DB::Contact')->find_or_create({
body => $division,
category => "Graffiti - photo required",
email => "graffiti\@example.org",
confirmed => 1,
deleted => 0,
editor => "editor",
whenedited => DateTime->now(),
note => "note for graffiti",
extra => { photo_required => 1 }
});
FixMyStreet::override_config {
MAPIT_TYPES => [ 'O08' ],
MAPIT_URL => 'http://global.mapit.mysociety.org/',
ALLOWED_COBRANDS => [ 'zurich' ],
MAPIT_ID_WHITELIST => [ 274456 ],
MAPIT_GENERATION => 2,
MAP_TYPE => 'Zurich,OSM',
}, sub {
$mech->post_ok( '/report/new', {
detail => 'Problem-Bericht',
lat => 47.381817,
lon => 8.529156,
email => 'user@example.org',
pc => '',
name => '',
category => 'Graffiti - photo required',
photo => '',
submit_problem => 1,
});
is $mech->res->code, 200, "missing photo shouldn't return anything but 200";
$mech->content_contains(_("Photo is required."), 'response should contain photo error message');
};
};
subtest "test stats" => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
}, sub {
$user = $mech->log_in_ok( 'super@example.org' );
$mech->get_ok( '/admin/stats' );
is $mech->res->code, 200, "superuser should be able to see stats page";
$mech->content_contains('Innerhalb eines Arbeitstages moderiert: 3');
$mech->content_contains('Innerhalb von fünf Arbeitstagen abgeschlossen: 3');
# my @data = $mech->content =~ /(?:moderiert|abgeschlossen): \d+/g;
# diag Dumper(\@data); use Data::Dumper;
my $export_count = get_export_rows_count($mech);
if (defined $export_count) {
is $export_count - $EXISTING_REPORT_COUNT, 3, 'Correct number of reports';
$mech->content_contains('fixed - council');
}
};
};
subtest "test admin_log" => sub {
diag $report->id;
my @entries = FixMyStreet::DB->resultset('AdminLog')->search({
object_type => 'problem',
object_id => $report->id,
});
# XXX: following is dependent on all of test up till now, rewrite to explicitly
# test which things need to be logged!
is scalar @entries, 4, 'State changes logged';
is $entries[-1]->action, 'state change to closed', 'State change logged as expected';
};
subtest 'email images to external partners' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
reset_report_state($report);
my $photo = path(__FILE__)->parent->child('zurich-logo_portal.x.jpg')->slurp_raw;
my $photoset = FixMyStreet::App::Model::PhotoSet->new({
data_items => [ $photo ],
});
my $fileid = $photoset->data;
$report->set_extra_metadata('publish_photo' => 1);
# The below email comparison must not have an external message.
$report->unset_extra_metadata('external_message');
$report->update({
state => 'closed',
photo => $fileid,
external_body => $external_body->id,
});
$mech->clear_emails_ok;
send_reports_for_zurich();
my @emails = $mech->get_email;
my $email_as_string = $mech->get_first_email(@emails);
my ($boundary) = $email_as_string =~ /boundary="([A-Za-z0-9.]*)"/ms;
my $changes = $email_as_string =~ s{$boundary}{}g;
is $changes, 4, '4 boundaries'; # header + 3 around the 2x parts (text + 1 image)
my $expected_email_content = path(__FILE__)->parent->child('zurich_attachments.txt')->slurp;
my $REPORT_ID = $report->id;
$expected_email_content =~ s{REPORT_ID}{$REPORT_ID}g;
is_string $email_as_string, $expected_email_content, 'MIME email text ok'
or do {
(my $test_name = $0) =~ s{/}{_}g;
my $path = path("test-output-$test_name.tmp");
$path->spew($email_as_string);
diag "Saved output in $path";
};
};
};
subtest 'Status update shown as appropriate' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
# ALL closed states must hide the public_response edit, and public ones
# must show the answer in blue.
for (['planned', 1, 0, 0],
['fixed - council', 0, 1, 0],
['closed', 0, 1, 0],
['hidden', 0, 0, 1])
{
my ($state, $update, $public, $user_response) = @$_;
$report->update({ state => $state });
$mech->get_ok( '/admin/report_edit/' . $report->id );
contains_or_lacks($mech, $update, "<textarea name='status_update'");
contains_or_lacks($mech, $public || $user_response, '<div class="admin-official-answer">');
if ($public) {
$mech->get_ok( '/report/' . $report->id );
$mech->content_contains('Antwort</h4>');
}
}
};
};
# TODO refactor into FixMyStreet::TestMech;
sub contains_or_lacks {
my ($mech, $contains, $text) = @_;
if ($contains) {
$mech->content_contains($text);
}
else {
$mech->content_lacks($text);
}
}
subtest 'time_spent' => sub {
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'zurich' ],
MAP_TYPE => 'Zurich,OSM',
}, sub {
my $report = $reports[0];
is $report->get_time_spent, 0, '0 minutes spent';
$report->update({ state => 'in progress' });
$mech->get_ok( '/admin/report_edit/' . $report->id );
$mech->form_with_fields( 'time_spent' );
$mech->submit_form_ok( {
with_fields => {
time_spent => 10,
} });
is $report->get_time_spent, 10, '10 minutes spent';
};
};
$mech->log_out_ok;
END {
$mech->delete_body($subdivision);
$mech->delete_body($division);
$mech->delete_body($zurich);
$mech->delete_body($external_body);
$mech->delete_user( 'dm1@example.org' );
$mech->delete_user( 'sdm1@example.org' );
ok $mech->host("www.fixmystreet.com"), "change host back";
done_testing();
}
1;
|