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 sub retrieve_penalty {
24 my $csp = $e->retrieve_config_standing_penalty($penalty);
28 sub retrieve_user_by_barcode {
30 return $apputils->simplereq(
32 'open-ils.actor.user.fleshed.retrieve_by_barcode',
38 sub apply_penalty_to_patron {
39 my ($staff, $patron, $penalty_id) = @_;
40 my $penalty = Fieldmapper::actor::user_standing_penalty->new();
41 $penalty->standing_penalty($penalty_id);
42 $penalty->usr($patron->id());
43 $penalty->set_date('now');
44 $penalty->staff($staff->id());
45 $penalty->org_unit(1); # Consortium-wide.
46 $penalty->note('LP 1592891 SIP standing penalties test');
47 my $r = $apputils->simplereq(
49 'open-ils.actor.user.penalty.apply',
61 sub remove_penalty_from_patron {
63 return $apputils->simplereq(
65 'open-ils.actor.user.penalty.remove',
72 my $patron_id = shift;
73 my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
74 return scalar(@{$patron->{user}->standing_penalties()});
77 sub patron_sip_too_many_overdue_test {
78 my $patron_id = shift;
79 my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
81 eval { $rv = $patron->too_many_overdue };
83 diag('$patron->too_many_overdue crashed: ' . $@);
90 sub patron_sip_excessive_fines_test {
91 my $patron_id = shift;
92 my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
94 eval { $rv = $patron->excessive_fines };
96 diag('$patron->excessive_fines crashed: ' . $@);
103 # In concerto, we need to register a workstation.
104 $script->authenticate({
106 password => 'demo123',
109 ok($script->authtoken, 'Initial Login');
112 my $ws = $script->find_workstation(WORKSTATION_NAME, WORKSTATION_LIB);
113 skip 'Workstation exists', 1 if ($ws);
114 $ws = $script->register_workstation(WORKSTATION_NAME, WORKSTATION_LIB) unless ($ws);
115 ok(! ref $ws, 'Registered a new workstation');
119 $script->authenticate({
121 password => 'demo123',
123 workstation => WORKSTATION_NAME
125 ok($script->authtoken, 'Login with workstaion');
127 # Get a CStoreEditor for later use.
128 my $editor = $script->editor(authtoken=>$script->authtoken);
129 my $staff = $editor->checkauth();
130 ok(ref($staff), 'Got a staff user');
132 # We retrieve STAFF_CHR penalty and check that it has an undefined
134 my $staff_chr = retrieve_penalty($editor, 25);
135 isa_ok($staff_chr, 'Fieldmapper::config::standing_penalty', 'STAFF_CHR');
136 is($staff_chr->name, 'STAFF_CHR', 'Penalty name is STAFF_CHR');
137 is($staff_chr->ignore_proximity, undef, 'STAFF_CHR ignore_proximity is undefined');
139 # We retrieve OILS_PENALTY_PATRON_EXCEEDS_FINES penalty and check that it has an undefined
141 my $csp_fines = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_FINES);
142 isa_ok($csp_fines, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_FINES');
143 is($csp_fines->name, 'PATRON_EXCEEDS_FINES', 'Penalty name is PATRON_EXCEEDS_FINES');
144 is($csp_fines->ignore_proximity, undef, 'PATRON_EXCEEDS_FINES ignore_proximity is undefined');
146 # We retrieve STAFF_CHR penalty and check that it has an undefined
148 my $csp_overdues = retrieve_penalty($editor, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
149 isa_ok($csp_overdues, 'Fieldmapper::config::standing_penalty', 'PATRON_EXCEEDS_OVERDUE_COUNT');
150 is($csp_overdues->name, 'PATRON_EXCEEDS_OVERDUE_COUNT', 'Penalty name is PATRON_EXCEEDS_OVERDUE_COUNT');
151 is($csp_overdues->ignore_proximity, undef, 'PATRON_EXCEEDS_OVERDUE_COUNT ignore_proximity is undefined');
153 # We need a patron with no penalties to test holds and circulation.
154 my $patron = retrieve_user_by_barcode("99999350419");
155 isa_ok($patron, 'Fieldmapper::actor::user', 'Patron');
157 # Patron should have no penalties.
158 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
159 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
160 is(patron_sip_too_many_overdue_test($patron->id()), 0, 'SIP says patron does not have too many overdues');
161 is(patron_sip_excessive_fines_test($patron->id()), 0, 'SIP says patron does not have excessive fines');
163 # Add STAFF_CHR penalty to the patron
164 my $penalty = apply_penalty_to_patron($staff, $patron, 25);
165 ok(ref $penalty, 'Added STAFF_CHR penalty to patron');
166 is(patron_sip_test($patron->id()), 1, 'SIP says patron has one penalty');
168 # Add PATRON_EXCEEDS_FINES penalty to the patron
169 my $fines_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_FINES);
170 ok(ref $fines_penalty, 'Added PATRON_EXCEEDS_FINES penalty to patron');
171 is(patron_sip_test($patron->id()), 2, 'SIP says patron has two penalties');
172 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
174 # Add PATRON_EXCEEDS_OVERDUE_COUNT penalty to the patron
175 my $overdues_penalty = apply_penalty_to_patron($staff, $patron, OILS_PENALTY_PATRON_EXCEEDS_OVERDUE_COUNT);
176 ok(ref $overdues_penalty, 'Added PATRON_EXCEEDS_OVERDUE_COUNT penalty to patron');
177 is(patron_sip_test($patron->id()), 3, 'SIP says patron has three penalties');
178 is(patron_sip_excessive_fines_test($patron->id()), 1, 'SIP says patron has excessive fines');
179 is(patron_sip_too_many_overdue_test($patron->id()), 1, 'SIP says patron has too many overdues');
181 # We remove the penalties from our test patron.
182 my $r = remove_penalty_from_patron($penalty);
183 ok( ! ref $r, 'STAFF_CHR removed from patron');
184 my $r_fines = remove_penalty_from_patron($fines_penalty);
185 ok( ! ref $r_fines, 'PATRON_EXCEEDS_FINES removed from patron');
186 my $r_overdues = remove_penalty_from_patron($overdues_penalty);
187 ok( ! ref $r_overdues, 'PATRON_EXCEEDS_OVERDUE_COUNT removed from patron');