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