]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/18-lp1592891_sip_standing_penalties.t
LP#1592891: Tests - SIP2 standing penalty failures
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 18-lp1592891_sip_standing_penalties.t
1 #!perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 30;
6
7 # This test includes much code copied or adapted from Jason Stephenson's tests
8 # in 14-lp1499123_csp_ignore_proximity.t
9
10 diag("Test bugfix for lp1592891 - SIP2 failures with standing penalties.");
11
12 use OpenILS::Const qw/:const/;
13 use OpenILS::Utils::TestUtils;
14 use OpenILS::SIP::Patron;
15 my $script = OpenILS::Utils::TestUtils->new();
16 our $apputils = 'OpenILS::Application::AppUtils';
17
18 use constant WORKSTATION_NAME => 'BR1-test-lp1592891_sip_standing_penalties.t';
19 use constant WORKSTATION_LIB => 4;
20
21 # Because this may run multiple times, without a DB reload, we search
22 # for the workstation before registering it.
23 sub find_workstation {
24     my $r = $apputils->simplereq(
25         'open-ils.actor',
26         'open-ils.actor.workstation.list',
27         $script->authtoken,
28         WORKSTATION_LIB
29     );
30     if ($r->{&WORKSTATION_LIB}) {
31         return scalar(grep {$_->name() eq WORKSTATION_NAME} @{$r->{&WORKSTATION_LIB}});
32     }
33     return 0;
34 }
35
36 sub retrieve_penalty {
37     my $e = shift;
38     my $penalty = shift;
39     my $csp = $e->retrieve_config_standing_penalty($penalty);
40     return $csp;
41 }
42
43 sub retrieve_user_by_barcode {
44     my $barcode = shift;
45     return $apputils->simplereq(
46         'open-ils.actor',
47         'open-ils.actor.user.fleshed.retrieve_by_barcode',
48         $script->authtoken,
49         $barcode
50     );
51 }
52
53 sub apply_penalty_to_patron {
54     my ($staff, $patron, $penalty_id) = @_;
55     my $penalty = Fieldmapper::actor::user_standing_penalty->new();
56     $penalty->standing_penalty($penalty_id);
57     $penalty->usr($patron->id());
58     $penalty->set_date('now');
59     $penalty->staff($staff->id());
60     $penalty->org_unit(1); # Consortium-wide.
61     $penalty->note('LP 1592891 SIP standing penalties test');
62     my $r = $apputils->simplereq(
63         'open-ils.actor',
64         'open-ils.actor.user.penalty.apply',
65         $script->authtoken,
66         $penalty
67     );
68     if (ref($r)) {
69         undef($penalty);
70     } else {
71         $penalty->id($r);
72     }
73     return $penalty;
74 }
75
76 sub remove_penalty_from_patron {
77     my $penalty = shift;
78     return $apputils->simplereq(
79         'open-ils.actor',
80         'open-ils.actor.user.penalty.remove',
81         $script->authtoken,
82         $penalty
83     );
84 }
85
86 sub patron_sip_test {
87     my $patron_id = shift;
88     my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
89     return scalar(@{$patron->{user}->standing_penalties()});
90 }
91
92 sub patron_sip_too_many_overdue_test {
93     my $patron_id = shift;
94     my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
95     return $patron->too_many_overdue;
96 }
97
98 sub patron_sip_excessive_fines_test {
99     my $patron_id = shift;
100     my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
101     return $patron->excessive_fines;
102 }
103
104 # In concerto, we need to register a workstation.
105 $script->authenticate({
106     username => 'admin',
107     password => 'demo123',
108     type => 'staff',
109 });
110 ok($script->authtoken, 'Initial Login');
111
112 SKIP: {
113     my $ws = find_workstation();
114     skip 'Workstation exists', 1 if ($ws);
115     $ws = $script->register_workstation(WORKSTATION_NAME, WORKSTATION_LIB) unless ($ws);
116     ok(! ref $ws, 'Registered a new workstation');
117 }
118
119 $script->logout();
120 $script->authenticate({
121     username => 'admin',
122     password => 'demo123',
123     type => 'staff',
124     workstation => WORKSTATION_NAME
125 });
126 ok($script->authtoken, 'Login with workstaion');
127
128 # Get a CStoreEditor for later use.
129 my $editor = $script->editor(authtoken=>$script->authtoken);
130 my $staff = $editor->checkauth();
131 ok(ref($staff), 'Got a staff user');
132
133 # We retrieve STAFF_CHR penalty and check that it has an undefined
134 # ignore_proximity.
135 my $staff_chr = retrieve_penalty($editor, 25);
136 isa_ok($staff_chr, 'Fieldmapper::config::standing_penalty', 'STAFF_CHR');
137 is($staff_chr->name, 'STAFF_CHR', 'Penalty name is STAFF_CHR');
138 is($staff_chr->ignore_proximity, undef, 'STAFF_CHR ignore_proximity is undefined');
139
140 # We retrieve OILS_PENALTY_PATRON_EXCEEDS_FINES penalty and check that it has an undefined
141 # ignore_proximity.
142 my $csp_fines = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_FINES);
143 isa_ok($csp_fines, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_FINES');
144 is($csp_fines->name, 'PATRON_EXCEEDS_FINES', 'Penalty name is PATRON_EXCEEDS_FINES');
145 is($csp_fines->ignore_proximity, undef, 'PATRON_EXCEEDS_FINES ignore_proximity is undefined');
146
147 # We retrieve STAFF_CHR penalty and check that it has an undefined
148 # ignore_proximity.
149 my $csp_overdues = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
150 isa_ok($csp_overdues, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_OVERDUE_COUNT');
151 is($csp_overdues->name, 'PATRON_EXCEEDS_OVERDUE_COUNT', 'Penalty name is PATRON_EXCEEDS_OVERDUE_COUNT');
152 is($csp_overdues->ignore_proximity, undef, 'PATRON_EXCEEDS_OVERDUE_COUNT ignore_proximity is undefined');
153
154 # We need a patron with no penalties to test holds and circulation.
155 my $patron = retrieve_user_by_barcode("99999350419");
156 isa_ok($patron, 'Fieldmapper::actor::user', 'Patron');
157
158 # Patron should have no penalties.
159 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
160 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
161 is(patron_sip_too_many_overdue_test($patron->id()), 0, 'SIP says patron does not have too many overdues');
162 is(patron_sip_excessive_fines_test($patron->id()), 0, 'SIP says patron does not have excessive fines');
163
164 # Add STAFF_CHR penalty to the patron
165 my $penalty = apply_penalty_to_patron($staff, $patron, 25);
166 ok(ref $penalty, 'Added STAFF_CHR penalty to patron');
167 is(patron_sip_test($patron->id()), 1, 'SIP says patron has one penalty');
168
169 # Add PATRON_EXCEEDS_FINES penalty to the patron
170 my $fines_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_FINES);
171 ok(ref $fines_penalty, 'Added PATRON_EXCEEDS_FINES penalty to patron');
172 is(patron_sip_test($patron->id()), 2, 'SIP says patron has two penalties');
173 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
174
175 # Add PATRON_EXCEEDS_OVERDUE_COUNT penalty to the patron
176 my $overdues_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
177 ok(ref $overdues_penalty, 'Added PATRON_EXCEEDS_OVERDUE_COUNT penalty to patron');
178 is(patron_sip_test($patron->id()), 3, 'SIP says patron has three penalties');
179 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
180 is(patron_sip_too_many_overdue_test($patron->id()), 1, 'SIP says patron has too many overdues');
181
182 # We remove the penalties from our test patron.
183 my $r = remove_penalty_from_patron($penalty);
184 ok( ! ref $r, 'STAFF_CHR removed from patron');
185 my $r_fines = remove_penalty_from_patron($fines_penalty);
186 ok( ! ref $r_fines, 'PATRON_EXCEEDS_FINES removed from patron');
187 my $r_overdues = remove_penalty_from_patron($overdues_penalty);
188 ok( ! ref $r_overdues, 'PATRON_EXCEEDS_OVERDUE_COUNT removed from patron');
189
190 $script->logout();