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
|
use strict;
use warnings;
use Test::More;
use FixMyStreet::TestMech;
use FixMyStreet::App;
use Web::Scraper;
# disable info logs for this test run
FixMyStreet::App->log->disable('info');
END { FixMyStreet::App->log->enable('info'); }
my $mech = FixMyStreet::TestMech->new;
my $body = $mech->create_body_ok(2651, 'City of Edinburgh Council');
$body->update({
endpoint => 'http://example.com/open311',
jurisdiction => 'mySociety',
api_key => 'apikey',
});
# Let's make some contacts to send things to!
my $contact1 = $mech->create_contact_ok(
body_id => $body->id, # Edinburgh
category => 'Street lighting',
email => '100',
extra => [ { description => 'Lamppost number', code => 'number', required => 'True' },
{ description => 'Lamppost type', code => 'type', required => 'False', values =>
{ value => [ { name => ['Gas'], key => ['old'] }, { name => [ 'Yellow' ], key => [ 'modern' ] } ] }
}
],
);
my $contact2 = $mech->create_contact_ok(
body_id => $body->id, # Edinburgh
category => 'Graffiti Removal',
email => '101',
);
# test that the various bit of form get filled in and errors correctly
# generated.
foreach my $test (
{
msg => 'all fields empty',
pc => 'EH99 1SP',
fields => {
title => '',
detail => '',
photo1 => '',
photo2 => '',
photo3 => '',
name => '',
may_show_name => '1',
email => '',
phone => '',
category => 'Street lighting',
password_sign_in => '',
password_register => '',
remember_me => undef,
},
changes => {
number => '',
type => 'old',
},
errors => [
'This information is required',
'Please enter a subject',
'Please enter some details',
'Please enter your email',
'Please enter your name',
],
submit_with => {
title => 'test',
detail => 'test detail',
name => 'Test User',
email => 'testopen311@example.com',
category => 'Street lighting',
number => 27,
},
extra => [
{
name => 'number',
value => 27,
description => 'Lamppost number',
},
{
name => 'type',
value => 'old',
description => 'Lamppost type',
}
]
},
)
{
subtest "check form errors where $test->{msg}" => sub {
$mech->log_out_ok;
$mech->clear_emails_ok;
# check that the user does not exist
my $test_email = $test->{submit_with}->{email};
my $user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
if ( $user ) {
$user->problems->delete;
$user->comments->delete;
$user->delete;
}
$mech->get_ok('/around');
# submit initial pc form
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
MAPIT_URL => 'http://mapit.mysociety.org/',
}, 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 $mech->page_errors, $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";
if ( $test->{fields}->{category} eq 'Street lighting' ) {
my $result = scraper {
process 'select#form_type option', 'option[]' => '@value';
}
->scrape( $mech->response );
is_deeply $result->{option}, [ qw/old modern/], 'displayed streetlight type select';
}
$new_values = {
%{ $test->{fields} },
%{ $test->{submit_with} },
};
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ { 'fixmystreet' => '.' } ],
MAPIT_URL => 'http://mapit.mysociety.org/',
}, sub {
$mech->submit_form_ok( { with_fields => $new_values } );
};
$user = FixMyStreet::App->model('DB::User')->find( { email => $test_email } );
ok $user, 'created user';
my $prob = $user->problems->first;
ok $prob, 'problem created';
is_deeply $prob->get_extra_fields, $test->{extra}, 'extra open311 data added to problem';
$user->problems->delete;
$user->delete;
};
}
done_testing();
END {
$mech->delete_body($body);
}
|