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
|
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();
}
|