aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/report_new.t
blob: 133dc804791fef87e6b993b4320b28f63b272762 (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
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
use Test::MockModule;
use FixMyStreet::TestMech;
use FixMyStreet::App;
use Web::Scraper;
use Path::Class;

# disable info logs for this test run
FixMyStreet::App->log->disable('info');
END { FixMyStreet::App->log->enable('info'); }

my $mech = FixMyStreet::TestMech->new;
$mech->get_ok('/report/new');

my $sample_file = file(__FILE__)->parent->file("sample.jpg")->stringify;
ok -e $sample_file, "sample file $sample_file exists";

subtest "test that bare requests to /report/new get redirected" => sub {

    $mech->get_ok('/report/new');
    is $mech->uri->path, '/', "went to /";
    is_deeply { $mech->uri->query_form }, {}, "query empty";

    FixMyStreet::override_config {
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->get_ok('/report/new?pc=SW1A%201AA');
    };
    is $mech->uri->path, '/around', "went to /around";
    is_deeply { $mech->uri->query_form }, { pc => 'SW1A 1AA' },
      "pc correctly transferred";
};

my %body_ids;
my @bodies;
for my $body (
    { area_id => 2651, name => 'City of Edinburgh Council' },
    { area_id => 2226, name => 'Gloucestershire County Council' },
    { area_id => 2326, name => 'Cheltenham Borough Council' },
    { area_id => 2504, name => 'Westminster City Council' },
    { area_id => 2482, name => 'Bromley Council' },
    { area_id => 2227, name => 'Hampshire County Council' },
    { area_id => 2333, name => 'Hart Council' },
    { area_id => 2535, name => 'Sandwell Borough Council' },
    { area_id => 1000, name => 'Highways England' },
    { area_id => 2217, name => 'Buckinghamshire County Council' },
    { area_id => 2232, name => 'Lincolnshire County Council' },
    { area_id => 2237, name => 'Oxfordshire County Council' },
    { area_id => 2600, name => 'Rutland County Council' },
) {
    my $body_obj = $mech->create_body_ok($body->{area_id}, $body->{name});
    push @bodies, $body_obj;
    $body_ids{$body->{area_id}} = $body_obj->id;
}

# Let's make some contacts to send things to!
my $contact1 = $mech->create_contact_ok(
    body_id => $body_ids{2651}, # Edinburgh
    category => 'Street lighting',
    email => 'highways@example.com',
);
my $contact2 = $mech->create_contact_ok(
    body_id => $body_ids{2226}, # Gloucestershire
    category => 'Potholes',
    email => 'potholes@example.com',
);
my $contact3 = $mech->create_contact_ok(
    body_id => $body_ids{2326}, # Cheltenham
    category => 'Trees',
    email => 'trees@example.com',
);
my $contact4 = $mech->create_contact_ok(
    body_id => $body_ids{2482}, # Bromley
    category => 'Trees',
    email => 'trees@example.com',
);
my $contact5 = $mech->create_contact_ok(
    body_id => $body_ids{2651}, # Edinburgh
    category => 'Trees',
    email => 'trees@example.com',
);
my $contact6 = $mech->create_contact_ok(
    body_id => $body_ids{2333}, # Hart
    category => 'Trees',
    email => 'trees@example.com',
);
my $contact7 = $mech->create_contact_ok(
    body_id => $body_ids{2227}, # Hampshire
    category => 'Street  lighting',
    email => 'highways@example.com',
);
my $contact8 = $mech->create_contact_ok(
    body_id => $body_ids{2504},
    category => 'Street lighting',
    email => 'highways@example.com'
);
my $contact9 = $mech->create_contact_ok(
    body_id => $body_ids{2226}, # Gloucestershire
    category => 'Street lighting',
    email => 'streetlights-2226@example.com',
);
my $contact10 = $mech->create_contact_ok(
    body_id => $body_ids{2326}, # Cheltenham
    category => 'Street lighting',
    email => 'streetlights-2326@example.com',
);
my $contact11 = $mech->create_contact_ok(
    body_id => $body_ids{1000}, # Highways
    category => 'Pothole',
    email => 'pothole-1000@example.com',
);
my $contact12 = $mech->create_contact_ok(
    body_id => $body_ids{2217}, # Buckinghamshire
    category => 'Street lighting',
    email => 'streetlights-2217@example.com',
);
my $contact13 = $mech->create_contact_ok(
    body_id => $body_ids{2232}, # Lincolnshire
    category => 'Trees',
    email => 'trees-2232@example.com',
);
my $contact14 = $mech->create_contact_ok(
    body_id => $body_ids{2237}, # Oxfordshire
    category => 'Trees',
    email => 'trees-2247@example.com',
);
my $contact15 = $mech->create_contact_ok(
    body_id => $body_ids{2600}, # Rutland
    category => 'Trees',
    email => 'trees-2600@example.com',
);

# test that the various bit of form get filled in and errors correctly
# generated.
foreach my $test (
    {
        msg    => 'all fields empty',
        pc     => 'OX1 3DH',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            # No category error, as no categories for Oxon at all, so is skipped
            'Please enter your email',
            'Please enter your name',
        ],
    },
    {
        msg    => 'all fields empty, bad category',
        pc     => 'GL50 2PR',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Something bad',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            category => '-- Pick a category --',
        },
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            'Please choose a category',
            'Please enter your email',
            'Please enter your name',
        ],
    },
    {
        msg    => 'all fields empty except category',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
            'Please enter your name',
        ],
    },
    {
        msg    => 'may_show_name is remembered',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '',
            may_show_name => undef,
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
            'Please enter your name',
        ],
    },
    {
        msg    => 'may_show_name unchanged if name is present (stays false)',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => undef,
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
        ],
    },
    {
        msg    => 'may_show_name unchanged if name is present (stays true)',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter a subject',
            'Please enter some details',
            'Please enter your email',
        ],
    },
    {
        msg    => 'title and details tidied up',
        pc     => 'SW1A 1AA',
        fields => {
            title         => "DOG SHIT\r\nON WALLS",
            detail        => "on this portakabin -\r\n\r\nmore of a portaloo HEH!!",
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            title => 'Dog poo on walls',
            detail =>
              "On this [portable cabin] -\n\nMore of a [portable loo] HEH!!",
        },
        errors => [ 'Please enter your email', ],
    },
    {
        msg    => 'name too short',
        pc     => 'SW1A 1AA',
        fields => {
            title         => 'Test title',
            detail        => 'Test detail',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'DUDE',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter your email',
'Please enter your full name, councils need this information – if you do not wish your name to be shown on the site, untick the box below',
        ],
    },
    {
        msg    => 'name is anonymous',
        pc     => 'SW1A 1AA',
        fields => {
            title         => 'Test title',
            detail        => 'Test detail',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'anonymous',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {},
        errors  => [
            'Please enter your email',
'Please enter your full name, councils need this information – if you do not wish your name to be shown on the site, untick the box below',
        ],
    },
    {
        msg    => 'email invalid',
        pc     => 'SW1A 1AA',
        fields => {
            title         => 'Test title',
            detail        => 'Test detail',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Joe Smith',
            may_show_name => '1',
            username      => 'not an email',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { username => 'notanemail', email => 'notanemail' },
        errors  => [ 'Please enter a valid email', ],
    },
    {
        msg    => 'cleanup title and detail',
        pc     => 'SW1A 1AA',
        fields => {
            title         => "   Test   title   ",
            detail        => "   first line   \n\n second\nline\n\n   ",
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '',
            may_show_name => '1',
            username      => '',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            title  => 'Test title',
            detail => "First line\n\nSecond line",
        },
        errors => [
            'Please enter your email',
            'Please enter your name',
        ],
    },
    {
        msg    => 'clean up name and email',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => '  Bob    Jones   ',
            may_show_name => '1',
            username      => '   BOB @ExAmplE.COM   ',
            email         => '',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            name  => 'Bob Jones',
            username => 'bob@example.com',
            email => 'bob@example.com',
        },
        errors => [ 'Please enter a subject', 'Please enter some details', ],
    },
    {
        msg    => 'non-photo upload gives error',
        pc     => 'SW1A 1AA',
        fields => {
            title         => 'Title',
            detail        => 'Detail',
            photo1        => [ [ undef, 'bad.txt', Content => 'This is not a JPEG', Content_Type => 'text/plain' ], 1 ],
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            photo1 => '',
        },
        errors => [ "Please upload an image only" ],
    },
    {
        msg    => 'bad photo upload gives error',
        pc     => 'SW1A 1AA',
        fields => {
            title         => 'Title',
            detail        => 'Detail',
            photo1        => [ [ undef, 'fake.jpeg', Content => 'This is not a JPEG', Content_Type => 'image/jpeg' ], 1 ],
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            photo1 => '',
        },
        errors => [ "That image doesn't appear to have uploaded correctly (Please upload an image only ), please try again." ],
    },
    {
        msg    => 'photo with octet-stream gets through okay',
        pc     => 'SW1A 1AA',
        fields => {
            title         => '',
            detail        => 'Detail',
            photo1        => [ [ $sample_file, undef, Content_Type => 'application/octet-stream' ], 1 ],
            photo2        => '',
            photo3        => '',
            name          => 'Bob Jones',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => {
            photo1 => '',
        },
        errors => [ "Please enter a subject" ],
    },
    {
        msg    => 'Bromley long detail',
        pc     => 'BR1 3UH',
        fields => {
            fms_extra_title => 'MR',
            title         => '',
            detail        => 'X' . 'x' x 1751,
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Example',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Trees',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { },
        errors => [ 'Please enter a subject', 'Reports are limited to 1750 characters in length. Please shorten your report' ],
    },
    {
        msg    => 'Oxfordshire long detail',
        pc     => 'OX20 1SZ',
        fields => {
            title         => '',
            detail        => 'X' . 'x' x 1701,
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Example',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Trees',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { },
        errors => [ 'Please enter a subject', 'Reports are limited to 1700 characters in length. Please shorten your report' ],
    },
    {
        msg    => 'Lincolnshire long phone',
        pc     => 'PE9 2GX',
        fields => {
            title         => '',
            detail        => 'Detail',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'Bob Example',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '123456789 12345678910',
            category      => 'Trees',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { },
        errors => [ 'Please enter a subject', 'Phone numbers are limited to 20 characters in length.' ],
    },
    {
        msg    => 'Buckinghamshire long name',
        pc     => 'RG9 6TL',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'This is a very long name that should fail validation',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Street lighting',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { },
        errors => [ 'Please enter a subject', 'Please enter some details', 'Names are limited to 50 characters in length.' ],
    },
    {
        msg    => 'Rutland long name',
        pc     => 'LE15 0GJ',
        fields => {
            title         => '',
            detail        => '',
            photo1        => '',
            photo2        => '',
            photo3        => '',
            name          => 'This is a very long name that should fail validation',
            may_show_name => '1',
            username      => 'bob@example.com',
            email         => 'bob@example.com',
            phone         => '',
            category      => 'Trees',
            password_sign_in => '',
            password_register => '',
            remember_me => undef,
        },
        changes => { },
        errors => [ 'Please enter a subject', 'Please enter some details', 'Names are limited to 40 characters in length.' ],
    },
  )
{
    subtest "check form errors where $test->{msg}" => sub {
        $mech->get_ok('/around');

        # submit initial pc form
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' }, 'bromley', 'oxfordshire', 'rutland', 'lincolnshire', 'buckinghamshire' ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } },
                "submit location" );
            is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'";

            # click through to the report page
            $mech->follow_link_ok( { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link" );

            # submit the main form
            $mech->submit_form_ok( { with_fields => $test->{fields} },
                "submit form" );
        };

        # check that we got the errors expected
        is_deeply [ sort @{$mech->page_errors} ], [ sort @{$test->{errors}} ], "check errors";

        # check that fields have changed as expected
        my $new_values = {
            %{ $test->{fields} },     # values added to form
            %{ $test->{changes} },    # changes we expect
        };
        is_deeply $mech->visible_form_values, $new_values,
          "values correctly changed";
    };
}

my $first_user;
foreach my $test (
    {
        desc => 'does not have an account, does not set a password',
        user => 0, password => 0,
    },
    {
        desc => 'does not have an account, sets a password',
        user => 0, password => 1,
    },
    {
        desc => 'does have an account and is not signed in; does not sign in, does not set a password',
        user => 1, password => 0,
    },
    {
        desc => 'does have an account and is not signed in; does not sign in, sets a password',
        user => 1, password => 1,
    },
) {
  subtest "test report creation for a user who " . $test->{desc} => sub {
    $mech->log_out_ok;
    $mech->clear_emails_ok;

    # check that the user does not exist
    my $test_email = 'test-1@example.com';
    if ($test->{user}) {
        my $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
        ok $user, "test user does exist";
        $user->problems->delete;
        $user->name( 'Old Name' );
        $user->password( 'old_password' );
        $user->update;
    } elsif (!$first_user) {
        ok !FixMyStreet::App->model('DB::User')->find( { email => $test_email } ),
          "test user does not exist";
        $first_user = 1;
    } else {
        # Not first pass, so will exist, but want no user to start, so delete it.
        $mech->delete_user($test_email);
    }

    # submit initial pc form
    $mech->get_ok('/around');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } },
            "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',
                    may_show_name => '1',
                    username      => 'test-1@example.com',
                    phone         => '07903 123 456',
                    category      => 'Street lighting',
                    password_register => $test->{password} ? 'secret' : '',
                }
            },
            "submit good details"
        );
    };

    # check that we got the errors expected
    is_deeply $mech->page_errors, [], "check there were no errors";

    # check that the user has been created/ not changed
    my $user =
      FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
    ok $user, "user found";
    if ($test->{user}) {
        is $user->name, 'Old Name', 'name unchanged';
        ok $user->check_password('old_password'), 'password unchanged';
    } else {
        is $user->name, undef, 'name not yet set';
        is $user->password, '', 'password not yet set for new user';
    }

    # find the report
    my $report = $user->problems->first;
    ok $report, "Found the report";

    # check that the report is not available yet.
    is $report->state, 'unconfirmed', "report not confirmed";
    is $mech->get( '/report/' . $report->id )->code, 404, "report not found";

    # Check the report has been assigned appropriately
    is $report->bodies_str, $body_ids{2651};

    # receive token
    my $email = $mech->get_email;
    ok $email, "got an email";
    like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem";

    my $url = $mech->get_link_from_email($email);

    # confirm token
    $mech->get_ok($url);
    $report->discard_changes;
    is $report->state, 'confirmed', "Report is now confirmed";

    $mech->get_ok( '/report/' . $report->id );

    is $report->name, 'Joe Bloggs', 'name updated correctly';
    if ($test->{password}) {
        ok $report->user->check_password('secret'), 'password updated correctly';
    } elsif ($test->{user}) {
        ok $report->user->check_password('old_password'), 'password unchanged, as no new one given';
    } else {
        is $report->user->password, '', 'password still not set, as none given';
    }

    # check that the reporter has an alert
    my $alert = FixMyStreet::App->model('DB::Alert')->find( {
        user       => $report->user,
        alert_type => 'new_updates',
        parameter  => $report->id,
    } );
    ok $alert, "created new alert";

    # user is created and logged in
    $mech->logged_in_ok;

    # cleanup
    $mech->delete_user($user)
        if $test->{user} && $test->{password};
  };
}

