3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Koha::DateUtils qw( dt_from_string );
25 use t::lib::TestBuilder;
27 use Test::More tests => 7;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'item relation tests' => sub {
36 $schema->storage->txn_begin;
38 my $item = $builder->build_sample_item();
39 my $transfer = $builder->build_object(
41 class => 'Koha::Item::Transfers',
43 itemnumber => $item->itemnumber,
48 my $transfer_item = $transfer->item;
49 is( ref( $transfer_item ), 'Koha::Item', 'Koha::Item::Transfer->item should return a Koha::Item' );
50 is( $transfer_item->itemnumber, $item->itemnumber, 'Koha::Item::Transfer->item should return the correct item' );
52 $schema->storage->txn_rollback;
55 subtest 'from_library relation tests' => sub {
58 $schema->storage->txn_begin;
60 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
61 my $transfer = $builder->build_object(
63 class => 'Koha::Item::Transfers',
65 frombranch => $library->branchcode,
70 my $from_library = $transfer->from_library;
71 is( ref( $from_library ), 'Koha::Library', 'Koha::Item::Transfer->from_library should return a Koha::Library' );
72 is( $from_library->branchcode, $library->branchcode, 'Koha::Item::Transfer->from_library should return the correct library' );
74 $schema->storage->txn_rollback;
77 subtest 'to_library relation tests' => sub {
80 $schema->storage->txn_begin;
82 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
83 my $transfer = $builder->build_object(
85 class => 'Koha::Item::Transfers',
87 tobranch => $library->branchcode,
92 my $to_library = $transfer->to_library;
93 is( ref( $to_library ), 'Koha::Library', 'Koha::Item::Transfer->to_library should return a Koha::Library' );
94 is( $to_library->branchcode, $library->branchcode, 'Koha::Item::Transfer->to_library should return the correct library' );
96 $schema->storage->txn_rollback;
99 subtest 'transit tests' => sub {
102 $schema->storage->txn_begin;
104 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
105 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
106 my $item = $builder->build_sample_item(
108 homebranch => $library1->branchcode,
109 holdingbranch => $library2->branchcode,
110 datelastseen => undef
114 my $transfer = $builder->build_object(
116 class => 'Koha::Item::Transfers',
118 itemnumber => $item->itemnumber,
119 frombranch => $library2->branchcode,
120 tobranch => $library1->branchcode,
125 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
127 # Item checked out should result in failure
128 my $checkout = $builder->build_object(
130 class => 'Koha::Checkouts',
132 itemnumber => $item->itemnumber
136 is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
138 throws_ok { $transfer->transit() }
139 'Koha::Exceptions::Item::Transfer::OnLoan',
140 'Exception thrown if item is checked out';
145 $item->set({ location => 'CART', permanent_location => 'TEST' })->store();
146 is ( $item->location, 'CART', 'Item location set to CART');
147 $transfer->discard_changes;
148 $transfer->transit();
149 $item->discard_changes;
150 is ( $item->location, 'TEST', 'Item location correctly restored to match permanent location');
153 ok( $transfer->datesent, 'Transit set the datesent for the transfer' );
156 ok ( $item->datelastseen, 'Transit set item datelastseen date');
158 $schema->storage->txn_rollback;
161 subtest 'receive tests' => sub {
164 $schema->storage->txn_begin;
166 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
167 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
168 my $item = $builder->build_sample_item(
170 homebranch => $library1->branchcode,
171 holdingbranch => $library2->branchcode,
172 datelastseen => undef
176 my $transfer = $builder->build_object(
178 class => 'Koha::Item::Transfers',
180 itemnumber => $item->itemnumber,
181 frombranch => $library2->branchcode,
182 tobranch => $library1->branchcode,
183 datearrived => undef,
188 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
190 # Item checked out should result in failure
191 my $checkout = $builder->build_object(
193 class => 'Koha::Checkouts',
195 itemnumber => $item->itemnumber
199 is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
201 throws_ok { $transfer->receive() }
202 'Koha::Exceptions::Item::Transfer::OnLoan',
203 'Exception thrown if item is checked out';
208 $transfer->discard_changes;
209 $transfer->receive();
210 ok( $transfer->datearrived, 'Receipt set the datearrived for the transfer' );
213 ok( $item->datelastseen, 'Receipt set item datelastseen date' );
215 $schema->storage->txn_rollback;
218 subtest 'in_transit tests' => sub {
222 $schema->storage->txn_begin;
224 my $library_from = $builder->build_object( { class => 'Koha::Libraries' } );
225 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
226 my $item = $builder->build_sample_item(
228 homebranch => $library_to->branchcode,
229 holdingbranch => $library_from->branchcode,
233 my $transfer = Koha::Item::Transfer->new(
235 itemnumber => $item->itemnumber,
236 frombranch => $library_from->branchcode,
237 tobranch => $library_to->branchcode,
238 daterequested => dt_from_string,
242 ok( !$transfer->in_transit, 'in_transit returns false when only daterequested is defined' );
244 $transfer->datesent(dt_from_string)->store;
245 ok( $transfer->in_transit, 'in_transit returns true when datesent is defined');
247 $transfer->datearrived(dt_from_string)->store;
248 ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
250 $transfer->set( { datearrived => undef, datecancelled => dt_from_string } )->store;
251 ok( !$transfer->in_transit, 'in_transit returns false when datecancelled is defined');
253 $schema->storage->txn_rollback;
256 subtest 'cancel tests' => sub {
259 $schema->storage->txn_begin;
261 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
262 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
263 my $item = $builder->build_sample_item(
265 homebranch => $library1->branchcode,
266 holdingbranch => $library2->branchcode,
267 datelastseen => undef
270 my $cancellation_reason = 'Manual';
272 my $transfer = $builder->build_object(
274 class => 'Koha::Item::Transfers',
276 itemnumber => $item->itemnumber,
277 frombranch => $library2->branchcode,
278 tobranch => $library1->branchcode,
279 datesent => \'NOW()',
280 datearrived => undef,
281 datecancelled => undef,
283 cancellation_reason => undef
287 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
289 # Missing mandatory parameter
290 throws_ok { $transfer->cancel() } 'Koha::Exceptions::MissingParameter',
291 'Exception thrown if a reason is not passed to cancel';
293 # Item in transit should result in failure
294 throws_ok { $transfer->cancel({ reason => $cancellation_reason }) }
295 'Koha::Exceptions::Item::Transfer::InTransit',
296 'Exception thrown if item is in transit';
298 $transfer->cancel({ reason => $cancellation_reason, force => 1});
299 ok( $transfer->datecancelled, 'Forced cancellation, cancellation date set' );
300 is( $transfer->cancellation_reason, 'Manual', 'Forced cancellation, cancellation reason is set');
302 $transfer->datecancelled(undef);
303 $transfer->cancellation_reason(undef);
304 $transfer->datesent(undef);
306 # Transit state unset
307 $transfer->store()->discard_changes;
308 $transfer->cancel({ reason => $cancellation_reason });
309 ok( $transfer->datecancelled, 'Cancellation date set upon call to cancel' );
310 is( $transfer->cancellation_reason, 'Manual', 'Cancellation reason is set');
312 $schema->storage->txn_rollback;