diff options
Diffstat (limited to 't')
-rw-r--r-- | t/app/model/responsepriority.t | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/t/app/model/responsepriority.t b/t/app/model/responsepriority.t new file mode 100644 index 000000000..03c5bccae --- /dev/null +++ b/t/app/model/responsepriority.t @@ -0,0 +1,103 @@ +use strict; +use warnings; +use Test::More; + +use FixMyStreet::App; +use FixMyStreet::TestMech; +use JSON::MaybeXS; + +my $mech = FixMyStreet::TestMech->new; +my $area_id = 2237; + +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 $potholes_response_priority = FixMyStreet::App->model('DB::ResponsePriority')->find_or_create( + { + body_id => $oxfordshire->id, + name => 'Potholes', + description => 'This priority is to do with potholes' + } +); +$potholes_response_priority->contact_response_priorities->find_or_create({ + contact_id => $potholes_contact->id, +}); + +my $general_response_priority = FixMyStreet::App->model('DB::ResponsePriority')->find_or_create( + { + body_id => $oxfordshire->id, + name => 'All categories', + description => 'This priority is for all categories' + } +); + +subtest 'for_bodies returns correct results' => sub { + my $priorities = FixMyStreet::App->model('DB::ResponsePriority')->for_bodies( + [ $oxfordshire->id ], + 'Potholes' + ); + + is $priorities->count, 2, 'Both priorities are included for Potholes category'; + + $priorities = FixMyStreet::App->model('DB::ResponsePriority')->for_bodies( + [ $oxfordshire->id ], + 'Traffic lights' + ); + + is $priorities->count, 1, 'Only 1 priority is included for Traffic lights category'; + is $priorities->first->name, $general_response_priority->name, 'Correct priority is returned for Traffic lights category'; +}; + +subtest 'by_categories returns allresponse priorities grouped by category' => sub { + my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all; + my $priorities = FixMyStreet::App->model('DB::ResponsePriority')->by_categories($area_id, @contacts); + my $potholes = decode_json($priorities->{Potholes}); + my $traffic_lights = decode_json($priorities->{'Traffic lights'}); + + is scalar @$potholes, 2, 'Potholes have 2 defect types'; + 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 { + my $other_response_priority = FixMyStreet::App->model('DB::ResponsePriority')->find_or_create( + { + body_id => $other_body->id, + name => 'All categories', + description => 'This priority is for all categories' + } + ); + + my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all; + my $priorities = FixMyStreet::App->model('DB::ResponsePriority')->by_categories($area_id, @contacts); + my $potholes = decode_json($priorities->{Potholes}); + my $traffic_lights = decode_json($priorities->{'Traffic lights'}); + + is scalar @$potholes, 3, 'Potholes have 3 defect types'; + is scalar @$traffic_lights, 2, 'Traffic lights have 2 defect types'; +}; + +subtest 'by_categories encodes HTML entities' => sub { + FixMyStreet::App->model('DB::ResponsePriority')->find_or_create( + { + body_id => $other_body->id, + name => 'This priority\'s name has an apostrophe', + description => 'This priority is for all categories' + } + ); + + my @contacts = FixMyStreet::DB->resultset('Contact')->not_deleted->search( { body_id => [ $oxfordshire->id ] } )->all; + my $priorities = FixMyStreet::App->model('DB::ResponsePriority')->by_categories($area_id, @contacts); + + my $traffic_lights = decode_json($priorities->{'Traffic lights'}); + use Data::Dumper; + my $priority = @$traffic_lights[2]; + is $priority->{name}, 'This priority's name has an apostrophe'; +}; + +END { + $mech->delete_body( $other_body ); + $mech->delete_body( $oxfordshire ); + done_testing(); +} |