3 use Test::More tests => 22;
5 diag("Test fine generation with closed date on checkin against the admin user.");
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;
16 use OpenSRF::AppSession;
17 use Digest::MD5 qw(md5_hex);
18 use OpenILS::Utils::Fieldmapper;
19 use OpenILS::Application::AppUtils;
21 use DateTime::Format::ISO8601;
22 use OpenSRF::Utils qw/cleanse_ISO8601/;
23 use OpenSRF::Utils::SettingsClient;
26 our $cache = "OpenSRF::Utils::Cache";
27 our $apputils = "OpenILS::Application::AppUtils";
32 #----------------------------------------------------------------
34 #----------------------------------------------------------------
36 my ($pkg, $file, $line, $sub) = _caller();
38 die "Script halted with error ".
39 "($pkg : $file : $line : $sub):\n" . shift() . "\n";
42 #----------------------------------------------------------------
43 # This is not the function you're looking for
44 #----------------------------------------------------------------
46 my ($pkg, $file, $line, $sub) = caller(2);
48 ($pkg, $file, $line) = caller(1);
51 return ($pkg, $file, $line, $sub);
54 #----------------------------------------------------------------
55 # Connect to the servers
56 #----------------------------------------------------------------
58 my $config = `osrf_config --sysconfdir`;
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"));
67 #----------------------------------------------------------------
68 # Is the given object an OILS event?
69 #----------------------------------------------------------------
72 if( $e and ref($e) eq 'HASH' ) {
73 return 1 if defined($e->{ilsevent});
78 #----------------------------------------------------------------
79 # If the given object is an event, this prints the event info
80 # and exits the script
81 #----------------------------------------------------------------
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));
93 #----------------------------------------------------------------
94 # Login to the auth server and set the global $authtoken var
95 #----------------------------------------------------------------
97 my( $username, $password, $type, $ws ) = @_;
101 my $seed = $apputils->simplereq( 'open-ils.auth',
102 'open-ils.auth.authenticate.init', $username );
103 err("No auth seed") unless $seed;
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 });
111 err("No auth response returned on login") unless $response;
113 oils_event_die($response);
115 $authtime = $response->{payload}->{authtime};
116 $authtoken = $response->{payload}->{authtoken};
117 diag("authtime is $authtime, authtoken is $authtoken");
121 #----------------------------------------------------------------
122 # Destroys the login session on the server
123 #----------------------------------------------------------------
125 $apputils->simplereq(
127 'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
130 #----------------------------------------------------------------
131 # var $response = simplereq( $service, $method, @params );
132 #----------------------------------------------------------------
133 sub simplereq { return $apputils->simplereq(@_); }
134 sub osrf_request { return $apputils->simplereq(@_); }
136 #----------------------------------------------------------------
138 sub register_workstation {
139 my $resp = osrf_request(
141 'open-ils.actor.workstation.register',
142 $authtoken, WORKSTATION_NAME, WORKSTATION_LIB);
147 my( $patronid, $barcode ) = @_;
148 my $args = { patron => $patronid, barcode => $barcode };
149 my $resp = osrf_request(
151 'open-ils.circ.checkout.full', $authtoken, $args );
157 my $args = { barcode => $barcode };
158 my $resp = osrf_request(
160 'open-ils.circ.checkin', $authtoken, $args );
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) );
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');
173 $apputils->epoch2ISO8601(
174 DateTime->today()->epoch() - $ten_days
178 $apputils->epoch2ISO8601(
179 DateTime->today()->epoch() - $ten_days + $almost_twenty_four_hours
182 my $resp = osrf_request(
184 'open-ils.actor.org_unit.closed.create',
189 sub delete_closed_date {
191 my $resp = osrf_request(
193 'open-ils.actor.org_unit.closed.delete',
194 $authtoken, ref $aoucd ? $aoucd->id : $aoucd );
198 #----------------------------------------------------------------
199 # The tests... assumes stock sample data, full-auto install by
200 # eg_wheezy_installer.sh, etc.
201 #----------------------------------------------------------------
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');
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) {
213 'Fieldmapper::actor::user',
214 'open-ils.storage.direct.actor.user.retrieve returned aou object'
219 'User with id = 1 is admin user'
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) {
229 'Fieldmapper::asset::copy',
230 'open-ils.storage.direct.asset.copy.retrieve returned acp object'
235 'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
238 $item->status == 7 || $item->status == 0,
239 'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
244 oils_login('admin','demo123','staff');
249 my $ws = register_workstation();
252 'Registered a new workstation'
256 oils_login('admin','demo123','staff',WORKSTATION_NAME);
259 'Have an authtoken associated with the workstation'
262 my $closed_date_obj = create_closed_date();
264 ref $closed_date_obj,
265 'Fieldmapper::actor::org_unit::closed_date',
266 'Created a closed date for 10 days ago'
269 my $checkout_resp = do_checkout(1, ITEM_BARCODE);
273 'Checkout request returned a HASH'
276 $checkout_resp->{ilsevent},
278 'Checkout returned a SUCCESS event'
281 ref $checkout_resp->{payload},
282 'Checkout response object has payload object'
285 ref $checkout_resp->{payload}->{circ},
286 'Payload object has circ object'
289 $checkout_resp->{payload}->{circ}->duration,
291 'Circ objection has loan duration of "7 days"'
294 my $circ = $checkout_resp->{payload}->{circ};
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) {
302 'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
307 my $bill_req = $circ_ses->request(
308 'open-ils.circ.money.billing.retrieve.all',
312 if (my $bill_resp = $bill_req->recv) {
313 if (my $bills = $bill_resp->content) {
315 scalar( @{ $bills } ),
317 'Zero bills associated with circulation'
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');
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) );
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',
335 if (my $update_resp = $update_req->gather(1)) {
337 'rewrote circ to have happened 20 days ago'
341 'rewrote circ to have happened 20 days ago'
344 $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
348 my $checkin_resp = do_checkin(ITEM_BARCODE);
352 'Checkin request returned a HASH'
355 $checkin_resp->{ilsevent},
357 'Checkin returned a SUCCESS event'
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) {
364 $item->status == 7 || $item->status == 0,
365 'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
370 $bill_req = $circ_ses->request(
371 'open-ils.circ.money.billing.retrieve.all',
375 if (my $bill_resp = $bill_req->recv) {
376 if (my $bills = $bill_resp->content) {
378 scalar( @{ $bills } ),
380 'Twelve bills associated with circulation (instead of 13, thanks to closed date)'
385 my $tmp = delete_closed_date($closed_date_obj);
386 is($tmp, 1, 'Removed closed date');