Bug 24606: Regression tests
[koha.git] / t / db_dependent / Koha / Item / Transfer.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Koha::Database;
23 use Koha::DateUtils qw( dt_from_string );
24
25 use t::lib::TestBuilder;
26
27 use Test::More tests => 7;
28 use Test::Exception;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'item relation tests' => sub {
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $item     = $builder->build_sample_item();
39     my $transfer = $builder->build_object(
40         {
41             class => 'Koha::Item::Transfers',
42             value => {
43                 itemnumber => $item->itemnumber,
44             }
45         }
46     );
47
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' );
51
52     $schema->storage->txn_rollback;
53 };
54
55 subtest 'from_library relation tests' => sub {
56     plan tests => 2;
57
58     $schema->storage->txn_begin;
59
60     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
61     my $transfer = $builder->build_object(
62         {
63             class => 'Koha::Item::Transfers',
64             value => {
65                 frombranch => $library->branchcode,
66             }
67         }
68     );
69
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' );
73
74     $schema->storage->txn_rollback;
75 };
76
77 subtest 'to_library relation tests' => sub {
78     plan tests => 2;
79
80     $schema->storage->txn_begin;
81
82     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
83     my $transfer = $builder->build_object(
84         {
85             class => 'Koha::Item::Transfers',
86             value => {
87                 tobranch => $library->branchcode,
88             }
89         }
90     );
91
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' );
95
96     $schema->storage->txn_rollback;
97 };
98
99 subtest 'transit tests' => sub {
100     plan tests => 7;
101
102     $schema->storage->txn_begin;
103
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(
107         {
108             homebranch    => $library1->branchcode,
109             holdingbranch => $library2->branchcode,
110             datelastseen  => undef
111         }
112     );
113
114     my $transfer = $builder->build_object(
115         {
116             class => 'Koha::Item::Transfers',
117             value => {
118                 itemnumber => $item->itemnumber,
119                 frombranch => $library2->branchcode,
120                 tobranch   => $library1->branchcode,
121                 reason     => 'Manual'
122             }
123         }
124     );
125     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
126
127     # Item checked out should result in failure
128     my $checkout = $builder->build_object(
129         {
130             class => 'Koha::Checkouts',
131             value => {
132                 itemnumber => $item->itemnumber
133             }
134         }
135     );
136     is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
137
138     throws_ok { $transfer->transit() }
139     'Koha::Exceptions::Item::Transfer::OnLoan',
140       'Exception thrown if item is checked out';
141
142     $checkout->delete;
143
144     # CartToShelf test
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');
151
152     # Transit state set
153     ok( $transfer->datesent, 'Transit set the datesent for the transfer' );
154
155     # Last seen
156     ok ( $item->datelastseen, 'Transit set item datelastseen date');
157
158     $schema->storage->txn_rollback;
159 };
160
161 subtest 'receive tests' => sub {
162     plan tests => 5;
163
164     $schema->storage->txn_begin;
165
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(
169         {
170             homebranch    => $library1->branchcode,
171             holdingbranch => $library2->branchcode,
172             datelastseen  => undef
173         }
174     );
175
176     my $transfer = $builder->build_object(
177         {
178             class => 'Koha::Item::Transfers',
179             value => {
180                 itemnumber   => $item->itemnumber,
181                 frombranch   => $library2->branchcode,
182                 tobranch     => $library1->branchcode,
183                 datearrived => undef,
184                 reason       => 'Manual'
185             }
186         }
187     );
188     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
189
190     # Item checked out should result in failure
191     my $checkout = $builder->build_object(
192         {
193             class => 'Koha::Checkouts',
194             value => {
195                 itemnumber => $item->itemnumber
196             }
197         }
198     );
199     is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
200
201     throws_ok { $transfer->receive() }
202     'Koha::Exceptions::Item::Transfer::OnLoan',
203       'Exception thrown if item is checked out';
204
205     $checkout->delete;
206
207     # Transit state set
208     $transfer->discard_changes;
209     $transfer->receive();
210     ok( $transfer->datearrived, 'Receipt set the datearrived for the transfer' );
211
212     # Last seen
213     ok( $item->datelastseen, 'Receipt set item datelastseen date' );
214
215     $schema->storage->txn_rollback;
216 };
217
218 subtest 'in_transit tests' => sub {
219
220     plan tests => 4;
221
222     $schema->storage->txn_begin;
223
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(
227         {
228             homebranch    => $library_to->branchcode,
229             holdingbranch => $library_from->branchcode,
230         }
231     );
232
233     my $transfer = Koha::Item::Transfer->new(
234         {
235             itemnumber    => $item->itemnumber,
236             frombranch    => $library_from->branchcode,
237             tobranch      => $library_to->branchcode,
238             daterequested => dt_from_string,
239         }
240     )->store;
241
242     ok( !$transfer->in_transit, 'in_transit returns false when only daterequested is defined' );
243
244     $transfer->datesent(dt_from_string)->store;
245     ok( $transfer->in_transit, 'in_transit returns true when datesent is defined');
246
247     $transfer->datearrived(dt_from_string)->store;
248     ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
249
250     $transfer->set( { datearrived => undef, datecancelled => dt_from_string } )->store;
251     ok( !$transfer->in_transit, 'in_transit returns false when datecancelled is defined');
252
253     $schema->storage->txn_rollback;
254 };
255
256 subtest 'cancel tests' => sub {
257     plan tests => 7;
258
259     $schema->storage->txn_begin;
260
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(
264         {
265             homebranch    => $library1->branchcode,
266             holdingbranch => $library2->branchcode,
267             datelastseen  => undef
268         }
269     );
270     my $cancellation_reason = 'Manual';
271
272     my $transfer = $builder->build_object(
273         {
274             class => 'Koha::Item::Transfers',
275             value => {
276                 itemnumber          => $item->itemnumber,
277                 frombranch          => $library2->branchcode,
278                 tobranch            => $library1->branchcode,
279                 datesent            => \'NOW()',
280                 datearrived         => undef,
281                 datecancelled       => undef,
282                 reason              => 'Manual',
283                 cancellation_reason => undef
284             }
285         }
286     );
287     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
288
289     # Missing mandatory parameter
290     throws_ok { $transfer->cancel() } 'Koha::Exceptions::MissingParameter',
291       'Exception thrown if a reason is not passed to cancel';
292
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';
297
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');
301
302     $transfer->datecancelled(undef);
303     $transfer->cancellation_reason(undef);
304     $transfer->datesent(undef);
305
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');
311
312     $schema->storage->txn_rollback;
313 };