]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/36-lp1752334-badcontact.t
LP#1752334: (follow-up) adjust to allow "0" as a note
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 36-lp1752334-badcontact.t
1 #!perl
2 use strict; use warnings;
3 use Test::More tests => 80;
4
5 use OpenILS::Utils::TestUtils;
6 use OpenILS::Const qw(:const);
7 use OpenILS::Utils::Fieldmapper;
8 use Data::Dumper;
9
10 my $script = OpenILS::Utils::TestUtils->new();
11 $script->bootstrap();
12
13 my $U = 'OpenILS::Application::AppUtils';
14
15 diag("Test LP 1752334 BadContact");
16
17 use constant {
18     BR1_ID => 4,
19     BR3_ID => 6,
20     PHONE => '218-555-0177',
21     EMAIL => 'nouser@evergreen-ils.test',
22     TESTMESSAGE => '123456 TEST Invalidate Message',
23     TESTMESSAGE_ZERO => '0',
24     PROFILE => 2, #patrons
25 };
26
27 ### Fields that can be invalidated
28 # email
29 # day_phone
30 # evening_phone
31 # other_phone
32
33 my @fields = ('email','day_phone','evening_phone','other_phone');
34 ### Notification data, field index 0 is the penalty type code
35 my %data =(
36     email => [ 31,'nouser1@evergreen-ils.test','nouser2@evergreen-ils.test',
37                'nouser3@evergreen-ils.test','nouser4@example.com',
38                'nouser5@example.test'],
39     day_phone => [32,'218-555-0177','218-555-0129','218-555-0110','218-555-0196','218-555-0181'],
40     evening_phone => [33,'701-555-0130','701-555-0104','701-555-0155','701-555-0156','701-555-0143'],
41     other_phone => [34,'612-555-0111','612-555-0115','612-555-0157','612-555-0162','612-555-0192'],
42 );
43
44 ### Options for invalidation
45 # Additional Note
46 # Penalty Org Unit -- ignore
47 # Notification string - invalidates all occurences of that type of notification.
48
49
50 # We are deliberately NOT using the admin user to check for a perm failure.
51 my $credentials = {
52     username => 'br1mtownsend',
53     password => 'maryt1234',
54     type => 'staff'
55 };
56
57 sub remove_penalty_from_patron {
58     my $penalty = shift;
59
60     #Fetch the ausp object, we have aump
61     my $ausp = $U->simplereq(
62         'open-ils.pcrud',
63         'open-ils.pcrud.search.ausp.atomic',
64         $script->authtoken,
65         {id => $penalty->id() }
66     );
67
68     #Use the ausp to remove the penalty
69     return $U->simplereq(
70         'open-ils.actor',
71         'open-ils.actor.user.penalty.remove',
72         $script->authtoken,
73         $ausp->[0]
74     );
75
76 }
77
78 sub set_notifications {
79     my ($user,$i) = @_;
80     #Set notifications
81     $user->email($data{email}[$i]);
82     $user->day_phone($data{day_phone}[$i]);
83     $user->evening_phone($data{evening_phone}[$i]);
84     $user->other_phone($data{other_phone}[$i]);
85     $user->ischanged(1); ## Has to be included or update won't happen
86
87     my $resp = $U->simplereq(
88         'open-ils.actor',
89         'open-ils.actor.patron.update',
90         $script->authtoken,
91         $user
92     );
93
94     return($resp);
95 }
96
97 sub invalidate {
98     my ($userid,$type,$note,$ou,$search) = @_;
99     my $respi = $U->simplereq(
100         'open-ils.actor',
101         'open-ils.actor.invalidate.'.$type,
102         $script->authtoken,
103         $userid,
104         $note,
105         $ou,
106         $search
107     );
108     return $respi
109 }
110
111 sub invalidate_all {
112     my ($userid,$note,$ou,$search) = @_;
113
114     foreach( @fields ){
115       my $respi = invalidate($userid,$_,$note,$ou, defined($search) ? $data{$_}[$search] : undef);
116       is($respi->{textcode},'SUCCESS',$_.' Invalidation was a success');
117     }
118 }
119
120 sub check_all_penalties {
121     my ($userid,$note,$ou,$search,$i) = @_;
122
123     foreach( @fields ){
124         my $respi = check_penalty($userid,$_,$note,$ou,
125                                   defined($search) ? $data{$_}[$search] : undef,$i);
126
127         
128     }
129 }
130
131 sub check_penalty {
132     my ($userid,$type,$note,$ou,$search,$i) = @_;
133
134     my $code = $data{$type}[0]; #Penalty Type Code
135     my $ausp = $U->simplereq(
136         'open-ils.pcrud',
137         'open-ils.pcrud.search.aump.atomic',
138         $script->authtoken,
139         {usr => $userid, standing_penalty => $code, stop_date => undef },
140         {limit => 1}
141     );
142
143     my $penalty = $ausp->[0];
144     #print ref($penalty)."\n";
145     my $message = $data{$type}[$i].(defined($note) ? ' '.$note : '');
146
147     isa_ok($penalty, 'Fieldmapper::actor::usr_message_penalty', 'User Penalty Found -- '.$type);
148     is($penalty->message(), $message, $type.' penalty note matches expected format.');
149
150     ## Remove penalty
151     ok( ! ref (remove_penalty_from_patron($penalty)), $type.' invalid notification penalty pemoved');
152 }
153
154
155 # Log in as staff.
156 my $authtoken = $script->authenticate($credentials);
157 ok(
158     $authtoken,
159     'Logged in'
160 ) or BAIL_OUT('Must log in');
161
162 # Get a cstore editor for later use.
163 my $editor = $script->editor(authtoken=>$script->authtoken);
164
165
166 # Find a patron to use.
167 my $aus = $U->simplereq(
168     'open-ils.pcrud',
169     'open-ils.pcrud.search.au.atomic',
170     $authtoken,
171     {profile => PROFILE, active => 't', home_ou => BR1_ID },
172     {limit => 5}
173 );
174 ok(@{$aus} == 5, 'Found 5 patrons');
175 my $user = $aus->[0];
176 isa_ok(
177     $user,
178     'Fieldmapper::actor::user',
179     'Found a patron'
180 ) or BAIL_OUT('Patron not found');
181
182
183 my $resp = set_notifications($user,1);
184 isa_ok($resp, 'Fieldmapper::actor::user', 'Notifications added patron 1');
185
186 #print Dumper($resp);
187
188 ## Next user
189 $user = $aus->[1];
190 $resp = set_notifications($user,2);
191 isa_ok($resp, 'Fieldmapper::actor::user', 'Notifications added patron 2');
192
193 ## Users 3,4,5 have the same notifications set
194 $user = $aus->[2];
195 $resp = set_notifications($user,3);
196 isa_ok($resp, 'Fieldmapper::actor::user', 'Notifications added patron 3');
197
198 $user = $aus->[3];
199 $resp = set_notifications($user,3);
200 isa_ok($resp, 'Fieldmapper::actor::user', 'Notifications added patron 4');
201
202 $user = $aus->[4];
203 $resp = set_notifications($user,3);
204 isa_ok($resp, 'Fieldmapper::actor::user', 'Notifications added patron 5');
205
206 #Invalidate all notifications for user 1 - default settings
207 diag("Patron 1 - default invalidate settings");
208 $user = $aus->[0];
209 invalidate_all($user->id(),undef,$user->home_ou(),undef);
210
211 #Invalidate all notifications for user 2 - added note
212 diag("Patron 2 - Added note");
213 $user = $aus->[1];
214 invalidate_all($user->id(),TESTMESSAGE_ZERO,$user->home_ou(),undef);
215
216 #Invalidate notifications for users 3,4,5 - using search method with test message
217 diag("Patron 3,4,5 - Added note - same contact info");
218 $user = $aus->[2];
219 invalidate_all(undef,TESTMESSAGE,$user->home_ou(),3); #Search is index to notification data
220
221
222 ## Check and clear standing penalties
223 diag("Patron 1 - default invalidate settings");
224 $user = $aus->[0];
225 check_all_penalties($user->id(),undef,$user->home_ou(),undef,1);
226
227 diag("Patron 2 - Added note");
228 $user = $aus->[1];
229 check_all_penalties($user->id(),TESTMESSAGE_ZERO,$user->home_ou(),undef,2);
230
231 diag("Patron 3 - Added note - same contact info");
232 $user = $aus->[2];
233 check_all_penalties($user->id(),TESTMESSAGE,$user->home_ou(),undef,3);
234
235 diag("Patron 4 - Added note - same contact info");
236 $user = $aus->[3];
237 check_all_penalties($user->id(),TESTMESSAGE,$user->home_ou(),undef,3);
238
239 diag("Patron 5 - Added note - same contact info");
240 $user = $aus->[4];
241 check_all_penalties($user->id(),TESTMESSAGE,$user->home_ou(),undef,3);
242
243 # Logout
244 $script->logout(); # Not a test, just to be pedantic.