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>.
25 use t::lib::TestBuilder;
27 use Test::More tests => 5;
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 'transit tests' => sub {
58 $schema->storage->txn_begin;
60 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
61 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
62 my $item = $builder->build_sample_item(
64 homebranch => $library1->branchcode,
65 holdingbranch => $library2->branchcode,
70 my $transfer = $builder->build_object(
72 class => 'Koha::Item::Transfers',
74 itemnumber => $item->itemnumber,
75 frombranch => $library2->branchcode,
76 tobranch => $library1->branchcode,
81 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
83 # Item checked out should result in failure
84 my $checkout = $builder->build_object(
86 class => 'Koha::Checkouts',
88 itemnumber => $item->itemnumber
92 is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
94 throws_ok { $transfer->transit() }
95 'Koha::Exceptions::Item::Transfer::Out',
96 'Exception thrown if item is checked out';
101 $item->set({ location => 'CART', permanent_location => 'TEST' })->store();
102 is ( $item->location, 'CART', 'Item location set to CART');
103 $transfer->discard_changes;
104 $transfer->transit();
105 $item->discard_changes;
106 is ( $item->location, 'TEST', 'Item location correctly restored to match permanent location');
109 ok( $transfer->datesent, 'Transit set the datesent for the transfer' );
112 ok ( $item->datelastseen, 'Transit set item datelastseen date');
114 $schema->storage->txn_rollback;
117 subtest 'receive tests' => sub {
120 $schema->storage->txn_begin;
122 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
123 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
124 my $item = $builder->build_sample_item(
126 homebranch => $library1->branchcode,
127 holdingbranch => $library2->branchcode,
128 datelastseen => undef
132 my $transfer = $builder->build_object(
134 class => 'Koha::Item::Transfers',
136 itemnumber => $item->itemnumber,
137 frombranch => $library2->branchcode,
138 tobranch => $library1->branchcode,
139 datearrived => undef,
144 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
146 # Item checked out should result in failure
147 my $checkout = $builder->build_object(
149 class => 'Koha::Checkouts',
151 itemnumber => $item->itemnumber
155 is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
157 throws_ok { $transfer->receive() }
158 'Koha::Exceptions::Item::Transfer::Out',
159 'Exception thrown if item is checked out';
164 $transfer->discard_changes;
165 $transfer->receive();
166 ok( $transfer->datearrived, 'Receipt set the datearrived for the transfer' );
169 ok( $item->datelastseen, 'Receipt set item datelastseen date' );
171 $schema->storage->txn_rollback;
174 subtest 'in_transit tests' => sub {
178 $schema->storage->txn_begin;
180 my $library_from = $builder->build_object( { class => 'Koha::Libraries' } );
181 my $library_to = $builder->build_object( { class => 'Koha::Libraries' } );
182 my $item = $builder->build_sample_item(
184 homebranch => $library_to->branchcode,
185 holdingbranch => $library_from->branchcode,
189 my $transfer = Koha::Item::Transfer->new(
191 itemnumber => $item->itemnumber,
192 frombranch => $library_from->branchcode,
193 tobranch => $library_to->branchcode,
194 daterequested => dt_from_string,
198 ok( !$transfer->in_transit, 'in_transit returns false when only daterequested is defined' );
200 $transfer->datesent(dt_from_string)->store;
201 ok( $transfer->in_transit, 'in_transit returns true when datesent is defined');
203 $transfer->datearrived(dt_from_string)->store;
204 ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
207 $schema->storage->txn_rollback;
210 subtest 'cancel tests' => sub {
213 $schema->storage->txn_begin;
215 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
216 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
217 my $item = $builder->build_sample_item(
219 homebranch => $library1->branchcode,
220 holdingbranch => $library2->branchcode,
221 datelastseen => undef
224 my $cancellation_reason = 'Manual';
226 my $transfer = $builder->build_object(
228 class => 'Koha::Item::Transfers',
230 itemnumber => $item->itemnumber,
231 frombranch => $library2->branchcode,
232 tobranch => $library1->branchcode,
233 datesent => \'NOW()',
234 datearrived => undef,
235 datecancelled => undef,
237 cancellation_reason => undef
241 is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
243 # Missing mandatory parameter
244 throws_ok { $transfer->cancel() } 'Koha::Exceptions::MissingParameter',
245 'Exception thrown if a reason is not passed to cancel';
247 # Item in transit should result in failure
248 throws_ok { $transfer->cancel({ reason => $cancellation_reason }) }
249 'Koha::Exceptions::Item::Transfer::Transit',
250 'Exception thrown if item is in transit';
252 $transfer->cancel({ reason => $cancellation_reason, force => 1});
253 ok( $transfer->datecancelled, 'Forced cancellation, cancellation date set' );
254 is( $transfer->cancellation_reason, 'Manual', 'Forced cancellation, cancellation reason is set');
256 $transfer->datecancelled(undef);
257 $transfer->cancellation_reason(undef);
258 $transfer->datesent(undef);
260 # Transit state unset
261 $transfer->store()->discard_changes;
262 $transfer->cancel({ reason => $cancellation_reason });
263 ok( $transfer->datecancelled, 'Cancellation date set upon call to cancel' );
264 is( $transfer->cancellation_reason, 'Manual', 'Cancellation reason is set');
266 $schema->storage->txn_rollback;