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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use FixMyStreet;
use FixMyStreet::App;
use mySociety::Locale;
mySociety::Locale::gettext_domain('FixMyStreet');
my $problem_rs = FixMyStreet::App->model('DB::Problem');
my $problem = $problem_rs->new(
{
postcode => 'EH99 1SP',
latitude => 1,
longitude => 1,
areas => 1,
title => '',
detail => '',
used_map => 1,
user_id => 1,
name => '',
state => 'confirmed',
service => '',
cobrand => 'default',
cobrand_data => '',
}
);
is $problem->confirmed_local, undef, 'inflating null confirmed ok';
is $problem->whensent_local, undef, 'inflating null confirmed ok';
is $problem->lastupdate_local, undef, 'inflating null confirmed ok';
is $problem->created_local, undef, 'inflating null confirmed ok';
for my $test (
{
desc => 'more or less empty problem',
changed => {},
errors => {
title => 'Please enter a subject',
detail => 'Please enter some details',
council => 'No council selected',
name => 'Please enter your name',
}
},
{
desc => 'name too short',
changed => {
name => 'xx',
},
errors => {
title => 'Please enter a subject',
detail => 'Please enter some details',
council => 'No council selected',
name => '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',
}
},
{
desc => 'name is anonymous',
changed => {
name => 'anonymous',
},
errors => {
title => 'Please enter a subject',
detail => 'Please enter some details',
council => 'No council selected',
name => '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',
}
},
{
desc => 'correct name',
changed => {
name => 'A User',
},
errors => {
title => 'Please enter a subject',
detail => 'Please enter some details',
council => 'No council selected',
}
},
{
desc => 'correct title',
changed => {
title => 'A Title',
},
errors => {
detail => 'Please enter some details',
council => 'No council selected',
}
},
{
desc => 'correct detail',
changed => {
detail => 'Some information about the problem',
},
errors => {
council => 'No council selected',
}
},
{
desc => 'incorrectly formatted council',
changed => {
council => 'my council',
},
errors => {
council => 'No council selected',
}
},
{
desc => 'correctly formatted council',
changed => {
council => '1001',
},
errors => {
}
},
{
desc => 'bad category',
changed => {
category => '-- Pick a category --',
},
errors => {
category => 'Please choose a category',
}
},
{
desc => 'bad category',
changed => {
category => '-- Pick a property type --',
},
errors => {
category => 'Please choose a property type',
}
},
{
desc => 'correct category',
changed => {
category => 'Horse!',
},
errors => {
}
},
) {
$problem->$_( $test->{changed}->{$_} ) for keys %{$test->{changed}};
subtest $test->{desc} => sub {
is_deeply $problem->check_for_errors, $test->{errors}, 'check for errors';
};
}
my $user = FixMyStreet::App->model('DB::User')->find_or_create(
{
email => 'system_user@example.com'
}
);
$problem->user( $user );
$problem->created( DateTime->now()->subtract( days => 1 ) );
$problem->lastupdate( DateTime->now()->subtract( days => 1 ) );
$problem->anonymous(1);
$problem->insert;
my $tz_local = DateTime::TimeZone->new( name => 'local' );
for my $test (
{
desc => 'request older than problem ignored',
lastupdate => '',
request => {
updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local )->subtract( days => 2 ) ),
},
council => {
name => 'Edinburgh City Council',
},
created => 0,
},
{
desc => 'request newer than problem created',
lastupdate => '',
request => {
updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local ) ),
status => 'open',
status_notes => 'this is an update from the council',
},
council => {
name => 'Edinburgh City Council',
},
created => 1,
state => 'confirmed',
mark_fixed => 0,
mark_open => 0,
},
{
desc => 'update with state of closed fixes problem',
lastupdate => '',
request => {
updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local ) ),
status => 'closed',
status_notes => 'the council have fixed this',
},
council => {
name => 'Edinburgh City Council',
},
created => 1,
state => 'fixed',
mark_fixed => 1,
mark_open => 0,
},
{
desc => 'update with state of open leaves problem as fixed',
lastupdate => '',
request => {
updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local ) ),
status => 'open',
status_notes => 'the council do not think this is fixed',
},
council => {
name => 'Edinburgh City Council',
},
created => 1,
start_state => 'fixed',
state => 'fixed',
mark_fixed => 0,
mark_open => 0,
},
) {
subtest $test->{desc} => sub {
# makes testing easier;
$problem->comments->delete;
$problem->created( DateTime->now()->subtract( days => 1 ) );
$problem->lastupdate( DateTime->now()->subtract( days => 1 ) );
$problem->state( $test->{start_state} || 'confirmed' );
$problem->update;
my $w3c = DateTime::Format::W3CDTF->new();
my $ret = $problem->update_from_open311_service_request( $test->{request}, $test->{council}, $user );
is $ret, $test->{created}, 'return value';
return unless $test->{created};
$problem->discard_changes;
is $problem->lastupdate, $w3c->parse_datetime($test->{request}->{updated_datetime}), 'lastupdate time';
my $update = $problem->comments->first;
ok $update, 'updated created';
is $problem->state, $test->{state}, 'problem state';
is $update->text, $test->{request}->{status_notes}, 'update text';
is $update->mark_open, $test->{mark_open}, 'update mark_open flag';
is $update->mark_fixed, $test->{mark_fixed}, 'update mark_fixed flag';
};
}
$problem->comments->delete;
$problem->delete;
$user->delete;
done_testing();
|