# this test to make sure that we don't see spurious error messages about
# the name being blank when there is a sign in error
subtest "test password errors for a user who is signing in as they report" => sub {
    $mech->log_out_ok;
    $mech->clear_emails_ok;

    # check that the user does not exist
    my $test_email = 'test-2@example.com';

    my $user = $mech->create_user_ok($test_email);

    # setup the user.
    ok $user->update( {
        name     => 'Joe Bloggs',
        phone    => '01234 567 890',
        password => 'secret2',
        phone_verified => 1,
    } ), "set user details";

    # submit initial pc form
    $mech->get_ok('/around');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } },
            "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_sign_in',
                with_fields => {
                    title         => 'Test Report',
                    detail        => 'Test report details.',
                    photo1        => '',
                    username      => 'test-2@example.com',
                    password_sign_in => 'secret1',
                    category      => 'Street lighting',
                }
            },
            "submit with wrong password"
        );
    };

    # check that we got the errors expected
    is_deeply $mech->page_errors, [
        "There was a problem with your login information. If you cannot remember your password, or do not have one, please fill in the \x{2018}No\x{2019} section of the form.",
    ], "check there were errors";

    $mech->content_lacks('1234', 'phone number not shown');
};

foreach my $test (
  { two_factor => 0, desc => '', },
  { two_factor => 1, desc => ' with two-factor', },
) {
  subtest "test report creation for a user who is signing in as they report$test->{desc}" => sub {
    $mech->log_out_ok;
    $mech->cookie_jar({});
    $mech->clear_emails_ok;

    # check that the user does not exist
    my $test_email = 'test-2@example.com';

    my $user = $mech->create_user_ok($test_email);

    # setup the user.
    ok $user->update( {
        name     => 'Joe Bloggs',
        phone    => '01234 567 890',
        password => 'secret2',
    } ), "set user details";

    my $auth;
    if ($test->{two_factor}) {
        use Auth::GoogleAuth;
        $auth = Auth::GoogleAuth->new;
        $user->is_superuser(1);
        $user->set_extra_metadata('2fa_secret', $auth->generate_secret32);
        $user->update;
    }

    # submit initial pc form
    $mech->get_ok('/around');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } },
            "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_sign_in',
                with_fields => {
                    title         => 'Test Report',
                    detail        => 'Test report details.',
                    photo1        => '',
                    username      => $test_email,
                    password_sign_in => 'secret2',
                    category      => 'Street lighting',
                }
            },
            "submit good details"
        );

        if ($test->{two_factor}) {
            my $code = $auth->code;
            my $wrong_code = $auth->code(undef, time() - 120);
            $mech->content_contains('Please generate a two-factor code');
            $mech->submit_form_ok({ with_fields => { '2fa_code' => $wrong_code } }, "provide wrong 2FA code" );
            $mech->content_contains('Try again');
            $mech->submit_form_ok({ with_fields => { '2fa_code' => $code } }, "provide correct 2FA code" );
        }

        # check that we got the message expected
        $mech->content_contains( 'You have successfully signed in; please check and confirm your details are accurate:' );

        # Now submit with a name
        $mech->submit_form_ok(
            {
                with_fields => {
                    name => 'Joe Bloggs',
                }
            },
            "submit good details"
        );
    };

    # find the report
    my $report = $user->problems->first;
    ok $report, "Found the report";

    if (!$test->{two_factor}) {
        # The superuser account will be immediately redirected
        $mech->content_contains('Thank you for reporting this issue');
    }

    # Check the report has been assigned appropriately
    is $report->bodies_str, $body_ids{2651};

    # check that no emails have been sent
    $mech->email_count_is(0);

    # check report is confirmed and available
    is $report->state, 'confirmed', "report is now confirmed";
    $mech->get_ok( '/report/' . $report->id );

    # check that the reporter has an alert
    my $alert = FixMyStreet::App->model('DB::Alert')->find( {
        user       => $report->user,
        alert_type => 'new_updates',
        parameter  => $report->id,
    } );
    ok $alert, "created new alert";

    # user is created and logged in
    $mech->logged_in_ok;

    # cleanup
    $mech->delete_user($user)
  };
}

