5 use Test::More tests => 30;
7 # This test includes much code copied or adapted from Jason Stephenson's tests
8 # in 14-lp1499123_csp_ignore_proximity.t
10 diag("Test bugfix for lp1592891 - SIP2 failures with standing penalties.");
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';
18 use constant WORKSTATION_NAME => 'BR1-test-lp1592891_sip_standing_penalties.t';
19 use constant WORKSTATION_LIB => 4;
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(
26 'open-ils.actor.workstation.list',
30 if ($r->{&WORKSTATION_LIB}) {
31 return scalar(grep {$_->name() eq WORKSTATION_NAME} @{$r->{&WORKSTATION_LIB}});
36 sub retrieve_penalty {
39 my $csp = $e->retrieve_config_standing_penalty($penalty);
43 sub retrieve_user_by_barcode {
45 return $apputils->simplereq(
47 'open-ils.actor.user.fleshed.retrieve_by_barcode',
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(
64 'open-ils.actor.user.penalty.apply',
76 sub remove_penalty_from_patron {
78 return $apputils->simplereq(
80 'open-ils.actor.user.penalty.remove',
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()});
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);
96 eval { $rv = $patron->too_many_overdue };
98 diag('$patron->too_many_overdue crashed: ' . $@);
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);
109 eval { $rv = $patron->excessive_fines };
111 diag('$patron->excessive_fines crashed: ' . $@);
118 # In concerto, we need to register a workstation.
119 $script->authenticate({
121 password => 'demo123',
124 ok($script->authtoken, 'Initial Login');
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');
134 $script->authenticate({
136 password => 'demo123',
138 workstation => WORKSTATION_NAME
140 ok($script->authtoken, 'Login with workstaion');
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');
147 # We retrieve STAFF_CHR penalty and check that it has an undefined
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');
154 # We retrieve OILS_PENALTY_PATRON_EXCEEDS_FINES penalty and check that it has an undefined
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');
161 # We retrieve STAFF_CHR penalty and check that it has an undefined
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');
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');
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');
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');
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');
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');
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');