]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/04-overdue_with_closed_dates.t
LP#1187035 Remove OpenILS::Utils::Editor part 4.
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 04-overdue_with_closed_dates.t
1 #!perl
2
3 use Test::More tests => 23;
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; use warnings;
13
14 use OpenILS::Utils::TestUtils;
15 my $script = OpenILS::Utils::TestUtils->new();
16
17 use DateTime;
18 use DateTime::Format::ISO8601;
19 use OpenSRF::Utils qw/cleanse_ISO8601/;
20
21 our $apputils   = "OpenILS::Application::AppUtils";
22
23 sub create_closed_date {
24     my $ten_days = OpenSRF::Utils->interval_to_seconds('240 h 0 m 0 s');
25     my $almost_twenty_four_hours = OpenSRF::Utils->interval_to_seconds('23 h 59 m 59 s');
26     #$circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
27
28     my $aoucd = Fieldmapper::actor::org_unit::closed_date->new;
29     $aoucd->org_unit(WORKSTATION_LIB);
30     $aoucd->reason('04-overdue_with_closed_dates.t');
31     $aoucd->close_start(
32         $apputils->epoch2ISO8601(
33             DateTime->today()->epoch() - $ten_days
34         )
35     );
36     $aoucd->close_end(
37         $apputils->epoch2ISO8601(
38             DateTime->today()->epoch() - $ten_days + $almost_twenty_four_hours
39         )
40     );
41     my $resp = $apputils->simplereq(
42         'open-ils.actor',
43         'open-ils.actor.org_unit.closed.create',
44         $script->authtoken, $aoucd);
45     return $resp;
46 }
47
48 # returns "1" on success, event on error
49 sub update_closed_date {
50     my $aoucd = shift;
51     $aoucd->reason($aoucd->reason . ' modified');
52     return $apputils->simplereq(
53         'open-ils.actor',
54         'open-ils.actor.org_unit.closed.update',
55         $script->authtoken, $aoucd);
56 }
57
58 sub delete_closed_date {
59     my $aoucd = shift;
60     my $resp = $apputils->simplereq(
61         'open-ils.actor',
62         'open-ils.actor.org_unit.closed.delete',
63         $script->authtoken, ref $aoucd ? $aoucd->id : $aoucd );
64     return $resp;
65 }
66
67 #----------------------------------------------------------------
68 # The tests...  assumes stock sample data, full-auto install by
69 # eg_wheezy_installer.sh, etc.
70 #----------------------------------------------------------------
71
72 my $storage_ses = $script->session('open-ils.storage');
73 my $circ_ses = $script->session('open-ils.circ');
74 my $cstore_ses = $script->session('open-ils.cstore');
75
76 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
77 if (my $user_resp = $user_req->recv) {
78     if (my $user = $user_resp->content) {
79         is(
80             ref $user,
81             'Fieldmapper::actor::user',
82             'open-ils.storage.direct.actor.user.retrieve returned aou object'
83         );
84         is(
85             $user->usrname,
86             'admin',
87             'User with id = 1 is admin user'
88         );
89     }
90 }
91
92 my $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
93 if (my $item_resp = $item_req->recv) {
94     if (my $item = $item_resp->content) {
95         is(
96             ref $item,
97             'Fieldmapper::asset::copy',
98             'open-ils.storage.direct.asset.copy.retrieve returned acp object'
99         );
100         is(
101             $item->barcode,
102             ITEM_BARCODE,
103             'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
104         );
105         ok(
106             $item->status == 7 || $item->status == 0,
107             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
108         );
109     }
110 }
111
112 $script->authenticate({
113     username => 'admin',
114     password => 'demo123',
115     type => 'staff'});
116 ok(
117     $script->authtoken,
118     'Have an authtoken'
119 );
120 my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
121 ok(
122     ! ref $ws,
123     'Registered a new workstation'
124 );
125
126 $script->logout();
127 $script->authenticate({
128     username => 'admin',
129     password => 'demo123',
130     type => 'staff',
131     workstation => WORKSTATION_NAME});
132 ok(
133     $script->authtoken,
134     'Have an authtoken associated with the workstation'
135 );
136
137 my $closed_date_obj = create_closed_date();
138 is(
139     ref $closed_date_obj,
140     'Fieldmapper::actor::org_unit::closed_date',
141     'Created a closed date for 10 days ago'
142 );
143
144 is(
145     update_closed_date($closed_date_obj),
146     '1',
147     'Updated closed date reason'
148 );
149
150 my $checkout_resp = $script->do_checkout({
151     patron => 1,
152     barcode => ITEM_BARCODE});
153 is(
154     ref $checkout_resp,
155     'HASH',
156     'Checkout request returned a HASH'
157 );
158 is(
159     $checkout_resp->{ilsevent},
160     0,
161     'Checkout returned a SUCCESS event'
162 );
163 ok(
164     ref $checkout_resp->{payload},
165     'Checkout response object has payload object'
166 );
167 ok(
168     ref $checkout_resp->{payload}->{circ},
169     'Payload object has circ object'
170 );
171 is(
172     $checkout_resp->{payload}->{circ}->duration,
173     '7 days',
174     'Circ objection has loan duration of "7 days"'
175 );
176
177 my $circ = $checkout_resp->{payload}->{circ};
178
179 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
180 if (my $item_resp = $item_req->recv) {
181     if (my $item = $item_resp->content) {
182         is(
183             $item->status,
184             1,
185             'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
186         );
187     }
188 }
189
190 my $bill_req = $circ_ses->request(
191     'open-ils.circ.money.billing.retrieve.all',
192     $script->authtoken,
193     $circ->id
194 );
195 if (my $bill_resp = $bill_req->recv) {
196     if (my $bills = $bill_resp->content) {
197         is(
198             scalar( @{ $bills } ),
199             0,
200             'Zero bills associated with circulation'
201         );
202     }
203 }
204
205 my $xact_start = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->xact_start))->epoch;
206 my $due_date = DateTime::Format::ISO8601->parse_datetime(cleanse_ISO8601($circ->due_date))->epoch;
207 my $twenty_days = OpenSRF::Utils->interval_to_seconds('480 h 0 m 0 s');
208
209 # Rewrite history; technically we should rewrite status_changed_item on the copy as well, but, meh...
210 $circ->xact_start( $apputils->epoch2ISO8601($xact_start - $twenty_days) );
211 $circ->due_date( $apputils->epoch2ISO8601($due_date - $twenty_days) );
212
213 $cstore_ses->connect; # need stateful connection
214 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
215 my $update_req = $cstore_ses->request(
216     'open-ils.cstore.direct.action.circulation.update',
217     $circ
218 );
219 if (my $update_resp = $update_req->gather(1)) {
220     pass(
221         'rewrote circ to have happened 20 days ago'
222     );
223 } else {
224     fail(
225         'rewrote circ to have happened 20 days ago'
226     );
227 }
228 $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
229
230 ########
231
232 my $checkin_resp = $script->do_checkin({
233     barcode => ITEM_BARCODE});
234 is(
235     ref $checkin_resp,
236     'HASH',
237     'Checkin request returned a HASH'
238 );
239 is(
240     $checkin_resp->{ilsevent},
241     0,
242     'Checkin returned a SUCCESS event'
243 );
244
245 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
246 if (my $item_resp = $item_req->recv) {
247     if (my $item = $item_resp->content) {
248         ok(
249             $item->status == 7 || $item->status == 0,
250             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
251         );
252     }
253 }
254
255 $bill_req = $circ_ses->request(
256     'open-ils.circ.money.billing.retrieve.all',
257     $script->authtoken,
258     $circ->id
259 );
260 if (my $bill_resp = $bill_req->recv) {
261     if (my $bills = $bill_resp->content) {
262         is(
263             scalar( @{ $bills } ),
264             12,
265             'Twelve bills associated with circulation (instead of 13, thanks to closed date)'
266         );
267     }
268 }
269
270 my $tmp = delete_closed_date($closed_date_obj);
271 is($tmp,    1,  'Removed closed date');
272
273 $script->logout();
274
275