]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/17-lp1306666-abort-transit-copy-status.t
fdf47d0853b265a516eda302f87a4a60bb85d497
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 17-lp1306666-abort-transit-copy-status.t
1 #!perl
2 use strict; use warnings;
3 use Test::More tests => 26;
4 use OpenILS::Utils::TestUtils;
5 use OpenILS::Const qw(:const);
6
7 diag("Test LP 1306666 abort transit copy status fix.");
8
9 my $script = OpenILS::Utils::TestUtils->new();
10 my $apputils = 'OpenILS::Application::AppUtils';
11
12 use constant {
13     BR1_WORKSTATION => 'BR1-test-lp1306666-abort-transit-copyt-status.t',
14     BR1_ID => 4,
15     BR3_WORKSTATION => 'BR3-test-lp1306666-abort-transit-copyt-status.t',
16     BR3_ID => 6,
17     PBARCODE => '99999378730',
18     CBARCODE => 'CONC4000036'
19 };
20
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(
27         'open-ils.actor',
28         'open-ils.actor.workstation.list',
29         $authtoken,
30         $lib
31     );
32     if ($r->{$lib}) {
33         return scalar(grep {$_->name() eq $workstation} @{$r->{$lib}});
34     }
35     return 0;
36 }
37
38 # Store authtokens
39 my @authtoken = ();
40
41 # Login as staff at BR1
42 $authtoken[0] = $script->authenticate({
43     username => 'br1vcampbell',
44     password => 'vincentc1234',
45     type => 'staff'
46 });
47
48 # Register workstation at BR1.
49 unless (find_workstation($authtoken[0], BR1_ID, BR1_WORKSTATION)) {
50     $script->register_workstation(BR1_WORKSTATION, BR1_ID);
51 }
52
53 # Logout of BR1.
54 $script->logout($authtoken[0]);
55
56 # Login as staff at BR1 using workstation.
57 $authtoken[0] = $script->authenticate({
58     username => 'br1vcampbell',
59     password => 'vincentc1234',
60     type => 'staff',
61     workstation => BR1_WORKSTATION
62 });
63
64 # Login as staff at BR3
65 $authtoken[1] = $script->authenticate({
66     username => 'br3sforbes',
67     password => 'samuelf1234',
68     type => 'staff'
69 });
70
71 # Register workstation at BR3.
72 unless (find_workstation($authtoken[1], BR3_ID, BR3_WORKSTATION)) {
73     $script->register_workstation(BR3_WORKSTATION, BR3_ID);
74 }
75
76 # Logout of BR3.
77 $script->logout($authtoken[1]);
78
79 # Login as staff at BR3 using workstation.
80 $authtoken[1] = $script->authenticate({
81     username => 'br3sforbes',
82     password => 'samuelf1234',
83     type => 'staff',
84     workstation => BR3_WORKSTATION
85 });
86
87 # Retrieve copy at BR1
88 my $copy = $apputils->simplereq(
89     'open-ils.search',
90     'open-ils.search.asset.copy.find_by_barcode',
91     CBARCODE
92 );
93
94 # Check that copy exists.
95 isa_ok(ref($copy), 'Fieldmapper::asset::copy', 'Got copy') or BAIL_OUT('Need copy');
96
97 # Retrieve patron at BR3
98 my $patron = $apputils->simplereq(
99     'open-ils.actor',
100     'open-ils.actor.user.fleshed.retrieve_by_barcode',
101     $authtoken[1],
102     PBARCODE
103 );
104
105 # Check that patron exists.
106 isa_ok(ref($patron), 'Fieldmapper::actor::user', 'Got patron') or BAIL_OUT('Need patron');
107
108 # Place copy hold for patron pickup at BR3.
109 my $hold = $apputils->simplereq(
110     'open-ils.circ',
111     'open-ils.circ.holds.test_and_create.batch',
112     $authtoken[1],
113     {
114         hold_type => 'C',
115         patronid => $patron->id(),
116         pickup_lib => BR3_ID
117     },
118     [$copy->id()]
119 );
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(
125             'open-ils.pcrud',
126             'open-ils.pcrud.search.ahr',
127             $authtoken[1],
128             {target => $target, usr => $patron->id(), fulfillment_time => undef, cancel_time => undef}
129         );
130     } else {
131         BAIL_OUT('Cannot place hold');
132     }
133 } else {
134     $hold = $apputils->simplereq(
135         'open-ils.pcrud',
136         'open-ils.pcrud.retrieve.ahr',
137         $authtoken[1],
138         $hold->{result}
139     );
140 }
141
142 # Check that hold exists.
143 isa_ok(ref($hold), 'Fieldmapper::action::hold_request', 'Got hold') or BAIL_OUT('Need hold');
144
145 # Check copy in at BR1
146 my $checkin = $apputils->simplereq(
147     'open-ils.circ',
148     'open-ils.circ.checkin',
149     $authtoken[0],
150     {barcode => CBARCODE}
151 );
152 subtest 'Got ROUTE_ITEM event 1' => sub {
153     plan tests => 3;
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');
157 };
158
159 # Check copy transit.
160 my $transit = $apputils->simplereq(
161     'open-ils.pcrud',
162     'open-ils.pcrud.search.ahtc',
163     $authtoken[0],
164     {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
165 );
166 subtest 'Got hold transit 1' => sub {
167     plan tests => 4;
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');
172 };
173
174 # Check copy status.
175 $copy = $apputils->simplereq(
176     'open-ils.pcrud',
177     'open-ils.pcrud.retrieve.acp',
178     $authtoken[0],
179     $copy->id()
180 );
181 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit');
182
183 # Abort the transit.
184 my $abort = $apputils->simplereq(
185     'open-ils.circ',
186     'open-ils.circ.transit.abort',
187     $authtoken[0],
188     {transitid => $transit->id()}
189 );
190 is($abort, 1, 'Transit aborted');
191
192 # Check copy status.
193 $copy = $apputils->simplereq(
194     'open-ils.pcrud',
195     'open-ils.pcrud.retrieve.acp',
196     $authtoken[0],
197     $copy->id()
198 );
199 is($copy->status(), OILS_COPY_STATUS_RESHELVING, 'Copy reshelving');
200
201 # Check copy in at BR1
202 $checkin = $apputils->simplereq(
203     'open-ils.circ',
204     'open-ils.circ.checkin',
205     $authtoken[0],
206     {barcode => CBARCODE}
207 );
208 subtest 'Got ROUTE_ITEM event 2' => sub {
209     plan tests => 3;
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');
213 };
214
215 # Check copy transit.
216 $transit = $apputils->simplereq(
217     'open-ils.pcrud',
218     'open-ils.pcrud.search.ahtc',
219     $authtoken[0],
220     {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
221 );
222 subtest 'Got hold transit 2' => sub {
223     plan tests => 4;
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');
228 };
229
230 # Check copy status.
231 $copy = $apputils->simplereq(
232     'open-ils.pcrud',
233     'open-ils.pcrud.retrieve.acp',
234     $authtoken[0],
235     $copy->id()
236 );
237 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit 2');
238
239 # Check copy in at BR3.
240 $checkin = $apputils->simplereq(
241     'open-ils.circ',
242     'open-ils.circ.checkin',
243     $authtoken[1],
244     {barcode => CBARCODE}
245 );
246 subtest 'Checkin at destination' => sub{
247     plan tests => 3;
248     is(ref($checkin), 'HASH', 'Got event');
249     is($checkin->{textcode}, 'SUCCESS', 'Event was successful');
250     is($checkin->{ishold}, 1, 'Check In filled hold');
251 };
252
253 # Check hold is on shelf.
254 $hold = $apputils->simplereq(
255     'open-ils.pcrud',
256     'open-ils.pcrud.retrieve.ahr',
257     $authtoken[1],
258     $hold->id()
259 );
260 ok(defined($hold->shelf_time()), 'Hold has shelf time');
261
262 # Check copy status.
263 $copy = $apputils->simplereq(
264     'open-ils.pcrud',
265     'open-ils.pcrud.retrieve.acp',
266     $authtoken[1],
267     $copy->id()
268 );
269 is($copy->status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Copy on holds shelf');
270
271 # Check copy out to patron at BR3.
272 my $checkout = $apputils->simplereq(
273     'open-ils.circ',
274     'open-ils.circ.checkout.full',
275     $authtoken[1],
276     {copy_id => $copy->id(), patron_id => $patron->id()}
277 );
278 subtest 'Checkout to patron' => sub {
279     plan tests => 2;
280     is(ref($checkout), 'HASH', 'Got checkout event');
281     is($checkout->{textcode}, 'SUCCESS', 'Checkout succeeded');
282 };
283
284 # Check the hold is fulfilled.
285 $hold = $apputils->simplereq(
286     'open-ils.pcrud',
287     'open-ils.pcrud.retrieve.ahr',
288     $authtoken[1],
289     $hold->id()
290 );
291 ok(defined($hold->fulfillment_time()), 'Hold was fulfilled');
292
293 # Check copy status.
294 $copy = $apputils->simplereq(
295     'open-ils.pcrud',
296     'open-ils.pcrud.retrieve.acp',
297     $authtoken[1],
298     $copy->id()
299 );
300 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out');
301
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',
315     $authtoken[1]
316 )->gather(1);
317 $transit = $pcrud_ses->request(
318     'open-ils.pcrud.create.atc',
319     $authtoken[1],
320     $new_transit
321 )->gather(1);
322 $pcrud_ses->request(
323     'open-ils.pcrud.transaction.commit',
324     $authtoken[1]
325 )->gather(1);
326 $pcrud_ses->disconnect();
327 undef($pcrud_ses);
328 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Transit created');
329
330 # Check the transit.
331 $transit = $apputils->simplereq(
332     'open-ils.pcrud',
333     'open-ils.pcrud.search.atc',
334     $authtoken[1],
335     {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
336 );
337 subtest 'Got transit 1' => sub {
338     plan tests => 4;
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');
343 };
344
345 # Abort the transit.
346 $abort = $apputils->simplereq(
347     'open-ils.circ',
348     'open-ils.circ.transit.abort',
349     $authtoken[1],
350     {transitid => $transit->id()}
351 );
352 is($abort, 1, 'Transit 1 aborted');
353
354 # Check copy status.
355 $copy = $apputils->simplereq(
356     'open-ils.pcrud',
357     'open-ils.pcrud.retrieve.acp',
358     $authtoken[1],
359     $copy->id()
360 );
361 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out after transit abort');
362
363 # Check copy in at BR3.
364 $checkin = $apputils->simplereq(
365     'open-ils.circ',
366     'open-ils.circ.checkin',
367     $authtoken[1],
368     {barcode => CBARCODE}
369 );
370 subtest 'Check in after circulation' => sub{
371     plan tests => 3;
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');
375 };
376
377 # Check for transit.
378 $transit = $apputils->simplereq(
379     'open-ils.pcrud',
380     'open-ils.pcrud.search.atc',
381     $authtoken[1],
382     {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
383 );
384 subtest 'Got transit after check in' => sub {
385     plan tests => 4;
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');
390 };
391
392 # Check copy status.
393 $copy = $apputils->simplereq(
394     'open-ils.pcrud',
395     'open-ils.pcrud.retrieve.acp',
396     $authtoken[1],
397     $copy->id()
398 );
399 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit after check in');
400
401 # Abort the transit.
402 $abort = $apputils->simplereq(
403     'open-ils.circ',
404     'open-ils.circ.transit.abort',
405     $authtoken[0],
406     {transitid => $transit->id()}
407 );
408 is($abort, 1, 'Transit aborted');
409
410 # Check copy status.
411 $copy = $apputils->simplereq(
412     'open-ils.pcrud',
413     'open-ils.pcrud.retrieve.acp',
414     $authtoken[0],
415     $copy->id()
416 );
417 is($copy->status(), OILS_COPY_STATUS_RESHELVING, 'Copy is reshelving after return transit abort');
418
419 # Logout at BR1.
420 $script->logout($authtoken[0]);
421
422 # Logout at BR3.
423 $script->logout($authtoken[1]);