#### test report creation for user with account and logged in
my ($saved_lat, $saved_lon);
foreach my $test (
    { category => 'Trees', council => 2326 },
    { category => 'Potholes', council => 2226 },
) {
    subtest "test report creation for a user who is logged in" => sub {

        # check that the user does not exist
        my $test_email = 'test-2@example.com';

        $mech->clear_emails_ok;
        my $user = $mech->log_in_ok($test_email);

        # setup the user.
        ok $user->update(
            {
                name  => 'Test User',
                phone => '01234 567 890',
            }
          ),
          "set users details";

        # submit initial pc form
        $mech->get_ok('/around');
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } },
                "submit location" );

            # click through to the report page
            $mech->follow_link_ok( { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link" );

            # check that the fields are correctly prefilled
            is_deeply(
                $mech->visible_form_values,
                {
                    title         => '',
                    detail        => '',
                    may_show_name => '1',
                    name          => 'Test User',
                    phone         => '01234 567 890',
                    photo1        => '',
                    photo2        => '',
                    photo3        => '',
                    category      => '-- Pick a category --',
                },
                "user's details prefilled"
            );

            $mech->submit_form_ok(
                {
                    with_fields => {
                        title         => "Test Report at café",
                        detail        => 'Test report details.',
                        photo1        => '',
                        name          => 'Joe Bloggs',
                        may_show_name => '1',
                        phone         => '07903 123 456',
                        category      => $test->{category},
                    }
                },
                "submit good details"
            );
        };

        # find the report
        my $report = $user->problems->first;
        ok $report, "Found the report";

        # Check the report has been assigned appropriately
        is $report->bodies_str, $body_ids{$test->{council}};

        $mech->content_contains('Thank you for reporting this issue');

        # check that no emails have been sent
        $mech->email_count_is(0);

        # check report is confirmed and available
        is $report->state, 'confirmed', "report is now confirmed";
        $mech->get_ok( '/report/' . $report->id );

        # check that the reporter has an alert
        my $alert = FixMyStreet::App->model('DB::Alert')->find( {
            user       => $report->user,
            alert_type => 'new_updates',
            parameter  => $report->id,
        } );
        ok $alert, "created new alert";

        # user is still logged in
        $mech->logged_in_ok;

        # Test that AJAX pages return the right data
        $mech->get_ok(
            '/around?ajax=1&bbox=' . ($report->longitude - 0.01) . ',' .  ($report->latitude - 0.01)
            . ',' . ($report->longitude + 0.01) . ',' .  ($report->latitude + 0.01)
        );
        $mech->content_contains( "Test Report at caf\xc3\xa9" );
        $saved_lat = $report->latitude;
        $saved_lon = $report->longitude;

        # cleanup
        $mech->delete_user($user);
    };

}

