2 use strict; use warnings;
3 use Test::More tests => 26;
4 use OpenILS::Utils::TestUtils;
5 use OpenILS::Const qw(:const);
7 diag("Test LP 1306666 abort transit copy status fix.");
9 my $script = OpenILS::Utils::TestUtils->new();
10 my $apputils = 'OpenILS::Application::AppUtils';
13 BR1_WORKSTATION => 'BR1-test-lp1306666-abort-transit-copyt-status.t',
15 BR3_WORKSTATION => 'BR3-test-lp1306666-abort-transit-copyt-status.t',
17 PBARCODE => '99999378730',
18 CBARCODE => 'CONC4000036'
21 # Because this may run multiple times, without a DB reload, we search
22 # for the workstation before registering it. Takes an authtoken, the
23 # id of the workstation lib, and the name of the workstation.
24 sub find_workstation {
25 my ($authtoken, $lib, $workstation) = @_;
26 my $r = $apputils->simplereq(
28 'open-ils.actor.workstation.list',
33 return scalar(grep {$_->name() eq $workstation} @{$r->{$lib}});
41 # Login as staff at BR1
42 $authtoken[0] = $script->authenticate({
43 username => 'br1vcampbell',
44 password => 'vincentc1234',
48 # Register workstation at BR1.
49 unless (find_workstation($authtoken[0], BR1_ID, BR1_WORKSTATION)) {
50 $script->register_workstation(BR1_WORKSTATION, BR1_ID);
54 $script->logout($authtoken[0]);
56 # Login as staff at BR1 using workstation.
57 $authtoken[0] = $script->authenticate({
58 username => 'br1vcampbell',
59 password => 'vincentc1234',
61 workstation => BR1_WORKSTATION
64 # Login as staff at BR3
65 $authtoken[1] = $script->authenticate({
66 username => 'br3sforbes',
67 password => 'samuelf1234',
71 # Register workstation at BR3.
72 unless (find_workstation($authtoken[1], BR3_ID, BR3_WORKSTATION)) {
73 $script->register_workstation(BR3_WORKSTATION, BR3_ID);
77 $script->logout($authtoken[1]);
79 # Login as staff at BR3 using workstation.
80 $authtoken[1] = $script->authenticate({
81 username => 'br3sforbes',
82 password => 'samuelf1234',
84 workstation => BR3_WORKSTATION
87 # Retrieve copy at BR1
88 my $copy = $apputils->simplereq(
90 'open-ils.search.asset.copy.find_by_barcode',
94 # Check that copy exists.
95 isa_ok(ref($copy), 'Fieldmapper::asset::copy', 'Got copy') or BAIL_OUT('Need copy');
97 # Retrieve patron at BR3
98 my $patron = $apputils->simplereq(
100 'open-ils.actor.user.fleshed.retrieve_by_barcode',
105 # Check that patron exists.
106 isa_ok(ref($patron), 'Fieldmapper::actor::user', 'Got patron') or BAIL_OUT('Need patron');
108 # Place copy hold for patron pickup at BR3.
109 my $hold = $apputils->simplereq(
111 'open-ils.circ.holds.test_and_create.batch',
115 patronid => $patron->id(),
120 if (ref($hold->{result})) {
121 my $event = (ref($hold->{result}) eq 'ARRAY') ? $hold->{result}->[0] : $hold->{result};
122 if ($event->{textcode} eq 'HOLD_EXISTS') {
123 my $target = $hold->{target};
124 $hold = $apputils->simplereq(
126 'open-ils.pcrud.search.ahr',
128 {target => $target, usr => $patron->id(), fulfillment_time => undef, cancel_time => undef}
131 BAIL_OUT('Cannot place hold');
134 $hold = $apputils->simplereq(
136 'open-ils.pcrud.retrieve.ahr',
142 # Check that hold exists.
143 isa_ok(ref($hold), 'Fieldmapper::action::hold_request', 'Got hold') or BAIL_OUT('Need hold');
145 # Check copy in at BR1
146 my $checkin = $apputils->simplereq(
148 'open-ils.circ.checkin',
150 {barcode => CBARCODE}
152 subtest 'Got ROUTE_ITEM event 1' => sub {
154 is(ref($checkin), 'HASH', 'Got event');
155 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
156 is($checkin->{org}, BR3_ID, 'ROUTE_ITEM event destination');
159 # Check copy transit.
160 my $transit = $apputils->simplereq(
162 'open-ils.pcrud.search.ahtc',
164 {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
166 subtest 'Got hold transit 1' => sub {
168 isa_ok(ref($transit), 'Fieldmapper::action::hold_transit_copy', 'Got hold transit copy');
169 is($transit->dest(), BR3_ID, 'Transit destination');
170 is($transit->source(), BR1_ID, 'Transit source');
171 is($transit->copy_status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Transit copy status');
175 $copy = $apputils->simplereq(
177 'open-ils.pcrud.retrieve.acp',
181 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit');
184 my $abort = $apputils->simplereq(
186 'open-ils.circ.transit.abort',
188 {transitid => $transit->id()}
190 is($abort, 1, 'Transit aborted');
193 $copy = $apputils->simplereq(
195 'open-ils.pcrud.retrieve.acp',
199 is($copy->status(), OILS_COPY_STATUS_RESHELVING, 'Copy reshelving');
201 # Check copy in at BR1
202 $checkin = $apputils->simplereq(
204 'open-ils.circ.checkin',
206 {barcode => CBARCODE}
208 subtest 'Got ROUTE_ITEM event 2' => sub {
210 is(ref($checkin), 'HASH', 'Got event');
211 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
212 is($checkin->{org}, BR3_ID, 'ROUTE_ITEM event destination');
215 # Check copy transit.
216 $transit = $apputils->simplereq(
218 'open-ils.pcrud.search.ahtc',
220 {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
222 subtest 'Got hold transit 2' => sub {
224 isa_ok(ref($transit), 'Fieldmapper::action::hold_transit_copy', 'Got hold transit copy');
225 is($transit->dest(), BR3_ID, 'Transit destination');
226 is($transit->source(), BR1_ID, 'Transit source');
227 is($transit->copy_status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Transit copy status');
231 $copy = $apputils->simplereq(
233 'open-ils.pcrud.retrieve.acp',
237 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit 2');
239 # Check copy in at BR3.
240 $checkin = $apputils->simplereq(
242 'open-ils.circ.checkin',
244 {barcode => CBARCODE}
246 subtest 'Checkin at destination' => sub{
248 is(ref($checkin), 'HASH', 'Got event');
249 is($checkin->{textcode}, 'SUCCESS', 'Event was successful');
250 is($checkin->{ishold}, 1, 'Check In filled hold');
253 # Check hold is on shelf.
254 $hold = $apputils->simplereq(
256 'open-ils.pcrud.retrieve.ahr',
260 ok(defined($hold->shelf_time()), 'Hold has shelf time');
263 $copy = $apputils->simplereq(
265 'open-ils.pcrud.retrieve.acp',
269 is($copy->status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Copy on holds shelf');
271 # Check copy out to patron at BR3.
272 my $checkout = $apputils->simplereq(
274 'open-ils.circ.checkout.full',
276 {copy_id => $copy->id(), patron_id => $patron->id()}
278 subtest 'Checkout to patron' => sub {
280 is(ref($checkout), 'HASH', 'Got checkout event');
281 is($checkout->{textcode}, 'SUCCESS', 'Checkout succeeded');
284 # Check the hold is fulfilled.
285 $hold = $apputils->simplereq(
287 'open-ils.pcrud.retrieve.ahr',
291 ok(defined($hold->fulfillment_time()), 'Hold was fulfilled');
294 $copy = $apputils->simplereq(
296 'open-ils.pcrud.retrieve.acp',
300 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out');
302 # Make a transit from BR3 to BR1 for the copy.
303 # We make the transit with pcrud, because
304 # open-ils.circ.copy_transit.create changes the copy status.
305 my $new_transit = Fieldmapper::action::transit_copy->new();
306 $new_transit->source(BR3_ID);
307 $new_transit->dest(BR1_ID);
308 $new_transit->target_copy($copy->id());
309 $new_transit->copy_status(OILS_COPY_STATUS_RESHELVING);
310 $new_transit->source_send_time('now');
311 my $pcrud_ses = $script->session('open-ils.pcrud');
312 $pcrud_ses->connect();
313 my $xact = $pcrud_ses->request(
314 'open-ils.pcrud.transaction.begin',
317 $transit = $pcrud_ses->request(
318 'open-ils.pcrud.create.atc',
323 'open-ils.pcrud.transaction.commit',
326 $pcrud_ses->disconnect();
328 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Transit created');
331 $transit = $apputils->simplereq(
333 'open-ils.pcrud.search.atc',
335 {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
337 subtest 'Got transit 1' => sub {
339 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Got transit copy');
340 is($transit->dest(), BR1_ID, 'Transit destination');
341 is($transit->source(), BR3_ID, 'Transit source');
342 is($transit->copy_status(), OILS_COPY_STATUS_RESHELVING, 'Transit copy status');
346 $abort = $apputils->simplereq(
348 'open-ils.circ.transit.abort',
350 {transitid => $transit->id()}
352 is($abort, 1, 'Transit 1 aborted');
355 $copy = $apputils->simplereq(
357 'open-ils.pcrud.retrieve.acp',
361 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out after transit abort');
363 # Check copy in at BR3.
364 $checkin = $apputils->simplereq(
366 'open-ils.circ.checkin',
368 {barcode => CBARCODE}
370 subtest 'Check in after circulation' => sub{
372 is(ref($checkin), 'HASH', 'Got event');
373 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
374 is($checkin->{org}, BR1_ID, 'ROUTE_ITEM event destination');
378 $transit = $apputils->simplereq(
380 'open-ils.pcrud.search.atc',
382 {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
384 subtest 'Got transit after check in' => sub {
386 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Got transit copy');
387 is($transit->dest(), BR1_ID, 'Transit destination');
388 is($transit->source(), BR3_ID, 'Transit source');
389 is($transit->copy_status(), OILS_COPY_STATUS_RESHELVING, 'Transit copy status');
393 $copy = $apputils->simplereq(
395 'open-ils.pcrud.retrieve.acp',
399 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit after check in');
402 $abort = $apputils->simplereq(
404 'open-ils.circ.transit.abort',
406 {transitid => $transit->id()}
408 is($abort, 1, 'Transit aborted');
411 $copy = $apputils->simplereq(
413 'open-ils.pcrud.retrieve.acp',
417 is($copy->status(), OILS_COPY_STATUS_RESHELVING, 'Copy is reshelving after return transit abort');
420 $script->logout($authtoken[0]);
423 $script->logout($authtoken[1]);