aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/model
diff options
context:
space:
mode:
Diffstat (limited to 't/app/model')
-rw-r--r--t/app/model/comment.t25
-rw-r--r--t/app/model/problem.t65
-rw-r--r--t/app/model/responsepriority.t49
-rw-r--r--t/app/model/responsetemplate.t8
4 files changed, 80 insertions, 67 deletions
diff --git a/t/app/model/comment.t b/t/app/model/comment.t
index 3f30b3a1e..dd9af9eb9 100644
--- a/t/app/model/comment.t
+++ b/t/app/model/comment.t
@@ -1,20 +1,27 @@
use FixMyStreet::Test;
-my $comment_rs = FixMyStreet::DB->resultset('Comment');
+my $user = FixMyStreet::DB->resultset('User')->new({ name => 'Test User', is_superuser => 1 });
+my $comment_rs = FixMyStreet::DB->resultset('Comment');
my $comment = $comment_rs->new(
{
- user_id => 1,
+ user => $user,
problem_id => 1,
text => '',
- state => 'confirmed',
- anonymous => 0,
- mark_fixed => 0,
- cobrand => 'default',
- cobrand_data => '',
}
);
-is $comment->confirmed, undef, 'inflating null confirmed ok';
-is $comment->created, undef, 'inflating null confirmed ok';
+is $comment->created, undef, 'inflating null created ok';
+is $comment->mark_fixed, 0, 'mark fixed default set';
+is $comment->state, 'confirmed', 'state default is confirmed';
+is $comment->name, 'an administrator';
+
+$user->is_superuser(0);
+$comment = $comment_rs->new({
+ user => $user,
+ problem_id => 1,
+ text => '',
+});
+is $comment->name, 'Test User';
+
done_testing();
diff --git a/t/app/model/problem.t b/t/app/model/problem.t
index 661a8827f..be949054b 100644
--- a/t/app/model/problem.t
+++ b/t/app/model/problem.t
@@ -2,6 +2,7 @@ use FixMyStreet::TestMech;
use FixMyStreet;
use FixMyStreet::DB;
use FixMyStreet::Script::Reports;
+use Open311::GetUpdates;
use Sub::Override;
my $problem_rs = FixMyStreet::DB->resultset('Problem');
@@ -133,6 +134,7 @@ $problem->anonymous(1);
$problem->insert;
my $tz_local = DateTime::TimeZone->new( name => 'local' );
+my $comment_time = DateTime->now->set_time_zone( $tz_local );
my $body = FixMyStreet::DB->resultset('Body')->new({
name => 'Edinburgh City Council'
@@ -140,52 +142,32 @@ my $body = FixMyStreet::DB->resultset('Body')->new({
for my $test (
{
- desc => 'request older than problem ignored',
- lastupdate => '',
+ desc => 'request after problem created',
request => {
- updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local )->subtract( days => 2 ) ),
- },
- 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',
+ comment_time => $comment_time,
+ description => 'this is an update from the 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 ) ),
+ comment_time => $comment_time,
status => 'closed',
- status_notes => 'the council have fixed this',
+ description => 'the council have fixed this',
},
- created => 1,
- state => 'fixed',
- mark_fixed => 1,
- mark_open => 0,
+ state => 'fixed - council',
},
{
- desc => 'update with state of open leaves problem as fixed',
- lastupdate => '',
+ desc => 'update with state of open reopens problem',
request => {
- updated_datetime => DateTime::Format::W3CDTF->new()->format_datetime( DateTime->now()->set_time_zone( $tz_local ) ),
+ comment_time => $comment_time,
status => 'open',
- status_notes => 'the council do not think this is fixed',
+ description => 'the council do not think this is fixed',
},
- created => 1,
- start_state => 'fixed',
- state => 'fixed',
- mark_fixed => 0,
- mark_open => 0,
+ start_state => 'fixed - council',
+ state => 'confirmed',
},
) {
subtest $test->{desc} => sub {
@@ -197,23 +179,20 @@ for my $test (
$problem->update;
my $w3c = DateTime::Format::W3CDTF->new();
- my $ret = $problem->update_from_open311_service_request( $test->{request}, $body, $user );
- is $ret, $test->{created}, 'return value';
-
- return unless $test->{created};
+ my $o = Open311->new( jurisdiction => 'mysociety', endpoint => 'http://example.com', test_mode => 1 );
+ my $updates = Open311::GetUpdates->new(
+ current_open311 => $o,
+ current_body => $body,
+ system_user => $user,
+ );
+ my $update = $updates->process_update($test->{request}, $problem);
$problem->discard_changes;
- is $problem->lastupdate, $w3c->parse_datetime($test->{request}->{updated_datetime}), 'lastupdate time';
-
- my $update = $problem->comments->first;
+ is $problem->lastupdate, $test->{request}->{comment_time}, 'lastupdate time';
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';
+ is $update->text, $test->{request}->{description}, 'update text';
};
}
diff --git a/t/app/model/responsepriority.t b/t/app/model/responsepriority.t
index bd09c2da0..0ecce1529 100644
--- a/t/app/model/responsepriority.t
+++ b/t/app/model/responsepriority.t
@@ -12,6 +12,7 @@ my $oxfordshire = $mech->create_body_ok($area_id, 'Oxfordshire County Council');
my $other_body = $mech->create_body_ok($area_id, 'Some Other Council');
my $potholes_contact = $mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Potholes', email => 'potholes@example.com' );
my $traffic_lights_contact =$mech->create_contact_ok( body_id => $oxfordshire->id, category => 'Traffic lights', email => 'lights@example.com' );
+my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all;
my $potholes_response_priority = FixMyStreet::DB->resultset('ResponsePriority')->find_or_create(
{
@@ -32,25 +33,42 @@ my $general_response_priority = FixMyStreet::DB->resultset('ResponsePriority')->
}
);
-subtest 'for_bodies returns correct results' => sub {
+my $deleted1_response_priority = FixMyStreet::DB->resultset('ResponsePriority')->find_or_create(
+ {
+ body_id => $oxfordshire->id,
+ name => 'Deleted priority 1',
+ description => 'This priority has been deleted',
+ deleted => 1,
+ }
+);
+
+my $deleted2_response_priority = FixMyStreet::DB->resultset('ResponsePriority')->find_or_create(
+ {
+ body_id => $oxfordshire->id,
+ name => 'Deleted priority 2',
+ description => 'This priority has been deleted',
+ deleted => 1,
+ }
+);
+
+subtest 'for_bodies returns correct results (including deleted)' => sub {
my $priorities = FixMyStreet::DB->resultset('ResponsePriority')->for_bodies(
[ $oxfordshire->id ],
'Potholes'
);
- is $priorities->count, 2, 'Both priorities are included for Potholes category';
+ is $priorities->count, 4, 'all priorities are included for Potholes category';
$priorities = FixMyStreet::DB->resultset('ResponsePriority')->for_bodies(
[ $oxfordshire->id ],
'Traffic lights'
);
- is $priorities->count, 1, 'Only 1 priority is included for Traffic lights category';
+ is $priorities->count, 3, 'Pothole-only priority ignored for Traffic lights category';
is $priorities->first->name, $general_response_priority->name, 'Correct priority is returned for Traffic lights category';
};
-subtest 'by_categories returns all response priorities grouped by category' => sub {
- my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all;
+subtest 'by_categories returns all undeleted response priorities grouped by category' => sub {
my $priorities = FixMyStreet::DB->resultset('ResponsePriority')->by_categories(\@contacts, body_id => $oxfordshire->id);
my $potholes = decode_json($priorities->{Potholes});
my $traffic_lights = decode_json($priorities->{'Traffic lights'});
@@ -59,7 +77,7 @@ subtest 'by_categories returns all response priorities grouped by category' => s
is scalar @$traffic_lights, 1, 'Traffic lights have 1 defect type';
};
-subtest 'by_categories returns all response priorities for an area with multiple bodies' => sub {
+subtest 'by_categories returns all undeleted response priorities for an area with multiple bodies' => sub {
my $other_response_priority = FixMyStreet::DB->resultset('ResponsePriority')->find_or_create(
{
body_id => $other_body->id,
@@ -68,7 +86,6 @@ subtest 'by_categories returns all response priorities for an area with multiple
}
);
- my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all;
my $priorities = FixMyStreet::DB->resultset('ResponsePriority')->by_categories(\@contacts, area_id => $area_id);
my $potholes = decode_json($priorities->{Potholes});
my $traffic_lights = decode_json($priorities->{'Traffic lights'});
@@ -77,8 +94,16 @@ subtest 'by_categories returns all response priorities for an area with multiple
is scalar @$traffic_lights, 2, 'Traffic lights have 2 defect types';
};
-END {
- $mech->delete_body( $other_body );
- $mech->delete_body( $oxfordshire );
- done_testing();
-}
+subtest 'test with existing problem' => sub {
+ my ($problem) = $mech->create_problems_for_body(1, $oxfordshire->id, 'Title', {
+ response_priority => $deleted1_response_priority,
+ });
+ my $priorities = FixMyStreet::DB->resultset('ResponsePriority')->by_categories(\@contacts,
+ area_id => $area_id, problem => $problem);
+ my $potholes = decode_json($priorities->{Potholes});
+ my $traffic_lights = decode_json($priorities->{'Traffic lights'});
+ is scalar @$potholes, 4, 'Potholes have 4 defect types, deleted is included';
+ is scalar @$traffic_lights, 3, 'Traffic lights have 3 defect types, deleted is included';
+};
+
+done_testing();
diff --git a/t/app/model/responsetemplate.t b/t/app/model/responsetemplate.t
index 9efc7e3b4..29c41a0e0 100644
--- a/t/app/model/responsetemplate.t
+++ b/t/app/model/responsetemplate.t
@@ -1,3 +1,4 @@
+use utf8;
use FixMyStreet::TestMech;
use JSON::MaybeXS;
@@ -7,7 +8,7 @@ my $area_id = 2651;
my $body = $mech->create_body_ok($area_id, 'Edinburgh Council');
my $c1 = $mech->create_contact_ok(category => 'Potholes', body_id => $body->id, email => 'p');
my $c2 = $mech->create_contact_ok(category => 'Graffiti', body_id => $body->id, email => 'g');
-my $t1 = FixMyStreet::DB->resultset('ResponseTemplate')->create({ body_id => $body->id, title => "Title 1", text => "Text 1" });
+my $t1 = FixMyStreet::DB->resultset('ResponseTemplate')->create({ body_id => $body->id, title => "Title 1", text => "Text 1 ⛄" });
my $t2 = FixMyStreet::DB->resultset('ResponseTemplate')->create({ body_id => $body->id, title => "Title 2", text => "Text 2", state => 'investigating' });
my $t3 = FixMyStreet::DB->resultset('ResponseTemplate')->create({ body_id => $body->id, title => "Title 3", text => "Text 3" });
$t1->add_to_contacts($c1);
@@ -17,12 +18,13 @@ my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { bod
subtest 'by_categories returns allresponse templates grouped by category' => sub {
my $templates = FixMyStreet::DB->resultset('ResponseTemplate')->by_categories(\@contacts, body_id => $body->id);
- my $potholes = decode_json($templates->{Potholes});
- my $graffiti = decode_json($templates->{Graffiti});
+ my $potholes = JSON::MaybeXS->new->decode($templates->{Potholes});
+ my $graffiti = JSON::MaybeXS->new->decode($templates->{Graffiti});
is scalar @$potholes, 2, 'Potholes have 2 templates';
is scalar @$graffiti, 2, 'Graffiti has 2 templates';
is $graffiti->[0]->{state}, 'investigating', 'Graffiti first template has right state';
+ is $potholes->[0]->{id}, 'Text 1 ⛄', 'Pothole first template has right text';
};
done_testing;