# XXX add test for category with multiple bodies
foreach my $test (
    {
        desc => "test report creation for multiple bodies",
        category => 'Street lighting',
        councils => [ 2226, 2326 ],
        extra_fields => {},
        email_count => 2,
    },
    {
        desc => "test single_body_only means only one report body",
        category => 'Street lighting',
        councils => [ 2326 ],
        extra_fields => { single_body_only => 'Cheltenham Borough Council' },
        email_count => 1,
    },
    {
        desc => "test invalid single_body_only means no report bodies",
        category => 'Street lighting',
        councils => [],
        extra_fields => { single_body_only => 'Invalid council' },
        email_count => 1,
    },
    {
        desc => "test do_not_send means body is ignored",
        category => 'Street lighting',
        councils => [ 2326 ],
        extra_fields => { do_not_send => 'Gloucestershire County Council' },
        email_count => 1,
    },
    {
        desc => "test single_body_only with Highways England",
        category => 'Street lighting',
        councils => [ 1000 ],
        extra_fields => { single_body_only => 'Highways England' },
        email_count => 1,
    },
) {
    subtest $test->{desc} => sub {

        # check that the user does not exist
        my $test_email = 'test-2@example.com';

        $mech->clear_emails_ok;
        my $user = $mech->log_in_ok($test_email);

        # setup the user.
        ok $user->update(
            {
                name  => 'Test User',
                phone => '01234 567 890',
            }
          ),
          "set users details";

        # submit initial pc form
        $mech->get_ok('/around');
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } },
                "submit location" );

            # click through to the report page
            $mech->follow_link_ok( { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link" );

            # check that the fields are correctly prefilled
            is_deeply(
                $mech->visible_form_values,
                {
                    title         => '',
                    detail        => '',
                    may_show_name => '1',
                    name          => 'Test User',
                    phone         => '01234 567 890',
                    photo1        => '',
                    photo2        => '',
                    photo3        => '',
                    category      => '-- Pick a category --',
                },
                "user's details prefilled"
            );

            $mech->submit_form_ok(
                {
                    with_fields => {
                        title         => "Test Report at café",
                        detail        => 'Test report details.',
                        photo1        => '',
                        name          => 'Joe Bloggs',
                        may_show_name => '1',
                        phone         => '07903 123 456',
                        category      => $test->{category},
                        %{$test->{extra_fields}}
                    }
                },
                "submit good details"
            );
        };

        # find the report
        my $report = $user->problems->first;
        ok $report, "Found the report";

        # Check the report has been assigned appropriately
        is $report->bodies_str, join(',', @body_ids{@{$test->{councils}}}) || undef;

        $mech->content_contains('Thank you for reporting this issue');

        # check that no emails have been sent
        $mech->email_count_is(0);

        # check report is confirmed and available
        is $report->state, 'confirmed', "report is now confirmed";
        $mech->get_ok( '/report/' . $report->id );

        # Test that AJAX pages return the right data
        $mech->get_ok(
            '/around?ajax=1&bbox=' . ($report->longitude - 0.01) . ',' .  ($report->latitude - 0.01)
            . ',' . ($report->longitude + 0.01) . ',' .  ($report->latitude + 0.01)
        );
        $mech->content_contains( "Test Report at caf\xc3\xa9" );
        $saved_lat = $report->latitude;
        $saved_lon = $report->longitude;

        # cleanup
        $mech->delete_user($user);
    };

}

subtest "Test inactive categories" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        BASE_URL => 'https://www.fixmystreet.com',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        # Around and New report have both categories
        $mech->get_ok('/around?pc=GL50+2PR');
        $mech->content_contains('Potholes');
        $mech->content_contains('Trees');
        $mech->get_ok("/report/new?lat=$saved_lat&lon=$saved_lon");
        $mech->content_contains('Potholes');
        $mech->content_contains('Trees');
        $contact2->update( { state => 'inactive' } ); # Potholes
        # But when Potholes is inactive, it's not on New report
        $mech->get_ok('/around?pc=GL50+2PR');
        $mech->content_contains('Potholes');
        $mech->content_contains('Trees');
        $mech->get_ok("/report/new?lat=$saved_lat&lon=$saved_lon");
        $mech->content_lacks('Potholes');
        $mech->content_contains('Trees');
        # Change back
        $contact2->update( { state => 'confirmed' } );
    };
};

subtest "category groups" => sub {
    my $cobrand = Test::MockModule->new('FixMyStreet::Cobrand::FixMyStreet');
    $cobrand->mock('enable_category_groups', sub { 1 });
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => 'fixmystreet',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $contact2->update( { extra => { group => 'Roads' } } );
        $contact9->update( { extra => { group => 'Roads' } } );
        $contact10->update( { extra => { group => 'Roads' } } );
        $mech->get_ok("/report/new?lat=$saved_lat&lon=$saved_lon");
        $mech->content_like(qr{<optgroup label="Roads">\s*<option value='Potholes'>Potholes</option>\s*<option value='Street lighting'>Street lighting</option></optgroup>});
    };
};

subtest "test report creation for a category that is non public" => sub {
    $mech->log_out_ok;
    $mech->clear_emails_ok;

    # check that the user does not exist
    my $test_email = 'test-2@example.com';

    my $user = $mech->create_user_ok($test_email);

    $contact1->update( { non_public => 1 } );

    # submit initial pc form
    $mech->get_ok('/around');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } },
            "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        => '',
                    username      => 'test-2@example.com',
                    name          => 'Joe Bloggs',
                    category      => 'Street lighting',
                }
            },
            "submit good details"
        );
    };

    # find the report
    my $report = $user->problems->first;
    ok $report, "Found the report";

    # Check the report is not public
    ok $report->non_public, 'report is not public';

    my $email = $mech->get_email;
    ok $email, "got an email";
    like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem";

    my $url = $mech->get_link_from_email($email);

    # confirm token
    $mech->get_ok($url);
    $report->discard_changes;

    is $report->state, 'confirmed', "Report is now confirmed";

    $mech->logged_in_ok;
    $mech->get_ok( '/report/' . $report->id, 'user can see own report' );

    $mech->log_out_ok;
    ok $mech->get("/report/" . $report->id), "fetched report";
    is $mech->res->code, 403, "access denied to report";

    # cleanup
    $mech->delete_user($user);
    $contact1->update( { non_public => 0 } );
};

