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 # Because this may run multiple times, without a DB reload, we search
18 # for the workstation before registering it.
19 sub find_workstation {
20 my $r = $apputils->simplereq(
22 'open-ils.actor.workstation.list',
26 if ($r->{&WORKSTATION_LIB}) {
27 return scalar(grep {$_->name() eq WORKSTATION_NAME} @{$r->{&WORKSTATION_LIB}});
32 sub retrieve_staff_chr {
34 my $staff_chr = $e->retrieve_config_standing_penalty(25);
38 sub update_staff_chr {
42 my $r = $e->update_config_standing_penalty($penalty) || $e->event();
51 sub retrieve_user_by_barcode {
53 return $apputils->simplereq(
55 'open-ils.actor.user.fleshed.retrieve_by_barcode',
61 sub retrieve_copy_by_barcode {
64 my $r = $editor->search_asset_copy({barcode => $barcode});
65 if (ref($r) eq 'ARRAY' && @$r) {
71 sub apply_staff_chr_to_patron {
72 my ($staff, $patron) = @_;
73 my $penalty = Fieldmapper::actor::user_standing_penalty->new();
74 $penalty->standing_penalty(25);
75 $penalty->usr($patron->id());
76 $penalty->set_date('now');
77 $penalty->staff($staff->id());
78 $penalty->org_unit(1); # Consortium-wide.
79 $penalty->note('LP 1499123 csp.ignore_proximity test');
80 my $r = $apputils->simplereq(
82 'open-ils.actor.user.penalty.apply',
94 sub remove_staff_chr_from_patron {
96 return $apputils->simplereq(
98 'open-ils.actor.user.penalty.remove',
104 sub checkout_permit_test {
106 my $copy_barcode = shift;
107 my $r = $apputils->simplereq(
109 'open-ils.circ.checkout.permit',
112 patron => $patron->id(),
113 barcode => $copy_barcode
116 if (ref($r) eq 'HASH' && $r->{textcode} eq 'SUCCESS') {
122 sub copy_hold_permit_test {
125 my $copy_barcode = shift;
126 my $copy = retrieve_copy_by_barcode($editor, $copy_barcode);
128 my $r = $apputils->simplereq(
130 'open-ils.circ.title_hold.is_possible',
133 patronid => $patron->id(),
135 copy_id => $copy->id(),
139 if (ref($r) && defined $r->{success}) {
140 return $r->{success};
146 sub patron_sip_test {
147 my $patron_id = shift;
148 my $patron = OpenILS::SIP::Patron->new(usr => $patron_id, authtoken => $script->authtoken);
149 return scalar(@{$patron->{user}->standing_penalties()});
152 # In concerto, we need to register a workstation.
153 $script->authenticate({
155 password => 'demo123',
158 ok($script->authtoken, 'Initial Login');
161 my $ws = find_workstation();
162 skip 'Workstation exists', 1 if ($ws);
163 $ws = $script->register_workstation(WORKSTATION_NAME, WORKSTATION_LIB) unless ($ws);
164 ok(! ref $ws, 'Registered a new workstation');
168 $script->authenticate({
170 password => 'demo123',
172 workstation => WORKSTATION_NAME
174 ok($script->authtoken, 'Login with workstaion');
176 # Get a CStoreEditor for later use.
177 my $editor = $script->editor(authtoken=>$script->authtoken);
178 my $staff = $editor->checkauth();
179 ok(ref($staff), 'Got a staff user');
181 # We retrieve STAFF_CHR block and check that it has an undefined
183 my $staff_chr = retrieve_staff_chr($editor);
184 isa_ok($staff_chr, 'Fieldmapper::config::standing_penalty', 'STAFF_CHR');
185 is($staff_chr->name, 'STAFF_CHR', 'Penalty name is STAFF_CHR');
186 is($staff_chr->ignore_proximity, undef, 'STAFF_CHR ignore_proximity is undefined');
188 # We set the ignore_proximity to 0.
189 $staff_chr->ignore_proximity(0);
190 ok(! ref update_staff_chr($editor, $staff_chr), 'Update of STAFF_CHR');
192 # We need a patron with no penalties to test holds and circulation.
193 my $patron = retrieve_user_by_barcode("99999350419");
194 isa_ok($patron, 'Fieldmapper::actor::user', 'Patron');
196 # Patron should have no penalties.
197 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
199 # Add the STAFF_CHR to the patron
200 my $penalty = apply_staff_chr_to_patron($staff, $patron);
201 ok(ref $penalty, 'Added STAFF_CHR to patron');
202 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
204 # See if we can place a hold on a copy owned by BR1.
205 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 1, 'Can place hold on copy from BR1');
206 # We should not be able to place a hold on a copy owned by a different branch.
207 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 0, 'Cannot place hold on copy from BR2');
209 # See if we can check out a copy owned by branch 4 out to the patron.
210 # This should succeed.
211 ok(checkout_permit_test($patron, "CONC4300036"), 'Can checkout copy from BR1');
213 # We should not be able to checkout a copy owned by a different branch.
214 ok(!checkout_permit_test($patron, "CONC51000636"), 'Cannot checkout copy from BR2');
216 # We reset the ignore_proximity of STAFF_CHR.
217 $staff_chr->clear_ignore_proximity();
218 ok(! ref update_staff_chr($editor, $staff_chr), 'Reset of STAFF_CHR');
219 is(patron_sip_test($patron->id()), 1, 'SIP says patron has one penalty');
221 # See if we can place a hold on a copy owned by BR1.
222 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 0, 'Cannot place hold on copy from BR1');
223 # We should not be able to place a hold on a copy owned by a different branch.
224 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 0, 'Cannot place hold on copy from BR2');
226 # See if we can check out a copy owned by branch 4 out to the patron.
227 # This should succeed.
228 ok(!checkout_permit_test($patron, "CONC4300036"), 'Cannot checkout copy from BR1');
230 # We should not be able to checkout a copy owned by a different branch.
231 ok(!checkout_permit_test($patron, "CONC51000636"), 'Cannot checkout copy from BR2');
233 # We remove the STAFF_CHR from our test patron.
234 my $r = remove_staff_chr_from_patron($penalty);
235 ok( ! ref $r, 'STAFF_CHR removed from patron');
237 # Do the checks again, all should pass.
238 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
240 # See if we can place a hold on a copy owned by BR1.
241 is(copy_hold_permit_test($editor, $patron, "CONC4300036"), 1, 'Can place hold on copy from BR1');
242 # We should now be able to place a hold on a copy owned by a different branch.
243 is(copy_hold_permit_test($editor, $patron, "CONC51000636"), 1, 'Can place hold on copy from BR2');
245 # See if we can check out a copy owned by branch 4 out to the patron.
246 # This should succeed.
247 ok(checkout_permit_test($patron, "CONC4300036"), 'Can checkout copy from BR1');
249 # We should not be able to checkout a copy owned by a different branch.
250 ok(checkout_permit_test($patron, "CONC51000636"), 'Can checkout copy from BR2');