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