$contact2->category( "Pothol\xc3\xa9s" );
$contact2->update;

subtest "check map click ajax response" => sub {
    my $extra_details;
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => 'fixmystreet',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=' . $saved_lat . '&longitude=' . $saved_lon );
    };
    # this order seems to be random so check individually/sort
    like $extra_details->{councils_text}, qr/Cheltenham Borough Council/, 'correct council text for two tier';
    like $extra_details->{councils_text}, qr/Gloucestershire County Council/, 'correct council text for two tier';
    like $extra_details->{category}, qr/Pothol\x{00E9}s.*Street lighting/, 'category looks correct for two tier council';
    my @sorted_bodies = sort @{ $extra_details->{bodies} };
    is_deeply \@sorted_bodies, [ "Cheltenham Borough Council", "Gloucestershire County Council" ], 'correct bodies for two tier';
    ok !$extra_details->{titles_list}, 'Non Bromley does not send back list of titles';

    FixMyStreet::override_config {
        ALLOWED_COBRANDS => 'fixmystreet',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=51.4021&longitude=0.01578');
    };
    ok $extra_details->{titles_list}, 'Bromley sends back list of titles';
    like $extra_details->{councils_text}, qr/Bromley Council/, 'correct council text';
    like $extra_details->{councils_text_private}, qr/^These will be sent to the council, but will never be shown online/, 'correct private council text';
    like $extra_details->{category}, qr/Trees/, 'category looks correct';
    is_deeply $extra_details->{bodies}, [ "Bromley Council" ], 'correct bodies';
    ok !$extra_details->{contribute_as}, 'no contribute as section';
    ok !$extra_details->{top_message}, 'no top message';
    ok $extra_details->{extra_name_info}, 'extra name info';

    FixMyStreet::override_config {
        ALLOWED_COBRANDS => 'fixmystreet',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=52.563074&longitude=-1.991032' );
    };
    like $extra_details->{councils_text}, qr/^These will be published online for others to see/, 'correct council text for council with no contacts';
    is $extra_details->{category}, '', 'category is empty for council with no contacts';
    is_deeply $extra_details->{bodies}, [ "Sandwell Borough Council" ], 'correct bodies for council with no contacts';
    ok !$extra_details->{extra_name_info}, 'no extra name info';
};

#### test uploading an image

#### test completing a partial report (eq flickr upload)

#### possibly manual testing
# create report without using map
# create report by clicking on map with javascript off
# create report with images off

subtest "check that a lat/lon off coast leads to /around" => sub {
    my $off_coast_latitude  = 50.78301;
    my $off_coast_longitude = -0.646929;

    FixMyStreet::override_config {
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->get_ok(    #
            "/report/new"
              . "?latitude=$off_coast_latitude"
              . "&longitude=$off_coast_longitude"
        );
    };

    is $mech->uri->path, '/around', "redirected to '/around'";

    is_deeply         #
      $mech->page_errors,
      [ 'That location does not appear to be covered by a council; perhaps it is offshore or outside the country. Please try again.' ],
      "Found location error";

};

subtest "check we load a partial report correctly" => sub {
    my $user = FixMyStreet::App->model('DB::User')->find_or_create(
        {
            email => 'test-partial@example.com'
        }
    );

    my $report = FixMyStreet::App->model('DB::Problem')->create( {
        name               => '',
        postcode           => '',
        category           => 'Street lighting',
        title              => 'Testing',
        detail             => "Testing Detail",
        anonymous          => 0,
        state              => 'partial',
        lang               => 'en-gb',
        service            => '',
        areas              => '',
        used_map           => 1,
        latitude           => '51.754926',
        longitude          => '-1.256179',
        user_id            => $user->id,
    } );

    my $report_id = $report->id;

    my $token = FixMyStreet::App->model("DB::Token")
        ->create( { scope => 'partial', data => $report->id } );

    my $token_code = $token->token;

    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    },
    sub {
        $mech->get("/L/$token_code");
        is $mech->res->previous->code, 302, 'partial token page redirects';
        is $mech->uri->path, "/report/new", "partial redirects to report page";
        $mech->content_contains('Testing Detail');
    };

    $mech->delete_user($user);
};

