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