2 use strict; use warnings;
4 use Test::More tests => 28;
7 diag("Test config.standing_penalty.ignore_proximity feature.");
9 use OpenILS::Utils::TestUtils;
10 use OpenILS::SIP::Patron;
11 my $script = OpenILS::Utils::TestUtils->new();
12 our $apputils = 'OpenILS::Application::AppUtils';
14 use constant WORKSTATION_NAME => 'BR1-test-lp1499123_csp_ignore_proximity.t';
15 use constant WORKSTATION_LIB => 4;
17 sub retrieve_staff_chr {
19 my $staff_chr = $e->retrieve_config_standing_penalty(25);
23 sub update_staff_chr {
27 my $r = $e->update_config_standing_penalty($penalty) || $e->event();
36 sub retrieve_user_by_barcode {
38 return $apputils->simplereq(
40 'open-ils.actor.user.fleshed.retrieve_by_barcode',
46 sub retrieve_copy_by_barcode {
49 my $r = $editor->search_asset_copy({barcode => $barcode});
50 if (ref($r) eq 'ARRAY' && @$r) {
56 sub apply_staff_chr_to_patron {
57 my ($staff, $patron) = @_;
58 my $penalty = Fieldmapper::actor::user_standing_penalty->new();
59 $penalty->standing_penalty(25);
60 $penalty->usr($patron->id());
61 $penalty->set_date('now');
62 $penalty->staff($staff->id());
63 $penalty->org_unit(1); # Consortium-wide.
64 $penalty->note('LP 1499123 csp.ignore_proximity test');
65 my $r = $apputils->simplereq(
67 'open-ils.actor.user.penalty.apply',
79 sub remove_staff_chr_from_patron {
81 return $apputils->simplereq(
83 'open-ils.actor.user.penalty.remove',
89 sub checkout_permit_test {
91 my $copy_barcode = shift;
92 my $r = $apputils->simplereq(
94 'open-ils.circ.checkout.permit',
97 patron => $patron->id(),
98 barcode => $copy_barcode
101 if (ref($r) eq 'HASH' && $r->{textcode} eq 'SUCCESS') {
107 sub copy_hold_permit_test {
110 my $copy_barcode = shift;
111 my $copy = retrieve_copy_by_barcode($editor, $copy_barcode);
113 my $r = $apputils->simplereq(
115 'open-ils.circ.title_hold.is_possible',
118 patronid => $patron->id(),
120 copy_id => $copy->id(),
124 if (ref($r) && defined $r->{success}) {
125 return $r->{success};
131 sub patron_sip_test {
132 my $patron_id = shift;
133 my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
134 return scalar(@{$patron->{user}->standing_penalties()});
137 # In concerto, we need to register a workstation.
138 $script->authenticate({
140 password => 'demo123',
143 ok($script->authtoken, 'Initial Login');
146 my $ws = $script->find_workstation(WORKSTATION_NAME, WORKSTATION_LIB);
147 skip 'Workstation exists', 1 if ($ws);
148 $ws = $script->register_workstation(WORKSTATION_NAME, WORKSTATION_LIB) unless ($ws);
149 ok(! ref $ws, 'Registered a new workstation');
153 $script->authenticate({
155 password => 'demo123',
157 workstation => WORKSTATION_NAME
159 ok($script->authtoken, 'Login with workstaion');
161 # Get a CStoreEditor for later use.
162 my $editor = $script->editor(authtoken=>$script->authtoken);
163 my $staff = $editor->checkauth();
164 ok(ref($staff), 'Got a staff user');
166 # We retrieve STAFF_CHR block and check that it has an undefined
168 my $staff_chr = retrieve_staff_chr($editor);
169 isa_ok($staff_chr, 'Fieldmapper::config::standing_penalty', 'STAFF_CHR');
170 is($staff_chr->name, 'STAFF_CHR', 'Penalty name is STAFF_CHR');
171 is($staff_chr->ignore_proximity, undef, 'STAFF_CHR ignore_proximity is undefined');
173 # We set the ignore_proximity to 0.
174 $staff_chr->ignore_proximity(0);
175 ok(! ref update_staff_chr($editor, $staff_chr), 'Update of STAFF_CHR');
177 # We need a patron with no penalties to test holds and circulation.
178 my $patron = retrieve_user_by_barcode("99999350419");
179 isa_ok($patron, 'Fieldmapper::actor::user', 'Patron');
181 # Patron should have no penalties.
182 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
184 # Add the STAFF_CHR to the patron
185 my $penalty = apply_staff_chr_to_patron($staff, $patron);
186 ok(ref $penalty, 'Added STAFF_CHR to patron');
187 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
189 # See if we can place a hold on a copy owned by BR1.
190 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 1, 'Can place hold on copy from BR1');
191 # We should not be able to place a hold on a copy owned by a different branch.
192 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 0, 'Cannot place hold on copy from BR2');
194 # See if we can check out a copy owned by branch 4 out to the patron.
195 # This should succeed.
196 ok(checkout_permit_test($patron, "CONC4300036"), 'Can checkout copy from BR1');
198 # We should not be able to checkout a copy owned by a different branch.
199 ok(!checkout_permit_test($patron, "CONC51000636"), 'Cannot checkout copy from BR2');
201 # We reset the ignore_proximity of STAFF_CHR.
202 $staff_chr->clear_ignore_proximity();
203 ok(! ref update_staff_chr($editor, $staff_chr), 'Reset of STAFF_CHR');
204 is(patron_sip_test($patron->id()), 1, 'SIP says patron has one penalty');
206 # See if we can place a hold on a copy owned by BR1.
207 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 0, 'Cannot place hold on copy from BR1');
208 # We should not be able to place a hold on a copy owned by a different branch.
209 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 0, 'Cannot place hold on copy from BR2');
211 # See if we can check out a copy owned by branch 4 out to the patron.
212 # This should succeed.
213 ok(!checkout_permit_test($patron, "CONC4300036"), 'Cannot checkout copy from BR1');
215 # We should not be able to checkout a copy owned by a different branch.
216 ok(!checkout_permit_test($patron, "CONC51000636"), 'Cannot checkout copy from BR2');
218 # We remove the STAFF_CHR from our test patron.
219 my $r = remove_staff_chr_from_patron($penalty);
220 ok( ! ref $r, 'STAFF_CHR removed from patron');
222 # Do the checks again, all should pass.
223 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
225 # See if we can place a hold on a copy owned by BR1.
226 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 1, 'Can place hold on copy from BR1');
227 # We should now be able to place a hold on a copy owned by a different branch.
228 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 1, 'Can place hold on copy from BR2');
230 # See if we can check out a copy owned by branch 4 out to the patron.
231 # This should succeed.
232 ok(checkout_permit_test($patron, "CONC4300036"), 'Can checkout copy from BR1');
234 # We should not be able to checkout a copy owned by a different branch.
235 ok(checkout_permit_test($patron, "CONC51000636"), 'Can checkout copy from BR2');