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