for my $test (
    {
        desc  => 'user title not set if not bromley problem',
        host  => 'www.fixmystreet.com',
        postcode => 'EH1 1BB',
        fms_extra_title => '',
        extra => [],
        user_title => undef,
    },
    {
        desc  => 'title shown for bromley problem on main site',
        host  => 'www.fixmystreet.com',
        postcode => 'BR1 3UH',
        fms_extra_title => 'MR',
        extra => [
            {
                name        => 'fms_extra_title',
                value       => 'MR',
                description => 'FMS_EXTRA_TITLE',
            },
        ],
        user_title => 'MR',
    },
    {
        desc =>
          'title, first and last name shown for bromley problem on cobrand',
        host       => 'bromley.fixmystreet.com',
        postcode => 'BR1 3UH',
        first_name => 'Test',
        last_name  => 'User',
        fms_extra_title => 'MR',
        extra      => [
            {
                name        => 'fms_extra_title',
                value       => 'MR',
                description => 'FMS_EXTRA_TITLE',
            },
            {
                name        => 'first_name',
                value       => 'Test',
                description => 'FIRST_NAME',
            },
            {
                name        => 'last_name',
                value       => 'User',
                description => 'LAST_NAME',
            },
        ],
        user_title => 'MR',
    },
  )
{
    subtest $test->{desc} => sub {
        my $override = {
            ALLOWED_COBRANDS => [ $test->{host} =~ /bromley/ ? 'bromley' : 'fixmystreet' ],
            MAPIT_URL => 'http://mapit.uk/',
        };

        $mech->host( $test->{host} );

        $mech->log_out_ok;
        $mech->clear_emails_ok;

        $mech->get_ok('/');
        FixMyStreet::override_config $override, sub {
            $mech->submit_form_ok( { with_fields => { pc => $test->{postcode}, } },
                "submit location" );
            $mech->follow_link_ok(
                { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link"
            );
        };

        my $fields = $mech->visible_form_values('mapSkippedForm');
        if ( $test->{fms_extra_title} ) {
            ok exists( $fields->{fms_extra_title} ), 'user title field displayed';
        } else {
            ok !exists( $fields->{fms_extra_title} ), 'user title field not displayed';
        }
        if ( $test->{first_name} ) {
            ok exists( $fields->{first_name} ), 'first name field displayed';
            ok exists( $fields->{last_name} ),  'last name field displayed';
            ok !exists( $fields->{name} ), 'no name field displayed';
        }
        else {
            ok !exists( $fields->{first_name} ),
              'first name field not displayed';
            ok !exists( $fields->{last_name} ), 'last name field not displayed';
            ok exists( $fields->{name} ), 'name field displayed';
        }

        my $submission_fields = {
            title             => "Test Report",
            detail            => 'Test report details.',
            photo1            => '',
            username          => 'firstlast@example.com',
            may_show_name     => '1',
            phone             => '07903 123 456',
            category          => 'Trees',
            password_register => '',
        };

        $submission_fields->{fms_extra_title} = $test->{fms_extra_title}
            if $test->{fms_extra_title};

        if ( $test->{first_name} ) {
            $submission_fields->{first_name} = $test->{first_name};
            $submission_fields->{last_name}  = $test->{last_name};
        }
        else {
            $submission_fields->{name} = 'Test User';
        }

        FixMyStreet::override_config $override, sub {
            $mech->submit_form_ok( { with_fields => $submission_fields },
                "submit good details" );
        };

        my $email = $mech->get_email;
        ok $email, "got an email";
        like $mech->get_text_body_from_email($email), qr/confirm that you want to send your\s+report/i, "confirm the problem";

        my $url = $mech->get_link_from_email($email);

        # confirm token in order to update the user details
        $mech->get_ok($url);

        my $user = FixMyStreet::App->model('DB::User')->find( { email => 'firstlast@example.com' } );

        my $report = $user->problems->first;
        ok $report, "Found the report";
        my $extras = $report->get_extra_fields;
        is $user->title, $test->{'user_title'}, 'user title correct';
        is_deeply $extras, $test->{extra}, 'extra contains correct values';

        $mech->delete_user($user);
    };
}

subtest 'user title not reset if no user title in submission' => sub {
        $mech->log_out_ok;
        $mech->host( 'www.fixmystreet.com' );

        my $user = $mech->log_in_ok( 'userwithtitle@example.com' );

        ok $user->update(
            {
                name => 'Has Title',
                phone => '0789 654321',
                title => 'MR',
            }
        ),
        "set users details";


        my $submission_fields = {
            title             => "Test Report",
            detail            => 'Test report details.',
            photo1            => '',
            name              => 'Has Title',
            may_show_name     => '1',
            phone             => '07903 123 456',
            category          => 'Trees',
        };

        $mech->get_ok('/');
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } },
                "submit location" );
            $mech->follow_link_ok(
                { text_regex => qr/skip this step/i, },
                "follow 'skip this step' link"
            );

            my $fields = $mech->visible_form_values('mapSkippedForm');
            ok !exists( $fields->{fms_extra_title} ), 'user title field not displayed';

            $mech->submit_form_ok( { with_fields => $submission_fields },
                "submit good details" );
        };

        $user->discard_changes;
        my $report = $user->problems->first;
        ok $report, "Found report";
        is $report->title, "Test Report", "Report title correct";
        is $user->title, 'MR', 'User title unchanged';
};

subtest "test Hart" => sub {
    for my $test (
        {
            desc      => 'confirm link for cobrand council in two tier cobrand links to cobrand site',
            category  => 'Trees',
            council   => 2333,
            national  => 0,
            button    => 'submit_register',
        },
          {
            desc      => 'confirm link for non cobrand council in two tier cobrand links to national site',
            category  => 'Street Lighting',
            council   => 2227,
            national  => 1,
            button    => 'submit_register',
          },
          {
            desc      => 'confirmation page for cobrand council in two tier cobrand links to cobrand site',
            category  => 'Trees',
            council   => 2333,
            national  => 0,
            confirm  => 1,
          },
          {
            desc      => 'confirmation page for non cobrand council in two tier cobrand links to national site',
            category  => 'Street Lighting',
            council   => 2227,
            national  => 1,
            confirm  => 1,
          },
    ) {
        subtest $test->{ desc } => sub {
            my $test_email = 'test-22@example.com';
            $mech->host( 'hart.fixmystreet.com' );
            $mech->clear_emails_ok;
            $mech->log_out_ok;

            my $user = $mech->log_in_ok($test_email) if $test->{confirm};

            FixMyStreet::override_config {
                ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ],
                BASE_URL => 'http://www.fixmystreet.com',
                MAPIT_URL => 'http://mapit.uk/',
            }, sub {
                $mech->get_ok('/around');
                $mech->content_contains( "Hart Council" );
                $mech->submit_form_ok( { with_fields => { pc => 'GU51 4AE' } }, "submit location" );
                $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
                my %optional_fields = $test->{confirm} ?  () :
                    ( username => $test_email, phone => '07903 123 456' );

                # we do this as otherwise test::www::mechanize::catalyst
                # goes to the value set in ->host above irregardless and
                # that is a 404. It works but it is not pleasant.
                $mech->clear_host if $test->{confirm} && $test->{national};
                $mech->submit_form_ok(
                    {
                        button      => $test->{button},
                        with_fields => {
                            title         => 'Test Report',
                            detail        => 'Test report details.',
                            photo1        => '',
                            name          => 'Joe Bloggs',
                            may_show_name => '1',
                            category      => $test->{category},
                            %optional_fields
                        }
                    },
                    "submit good details"
                );
            };
            is_deeply $mech->page_errors, [], "check there were no errors";

            # check that the user has been created/ not changed
            $user =
              FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
            ok $user, "user found";

            # find the report
            my $report = $user->problems->first;
            ok $report, "Found the report";

            # Check the report has been assigned appropriately
            is $report->bodies_str, $body_ids{$test->{council}};

            if ( $test->{confirm} ) {
                is $mech->uri->path, "/report/new";
                my $base = 'www.fixmystreet.com';
                $base = "hart.fixmystreet.com" unless $test->{national};
                $mech->content_contains("$base/report/" . $report->id, "links to correct site");
            } else {
                # receive token
                my $email = $mech->get_email;
                ok $email, "got an email";
                my $body = $mech->get_text_body_from_email($email);
                like $body, qr/to confirm that you want to send your/i, "confirm the problem";

                # does it reference the fact that this report hasn't been sent to Hart?
                if ( $test->{national} ) {
                    like $body, qr/Hart Council is not responsible for this type/i, "mentions report hasn't gone to Hart";
                } else {
                    unlike $body, qr/Hart Council is not responsible for this type/i, "doesn't mention report hasn't gone to Hart";
                }

                my $url = $mech->get_link_from_email($email);

                # confirm token
                FixMyStreet::override_config {
                    ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ],
                    BASE_URL => 'http://www.fixmystreet.com',
                }, sub {
                    $mech->get_ok($url);
                };

                my $base = 'www.fixmystreet.com';
                $base = 'hart.fixmystreet.com' unless $test->{national};
                $mech->content_contains( $base . '/report/' .
                    $report->id, 'confirm page links to correct site' );

                if ( $test->{national} ) {
                    # Shouldn't be found, as it was a county problem
                    FixMyStreet::override_config {
                        ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ],
                    }, sub {
                        is $mech->get( '/report/' . $report->id )->code, 404, "report not found";
                    };

                    # But should be on the main site
                    $mech->host( 'www.fixmystreet.com' );
                }
                FixMyStreet::override_config {
                    ALLOWED_COBRANDS => [ 'hart', 'fixmystreet' ],
                }, sub {
                    $mech->get_ok( '/report/' . $report->id );
                };
            }

            $report->discard_changes;
            is $report->state, 'confirmed', "Report is now confirmed";

            is $report->name, 'Joe Bloggs', 'name updated correctly';

            $mech->delete_user($user);
        };
    }
};

