]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/18-lp1592891_sip_standing_penalties.t
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 18-lp1592891_sip_standing_penalties.t
1 #!perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 30;
6
7 # This test includes much code copied or adapted from Jason Stephenson's tests
8 # in 14-lp1499123_csp_ignore_proximity.t
9
10 diag("Test bugfix for lp1592891 - SIP2 failures with standing penalties.");
11
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';
17
18 use constant WORKSTATION_NAME => 'BR1-test-lp1592891_sip_standing_penalties.t';
19 use constant WORKSTATION_LIB => 4;
20
21 sub retrieve_penalty {
22     my $e = shift;
23     my $penalty = shift;
24     my $csp = $e->retrieve_config_standing_penalty($penalty);
25     return $csp;
26 }
27
28 sub retrieve_user_by_barcode {
29     my $barcode = shift;
30     return $apputils->simplereq(
31         'open-ils.actor',
32         'open-ils.actor.user.fleshed.retrieve_by_barcode',
33         $script->authtoken,
34         $barcode
35     );
36 }
37
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(
48         'open-ils.actor',
49         'open-ils.actor.user.penalty.apply',
50         $script->authtoken,
51         $penalty
52     );
53     if (ref($r)) {
54         undef($penalty);
55     } else {
56         $penalty->id($r);
57     }
58     return $penalty;
59 }
60
61 sub remove_penalty_from_patron {
62     my $penalty = shift;
63     return $apputils->simplereq(
64         'open-ils.actor',
65         'open-ils.actor.user.penalty.remove',
66         $script->authtoken,
67         $penalty
68     );
69 }
70
71 sub patron_sip_test {
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()});
75 }
76
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);
80     my $rv;
81     eval { $rv = $patron->too_many_overdue };
82     if ($@) {
83         diag('$patron->too_many_overdue crashed: ' . $@);
84         return;
85     } else {
86         return $rv;
87     }
88 }
89
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);
93     my $rv;
94     eval { $rv = $patron->excessive_fines };
95     if ($@) {
96         diag('$patron->excessive_fines crashed: ' . $@);
97         return;
98     } else {
99         return $rv;
100     }
101 }
102
103 # In concerto, we need to register a workstation.
104 $script->authenticate({
105     username => 'admin',
106     password => 'demo123',
107     type => 'staff',
108 });
109 ok($script->authtoken, 'Initial Login');
110
111 SKIP: {
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');
116 }
117
118 $script->logout();
119 $script->authenticate({
120     username => 'admin',
121     password => 'demo123',
122     type => 'staff',
123     workstation => WORKSTATION_NAME
124 });
125 ok($script->authtoken, 'Login with workstaion');
126
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');
131
132 # We retrieve STAFF_CHR penalty and check that it has an undefined
133 # ignore_proximity.
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');
138
139 # We retrieve OILS_PENALTY_PATRON_EXCEEDS_FINES penalty and check that it has an undefined
140 # ignore_proximity.
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');
145
146 # We retrieve STAFF_CHR penalty and check that it has an undefined
147 # ignore_proximity.
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');
152
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');
156
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');
162
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');
167
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');
173
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');
180
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');
188
189 $script->logout();