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