subtest "categories from deleted bodies shouldn't be visible for new reports" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->get_ok('/report/new/ajax?latitude=51.896268&longitude=-2.093063'); # Cheltenham
        ok $mech->content_contains( $contact3->category );

        # Delete the body which the contact belongs to.
        $contact3->body->update( { deleted => 1 } );

        $mech->get_ok('/report/new/ajax?latitude=51.896268&longitude=-2.093063'); # Cheltenham
        ok $mech->content_lacks( $contact3->category );

        $contact3->body->update( { deleted => 0 } );
    };
};

subtest "unresponsive body handling works" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        # Test body-level send method
        my $old_send = $contact1->body->send_method;
        $contact1->body->update( { send_method => 'Refused' } );
        my $body_id = $contact1->body->id;
        my $extra_details = $mech->get_ok_json('/report/new/ajax?latitude=55.952055&longitude=-3.189579');
        like $extra_details->{top_message}, qr{Edinburgh.*accept reports.*/unresponsive\?body=$body_id};
        is $extra_details->{unresponsive}, $body_id, "unresponsive json set";
        $extra_details = $mech->get_ok_json('/report/new/category_extras?category=Street%20lighting&latitude=55.952055&longitude=-3.189579');
        is $extra_details->{unresponsive}, $body_id, "unresponsive json set";

        my $test_email = 'test-2@example.com';
        $mech->log_out_ok;
        $mech->get_ok('/around');
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB', } }, "submit location" );
        $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
        $mech->submit_form_ok(
            {
                with_fields => {
                    title         => "Test Report at café",
                    detail        => 'Test report details.',
                    photo1        => '',
                    name          => 'Joe Bloggs',
                    username      => $test_email,
                    may_show_name => '1',
                    phone         => '07903 123 456',
                    category      => 'Trees',
                }
            },
            "submit good details"
        );

        my $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
        ok $user, "test user does exist";

        my $report = $user->problems->first;
        ok $report, "Found the report";
        is $report->bodies_str, undef, "Report not going anywhere";

        like $mech->get_text_body_from_email, qr/despite not being sent/i, "correct email sent";

        $user->problems->delete;
        $mech->clear_emails_ok;

        # Make sure the same behaviour occurs for reports from the mobile app
        $mech->log_out_ok;
        $mech->post_ok( '/report/new/mobile', {
            title               => "Test Report at café",
            detail              => 'Test report details.',
            photo1              => '',
            name                => 'Joe Bloggs',
            email               => $test_email,
            may_show_name       => '1',
            phone               => '07903 123 456',
            category            => 'Trees',
            service             => 'iOS',
            lat                 => 55.952055,
            lon                 => -3.189579,
            pc                  => '',
            used_map            => '1',
            submit_register     => '1',
            password_register   => '',
        });
        my $res = $mech->response;
        ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json';

        $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
        ok $user, "test user does exist";

        $report = $user->problems->first;
        ok $report, "Found the report";
        is $report->bodies_str, undef, "Report not going anywhere";

        like $mech->get_text_body_from_email, qr/despite not being sent/i, "correct email sent";

        $user->problems->delete;
        $mech->clear_emails_ok;

        $contact1->body->update( { send_method => $old_send } );

        # And test per-category refusing
        my $old_email = $contact3->email;
        $contact3->update( { email => 'REFUSED' } );
        $extra_details = $mech->get_ok_json('/report/new/ajax?latitude=51.896268&longitude=-2.093063');
        like $extra_details->{by_category}{$contact3->category}{category_extra}, qr/Cheltenham.*Trees.*unresponsive.*category=Trees/s;
        $extra_details = $mech->get_ok_json('/report/new/category_extras?category=Trees&latitude=51.896268&longitude=-2.093063');
        is $extra_details->{unresponsive}, $contact3->body->id, "unresponsive json set";

        $mech->get_ok('/around');
        $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } }, "submit location" );
        $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
        $mech->submit_form_ok(
            {
                with_fields => {
                    title         => "Test Report at café",
                    detail        => 'Test report details.',
                    photo1        => '',
                    name          => 'Joe Bloggs',
                    username      => $test_email,
                    may_show_name => '1',
                    phone         => '07903 123 456',
                    category      => 'Trees',
                }
            },
            "submit good details"
        );

        $report = $user->problems->first;
        ok $report, "Found the report";
        is $report->bodies_str, undef, "Report not going anywhere";

        $contact3->update( { email => $old_email } );
        $mech->delete_user($user);
    };
};

subtest "unresponsive body page works" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
    }, sub {
        my $old_send = $contact1->body->send_method;
        my $body_id = $contact1->body->id;
        my $url = "/unresponsive?body=$body_id";
        is $mech->get($url)->code, 404, "page not found";
        $contact1->body->update( { send_method => 'Refused' } );
        $mech->get_ok($url);
        $mech->content_contains('Edinburgh');
        $contact1->body->update( { send_method => $old_send } );

        my $old_email = $contact3->email;
        $body_id = $contact3->body->id;
        $url = "/unresponsive?body=$body_id;category=Trees";
        is $mech->get($url)->code, 404, "page not found";
        $contact3->update( { email => 'REFUSED' } );
        $mech->get_ok($url);
        $mech->content_contains('Cheltenham');
        $mech->content_contains('Trees');
        $contact3->update( { email => $old_email } );
    };
};

subtest "extra google analytics code displayed on logged in problem creation" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        BASE_URL => 'https://www.fixmystreet.com',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        # check that the user does not exist
        my $test_email = 'test-2@example.com';

        $mech->clear_emails_ok;
        my $user = $mech->log_in_ok($test_email);

        # setup the user.
        ok $user->update(
            {
                name  => 'Test User',
                phone => '01234 567 890',
            }
          ),
          "set users details";

        # submit initial pc form
        $mech->get_ok('/around');
        $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR', } },
            "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(
            {
                with_fields => {
                    title         => "Test Report at café",
                    detail        => 'Test report details.',
                    photo1        => '',
                    name          => 'Joe Bloggs',
                    may_show_name => '1',
                    phone         => '07903 123 456',
                    category      => 'Trees',
                }
            },
            "submit good details"
        );

        # find the report
        my $report = $user->problems->first;
        ok $report, "Found the report";

        $mech->content_contains( "'id': 'report/" . $report->id . "'", 'extra google code present' );

        # cleanup
        $mech->delete_user($user);
    };
};

