]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/14-lp1499123_csp_ignore_proximity.t
LP#1796945 Reporter cloning and creation fixes
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 14-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 sub retrieve_staff_chr {
18     my $e = shift;
19     my $staff_chr = $e->retrieve_config_standing_penalty(25);
20     return $staff_chr;
21 }
22
23 sub update_staff_chr {
24     my $e = shift;
25     my $penalty = shift;
26     $e->xact_begin;
27     my $r = $e->update_config_standing_penalty($penalty) || $e->event();
28     if (ref($r)) {
29         $e->rollback();
30     } else {
31         $e->commit;
32     }
33     return $r;
34 }
35
36 sub retrieve_user_by_barcode {
37     my $barcode = shift;
38     return $apputils->simplereq(
39         'open-ils.actor',
40         'open-ils.actor.user.fleshed.retrieve_by_barcode',
41         $script->authtoken,
42         $barcode
43     );
44 }
45
46 sub retrieve_copy_by_barcode {
47     my $editor = shift;
48     my $barcode = shift;
49     my $r = $editor->search_asset_copy({barcode => $barcode});
50     if (ref($r) eq 'ARRAY' && @$r) {
51         return $r->[0];
52     }
53     return undef;
54 }
55
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(
66         'open-ils.actor',
67         'open-ils.actor.user.penalty.apply',
68         $script->authtoken,
69         $penalty
70     );
71     if (ref($r)) {
72         undef($penalty);
73     } else {
74         $penalty->id($r);
75     }
76     return $penalty;
77 }
78
79 sub remove_staff_chr_from_patron {
80     my $penalty = shift;
81     return $apputils->simplereq(
82         'open-ils.actor',
83         'open-ils.actor.user.penalty.remove',
84         $script->authtoken,
85         $penalty
86     );
87 }
88
89 sub checkout_permit_test {
90     my $patron = shift;
91     my $copy_barcode = shift;
92     my $r = $apputils->simplereq(
93         'open-ils.circ',
94         'open-ils.circ.checkout.permit',
95         $script->authtoken,
96         {
97             patron => $patron->id(),
98             barcode => $copy_barcode
99         }
100     );
101     if (ref($r) eq 'HASH' && $r->{textcode} eq 'SUCCESS') {
102         return 1;
103     }
104     return 0;
105 }
106
107 sub copy_hold_permit_test {
108     my $editor = shift;
109     my $patron = shift;
110     my $copy_barcode = shift;
111     my $copy = retrieve_copy_by_barcode($editor, $copy_barcode);
112     if ($copy) {
113         my $r = $apputils->simplereq(
114             'open-ils.circ',
115             'open-ils.circ.title_hold.is_possible',
116             $script->authtoken,
117             {
118                 patronid => $patron->id(),
119                 pickup_lib => 4,
120                 copy_id => $copy->id(),
121                 hold_type => 'C'
122             }
123         );
124         if (ref($r) && defined $r->{success}) {
125             return $r->{success};
126         }
127     }
128     return undef;
129 }
130
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()});
135 }
136
137 # In concerto, we need to register a workstation.
138 $script->authenticate({
139     username => 'admin',
140     password => 'demo123',
141     type => 'staff',
142 });
143 ok($script->authtoken, 'Initial Login');
144
145 SKIP: {
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');
150 }
151
152 $script->logout();
153 $script->authenticate({
154     username => 'admin',
155     password => 'demo123',
156     type => 'staff',
157     workstation => WORKSTATION_NAME
158 });
159 ok($script->authtoken, 'Login with workstaion');
160
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');
165
166 # We retrieve STAFF_CHR block and check that it has an undefined
167 # ignore_proximity.
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');
172
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');
176
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');
180
181 # Patron should have no penalties.
182 ok(! scalar(@{$patron->standing_penalties()}), 'Patron has no penalties');
183
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');
188
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');
193
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');
197
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');
200
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');
205
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');
210
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');
214
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');
217
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');
221
222 # Do the checks again, all should pass.
223 is(patron_sip_test($patron->id()), 0, 'SIP says patron has no penalties');
224
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');
229
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');
233
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');
236
237 $script->logout();