]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/12-lp1499123_csp_ignore_proximity.t
Docs: Updated attributions.txt
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 12-lp1499123_csp_ignore_proximity.t
1 #!perl
2 use strict; use warnings;
3
4 use Test::More tests => 28;
5 use Data::Dumper;
6
7 diag("Test config.standing_penalty.ignore_proximity feature.");
8
9 use OpenILS::Utils::TestUtils;
10 use OpenILS::SIP::Patron;
11 my $script = OpenILS::Utils::TestUtils->new();
12 our $apputils = 'OpenILS::Application::AppUtils';
13
14 use constant WORKSTATION_NAME => 'BR1-test-lp1499123_csp_ignore_proximity.t';
15 use constant WORKSTATION_LIB => 4;
16
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(
21         'open-ils.actor',
22         'open-ils.actor.workstation.list',
23         $script->authtoken,
24         WORKSTATION_LIB
25     );
26     if ($r->{&WORKSTATION_LIB}) {
27         return scalar(grep {$_->name() eq WORKSTATION_NAME} @{$r->{&WORKSTATION_LIB}});
28     }
29     return 0;
30 }
31
32 sub retrieve_staff_chr {
33     my $e = shift;
34     my $staff_chr = $e->retrieve_config_standing_penalty(25);
35     return $staff_chr;
36 }
37
38 sub update_staff_chr {
39     my $e = shift;
40     my $penalty = shift;
41     $e->xact_begin;
42     my $r = $e->update_config_standing_penalty($penalty) || $e->event();
43     if (ref($r)) {
44         $e->rollback();
45     } else {
46         $e->commit;
47     }
48     return $r;
49 }
50
51 sub retrieve_user_by_barcode {
52     my $barcode = shift;
53     return $apputils->simplereq(
54         'open-ils.actor',
55         'open-ils.actor.user.fleshed.retrieve_by_barcode',
56         $script->authtoken,
57         $barcode
58     );
59 }
60
61 sub retrieve_copy_by_barcode {
62     my $editor = shift;
63     my $barcode = shift;
64     my $r = $editor->search_asset_copy({barcode => $barcode});
65     if (ref($r) eq 'ARRAY' && @$r) {
66         return $r->[0];
67     }
68     return undef;
69 }
70
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(
81         'open-ils.actor',
82         'open-ils.actor.user.penalty.apply',
83         $script->authtoken,
84         $penalty
85     );
86     if (ref($r)) {
87         undef($penalty);
88     } else {
89         $penalty->id($r);
90     }
91     return $penalty;
92 }
93
94 sub remove_staff_chr_from_patron {
95     my $penalty = shift;
96     return $apputils->simplereq(
97         'open-ils.actor',
98         'open-ils.actor.user.penalty.remove',
99         $script->authtoken,
100         $penalty
101     );
102 }
103
104 sub checkout_permit_test {
105     my $patron = shift;
106     my $copy_barcode = shift;
107     my $r = $apputils->simplereq(
108         'open-ils.circ',
109         'open-ils.circ.checkout.permit',
110         $script->authtoken,
111         {
112             patron => $patron->id(),
113             barcode => $copy_barcode
114         }
115     );
116     if (ref($r) eq 'HASH' && $r->{textcode} eq 'SUCCESS') {
117         return 1;
118     }
119     return 0;
120 }
121
122 sub copy_hold_permit_test {
123     my $editor = shift;
124     my $patron = shift;
125     my $copy_barcode = shift;
126     my $copy = retrieve_copy_by_barcode($editor, $copy_barcode);
127     if ($copy) {
128         my $r = $apputils->simplereq(
129             'open-ils.circ',
130             'open-ils.circ.title_hold.is_possible',
131             $script->authtoken,
132             {
133                 patronid => $patron->id(),
134                 pickup_lib => 4,
135                 copy_id => $copy->id(),
136                 hold_type => 'C'
137             }
138         );
139         if (ref($r) && defined $r->{success}) {
140             return $r->{success};
141         }
142     }
143     return undef;
144 }
145
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()});
150 }
151
152 # In concerto, we need to register a workstation.
153 $script->authenticate({
154     username => 'admin',
155     password => 'demo123',
156     type => 'staff',
157 });
158 ok($script->authtoken, 'Initial Login');
159
160 SKIP: {
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');
165 }
166
167 $script->logout();
168 $script->authenticate({
169     username => 'admin',
170     password => 'demo123',
171     type => 'staff',
172     workstation => WORKSTATION_NAME
173 });
174 ok($script->authtoken, 'Login with workstaion');
175
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');
180
181 # We retrieve STAFF_CHR block and check that it has an undefined
182 # ignore_proximity.
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');
187
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');
191
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');
195
196 # Patron should have no penalties.
197 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
198
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');
203
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');
208
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');
212
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');
215
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');
220
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');
225
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');
229
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');
232
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');
236
237 # Do the checks again, all should pass.
238 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
239
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');
244
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');
248
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');
251
252 $script->logout();