Bug 26481: Add Koha::Item::Transfer->in_transit method
[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;
24
25 use t::lib::TestBuilder;
26
27 use Test::More tests => 4;
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 'transit tests' => sub {
56     plan tests => 7;
57
58     $schema->storage->txn_begin;
59
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(
63         {
64             homebranch    => $library1->branchcode,
65             holdingbranch => $library2->branchcode,
66             datelastseen  => undef
67         }
68     );
69
70     my $transfer = $builder->build_object(
71         {
72             class => 'Koha::Item::Transfers',
73             value => {
74                 itemnumber => $item->itemnumber,
75                 frombranch => $library2->branchcode,
76                 tobranch   => $library1->branchcode,
77                 reason     => 'Manual'
78             }
79         }
80     );
81     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
82
83     # Item checked out should result in failure
84     my $checkout = $builder->build_object(
85         {
86             class => 'Koha::Checkouts',
87             value => {
88                 itemnumber => $item->itemnumber
89             }
90         }
91     );
92     is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
93
94     throws_ok { $transfer->transit() }
95     'Koha::Exceptions::Item::Transfer::Out',
96       'Exception thrown if item is checked out';
97
98     $checkout->delete;
99
100     # CartToShelf test
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');
107
108     # Transit state set
109     ok( $transfer->datesent, 'Transit set the datesent for the transfer' );
110
111     # Last seen
112     ok ( $item->datelastseen, 'Transit set item datelastseen date');
113
114     $schema->storage->txn_rollback;
115 };
116
117 subtest 'receive tests' => sub {
118     plan tests => 5;
119
120     $schema->storage->txn_begin;
121
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(
125         {
126             homebranch    => $library1->branchcode,
127             holdingbranch => $library2->branchcode,
128             datelastseen  => undef
129         }
130     );
131
132     my $transfer = $builder->build_object(
133         {
134             class => 'Koha::Item::Transfers',
135             value => {
136                 itemnumber   => $item->itemnumber,
137                 frombranch   => $library2->branchcode,
138                 tobranch     => $library1->branchcode,
139                 datearrived => undef,
140                 reason       => 'Manual'
141             }
142         }
143     );
144     is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
145
146     # Item checked out should result in failure
147     my $checkout = $builder->build_object(
148         {
149             class => 'Koha::Checkouts',
150             value => {
151                 itemnumber => $item->itemnumber
152             }
153         }
154     );
155     is( ref($checkout), 'Koha::Checkout', 'Mock checkout added' );
156
157     throws_ok { $transfer->receive() }
158     'Koha::Exceptions::Item::Transfer::Out',
159       'Exception thrown if item is checked out';
160
161     $checkout->delete;
162
163     # Transit state set
164     $transfer->discard_changes;
165     $transfer->receive();
166     ok( $transfer->datearrived, 'Receipt set the datearrived for the transfer' );
167
168     # Last seen
169     ok( $item->datelastseen, 'Receipt set item datelastseen date' );
170
171     $schema->storage->txn_rollback;
172 };
173
174 subtest 'in_transit tests' => sub {
175
176     plan tests => 3;
177
178     $schema->storage->txn_begin;
179
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(
183         {
184             homebranch    => $library_to->branchcode,
185             holdingbranch => $library_from->branchcode,
186         }
187     );
188
189     my $transfer = Koha::Item::Transfer->new(
190         {
191             itemnumber    => $item->itemnumber,
192             frombranch    => $library_from->branchcode,
193             tobranch      => $library_to->branchcode,
194             daterequested => dt_from_string,
195         }
196     )->store;
197
198     ok( !$transfer->in_transit, 'in_transit returns false when only daterequested is defined' );
199
200     $transfer->datesent(dt_from_string)->store;
201     ok( $transfer->in_transit, 'in_transit returns true when datesent is defined');
202
203     $transfer->datearrived(dt_from_string)->store;
204     ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
205
206
207     $schema->storage->txn_rollback;
208 };