]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/04-overdue_with_closed_dates.t
tests against stock test data and live Evergreen
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 04-overdue_with_closed_dates.t
1 #!perl
2
3 use Test::More tests => 22;
4
5 diag("Test fine generation with closed date on checkin against the admin user.");
6
7 use constant WORKSTATION_NAME => 'BR4-test-04-overdue-with-closed-dates.t';
8 use constant WORKSTATION_LIB => 7;
9 use constant ITEM_BARCODE => 'CONC72000345';
10 use constant ITEM_ID => 1310;
11
12 use strict;
13 use warnings;
14 use Data::Dumper;
15 use OpenSRF::System;
16 use OpenSRF::AppSession;
17 use Digest::MD5 qw(md5_hex);
18 use OpenILS::Utils::Fieldmapper;
19 use OpenILS::Application::AppUtils;
20 use DateTime;
21 use DateTime::Format::ISO8601;
22 use OpenSRF::Utils qw/cleanse_ISO8601/;
23 use OpenSRF::Utils::SettingsClient;
24
25 # Some useful objects
26 our $cache      = "OpenSRF::Utils::Cache";
27 our $apputils   = "OpenILS::Application::AppUtils";
28 our $memcache;
29 our $authtoken;
30 our $authtime;
31
32 #----------------------------------------------------------------
33 # Exit a script
34 #----------------------------------------------------------------
35 sub err {
36     my ($pkg, $file, $line, $sub)  = _caller();
37     no warnings;
38     die "Script halted with error ".
39         "($pkg : $file : $line : $sub):\n" . shift() . "\n";
40 }
41
42 #----------------------------------------------------------------
43 # This is not the function you're looking for
44 #----------------------------------------------------------------
45 sub _caller {
46     my ($pkg, $file, $line, $sub)  = caller(2);
47     if(!$line) {
48         ($pkg, $file, $line)  = caller(1);
49         $sub = "";
50     }
51     return ($pkg, $file, $line, $sub);
52 }
53
54 #----------------------------------------------------------------
55 # Connect to the servers
56 #----------------------------------------------------------------
57 sub osrf_connect {
58     my $config = `osrf_config --sysconfdir`;
59     chomp $config;
60     $config .= '/opensrf_core.xml';
61     err("Bootstrap config required") unless $config;
62     OpenSRF::System->bootstrap_client( config_file => $config );
63     Fieldmapper->import(IDL =>
64         OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
65 }
66
67 #----------------------------------------------------------------
68 # Is the given object an OILS event?
69 #----------------------------------------------------------------
70 sub oils_is_event {
71     my $e = shift;
72     if( $e and ref($e) eq 'HASH' ) {
73         return 1 if defined($e->{ilsevent});
74     }
75     return 0;
76 }
77
78 #----------------------------------------------------------------
79 # If the given object is an event, this prints the event info 
80 # and exits the script
81 #----------------------------------------------------------------
82 sub oils_event_die {
83     my $evt = shift;
84     my ($pkg, $file, $line, $sub)  = _caller();
85     if(oils_is_event($evt)) {
86         if($evt->{ilsevent}) {
87             diag("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
88             exit 1;
89         }
90     }
91 }
92
93 #----------------------------------------------------------------
94 # Login to the auth server and set the global $authtoken var
95 #----------------------------------------------------------------
96 sub oils_login {
97     my( $username, $password, $type, $ws ) = @_;
98
99     $type |= "staff";
100
101     my $seed = $apputils->simplereq( 'open-ils.auth',
102         'open-ils.auth.authenticate.init', $username );
103     err("No auth seed") unless $seed;
104
105     my $response = $apputils->simplereq( 'open-ils.auth',
106         'open-ils.auth.authenticate.complete',
107         {   username => $username,
108             password => md5_hex($seed . md5_hex($password)),
109             type => $type, workstation => $ws });
110
111     err("No auth response returned on login") unless $response;
112
113     oils_event_die($response);
114
115     $authtime  = $response->{payload}->{authtime};
116     $authtoken = $response->{payload}->{authtoken};
117     diag("authtime is $authtime, authtoken is $authtoken");
118     return $authtoken;
119 }
120
121 #----------------------------------------------------------------
122 # Destroys the login session on the server
123 #----------------------------------------------------------------
124 sub oils_logout {
125     $apputils->simplereq(
126         'open-ils.auth',
127         'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
128 }
129
130 #----------------------------------------------------------------
131 # var $response = simplereq( $service, $method, @params );
132 #----------------------------------------------------------------
133 sub simplereq    { return $apputils->simplereq(@_); }
134 sub osrf_request { return $apputils->simplereq(@_); }
135
136 #----------------------------------------------------------------
137
138 sub register_workstation {
139     my $resp = osrf_request(
140         'open-ils.actor',
141         'open-ils.actor.workstation.register',
142         $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
143     return $resp;
144 }
145
146 sub do_checkout {
147     my( $patronid, $barcode ) = @_;
148     my $args = { patron => $patronid, barcode => $barcode };
149     my $resp = osrf_request(
150         'open-ils.circ',
151         'open-ils.circ.checkout.full', $authtoken, $args );
152     return $resp;
153 }
154
155 sub do_checkin {
156     my $barcode  = shift;
157     my $args = { barcode => $barcode };
158     my $resp = osrf_request(
159         'open-ils.circ',
160         'open-ils.circ.checkin', $authtoken, $args );
161     return $resp;
162 }
163
164 sub create_closed_date {
165     my $ten_days = OpenSRF::Utils->interval_to_seconds('240 h 0 m 0 s');
166     my $almost_twenty_four_hours = OpenSRF::Utils->interval_to_seconds('23 h 59 m 59 s');
167     #$circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
168
169     my $aoucd = Fieldmapper::actor::org_unit::closed_date->new;
170     $aoucd->org_unit(WORKSTATION_LIB);
171     $aoucd->reason('04-overdue_with_closed_dates.t');
172     $aoucd->close_start(
173         $apputils->epoch2ISO8601(
174             DateTime->today()->epoch() - $ten_days
175         )
176     );
177     $aoucd->close_end(
178         $apputils->epoch2ISO8601(
179             DateTime->today()->epoch() - $ten_days + $almost_twenty_four_hours
180         )
181     );
182     my $resp = osrf_request(
183         'open-ils.actor',
184         'open-ils.actor.org_unit.closed.create',
185         $authtoken, $aoucd);
186     return $resp;
187 }
188
189 sub delete_closed_date {
190     my $aoucd = shift;
191     my $resp = osrf_request(
192         'open-ils.actor',
193         'open-ils.actor.org_unit.closed.delete',
194         $authtoken, ref $aoucd ? $aoucd->id : $aoucd );
195     return $resp;
196 }
197
198 #----------------------------------------------------------------
199 # The tests...  assumes stock sample data, full-auto install by
200 # eg_wheezy_installer.sh, etc.
201 #----------------------------------------------------------------
202
203 osrf_connect();
204 my $storage_ses = OpenSRF::AppSession->create('open-ils.storage');
205 my $circ_ses = OpenSRF::AppSession->create('open-ils.circ');
206 my $cstore_ses = OpenSRF::AppSession->connect('open-ils.cstore');
207
208 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
209 if (my $user_resp = $user_req->recv) {
210     if (my $user = $user_resp->content) {
211         is(
212             ref $user,
213             'Fieldmapper::actor::user',
214             'open-ils.storage.direct.actor.user.retrieve returned aou object'
215         );
216         is(
217             $user->usrname,
218             'admin',
219             'User with id = 1 is admin user'
220         );
221     }
222 }
223
224 my $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
225 if (my $item_resp = $item_req->recv) {
226     if (my $item = $item_resp->content) {
227         is(
228             ref $item,
229             'Fieldmapper::asset::copy',
230             'open-ils.storage.direct.asset.copy.retrieve returned acp object'
231         );
232         is(
233             $item->barcode,
234             ITEM_BARCODE,
235             'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
236         );
237         ok(
238             $item->status == 7 || $item->status == 0,
239             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
240         );
241     }
242 }
243
244 oils_login('admin','demo123','staff');
245 ok(
246     $authtoken,
247     'Have an authtoken'
248 );
249 my $ws = register_workstation();
250 ok(
251     ! ref $ws,
252     'Registered a new workstation'
253 );
254
255 oils_logout();
256 oils_login('admin','demo123','staff',WORKSTATION_NAME);
257 ok(
258     $authtoken,
259     'Have an authtoken associated with the workstation'
260 );
261
262 my $closed_date_obj = create_closed_date();
263 is(
264     ref $closed_date_obj,
265     'Fieldmapper::actor::org_unit::closed_date',
266     'Created a closed date for 10 days ago'
267 );
268
269 my $checkout_resp = do_checkout(1, ITEM_BARCODE);
270 is(
271     ref $checkout_resp,
272     'HASH',
273     'Checkout request returned a HASH'
274 );
275 is(
276     $checkout_resp->{ilsevent},
277     0,
278     'Checkout returned a SUCCESS event'
279 );
280 ok(
281     ref $checkout_resp->{payload},
282     'Checkout response object has payload object'
283 );
284 ok(
285     ref $checkout_resp->{payload}->{circ},
286     'Payload object has circ object'
287 );
288 is(
289     $checkout_resp->{payload}->{circ}->duration,
290     '7 days',
291     'Circ objection has loan duration of "7 days"'
292 );
293
294 my $circ = $checkout_resp->{payload}->{circ};
295
296 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
297 if (my $item_resp = $item_req->recv) {
298     if (my $item = $item_resp->content) {
299         is(
300             $item->status,
301             1,
302             'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
303         );
304     }
305 }
306
307 my $bill_req = $circ_ses->request(
308     'open-ils.circ.money.billing.retrieve.all',
309     $authtoken,
310     $circ->id
311 );
312 if (my $bill_resp = $bill_req->recv) {
313     if (my $bills = $bill_resp->content) {
314         is(
315             scalar( @{ $bills } ),
316             0,
317             'Zero bills associated with circulation'
318         );
319     }
320 }
321
322 my $xact_start = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->xact_start))->epoch;
323 my $due_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->due_date))->epoch;
324 my $twenty_days = OpenSRF::Utils->interval_to_seconds('480 h 0 m 0 s');
325
326 # Rewrite history; technically we should rewrite status_changed_item on the copy as well, but, meh...
327 $circ->xact_start( $apputils->epoch2ISO8601($xact_start - $twenty_days) );
328 $circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
329
330 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
331 my $update_req = $cstore_ses->request(
332     'open-ils.cstore.direct.action.circulation.update',
333     $circ
334 );
335 if (my $update_resp = $update_req->gather(1)) {
336     pass(
337         'rewrote circ to have happened 20 days ago'
338     );
339 } else {
340     fail(
341         'rewrote circ to have happened 20 days ago'
342     );
343 }
344 $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
345
346 ########
347
348 my $checkin_resp = do_checkin(ITEM_BARCODE);
349 is(
350     ref $checkin_resp,
351     'HASH',
352     'Checkin request returned a HASH'
353 );
354 is(
355     $checkin_resp->{ilsevent},
356     0,
357     'Checkin returned a SUCCESS event'
358 );
359
360 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
361 if (my $item_resp = $item_req->recv) {
362     if (my $item = $item_resp->content) {
363         ok(
364             $item->status == 7 || $item->status == 0,
365             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
366         );
367     }
368 }
369
370 $bill_req = $circ_ses->request(
371     'open-ils.circ.money.billing.retrieve.all',
372     $authtoken,
373     $circ->id
374 );
375 if (my $bill_resp = $bill_req->recv) {
376     if (my $bills = $bill_resp->content) {
377         is(
378             scalar( @{ $bills } ),
379             12,
380             'Twelve bills associated with circulation (instead of 13, thanks to closed date)'
381         );
382     }
383 }
384
385 my $tmp = delete_closed_date($closed_date_obj);
386 is($tmp,    1,  'Removed closed date');
387
388 oils_logout();
389
390