]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/03-overdue_circ.t
LP#1552778: copy some date/time utils from OpenSRF
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 03-overdue_circ.t
1 #!perl
2
3 use Test::More tests => 20;
4
5 diag("Test fine generation on checkin against the admin user.");
6
7 use constant WORKSTATION_NAME => 'BR4-test-03-overdue-circ.t';
8 use constant WORKSTATION_LIB => 7;
9 use constant ITEM_BARCODE => 'CONC71000345';
10 use constant ITEM_ID => 810;
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 OpenILS::Utils::DateTime qw/clean_ISO8601/;
20
21 our $apputils = 'OpenILS::Application::AppUtils';
22
23 #----------------------------------------------------------------
24 # The tests...  assumes stock sample data
25 #----------------------------------------------------------------
26
27 my $storage_ses = $script->session('open-ils.storage');
28 my $circ_ses = $script->session('open-ils.circ');
29 my $cstore_ses = $script->session('open-ils.cstore');
30
31 my $user_req = $storage_ses->request('open-ils.storage.direct.actor.user.retrieve', 1);
32 if (my $user_resp = $user_req->recv) {
33     if (my $user = $user_resp->content) {
34         is(
35             ref $user,
36             'Fieldmapper::actor::user',
37             'open-ils.storage.direct.actor.user.retrieve returned aou object'
38         );
39         is(
40             $user->usrname,
41             'admin',
42             'User with id = 1 is admin user'
43         );
44     }
45 }
46
47 my $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
48 if (my $item_resp = $item_req->recv) {
49     if (my $item = $item_resp->content) {
50         is(
51             ref $item,
52             'Fieldmapper::asset::copy',
53             'open-ils.storage.direct.asset.copy.retrieve returned acp object'
54         );
55         is(
56             $item->barcode,
57             ITEM_BARCODE,
58             'Item with id = ' . ITEM_ID . ' has barcode ' . ITEM_BARCODE
59         );
60         ok(
61             $item->status == 7 || $item->status == 0,
62             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available'
63         );
64     }
65 }
66
67 $script->authenticate({
68     username => 'admin',
69     password => 'demo123',
70     type => 'staff'});
71 ok(
72     $script->authtoken,
73     'Have an authtoken'
74 );
75 my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
76 ok(
77     ! ref $ws,
78     'Registered a new workstation'
79 );
80
81 $script->logout();
82 $script->authenticate({
83     username => 'admin',
84     password => 'demo123',
85     type => 'staff',
86     workstation => WORKSTATION_NAME});
87 ok(
88     $script->authtoken,
89     'Have an authtoken associated with the workstation'
90 );
91
92 my $checkout_resp = $script->do_checkout({
93     patron => 1,
94     barcode => ITEM_BARCODE});
95 is(
96     ref $checkout_resp,
97     'HASH',
98     'Checkout request returned a HASH'
99 );
100 is(
101     $checkout_resp->{ilsevent},
102     0,
103     'Checkout returned a SUCCESS event'
104 );
105 ok(
106     ref $checkout_resp->{payload},
107     'Checkout response object has payload object'
108 );
109 ok(
110     ref $checkout_resp->{payload}->{circ},
111     'Payload object has circ object'
112 );
113 is(
114     $checkout_resp->{payload}->{circ}->duration,
115     '7 days',
116     'Circ objection has loan duration of "7 days"'
117 );
118
119 my $circ = $checkout_resp->{payload}->{circ};
120
121 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
122 if (my $item_resp = $item_req->recv) {
123     if (my $item = $item_resp->content) {
124         is(
125             $item->status,
126             1,
127             'Item with id = ' . ITEM_ID . ' has status of Checked Out after fresh Storage request'
128         );
129     }
130 }
131
132 my $bill_req = $circ_ses->request(
133     'open-ils.circ.money.billing.retrieve.all',
134     $script->authtoken,
135     $circ->id
136 );
137 if (my $bill_resp = $bill_req->recv) {
138     if (my $bills = $bill_resp->content) {
139         is(
140             scalar( @{ $bills } ),
141             0,
142             'Zero bills associated with circulation'
143         );
144     }
145 }
146
147 my $xact_start = DateTime::Format::ISO8601->parse_datetime(clean_ISO8601($circ->xact_start));
148 my $due_date = DateTime::Format::ISO8601->parse_datetime(clean_ISO8601($circ->due_date));
149
150 # Rewrite history; technically we should rewrite status_changed_item on the copy as well, but, meh...
151 $circ->xact_start( $xact_start->subtract( days => 20 )->iso8601() );
152 $circ->due_date( $due_date->subtract( days => 20 )->iso8601() );
153
154 $cstore_ses->connect; # need stateful connection
155 my $xact = $cstore_ses->request('open-ils.cstore.transaction.begin')->gather(1);
156 my $update_req = $cstore_ses->request(
157     'open-ils.cstore.direct.action.circulation.update',
158     $circ
159 );
160 if (my $update_resp = $update_req->gather(1)) {
161     pass(
162         'rewrote circ to have happened 20 days ago'
163     );
164 } else {
165     fail(
166         'rewrote circ to have happened 20 days ago'
167     );
168 }
169 $cstore_ses->request('open-ils.cstore.transaction.commit')->gather(1);
170
171 ########
172
173 my $checkin_resp = $script->do_checkin({
174     barcode => ITEM_BARCODE});
175 is(
176     ref $checkin_resp,
177     'HASH',
178     'Checkin request returned a HASH'
179 );
180 is(
181     $checkin_resp->{ilsevent},
182     0,
183     'Checkin returned a SUCCESS event'
184 );
185
186 $item_req = $storage_ses->request('open-ils.storage.direct.asset.copy.retrieve', ITEM_ID);
187 if (my $item_resp = $item_req->recv) {
188     if (my $item = $item_resp->content) {
189         ok(
190             $item->status == 7 || $item->status == 0,
191             'Item with id = ' . ITEM_ID . ' has status of Reshelving or Available after fresh Storage request'
192         );
193     }
194 }
195
196 $bill_req = $circ_ses->request(
197     'open-ils.circ.money.billing.retrieve.all',
198     $script->authtoken,
199     $circ->id
200 );
201 if (my $bill_resp = $bill_req->recv) {
202     if (my $bills = $bill_resp->content) {
203         is(
204             scalar( @{ $bills } ),
205             13,
206             'Thirteen bills associated with circulation'
207         );
208     }
209 }
210
211
212 $script->logout();
213
214