subtest "extra google analytics code displayed on email confirmation problem creation" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        BASE_URL => 'https://www.fixmystreet.com',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->log_out_ok;
        $mech->clear_emails_ok;

        $mech->get_ok('/');
        $mech->submit_form_ok( { with_fields => { pc => 'GL50 2PR' } },
            "submit location" );
        $mech->follow_link_ok(
            { text_regex => qr/skip this step/i, },
            "follow 'skip this step' link"
        );

        my $fields = $mech->visible_form_values('mapSkippedForm');
        my $submission_fields = {
            title             => "Test Report",
            detail            => 'Test report details.',
            photo1            => '',
            username          => 'firstlast@example.com',
            name              => 'Test User',
            may_show_name     => '1',
            phone             => '07903 123 456',
            category          => 'Trees',
            password_register => '',
        };

        $mech->submit_form_ok( { with_fields => $submission_fields },
            "submit good details" );

        my $email = $mech->get_email;
        ok $email, "got an email";
        like $mech->get_text_body_from_email($email), qr/confirm that you want to/i, "confirm the problem";

        my $url = $mech->get_link_from_email($email);

        # confirm token in order to update the user details
        $mech->get_ok($url);

        # find the report
        my $user = FixMyStreet::App->model('DB::User')->find( { email => 'firstlast@example.com' } );

        my $report = $user->problems->first;
        ok $report, "Found the report";

        $mech->content_contains( "'id': 'report/" . $report->id . "'", 'extra google code present' );

        $mech->delete_user($user);
    };
};


my $private_perms = $mech->create_user_ok('private_perms@example.org', name => 'private', from_body => $bodies[0]);
subtest "report_mark_private allows users to mark reports as private" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        BASE_URL => 'https://www.fixmystreet.com',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->log_out_ok;

        $private_perms->user_body_permissions->find_or_create({
            body => $bodies[0],
            permission_type => 'report_mark_private',
        });

        $mech->log_in_ok('private_perms@example.org');
        $mech->get_ok('/');
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB' } },
            "submit location" );
        $mech->follow_link_ok(
            { text_regex => qr/skip this step/i, },
            "follow 'skip this step' link"
        );

        $mech->submit_form_ok(
            {
                with_fields => {
                    title         => "Private report",
                    detail        => 'Private report details.',
                    photo1        => '',
                    name          => 'Joe Bloggs',
                    may_show_name => '1',
                    phone         => '07903 123 456',
                    category      => 'Trees',
                    non_public    => 1,
                }
            },
            "submit good details"
        );

        $mech->content_contains('Great work. Now spread the word', 'shown confirmation page');
    }
};

my $inspector = $mech->create_user_ok('inspector@example.org', name => 'inspector', from_body => $bodies[0]);
foreach my $test (
  { non_public => 0 },
  { non_public => 1 },
) {
  subtest "inspectors get redirected directly to the report page, non_public=$test->{non_public}" => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        BASE_URL => 'https://www.fixmystreet.com',
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $mech->log_out_ok;

        $inspector->user_body_permissions->find_or_create({
            body => $bodies[0],
            permission_type => 'planned_reports',
        });
        $inspector->user_body_permissions->find_or_create({
            body => $bodies[0],
            permission_type => 'report_inspect',
        });

        $mech->log_in_ok('inspector@example.org');
        $mech->get_ok('/');
        $mech->submit_form_ok( { with_fields => { pc => 'EH1 1BB' } },
            "submit location" );
        $mech->follow_link_ok(
            { text_regex => qr/skip this step/i, },
            "follow 'skip this step' link"
        );

        $mech->submit_form_ok(
            {
                with_fields => {
                    title         => "Inspector report",
                    detail        => 'Inspector report details.',
                    photo1        => '',
                    name          => 'Joe Bloggs',
                    may_show_name => '1',
                    phone         => '07903 123 456',
                    category      => 'Trees',
                    non_public => $test->{non_public},
                }
            },
            "submit good details"
        );

        like $mech->uri->path, qr/\/report\/[0-9]+/, 'Redirects directly to report';
    }
  };
}

subtest "check map click ajax response for inspector" => sub {
    $mech->log_out_ok;

    my $extra_details;
    $inspector->user_body_permissions->find_or_create({
        body => $bodies[0],
        permission_type => 'planned_reports',
    });
    $inspector->user_body_permissions->find_or_create({
        body => $bodies[0],
        permission_type => 'report_inspect',
    });

    $mech->log_in_ok('inspector@example.org');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=55.952055&longitude=-3.189579' );
    };
    like $extra_details->{category}, qr/data-prefill="0/, 'inspector prefill not set';
    ok !$extra_details->{contribute_as}, 'no contribute as section';
};

subtest "check map click ajax response for inspector and uk cobrand" => sub {
    $mech->log_out_ok;

    my $extra_details;
    $inspector->user_body_permissions->find_or_create({
        body => $bodies[4],
        permission_type => 'planned_reports',
    });
    $inspector->user_body_permissions->find_or_create({
        body => $bodies[4],
        permission_type => 'report_inspect',
    });

    $mech->log_in_ok('inspector@example.org');
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => [ { bromley => '.' } ],
        MAPIT_URL => 'http://mapit.uk/',
    }, sub {
        $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=51.402096&longitude=0.015784' );
    };
    like $extra_details->{category}, qr/data-prefill="0/, 'inspector prefill not set';
};

for my $test (
    {
        desc => 'map click ajax for contribute_as_another_user',
        permissions => {
            contribute_as_another_user => 1,
            contribute_as_anonymous_user => undef,
            contribute_as_body => undef,
        }
    },
    {
        desc => 'map click ajax for contribute_as_anonymous_user',
        permissions => {
            contribute_as_another_user => undef,
            contribute_as_anonymous_user => 1,
            contribute_as_body => undef,
        }
    },
    {
        desc => 'map click ajax for contribute_as_body',
        permissions => {
            contribute_as_another_user => undef,
            contribute_as_anonymous_user => undef,
            contribute_as_body => 1,
        }
    },
) {
    subtest $test->{desc} => sub {
        $mech->log_out_ok;
        my $extra_details;
        (my $name = $test->{desc}) =~ s/.*(contri.*)/$1/;
        my $user = $mech->create_user_ok("$name\@example.org", name => 'test user', from_body => $bodies[0]);
        for my $p ( keys %{$test->{permissions}} ) {
            next unless $test->{permissions}->{$p};
            $user->user_body_permissions->find_or_create({
                body => $bodies[0],
                permission_type => $p,
            });
        }
        $mech->log_in_ok("$name\@example.org");
        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=55.952055&longitude=-3.189579' );
        };
        for my $p ( keys %{$test->{permissions}} ) {
            (my $key = $p) =~ s/contribute_as_//;
            is $extra_details->{contribute_as}->{$key}, $test->{permissions}->{$p}, "$key correctly set";
        }

        FixMyStreet::override_config {
            ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
            MAPIT_URL => 'http://mapit.uk/',
        }, sub {
            $extra_details = $mech->get_ok_json( '/report/new/ajax?latitude=51.754926&longitude=-1.256179' );
        };
        ok !$extra_details->{contribute_as}, 'no contribute as section for other council';
    };
}

done_testing();