]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/18-lp1592891_sip_standing_penalties.t
bf695bce55a75695d5828a8314b3c5d04600eb46
[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     my $rv;
96     eval { $rv = $patron->too_many_overdue };
97     if ($@) {
98         diag('$patron->too_many_overdue crashed: ' . $@);
99         return;
100     } else {
101         return $rv;
102     }
103 }
104
105 sub patron_sip_excessive_fines_test {
106     my $patron_id = shift;
107     my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
108     my $rv;
109     eval { $rv = $patron->excessive_fines };
110     if ($@) {
111         diag('$patron->excessive_fines crashed: ' . $@);
112         return;
113     } else {
114         return $rv;
115     }
116 }
117
118 # In concerto, we need to register a workstation.
119 $script->authenticate({
120     username => 'admin',
121     password => 'demo123',
122     type => 'staff',
123 });
124 ok($script->authtoken, 'Initial Login');
125
126 SKIP: {
127     my $ws = find_workstation();
128     skip 'Workstation exists', 1 if ($ws);
129     $ws = $script->register_workstation(WORKSTATION_NAME, WORKSTATION_LIB) unless ($ws);
130     ok(! ref $ws, 'Registered a new workstation');
131 }
132
133 $script->logout();
134 $script->authenticate({
135     username => 'admin',
136     password => 'demo123',
137     type => 'staff',
138     workstation => WORKSTATION_NAME
139 });
140 ok($script->authtoken, 'Login with workstaion');
141
142 # Get a CStoreEditor for later use.
143 my $editor = $script->editor(authtoken=>$script->authtoken);
144 my $staff = $editor->checkauth();
145 ok(ref($staff), 'Got a staff user');
146
147 # We retrieve STAFF_CHR penalty and check that it has an undefined
148 # ignore_proximity.
149 my $staff_chr = retrieve_penalty($editor, 25);
150 isa_ok($staff_chr, 'Fieldmapper::config::standing_penalty', 'STAFF_CHR');
151 is($staff_chr->name, 'STAFF_CHR', 'Penalty name is STAFF_CHR');
152 is($staff_chr->ignore_proximity, undef, 'STAFF_CHR ignore_proximity is undefined');
153
154 # We retrieve OILS_PENALTY_PATRON_EXCEEDS_FINES penalty and check that it has an undefined
155 # ignore_proximity.
156 my $csp_fines = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_FINES);
157 isa_ok($csp_fines, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_FINES');
158 is($csp_fines->name, 'PATRON_EXCEEDS_FINES', 'Penalty name is PATRON_EXCEEDS_FINES');
159 is($csp_fines->ignore_proximity, undef, 'PATRON_EXCEEDS_FINES ignore_proximity is undefined');
160
161 # We retrieve STAFF_CHR penalty and check that it has an undefined
162 # ignore_proximity.
163 my $csp_overdues = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
164 isa_ok($csp_overdues, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_OVERDUE_COUNT');
165 is($csp_overdues->name, 'PATRON_EXCEEDS_OVERDUE_COUNT', 'Penalty name is PATRON_EXCEEDS_OVERDUE_COUNT');
166 is($csp_overdues->ignore_proximity, undef, 'PATRON_EXCEEDS_OVERDUE_COUNT ignore_proximity is undefined');
167
168 # We need a patron with no penalties to test holds and circulation.
169 my $patron = retrieve_user_by_barcode("99999350419");
170 isa_ok($patron, 'Fieldmapper::actor::user', 'Patron');
171
172 # Patron should have no penalties.
173 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
174 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
175 is(patron_sip_too_many_overdue_test($patron->id()), 0, 'SIP says patron does not have too many overdues');
176 is(patron_sip_excessive_fines_test($patron->id()), 0, 'SIP says patron does not have excessive fines');
177
178 # Add STAFF_CHR penalty to the patron
179 my $penalty = apply_penalty_to_patron($staff, $patron, 25);
180 ok(ref $penalty, 'Added STAFF_CHR penalty to patron');
181 is(patron_sip_test($patron->id()), 1, 'SIP says patron has one penalty');
182
183 # Add PATRON_EXCEEDS_FINES penalty to the patron
184 my $fines_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_FINES);
185 ok(ref $fines_penalty, 'Added PATRON_EXCEEDS_FINES penalty to patron');
186 is(patron_sip_test($patron->id()), 2, 'SIP says patron has two penalties');
187 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
188
189 # Add PATRON_EXCEEDS_OVERDUE_COUNT penalty to the patron
190 my $overdues_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
191 ok(ref $overdues_penalty, 'Added PATRON_EXCEEDS_OVERDUE_COUNT penalty to patron');
192 is(patron_sip_test($patron->id()), 3, 'SIP says patron has three penalties');
193 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
194 is(patron_sip_too_many_overdue_test($patron->id()), 1, 'SIP says patron has too many overdues');
195
196 # We remove the penalties from our test patron.
197 my $r = remove_penalty_from_patron($penalty);
198 ok( ! ref $r, 'STAFF_CHR removed from patron');
199 my $r_fines = remove_penalty_from_patron($fines_penalty);
200 ok( ! ref $r_fines, 'PATRON_EXCEEDS_FINES removed from patron');
201 my $r_overdues = remove_penalty_from_patron($overdues_penalty);
202 ok( ! ref $r_overdues, 'PATRON_EXCEEDS_OVERDUE_COUNT removed from patron');
203
204 $script->logout();